1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
// Copyright 2020 Google LLC.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package idtoken
import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"math/big"
"net/http"
"strings"
"time"
"google.golang.org/api/option/internaloption"
htransport "google.golang.org/api/transport/http"
)
const (
es256KeySize int = 32
googleIAPCertsURL string = "https://www.gstatic.com/iap/verify/public_key-jwk"
googleSACertsURL string = "https://www.googleapis.com/oauth2/v3/certs"
)
var (
defaultValidator = &Validator{client: newCachingClient(http.DefaultClient)}
// now aliases time.Now for testing.
now = time.Now
)
func defaultValidatorOpts() []ClientOption {
return []ClientOption{internaloption.WithDefaultScopes("https://www.googleapis.com/auth/cloud-platform")}
}
// Payload represents a decoded payload of an ID Token.
type Payload struct {
Issuer string `json:"iss"`
Audience string `json:"aud"`
Expires int64 `json:"exp"`
IssuedAt int64 `json:"iat"`
Subject string `json:"sub,omitempty"`
Claims map[string]interface{} `json:"-"`
}
// jwt represents the segments of a jwt and exposes convenience methods for
// working with the different segments.
type jwt struct {
header string
payload string
signature string
}
// jwtHeader represents a parted jwt's header segment.
type jwtHeader struct {
Algorithm string `json:"alg"`
Type string `json:"typ"`
KeyID string `json:"kid"`
}
// certResponse represents a list jwks. It is the format returned from known
// Google cert endpoints.
type certResponse struct {
Keys []jwk `json:"keys"`
}
// jwk is a simplified representation of a standard jwk. It only includes the
// fields used by Google's cert endpoints.
type jwk struct {
Alg string `json:"alg"`
Crv string `json:"crv"`
Kid string `json:"kid"`
Kty string `json:"kty"`
Use string `json:"use"`
E string `json:"e"`
N string `json:"n"`
X string `json:"x"`
Y string `json:"y"`
}
// Validator provides a way to validate Google ID Tokens with a user provided
// http.Client.
type Validator struct {
client *cachingClient
}
// NewValidator creates a Validator that uses the options provided to configure
// a the internal http.Client that will be used to make requests to fetch JWKs.
func NewValidator(ctx context.Context, opts ...ClientOption) (*Validator, error) {
opts = append(defaultValidatorOpts(), opts...)
client, _, err := htransport.NewClient(ctx, opts...)
if err != nil {
return nil, err
}
return &Validator{client: newCachingClient(client)}, nil
}
// Validate is used to validate the provided idToken with a known Google cert
// URL. If audience is not empty the audience claim of the Token is validated.
// Upon successful validation a parsed token Payload is returned allowing the
// caller to validate any additional claims.
func (v *Validator) Validate(ctx context.Context, idToken string, audience string) (*Payload, error) {
return v.validate(ctx, idToken, audience)
}
// Validate is used to validate the provided idToken with a known Google cert
// URL. If audience is not empty the audience claim of the Token is validated.
// Upon successful validation a parsed token Payload is returned allowing the
// caller to validate any additional claims.
func Validate(ctx context.Context, idToken string, audience string) (*Payload, error) {
// TODO(codyoss): consider adding a check revoked version of the api. See: https://pkg.go.dev/firebase.google.com/go/auth?tab=doc#Client.VerifyIDTokenAndCheckRevoked
return defaultValidator.validate(ctx, idToken, audience)
}
func (v *Validator) validate(ctx context.Context, idToken string, audience string) (*Payload, error) {
jwt, err := parseJWT(idToken)
if err != nil {
return nil, err
}
header, err := jwt.parsedHeader()
if err != nil {
return nil, err
}
payload, err := jwt.parsedPayload()
if err != nil {
return nil, err
}
sig, err := jwt.decodedSignature()
if err != nil {
return nil, err
}
if audience != "" && payload.Audience != audience {
return nil, fmt.Errorf("idtoken: audience provided does not match aud claim in the JWT")
}
if now().Unix() > payload.Expires {
return nil, fmt.Errorf("idtoken: token expired")
}
switch header.Algorithm {
case "RS256":
if err := v.validateRS256(ctx, header.KeyID, jwt.hashedContent(), sig); err != nil {
return nil, err
}
case "ES256":
if err := v.validateES256(ctx, header.KeyID, jwt.hashedContent(), sig); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("idtoken: expected JWT signed with RS256 or ES256 but found %q", header.Algorithm)
}
return payload, nil
}
func (v *Validator) validateRS256(ctx context.Context, keyID string, hashedContent []byte, sig []byte) error {
certResp, err := v.client.getCert(ctx, googleSACertsURL)
if err != nil {
return err
}
j, err := findMatchingKey(certResp, keyID)
if err != nil {
return err
}
dn, err := decode(j.N)
if err != nil {
return err
}
de, err := decode(j.E)
if err != nil {
return err
}
pk := &rsa.PublicKey{
N: new(big.Int).SetBytes(dn),
E: int(new(big.Int).SetBytes(de).Int64()),
}
return rsa.VerifyPKCS1v15(pk, crypto.SHA256, hashedContent, sig)
}
func (v *Validator) validateES256(ctx context.Context, keyID string, hashedContent []byte, sig []byte) error {
certResp, err := v.client.getCert(ctx, googleIAPCertsURL)
if err != nil {
return err
}
j, err := findMatchingKey(certResp, keyID)
if err != nil {
return err
}
dx, err := decode(j.X)
if err != nil {
return err
}
dy, err := decode(j.Y)
if err != nil {
return err
}
pk := &ecdsa.PublicKey{
Curve: elliptic.P256(),
X: new(big.Int).SetBytes(dx),
Y: new(big.Int).SetBytes(dy),
}
r := big.NewInt(0).SetBytes(sig[:es256KeySize])
s := big.NewInt(0).SetBytes(sig[es256KeySize:])
if valid := ecdsa.Verify(pk, hashedContent, r, s); !valid {
return fmt.Errorf("idtoken: ES256 signature not valid")
}
return nil
}
func findMatchingKey(response *certResponse, keyID string) (*jwk, error) {
if response == nil {
return nil, fmt.Errorf("idtoken: cert response is nil")
}
for _, v := range response.Keys {
if v.Kid == keyID {
return &v, nil
}
}
return nil, fmt.Errorf("idtoken: could not find matching cert keyId for the token provided")
}
func parseJWT(idToken string) (*jwt, error) {
segments := strings.Split(idToken, ".")
if len(segments) != 3 {
return nil, fmt.Errorf("idtoken: invalid token, token must have three segments; found %d", len(segments))
}
return &jwt{
header: segments[0],
payload: segments[1],
signature: segments[2],
}, nil
}
// decodedHeader base64 decodes the header segment.
func (j *jwt) decodedHeader() ([]byte, error) {
dh, err := decode(j.header)
if err != nil {
return nil, fmt.Errorf("idtoken: unable to decode JWT header: %v", err)
}
return dh, nil
}
// decodedPayload base64 payload the header segment.
func (j *jwt) decodedPayload() ([]byte, error) {
p, err := decode(j.payload)
if err != nil {
return nil, fmt.Errorf("idtoken: unable to decode JWT payload: %v", err)
}
return p, nil
}
// decodedPayload base64 payload the header segment.
func (j *jwt) decodedSignature() ([]byte, error) {
p, err := decode(j.signature)
if err != nil {
return nil, fmt.Errorf("idtoken: unable to decode JWT signature: %v", err)
}
return p, nil
}
// parsedHeader returns a struct representing a JWT header.
func (j *jwt) parsedHeader() (jwtHeader, error) {
var h jwtHeader
dh, err := j.decodedHeader()
if err != nil {
return h, err
}
err = json.Unmarshal(dh, &h)
if err != nil {
return h, fmt.Errorf("idtoken: unable to unmarshal JWT header: %v", err)
}
return h, nil
}
// parsedPayload returns a struct representing a JWT payload.
func (j *jwt) parsedPayload() (*Payload, error) {
var p Payload
dp, err := j.decodedPayload()
if err != nil {
return nil, err
}
if err := json.Unmarshal(dp, &p); err != nil {
return nil, fmt.Errorf("idtoken: unable to unmarshal JWT payload: %v", err)
}
if err := json.Unmarshal(dp, &p.Claims); err != nil {
return nil, fmt.Errorf("idtoken: unable to unmarshal JWT payload claims: %v", err)
}
return &p, nil
}
// hashedContent gets the SHA256 checksum for verification of the JWT.
func (j *jwt) hashedContent() []byte {
signedContent := j.header + "." + j.payload
hashed := sha256.Sum256([]byte(signedContent))
return hashed[:]
}
func (j *jwt) String() string {
return fmt.Sprintf("%s.%s.%s", j.header, j.payload, j.signature)
}
func decode(s string) ([]byte, error) {
return base64.RawURLEncoding.DecodeString(s)
}
|