|
1 | 1 | import { describe, it, expect } from 'vitest';
|
2 | 2 | import { PublicKeySignatureVerifier, rs256alg } from '../src/jws-verifier';
|
3 |
| -import type { FirebaseIdToken } from '../src/token-verifier'; |
4 |
| -import { createFirebaseTokenVerifier, FIREBASE_AUDIENCE } from '../src/token-verifier'; |
5 |
| -import { genIss, genTime, signJWT, TestingKeyFetcher } from './jwk-utils'; |
| 3 | +import { FIREBASE_AUDIENCE, baseCreateIdTokenVerifier, baseCreateSessionCookieVerifier } from '../src/token-verifier'; |
| 4 | +import { createTestingSignVerifyPair, generateIdToken, generateSessionCookie, projectId } from './firebase-utils'; |
| 5 | +import { genTime, signJWT, TestingKeyFetcher } from './jwk-utils'; |
6 | 6 |
|
7 | 7 | describe('FirebaseTokenVerifier', () => {
|
8 |
| - const kid = 'kid123456'; |
9 |
| - const projectId = 'projectId1234'; |
10 |
| - const currentTimestamp = genTime(Date.now()); |
11 |
| - const userId = 'userId12345'; |
12 |
| - const payload: FirebaseIdToken = { |
13 |
| - aud: projectId, |
14 |
| - exp: currentTimestamp + 9999, |
15 |
| - iat: currentTimestamp - 10000, // -10s |
16 |
| - iss: genIss(projectId), |
17 |
| - sub: userId, |
18 |
| - auth_time: currentTimestamp - 20000, // -20s |
19 |
| - uid: userId, |
20 |
| - firebase: { |
21 |
| - identities: {}, |
22 |
| - sign_in_provider: 'google.com', |
| 8 | + const testCases = [ |
| 9 | + { |
| 10 | + name: 'createIdTokenVerifier', |
| 11 | + tokenGenerator: generateIdToken, |
| 12 | + firebaseTokenVerifier: baseCreateIdTokenVerifier, |
23 | 13 | },
|
24 |
| - }; |
25 |
| - |
26 |
| - it.each([ |
27 |
| - ['valid without firebase emulator', payload], |
28 |
| - [ |
29 |
| - 'valid custom token without firebase emulator', |
30 |
| - { |
31 |
| - ...payload, |
32 |
| - aud: FIREBASE_AUDIENCE, |
33 |
| - }, |
34 |
| - ], |
35 |
| - ])('%s', async (_, payload) => { |
36 |
| - const testingKeyFetcher = await TestingKeyFetcher.withKeyPairGeneration(kid); |
37 |
| - const jwt = await signJWT(kid, payload, testingKeyFetcher.getPrivateKey()); |
38 |
| - |
39 |
| - const verifier = new PublicKeySignatureVerifier(testingKeyFetcher); |
40 |
| - const ftv = createFirebaseTokenVerifier(verifier, projectId); |
41 |
| - const token = await ftv.verifyJWT(jwt, false); |
| 14 | + { |
| 15 | + name: 'createSessionCookieVerifier', |
| 16 | + tokenGenerator: generateSessionCookie, |
| 17 | + firebaseTokenVerifier: baseCreateSessionCookieVerifier, |
| 18 | + }, |
| 19 | + ]; |
| 20 | + for (const tc of testCases) { |
| 21 | + describe(tc.name, () => { |
| 22 | + const currentTimestamp = genTime(Date.now()); |
42 | 23 |
|
43 |
| - expect(token).toStrictEqual(payload); |
44 |
| - }); |
| 24 | + it.each([ |
| 25 | + ['valid without firebase emulator', tc.tokenGenerator(currentTimestamp)], |
| 26 | + [ |
| 27 | + 'valid custom token without firebase emulator', |
| 28 | + tc.tokenGenerator(currentTimestamp, { aud: FIREBASE_AUDIENCE }), |
| 29 | + ], |
| 30 | + ])('%s', async (_, promise) => { |
| 31 | + const payload = await promise; |
| 32 | + const pair = await createTestingSignVerifyPair(payload); |
| 33 | + const ftv = tc.firebaseTokenVerifier(pair.verifier, projectId); |
| 34 | + const token = await ftv.verifyJWT(pair.jwt, false); |
45 | 35 |
|
46 |
| - it('valid with firebase emulator', async () => { |
47 |
| - const testingKeyFetcher = await TestingKeyFetcher.withKeyPairGeneration(kid); |
| 36 | + expect(token).toStrictEqual(payload); |
| 37 | + }); |
48 | 38 |
|
49 |
| - // sign as invalid private key with fetched public key |
50 |
| - const keyPair = await crypto.subtle.generateKey(rs256alg, true, ['sign', 'verify']); |
| 39 | + it.each([ |
| 40 | + ['aud', tc.tokenGenerator(currentTimestamp, { aud: 'unknown' }), 'has incorrect "aud" (audience) claim.'], |
| 41 | + [ |
| 42 | + 'iss', |
| 43 | + tc.tokenGenerator(currentTimestamp, { |
| 44 | + iss: projectId, // set just projectId |
| 45 | + }), |
| 46 | + 'has incorrect "iss" (issuer) claim.', |
| 47 | + ], |
| 48 | + [ |
| 49 | + 'sub', |
| 50 | + tc.tokenGenerator(currentTimestamp, { |
| 51 | + sub: 'x'.repeat(129), |
| 52 | + }), |
| 53 | + 'has "sub" (subject) claim longer than 128 characters.', |
| 54 | + ], |
| 55 | + [ |
| 56 | + 'auth_time', |
| 57 | + tc.tokenGenerator(currentTimestamp, { |
| 58 | + auth_time: undefined, |
| 59 | + }), |
| 60 | + 'has no "auth_time" claim.', |
| 61 | + ], |
| 62 | + [ |
| 63 | + 'auth_time is in future', |
| 64 | + tc.tokenGenerator(currentTimestamp, { |
| 65 | + auth_time: currentTimestamp + 3000, // +3s |
| 66 | + }), |
| 67 | + 'has incorrect "auth_time" claim.', |
| 68 | + ], |
| 69 | + ])('invalid verifyPayload %s', async (_, promise, wantContainMsg) => { |
| 70 | + const payload = await promise; |
| 71 | + const pair = await createTestingSignVerifyPair(payload); |
| 72 | + const ftv = tc.firebaseTokenVerifier(pair.verifier, projectId); |
| 73 | + expect(() => ftv.verifyJWT(pair.jwt, false)).rejects.toThrowError(wantContainMsg); |
| 74 | + }); |
51 | 75 |
|
52 |
| - // set with invalid kid because jwt does not contain kid which issued from firebase emulator. |
53 |
| - const jwt = await signJWT('invalid-kid', payload, keyPair.privateKey); |
| 76 | + it('valid with firebase emulator', async () => { |
| 77 | + const payload = await tc.tokenGenerator(currentTimestamp); |
| 78 | + const testingKeyFetcher = await TestingKeyFetcher.withKeyPairGeneration('valid-kid'); |
54 | 79 |
|
55 |
| - const verifier = new PublicKeySignatureVerifier(testingKeyFetcher); |
56 |
| - const ftv = createFirebaseTokenVerifier(verifier, projectId); |
| 80 | + // sign as invalid private key with fetched public key |
| 81 | + const keyPair = await crypto.subtle.generateKey(rs256alg, true, ['sign', 'verify']); |
57 | 82 |
|
58 |
| - // firebase emulator ignores signature verification step. |
59 |
| - const token = await ftv.verifyJWT(jwt, true); |
| 83 | + // set with invalid kid because jwt does not contain kid which issued from firebase emulator. |
| 84 | + const jwt = await signJWT('invalid-kid', payload, keyPair.privateKey); |
60 | 85 |
|
61 |
| - expect(token).toStrictEqual(payload); |
62 |
| - }); |
| 86 | + const verifier = new PublicKeySignatureVerifier(testingKeyFetcher); |
| 87 | + const ftv = tc.firebaseTokenVerifier(verifier, projectId); |
63 | 88 |
|
64 |
| - it.each([ |
65 |
| - [ |
66 |
| - 'aud', |
67 |
| - { |
68 |
| - ...payload, |
69 |
| - aud: 'unknown', |
70 |
| - }, |
71 |
| - 'has incorrect "aud" (audience) claim.', |
72 |
| - ], |
73 |
| - [ |
74 |
| - 'iss', |
75 |
| - { |
76 |
| - ...payload, |
77 |
| - iss: projectId, // set just projectId |
78 |
| - }, |
79 |
| - 'has incorrect "iss" (issuer) claim.', |
80 |
| - ], |
81 |
| - [ |
82 |
| - 'sub', |
83 |
| - { |
84 |
| - ...payload, |
85 |
| - sub: 'x'.repeat(129), |
86 |
| - }, |
87 |
| - 'has "sub" (subject) claim longer than 128 characters.', |
88 |
| - ], |
89 |
| - [ |
90 |
| - 'auth_time', |
91 |
| - { |
92 |
| - ...payload, |
93 |
| - auth_time: undefined, |
94 |
| - }, |
95 |
| - 'has no "auth_time" claim.', |
96 |
| - ], |
97 |
| - [ |
98 |
| - 'auth_time is in future', |
99 |
| - { |
100 |
| - ...payload, |
101 |
| - auth_time: currentTimestamp + 3000, // +3s |
102 |
| - }, |
103 |
| - 'has incorrect "auth_time" claim.', |
104 |
| - ], |
105 |
| - ])('invalid verifyPayload %s', async (_, payload, wantContainMsg) => { |
106 |
| - const testingKeyFetcher = await TestingKeyFetcher.withKeyPairGeneration(kid); |
107 |
| - const jwt = await signJWT(kid, payload, testingKeyFetcher.getPrivateKey()); |
| 89 | + // firebase emulator ignores signature verification step. |
| 90 | + const token = await ftv.verifyJWT(jwt, true); |
108 | 91 |
|
109 |
| - const verifier = new PublicKeySignatureVerifier(testingKeyFetcher); |
110 |
| - const ftv = createFirebaseTokenVerifier(verifier, projectId); |
111 |
| - expect(() => ftv.verifyJWT(jwt, false)).rejects.toThrowError(wantContainMsg); |
112 |
| - }); |
| 92 | + expect(token).toStrictEqual(payload); |
| 93 | + }); |
| 94 | + }); |
| 95 | + } |
113 | 96 | });
|
0 commit comments