-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebasejwt.go
88 lines (74 loc) · 1.87 KB
/
firebasejwt.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
76
77
78
79
80
81
82
83
84
85
86
87
88
package firebasejwt
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/golang-jwt/jwt/v5"
)
const FirebaseKeysEndpoint = "https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com"
// Read public certificate
func readPEM(kid string) string {
resp, err := http.Get(FirebaseKeysEndpoint)
if err != nil {
return ""
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return ""
}
publicCertificates := make(map[string]string)
err = json.Unmarshal(body, &publicCertificates)
if err != nil {
return ""
}
return publicCertificates[kid]
}
// Do basic format checks on token string
func verifyBasics(tokenString string) error {
if tokenString == "" {
return fmt.Errorf("token string is empty")
}
parts := strings.Split(tokenString, ".")
if len(parts) != 3 {
return fmt.Errorf("wrong number of parts")
}
return nil
}
// Parse base64 encoded JWT issued by Firebase authenticator service
// and return token's claim in a map
func ParseFirebaseJWT(tokenString string) (map[string]interface{}, error) {
err := verifyBasics(tokenString)
if err != nil {
return nil, err
}
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
// Validate the alg is as expected
if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
kid, ok := token.Header["kid"]
if !ok {
return nil, fmt.Errorf("key id is missing in JWT header")
}
pem := readPEM(kid.(string))
pubKey, err := jwt.ParseRSAPublicKeyFromPEM([]byte(pem))
if err != nil {
return nil, err
}
return pubKey, nil
})
if err != nil {
if token.Claims != nil {
claims := token.Claims.(jwt.MapClaims)
return claims, err
}
return nil, err
}
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
return claims, err
}
return claims, nil
}