-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtoken_verify.go
64 lines (55 loc) · 1.53 KB
/
token_verify.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
package firebase
import (
"errors"
"fmt"
"github.com/SermoDigital/jose/crypto"
"github.com/SermoDigital/jose/jws"
"github.com/SermoDigital/jose/jwt"
"golang.org/x/net/context"
)
func (a *Auth) VerifyIDToken(ctx context.Context, token string) (*Token, error) {
decodedJWT, err := jws.ParseJWT([]byte(token))
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) {
kid, ok := j.Protected().Get("kid").(string)
if !ok {
return nil, errors.New("Firebase Auth ID Token has no 'kid' claim")
}
cert, err := certs.Get(ctx, kid)
if err != nil {
return nil, err
}
return []interface{}{cert.PublicKey}, nil
}
if err := decodedJWS.VerifyCallback(keys,
[]crypto.SigningMethod{crypto.SigningMethodRS256},
&jws.SigningOpts{Number: 1, Indices: []int{0}}); err != nil {
return nil, err
}
ks, _ := keys(decodedJWS)
key := ks[0]
if err := decodedJWT.Validate(key, crypto.SigningMethodRS256, validator(a.app.creds.ProjectID)); err != nil {
return nil, err
}
return &Token{decodedJWT}, nil
}
func validator(projectID string) *jwt.Validator {
v := &jwt.Validator{}
v.EXP = acceptableExpSkew
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
}