@@ -16,6 +16,7 @@ package auth
16
16
17
17
import (
18
18
"errors"
19
+ "io/ioutil"
19
20
"log"
20
21
"os"
21
22
"strings"
@@ -36,68 +37,6 @@ import (
36
37
var client * Client
37
38
var testIDToken string
38
39
39
- func verifyCustomToken (t * testing.T , token string , expected map [string ]interface {}) {
40
- h := & jwtHeader {}
41
- p := & customToken {}
42
- if err := decodeToken (token , client .ks , h , p ); err != nil {
43
- t .Fatal (err )
44
- }
45
-
46
- if h .Algorithm != "RS256" {
47
- t .Errorf ("Algorithm: %q; want: 'RS256'" , h .Algorithm )
48
- } else if h .Type != "JWT" {
49
- t .Errorf ("Type: %q; want: 'JWT'" , h .Type )
50
- } else if p .Aud != firebaseAudience {
51
- t .Errorf ("Audience: %q; want: %q" , p .Aud , firebaseAudience )
52
- }
53
-
54
- for k , v := range expected {
55
- if p .Claims [k ] != v {
56
- t .Errorf ("Claim[%q]: %v; want: %v" , k , p .Claims [k ], v )
57
- }
58
- }
59
- }
60
-
61
- func getIDToken (p mockIDTokenPayload ) string {
62
- return getIDTokenWithKid ("mock-key-id-1" , p )
63
- }
64
-
65
- func getIDTokenWithKid (kid string , p mockIDTokenPayload ) string {
66
- pCopy := mockIDTokenPayload {
67
- "aud" : client .projectID ,
68
- "iss" : "https://securetoken.google.com/" + client .projectID ,
69
- "iat" : time .Now ().Unix () - 100 ,
70
- "exp" : time .Now ().Unix () + 3600 ,
71
- "sub" : "1234567890" ,
72
- "admin" : true ,
73
- }
74
- for k , v := range p {
75
- pCopy [k ] = v
76
- }
77
- h := defaultHeader ()
78
- h .KeyID = kid
79
- token , err := encodeToken (client .snr , h , pCopy )
80
- if err != nil {
81
- log .Fatalln (err )
82
- }
83
- return token
84
- }
85
-
86
- type mockIDTokenPayload map [string ]interface {}
87
-
88
- func (p mockIDTokenPayload ) decode (s string ) error {
89
- return decode (s , & p )
90
- }
91
-
92
- type mockKeySource struct {
93
- keys []* publicKey
94
- err error
95
- }
96
-
97
- func (t * mockKeySource ) Keys () ([]* publicKey , error ) {
98
- return t .keys , t .err
99
- }
100
-
101
40
func TestMain (m * testing.M ) {
102
41
var (
103
42
err error
@@ -286,6 +225,98 @@ func TestCertificateRequestError(t *testing.T) {
286
225
}
287
226
}
288
227
228
+ func verifyCustomToken (t * testing.T , token string , expected map [string ]interface {}) {
229
+ h := & jwtHeader {}
230
+ p := & customToken {}
231
+ if err := decodeToken (token , client .ks , h , p ); err != nil {
232
+ t .Fatal (err )
233
+ }
234
+
235
+ email , err := client .snr .Email ()
236
+ if err != nil {
237
+ t .Fatal (err )
238
+ }
239
+
240
+ if h .Algorithm != "RS256" {
241
+ t .Errorf ("Algorithm: %q; want: 'RS256'" , h .Algorithm )
242
+ } else if h .Type != "JWT" {
243
+ t .Errorf ("Type: %q; want: 'JWT'" , h .Type )
244
+ } else if p .Aud != firebaseAudience {
245
+ t .Errorf ("Audience: %q; want: %q" , p .Aud , firebaseAudience )
246
+ } else if p .Iss != email {
247
+ t .Errorf ("Issuer: %q; want: %q" , p .Iss , email )
248
+ } else if p .Sub != email {
249
+ t .Errorf ("Subject: %q; want: %q" , p .Sub , email )
250
+ }
251
+
252
+ for k , v := range expected {
253
+ if p .Claims [k ] != v {
254
+ t .Errorf ("Claim[%q]: %v; want: %v" , k , p .Claims [k ], v )
255
+ }
256
+ }
257
+ }
258
+
259
+ func getIDToken (p mockIDTokenPayload ) string {
260
+ return getIDTokenWithKid ("mock-key-id-1" , p )
261
+ }
262
+
263
+ func getIDTokenWithKid (kid string , p mockIDTokenPayload ) string {
264
+ pCopy := mockIDTokenPayload {
265
+ "aud" : client .projectID ,
266
+ "iss" : "https://securetoken.google.com/" + client .projectID ,
267
+ "iat" : time .Now ().Unix () - 100 ,
268
+ "exp" : time .Now ().Unix () + 3600 ,
269
+ "sub" : "1234567890" ,
270
+ "admin" : true ,
271
+ }
272
+ for k , v := range p {
273
+ pCopy [k ] = v
274
+ }
275
+ h := defaultHeader ()
276
+ h .KeyID = kid
277
+ token , err := encodeToken (client .snr , h , pCopy )
278
+ if err != nil {
279
+ log .Fatalln (err )
280
+ }
281
+ return token
282
+ }
283
+
284
+ type mockIDTokenPayload map [string ]interface {}
285
+
286
+ func (p mockIDTokenPayload ) decode (s string ) error {
287
+ return decode (s , & p )
288
+ }
289
+
290
+ // mockKeySource provides access to a set of in-memory public keys.
291
+ type mockKeySource struct {
292
+ keys []* publicKey
293
+ err error
294
+ }
295
+
296
+ func (t * mockKeySource ) Keys () ([]* publicKey , error ) {
297
+ return t .keys , t .err
298
+ }
299
+
300
+ // fileKeySource loads a set of public keys from the local file system.
301
+ type fileKeySource struct {
302
+ FilePath string
303
+ CachedKeys []* publicKey
304
+ }
305
+
306
+ func (f * fileKeySource ) Keys () ([]* publicKey , error ) {
307
+ if f .CachedKeys == nil {
308
+ certs , err := ioutil .ReadFile (f .FilePath )
309
+ if err != nil {
310
+ return nil , err
311
+ }
312
+ f .CachedKeys , err = parsePublicKeys (certs )
313
+ if err != nil {
314
+ return nil , err
315
+ }
316
+ }
317
+ return f .CachedKeys , nil
318
+ }
319
+
289
320
// aeKeySource provides access to the public keys associated with App Engine apps. This
290
321
// is used in tests to verify custom tokens and mock ID tokens when they are signed with
291
322
// App Engine private keys.
0 commit comments