-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfirejwt.go
215 lines (180 loc) · 5.01 KB
/
firejwt.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
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
package firejwt
import (
"context"
"crypto/rsa"
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"log"
"net/http"
"sync/atomic"
"time"
"github.com/golang-jwt/jwt/v4"
)
func init() {
jwt.MarshalSingleStringAsArray = false
}
const defaultURL = "https://www.googleapis.com/robot/v1/metadata/x509/[email protected]"
// Validator validates Firebase JWTs
type Validator struct {
audience string
issuer string
url string
htc http.Client
cancel context.CancelFunc
keyset atomic.Value
expires int64
}
// New issues a new Validator with a projectID, a unique identifier for your
// Firebase project, which can be found in the URL of that project's console.
func New(projectID string) (*Validator, error) {
return newValidator(projectID, defaultURL)
}
func newValidator(projectID, url string) (*Validator, error) {
v := &Validator{
audience: projectID,
issuer: "https://securetoken.google.com/" + projectID,
url: url,
}
if err := v.Refresh(); err != nil {
return nil, err
}
ctx, cancel := context.WithCancel(context.Background())
v.cancel = cancel
go v.loop(ctx)
return v, nil
}
// Stop stops the validator updates.
func (v *Validator) Stop() {
v.cancel()
}
// Decode decodes the token
func (v *Validator) Decode(tokenString string) (*Claims, error) {
claims := new(Claims)
token, err := jwt.ParseWithClaims(tokenString, claims, v.verify)
if err != nil {
return nil, err
} else if !token.Valid {
return nil, errTokenInvalid
}
return claims, nil
}
// ExpTime returns the expiration time.
func (v *Validator) ExpTime() time.Time {
return time.Unix(atomic.LoadInt64(&v.expires), 0)
}
// Refresh retrieves the latest keys.
func (v *Validator) Refresh() error {
resp, err := v.htc.Get(v.url)
if err != nil {
return err
}
defer resp.Body.Close()
exp, err := time.Parse(time.RFC1123, resp.Header.Get("Expires"))
if err != nil {
return err
}
var keyset map[string]publicKey
if err := json.NewDecoder(resp.Body).Decode(&keyset); err != nil {
return err
}
v.keyset.Store(keyset)
atomic.StoreInt64(&v.expires, exp.Unix())
return nil
}
var (
errKIDMissing = errors.New("token is missing kid header")
errNoSubject = errors.New("token has no subject")
errAuthFuture = errors.New("token auth_time not valid")
errTokenInvalid = errors.New("token is invalid")
)
func (v *Validator) verify(token *jwt.Token) (interface{}, error) {
kid, ok := token.Header["kid"].(string)
if !ok {
return nil, errKIDMissing
}
key, ok := v.keyset.Load().(map[string]publicKey)[kid]
if !ok {
return nil, fmt.Errorf("invalid kid header %q", kid)
}
claims := token.Claims.(*Claims)
if err := claims.validate(time.Now(), v.audience, v.issuer); err != nil {
return nil, err
}
return key.PublicKey, nil
}
func (v *Validator) loop(ctx context.Context) {
t := time.NewTimer(time.Minute)
defer t.Stop()
for {
d := time.Until(v.ExpTime()) - time.Hour
if d < time.Minute {
d = time.Minute
}
t.Reset(d)
select {
case <-ctx.Done():
return
case <-t.C:
if err := v.Refresh(); err != nil {
log.Printf("[firejwt] failed to refresh keyset: %v", err)
}
}
}
}
// --------------------------------------------------------------------
type publicKey struct {
*rsa.PublicKey
}
func (k *publicKey) UnmarshalText(data []byte) error {
block, _ := pem.Decode(data)
if block == nil {
return fmt.Errorf("invalid certificate")
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return err
}
if cert.PublicKeyAlgorithm != x509.RSA {
return fmt.Errorf("unexpected public key algorithm: %s", cert.PublicKeyAlgorithm)
}
*k = publicKey{PublicKey: cert.PublicKey.(*rsa.PublicKey)}
return nil
}
// --------------------------------------------------------------------
// Claims are included in the token.
type Claims struct {
Name string `json:"name,omitempty"`
Picture string `json:"picture,omitempty"`
UserID string `json:"user_id,omitempty"`
AuthAt *jwt.NumericDate `json:"auth_time,omitempty"`
Email string `json:"email,omitempty"`
EmailVerified bool `json:"email_verified"`
Firebase *FirebaseClaim `json:"firebase,omitempty"`
jwt.RegisteredClaims
}
func (c *Claims) validate(now time.Time, audience, issuer string) error {
if !c.VerifyExpiresAt(now, false) {
return jwt.ErrTokenExpired
} else if !c.VerifyIssuedAt(now, false) {
return jwt.ErrTokenUsedBeforeIssued
} else if !c.VerifyNotBefore(now, false) {
return jwt.ErrTokenNotValidYet
} else if !c.VerifyAudience(audience, true) {
return jwt.ErrTokenInvalidAudience
} else if !c.VerifyIssuer(issuer, true) {
return jwt.ErrTokenInvalidIssuer
} else if c.Subject == "" {
return errNoSubject
} else if c.AuthAt.After(now) {
return errAuthFuture
}
return nil
}
// FirebaseClaim represents firebase specific claim.
type FirebaseClaim struct {
SignInProvider string `json:"sign_in_provider,omitempty"`
Identities map[string][]string `json:"identities,omitempty"`
}