-
Notifications
You must be signed in to change notification settings - Fork 31
/
token.go
42 lines (37 loc) · 998 Bytes
/
token.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
package jwt
import (
"fmt"
"github.com/dgrijalva/jwt-go"
)
const (
tokenHeader = "X-Goog-IAP-JWT-Assertion"
algorithm = "ES256"
algorithmClaim = "alg"
keyIDClaim = "kid"
issuerClaim = "https://cloud.google.com/iap"
)
func tokenKey(token *jwt.Token) (interface{}, error) {
if _, ok := tokenMethod(token); !ok {
return nil, fmt.Errorf("Invalid algorithm: %v", token.Header[algorithmClaim])
}
keyID, _ := token.Header[keyIDClaim].(string)
key := token.Claims.(*Claims).cfg.PublicKeys[keyID]
if len(key) == 0 {
return nil, fmt.Errorf("No public key for %q", keyID)
}
parsedKey, err := jwt.ParseECPublicKeyFromPEM(key)
if err != nil {
return nil, fmt.Errorf("Failed to parse key: %v", err)
}
return parsedKey, nil
}
func tokenMethod(token *jwt.Token) (jwt.SigningMethod, bool) {
if token.Header[algorithmClaim] != algorithm {
return nil, false
}
method, ok := token.Method.(*jwt.SigningMethodECDSA)
if !ok {
return nil, false
}
return method, true
}