-
Notifications
You must be signed in to change notification settings - Fork 18
/
token_verifier.go
75 lines (63 loc) · 1.96 KB
/
token_verifier.go
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
package firebase
import (
"errors"
"fmt"
"time"
"net/http"
"github.com/SermoDigital/jose/crypto"
"github.com/SermoDigital/jose/jws"
"github.com/SermoDigital/jose/jwt"
)
// clientCertURL is the URL containing the public keys for the Google certs
// (whose private keys are used to sign Firebase Auth ID Tokens).
const clientCertURL = "https://www.googleapis.com/robot/v1/metadata/x509/[email protected]"
// defaultAcceptableExpSkew is the default expiry leeway.
const defaultAcceptableExpSkew = 300 * time.Second
func verify(projectID, tokenString string, transport http.RoundTripper) (*Token, error) {
decodedJWT, err := jws.ParseJWT([]byte(tokenString))
if err != nil {
return nil, err
}
decodedJWS, ok := decodedJWT.(jws.JWS)
if !ok {
return nil, errors.New("Firebase Auth ID Token cannot be decoded")
}
keys := func(j jws.JWS) ([]interface{}, error) {
certs := &Certificates{URL: clientCertURL, Transport: transport}
kid, ok := j.Protected().Get("kid").(string)
if !ok {
return nil, errors.New("Firebase Auth ID Token has no 'kid' claim")
}
cert, err := certs.Cert(kid)
if err != nil {
return nil, err
}
return []interface{}{cert.PublicKey}, nil
}
err = decodedJWS.VerifyCallback(keys,
[]crypto.SigningMethod{crypto.SigningMethodRS256},
&jws.SigningOpts{Number: 1, Indices: []int{0}})
if err != nil {
return nil, err
}
ks, _ := keys(decodedJWS)
key := ks[0]
if err := decodedJWT.Validate(key, crypto.SigningMethodRS256, validator(projectID)); err != nil {
return nil, err
}
return &Token{delegate: decodedJWT}, nil
}
func validator(projectID string) *jwt.Validator {
v := &jwt.Validator{}
v.EXP = defaultAcceptableExpSkew
v.SetAudience(projectID)
v.SetIssuer(fmt.Sprintf("https://securetoken.google.com/%s", projectID))
v.Fn = func(claims jwt.Claims) error {
subject, ok := claims.Subject()
if !ok || len(subject) == 0 || len(subject) > 128 {
return jwt.ErrInvalidSUBClaim
}
return nil
}
return v
}