@geekmidas/auth 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/{cacheTokenStorage-Ca05vf3T.mjs → cacheTokenStorage-16NE8Cd9.mjs} +3 -3
  3. package/dist/cacheTokenStorage-16NE8Cd9.mjs.map +1 -0
  4. package/dist/{cacheTokenStorage-BzBRJfd-.d.mts → cacheTokenStorage-B7sOoysu.d.cts} +8 -6
  5. package/dist/cacheTokenStorage-B7sOoysu.d.cts.map +1 -0
  6. package/dist/{cacheTokenStorage-CvptztkW.cjs → cacheTokenStorage-CEpMniTi.cjs} +3 -3
  7. package/dist/cacheTokenStorage-CEpMniTi.cjs.map +1 -0
  8. package/dist/{cacheTokenStorage-yNUpDD8W.d.cts → cacheTokenStorage-_tq4Kmhc.d.mts} +8 -6
  9. package/dist/cacheTokenStorage-_tq4Kmhc.d.mts.map +1 -0
  10. package/dist/cacheTokenStorage.cjs +1 -1
  11. package/dist/cacheTokenStorage.d.cts +1 -1
  12. package/dist/cacheTokenStorage.d.mts +1 -1
  13. package/dist/cacheTokenStorage.mjs +1 -1
  14. package/dist/{client-DaiUm0UZ.cjs → client-BPl_mMvb.cjs} +14 -8
  15. package/dist/client-BPl_mMvb.cjs.map +1 -0
  16. package/dist/{client-DjNZS6vZ.mjs → client-eidYNJSH.mjs} +14 -8
  17. package/dist/client-eidYNJSH.mjs.map +1 -0
  18. package/dist/client.cjs +2 -2
  19. package/dist/client.d.cts +1 -1
  20. package/dist/client.d.mts +1 -1
  21. package/dist/client.mjs +2 -2
  22. package/dist/hono/jwt.cjs +1 -1
  23. package/dist/hono/jwt.cjs.map +1 -1
  24. package/dist/hono/jwt.d.cts +2 -1
  25. package/dist/hono/jwt.d.cts.map +1 -0
  26. package/dist/hono/jwt.d.mts +2 -1
  27. package/dist/hono/jwt.d.mts.map +1 -0
  28. package/dist/hono/jwt.mjs +1 -1
  29. package/dist/hono/jwt.mjs.map +1 -1
  30. package/dist/hono/oidc.cjs +1 -1
  31. package/dist/hono/oidc.cjs.map +1 -1
  32. package/dist/hono/oidc.d.cts +3 -2
  33. package/dist/hono/oidc.d.cts.map +1 -0
  34. package/dist/hono/oidc.d.mts +3 -2
  35. package/dist/hono/oidc.d.mts.map +1 -0
  36. package/dist/hono/oidc.mjs +1 -1
  37. package/dist/hono/oidc.mjs.map +1 -1
  38. package/dist/index.cjs +3 -3
  39. package/dist/index.d.cts +2 -2
  40. package/dist/index.d.mts +2 -2
  41. package/dist/index.mjs +3 -3
  42. package/dist/{jwt-DF9kFbmh.d.cts → jwt-3eIuY2dg.d.cts} +2 -1
  43. package/dist/jwt-3eIuY2dg.d.cts.map +1 -0
  44. package/dist/jwt-Bo_eACEi.cjs.map +1 -1
  45. package/dist/{jwt-DH8CJ2xC.d.mts → jwt-BqLlRkw-.d.mts} +2 -1
  46. package/dist/jwt-BqLlRkw-.d.mts.map +1 -0
  47. package/dist/jwt-D88cPJyT.mjs.map +1 -1
  48. package/dist/jwt.d.cts +1 -1
  49. package/dist/jwt.d.mts +1 -1
  50. package/dist/lambda/jwt.cjs +2 -2
  51. package/dist/lambda/jwt.cjs.map +1 -1
  52. package/dist/lambda/jwt.d.cts +2 -1
  53. package/dist/lambda/jwt.d.cts.map +1 -0
  54. package/dist/lambda/jwt.d.mts +2 -1
  55. package/dist/lambda/jwt.d.mts.map +1 -0
  56. package/dist/lambda/jwt.mjs +2 -2
  57. package/dist/lambda/jwt.mjs.map +1 -1
  58. package/dist/lambda/oidc.cjs +2 -2
  59. package/dist/lambda/oidc.cjs.map +1 -1
  60. package/dist/lambda/oidc.d.cts +3 -2
  61. package/dist/lambda/oidc.d.cts.map +1 -0
  62. package/dist/lambda/oidc.d.mts +3 -2
  63. package/dist/lambda/oidc.d.mts.map +1 -0
  64. package/dist/lambda/oidc.mjs +2 -2
  65. package/dist/lambda/oidc.mjs.map +1 -1
  66. package/dist/oidc-C0KyYaAK.cjs.map +1 -1
  67. package/dist/{oidc-CKOGLxjV.d.mts → oidc-ChA17wLB.d.cts} +3 -2
  68. package/dist/oidc-ChA17wLB.d.cts.map +1 -0
  69. package/dist/{oidc-DDLIB5gb.d.cts → oidc-DTRgBBl6.d.mts} +3 -2
  70. package/dist/oidc-DTRgBBl6.d.mts.map +1 -0
  71. package/dist/oidc-KA30gMlx.mjs.map +1 -1
  72. package/dist/oidc.d.cts +2 -2
  73. package/dist/oidc.d.mts +2 -2
  74. package/dist/server.cjs +2 -2
  75. package/dist/server.d.cts +2 -2
  76. package/dist/server.d.mts +2 -2
  77. package/dist/server.mjs +2 -2
  78. package/dist/{tokenManager--Q93eNYr.d.mts → tokenManager-CL3yE06D.d.cts} +2 -1
  79. package/dist/tokenManager-CL3yE06D.d.cts.map +1 -0
  80. package/dist/{tokenManager-BVXxfHRa.mjs → tokenManager-Ci2Tgxp8.mjs} +5 -3
  81. package/dist/tokenManager-Ci2Tgxp8.mjs.map +1 -0
  82. package/dist/{tokenManager-EPmwgai2.cjs → tokenManager-CvEMgxOp.cjs} +5 -3
  83. package/dist/tokenManager-CvEMgxOp.cjs.map +1 -0
  84. package/dist/{tokenManager-H6-lWEah.d.cts → tokenManager-RrwjL0y-.d.mts} +2 -1
  85. package/dist/tokenManager-RrwjL0y-.d.mts.map +1 -0
  86. package/dist/tokenManager.cjs +1 -1
  87. package/dist/tokenManager.d.cts +1 -1
  88. package/dist/tokenManager.d.mts +1 -1
  89. package/dist/tokenManager.mjs +1 -1
  90. package/package.json +3 -3
  91. package/src/__tests__/cacheTokenStorage.spec.ts +148 -148
  92. package/src/__tests__/client.spec.ts +339 -339
  93. package/src/__tests__/hono-jwt.spec.ts +219 -219
  94. package/src/__tests__/hono-oidc.spec.ts +560 -560
  95. package/src/__tests__/jwt.spec.ts +150 -127
  96. package/src/__tests__/lambda-jwt.spec.ts +205 -205
  97. package/src/__tests__/lambda-oidc.spec.ts +580 -579
  98. package/src/__tests__/oidc.spec.ts +365 -365
  99. package/src/__tests__/tokenManager.spec.ts +254 -254
  100. package/src/cacheTokenStorage.ts +32 -32
  101. package/src/client.ts +199 -190
  102. package/src/hono/jwt.ts +111 -111
  103. package/src/hono/oidc.ts +131 -131
  104. package/src/index.ts +2 -2
  105. package/src/jwt.ts +57 -57
  106. package/src/lambda/jwt.ts +170 -170
  107. package/src/lambda/oidc.ts +165 -165
  108. package/src/oidc.ts +121 -121
  109. package/src/server.ts +6 -7
  110. package/src/tokenManager.ts +89 -87
  111. package/tsconfig.json +10 -0
  112. package/dist/cacheTokenStorage-Ca05vf3T.mjs.map +0 -1
  113. package/dist/cacheTokenStorage-CvptztkW.cjs.map +0 -1
  114. package/dist/client-DaiUm0UZ.cjs.map +0 -1
  115. package/dist/client-DjNZS6vZ.mjs.map +0 -1
  116. package/dist/tokenManager-BVXxfHRa.mjs.map +0 -1
  117. package/dist/tokenManager-EPmwgai2.cjs.map +0 -1
@@ -1,405 +1,405 @@
1
1
  // @vitest-environment node
2
2
  import * as jose from 'jose';
3
- import { http, HttpResponse } from 'msw';
3
+ import { HttpResponse, http } from 'msw';
4
4
  import { setupServer } from 'msw/node';
5
5
  import {
6
- afterAll,
7
- afterEach,
8
- beforeAll,
9
- describe,
10
- expect,
11
- it,
12
- vi,
6
+ afterAll,
7
+ afterEach,
8
+ beforeAll,
9
+ describe,
10
+ expect,
11
+ it,
12
+ vi,
13
13
  } from 'vitest';
14
14
  import { OidcVerifier } from '../oidc';
15
15
 
16
16
  // Mock discovery document
17
17
  const mockDiscovery = {
18
- issuer: 'https://auth.example.com',
19
- authorization_endpoint: 'https://auth.example.com/authorize',
20
- token_endpoint: 'https://auth.example.com/token',
21
- userinfo_endpoint: 'https://auth.example.com/userinfo',
22
- jwks_uri: 'https://auth.example.com/.well-known/jwks.json',
23
- scopes_supported: ['openid', 'profile', 'email'],
24
- response_types_supported: ['code', 'token'],
25
- claims_supported: ['sub', 'name', 'email'],
18
+ issuer: 'https://auth.example.com',
19
+ authorization_endpoint: 'https://auth.example.com/authorize',
20
+ token_endpoint: 'https://auth.example.com/token',
21
+ userinfo_endpoint: 'https://auth.example.com/userinfo',
22
+ jwks_uri: 'https://auth.example.com/.well-known/jwks.json',
23
+ scopes_supported: ['openid', 'profile', 'email'],
24
+ response_types_supported: ['code', 'token'],
25
+ claims_supported: ['sub', 'name', 'email'],
26
26
  };
27
27
 
28
28
  // Mock user info
29
29
  const mockUserInfo = {
30
- sub: 'user-123',
31
- name: 'Test User',
32
- email: 'test@example.com',
33
- email_verified: true,
30
+ sub: 'user-123',
31
+ name: 'Test User',
32
+ email: 'test@example.com',
33
+ email_verified: true,
34
34
  };
35
35
 
36
36
  // Test keys - stored in object so the mock can access after initialization
37
37
  const testKeys: {
38
- privateKey: jose.KeyLike | null;
39
- publicKey: jose.KeyLike | null;
38
+ privateKey: jose.KeyLike | null;
39
+ publicKey: jose.KeyLike | null;
40
40
  } = {
41
- privateKey: null,
42
- publicKey: null,
41
+ privateKey: null,
42
+ publicKey: null,
43
43
  };
44
44
  let jwks: jose.JSONWebKeySet;
45
45
 
46
46
  async function setupKeys() {
47
- const keyPair = await jose.generateKeyPair('RS256');
48
- testKeys.privateKey = keyPair.privateKey;
49
- testKeys.publicKey = keyPair.publicKey;
50
- const publicJwk = await jose.exportJWK(keyPair.publicKey);
51
- jwks = {
52
- keys: [{ ...publicJwk, kid: 'test-key-id', use: 'sig', alg: 'RS256' }],
53
- };
47
+ const keyPair = await jose.generateKeyPair('RS256');
48
+ testKeys.privateKey = keyPair.privateKey;
49
+ testKeys.publicKey = keyPair.publicKey;
50
+ const publicJwk = await jose.exportJWK(keyPair.publicKey);
51
+ jwks = {
52
+ keys: [{ ...publicJwk, kid: 'test-key-id', use: 'sig', alg: 'RS256' }],
53
+ };
54
54
  }
55
55
 
56
56
  // Mock createRemoteJWKSet to return test keys directly.
57
57
  // MSW intercepts jose's fetch in standalone scripts but fails in vitest for unknown reasons.
58
58
  // TODO: Investigate vitest/MSW/jose interaction and remove this mock if possible.
59
59
  vi.mock('jose', async (importOriginal) => {
60
- const actual = await importOriginal<typeof jose>();
61
- return {
62
- ...actual,
63
- createRemoteJWKSet: () => {
64
- return async (protectedHeader: jose.JWSHeaderParameters) => {
65
- if (protectedHeader.kid === 'test-key-id' && testKeys.publicKey) {
66
- return testKeys.publicKey;
67
- }
68
- throw new Error(`Unknown key ID: ${protectedHeader.kid}`);
69
- };
70
- },
71
- };
60
+ const actual = await importOriginal<typeof jose>();
61
+ return {
62
+ ...actual,
63
+ createRemoteJWKSet: () => {
64
+ return async (protectedHeader: jose.JWSHeaderParameters) => {
65
+ if (protectedHeader.kid === 'test-key-id' && testKeys.publicKey) {
66
+ return testKeys.publicKey;
67
+ }
68
+ throw new Error(`Unknown key ID: ${protectedHeader.kid}`);
69
+ };
70
+ },
71
+ };
72
72
  });
73
73
 
74
74
  async function createTestToken(
75
- claims: Record<string, unknown> = {},
76
- options: { expiresIn?: string } = {},
75
+ claims: Record<string, unknown> = {},
76
+ options: { expiresIn?: string } = {},
77
77
  ) {
78
- if (!testKeys.privateKey) {
79
- throw new Error('Keys not initialized');
80
- }
81
- return await new jose.SignJWT({ sub: 'user-123', ...claims })
82
- .setProtectedHeader({ alg: 'RS256', kid: 'test-key-id' })
83
- .setIssuedAt()
84
- .setIssuer('https://auth.example.com')
85
- .setAudience('my-api')
86
- .setExpirationTime(options.expiresIn ?? '1h')
87
- .sign(testKeys.privateKey);
78
+ if (!testKeys.privateKey) {
79
+ throw new Error('Keys not initialized');
80
+ }
81
+ return await new jose.SignJWT({ sub: 'user-123', ...claims })
82
+ .setProtectedHeader({ alg: 'RS256', kid: 'test-key-id' })
83
+ .setIssuedAt()
84
+ .setIssuer('https://auth.example.com')
85
+ .setAudience('my-api')
86
+ .setExpirationTime(options.expiresIn ?? '1h')
87
+ .sign(testKeys.privateKey);
88
88
  }
89
89
 
90
90
  // MSW server for discovery, userinfo, and other HTTP endpoints
91
91
  const server = setupServer(
92
- http.get('https://auth.example.com/.well-known/openid-configuration', () => {
93
- return HttpResponse.json(mockDiscovery);
94
- }),
95
- http.get('https://auth.example.com/.well-known/jwks.json', () => {
96
- return HttpResponse.json(jwks);
97
- }),
98
- http.get('https://auth.example.com/userinfo', () => {
99
- return HttpResponse.json(mockUserInfo);
100
- }),
101
- http.get(
102
- 'https://other-auth.example.com/.well-known/openid-configuration',
103
- () => {
104
- return HttpResponse.json({
105
- ...mockDiscovery,
106
- issuer: 'https://other-auth.example.com',
107
- jwks_uri: 'https://other-auth.example.com/.well-known/jwks.json',
108
- });
109
- },
110
- ),
111
- http.get('https://other-auth.example.com/.well-known/jwks.json', () => {
112
- return HttpResponse.json(jwks);
113
- }),
92
+ http.get('https://auth.example.com/.well-known/openid-configuration', () => {
93
+ return HttpResponse.json(mockDiscovery);
94
+ }),
95
+ http.get('https://auth.example.com/.well-known/jwks.json', () => {
96
+ return HttpResponse.json(jwks);
97
+ }),
98
+ http.get('https://auth.example.com/userinfo', () => {
99
+ return HttpResponse.json(mockUserInfo);
100
+ }),
101
+ http.get(
102
+ 'https://other-auth.example.com/.well-known/openid-configuration',
103
+ () => {
104
+ return HttpResponse.json({
105
+ ...mockDiscovery,
106
+ issuer: 'https://other-auth.example.com',
107
+ jwks_uri: 'https://other-auth.example.com/.well-known/jwks.json',
108
+ });
109
+ },
110
+ ),
111
+ http.get('https://other-auth.example.com/.well-known/jwks.json', () => {
112
+ return HttpResponse.json(jwks);
113
+ }),
114
114
  );
115
115
 
116
116
  describe('OidcVerifier', () => {
117
- beforeAll(async () => {
118
- await setupKeys();
119
- server.listen({ onUnhandledRequest: 'error' });
120
- });
121
-
122
- afterEach(() => {
123
- server.resetHandlers();
124
- });
125
-
126
- afterAll(() => {
127
- server.close();
128
- });
129
-
130
- describe('getDiscovery', () => {
131
- it('should fetch OIDC discovery document', async () => {
132
- const verifier = new OidcVerifier({
133
- issuer: 'https://auth.example.com',
134
- audience: 'my-api',
135
- });
136
-
137
- const discovery = await verifier.getDiscovery();
138
-
139
- expect(discovery.issuer).toBe('https://auth.example.com');
140
- expect(discovery.jwks_uri).toBe(
141
- 'https://auth.example.com/.well-known/jwks.json',
142
- );
143
- });
144
-
145
- it('should cache discovery document by default', async () => {
146
- let callCount = 0;
147
- server.use(
148
- http.get(
149
- 'https://auth.example.com/.well-known/openid-configuration',
150
- () => {
151
- callCount++;
152
- return HttpResponse.json(mockDiscovery);
153
- },
154
- ),
155
- );
156
-
157
- const verifier = new OidcVerifier({
158
- issuer: 'https://auth.example.com',
159
- audience: 'my-api',
160
- });
161
-
162
- await verifier.getDiscovery();
163
- await verifier.getDiscovery();
164
-
165
- expect(callCount).toBe(1);
166
- });
167
-
168
- it('should handle issuer with trailing slash', async () => {
169
- let requestedUrl = '';
170
- server.use(
171
- http.get(
172
- 'https://auth.example.com/.well-known/openid-configuration',
173
- ({ request }) => {
174
- requestedUrl = request.url;
175
- return HttpResponse.json(mockDiscovery);
176
- },
177
- ),
178
- );
179
-
180
- const verifier = new OidcVerifier({
181
- issuer: 'https://auth.example.com/',
182
- audience: 'my-api',
183
- });
184
-
185
- await verifier.getDiscovery();
186
-
187
- expect(requestedUrl).toBe(
188
- 'https://auth.example.com/.well-known/openid-configuration',
189
- );
190
- });
191
-
192
- it('should throw on failed discovery fetch', async () => {
193
- server.use(
194
- http.get(
195
- 'https://auth.example.com/.well-known/openid-configuration',
196
- () => {
197
- return new HttpResponse(null, {
198
- status: 500,
199
- statusText: 'Internal Server Error',
200
- });
201
- },
202
- ),
203
- );
204
-
205
- const verifier = new OidcVerifier({
206
- issuer: 'https://auth.example.com',
207
- audience: 'my-api',
208
- });
209
-
210
- await expect(verifier.getDiscovery()).rejects.toThrow(
211
- 'Failed to fetch OIDC discovery',
212
- );
213
- });
214
- });
215
-
216
- describe('verify', () => {
217
- it('should verify a valid token', async () => {
218
- const verifier = new OidcVerifier({
219
- issuer: 'https://auth.example.com',
220
- audience: 'my-api',
221
- });
222
- const token = await createTestToken();
223
-
224
- const claims = await verifier.verify(token);
225
-
226
- expect(claims.sub).toBe('user-123');
227
- });
228
-
229
- it('should throw for invalid token', async () => {
230
- const verifier = new OidcVerifier({
231
- issuer: 'https://auth.example.com',
232
- audience: 'my-api',
233
- });
234
-
235
- await expect(verifier.verify('invalid-token')).rejects.toThrow();
236
- });
237
-
238
- it('should throw for expired token', async () => {
239
- const verifier = new OidcVerifier({
240
- issuer: 'https://auth.example.com',
241
- audience: 'my-api',
242
- });
243
- const token = await createTestToken({}, { expiresIn: '-1h' });
244
-
245
- await expect(verifier.verify(token)).rejects.toThrow();
246
- });
247
-
248
- it('should throw for wrong issuer', async () => {
249
- const verifier = new OidcVerifier({
250
- issuer: 'https://other-auth.example.com',
251
- audience: 'my-api',
252
- });
253
- const token = await createTestToken();
254
-
255
- await expect(verifier.verify(token)).rejects.toThrow();
256
- });
257
-
258
- it('should throw for wrong audience', async () => {
259
- const verifier = new OidcVerifier({
260
- issuer: 'https://auth.example.com',
261
- audience: 'other-api',
262
- });
263
- const token = await createTestToken();
264
-
265
- await expect(verifier.verify(token)).rejects.toThrow();
266
- });
267
- });
268
-
269
- describe('verifyOrNull', () => {
270
- it('should return claims for valid token', async () => {
271
- const verifier = new OidcVerifier({
272
- issuer: 'https://auth.example.com',
273
- audience: 'my-api',
274
- });
275
- const token = await createTestToken();
276
-
277
- const claims = await verifier.verifyOrNull(token);
278
-
279
- expect(claims).not.toBeNull();
280
- expect(claims?.sub).toBe('user-123');
281
- });
282
-
283
- it('should return null for invalid token', async () => {
284
- const verifier = new OidcVerifier({
285
- issuer: 'https://auth.example.com',
286
- audience: 'my-api',
287
- });
288
-
289
- const claims = await verifier.verifyOrNull('invalid-token');
290
-
291
- expect(claims).toBeNull();
292
- });
293
- });
294
-
295
- describe('fetchUserInfo', () => {
296
- it('should fetch user info with valid token', async () => {
297
- const verifier = new OidcVerifier({
298
- issuer: 'https://auth.example.com',
299
- audience: 'my-api',
300
- });
301
- const token = await createTestToken();
302
-
303
- const userInfo = await verifier.fetchUserInfo(token);
304
-
305
- expect(userInfo).not.toBeNull();
306
- expect(userInfo?.sub).toBe('user-123');
307
- expect(userInfo?.name).toBe('Test User');
308
- expect(userInfo?.email).toBe('test@example.com');
309
- });
310
-
311
- it('should return null when userinfo endpoint is not available', async () => {
312
- server.use(
313
- http.get(
314
- 'https://auth.example.com/.well-known/openid-configuration',
315
- () => {
316
- return HttpResponse.json({
317
- ...mockDiscovery,
318
- userinfo_endpoint: undefined,
319
- });
320
- },
321
- ),
322
- );
323
-
324
- const verifier = new OidcVerifier({
325
- issuer: 'https://auth.example.com',
326
- audience: 'my-api',
327
- });
328
-
329
- const userInfo = await verifier.fetchUserInfo('some-token');
330
-
331
- expect(userInfo).toBeNull();
332
- });
333
-
334
- it('should return null on userinfo fetch error', async () => {
335
- server.use(
336
- http.get('https://auth.example.com/userinfo', () => {
337
- return new HttpResponse(null, { status: 401 });
338
- }),
339
- );
340
-
341
- const verifier = new OidcVerifier({
342
- issuer: 'https://auth.example.com',
343
- audience: 'my-api',
344
- });
345
-
346
- const userInfo = await verifier.fetchUserInfo('invalid-token');
347
-
348
- expect(userInfo).toBeNull();
349
- });
350
- });
351
-
352
- describe('clearCache', () => {
353
- it('should clear cached discovery and jwks', async () => {
354
- let callCount = 0;
355
- server.use(
356
- http.get(
357
- 'https://auth.example.com/.well-known/openid-configuration',
358
- () => {
359
- callCount++;
360
- return HttpResponse.json(mockDiscovery);
361
- },
362
- ),
363
- );
364
-
365
- const verifier = new OidcVerifier({
366
- issuer: 'https://auth.example.com',
367
- audience: 'my-api',
368
- });
369
-
370
- await verifier.getDiscovery();
371
- expect(callCount).toBe(1);
372
-
373
- verifier.clearCache();
374
-
375
- await verifier.getDiscovery();
376
- expect(callCount).toBe(2);
377
- });
378
- });
379
-
380
- describe('cacheDiscovery option', () => {
381
- it('should not cache when cacheDiscovery is false', async () => {
382
- let callCount = 0;
383
- server.use(
384
- http.get(
385
- 'https://auth.example.com/.well-known/openid-configuration',
386
- () => {
387
- callCount++;
388
- return HttpResponse.json(mockDiscovery);
389
- },
390
- ),
391
- );
392
-
393
- const verifier = new OidcVerifier({
394
- issuer: 'https://auth.example.com',
395
- audience: 'my-api',
396
- cacheDiscovery: false,
397
- });
398
-
399
- await verifier.getDiscovery();
400
- await verifier.getDiscovery();
401
-
402
- expect(callCount).toBe(2);
403
- });
404
- });
117
+ beforeAll(async () => {
118
+ await setupKeys();
119
+ server.listen({ onUnhandledRequest: 'error' });
120
+ });
121
+
122
+ afterEach(() => {
123
+ server.resetHandlers();
124
+ });
125
+
126
+ afterAll(() => {
127
+ server.close();
128
+ });
129
+
130
+ describe('getDiscovery', () => {
131
+ it('should fetch OIDC discovery document', async () => {
132
+ const verifier = new OidcVerifier({
133
+ issuer: 'https://auth.example.com',
134
+ audience: 'my-api',
135
+ });
136
+
137
+ const discovery = await verifier.getDiscovery();
138
+
139
+ expect(discovery.issuer).toBe('https://auth.example.com');
140
+ expect(discovery.jwks_uri).toBe(
141
+ 'https://auth.example.com/.well-known/jwks.json',
142
+ );
143
+ });
144
+
145
+ it('should cache discovery document by default', async () => {
146
+ let callCount = 0;
147
+ server.use(
148
+ http.get(
149
+ 'https://auth.example.com/.well-known/openid-configuration',
150
+ () => {
151
+ callCount++;
152
+ return HttpResponse.json(mockDiscovery);
153
+ },
154
+ ),
155
+ );
156
+
157
+ const verifier = new OidcVerifier({
158
+ issuer: 'https://auth.example.com',
159
+ audience: 'my-api',
160
+ });
161
+
162
+ await verifier.getDiscovery();
163
+ await verifier.getDiscovery();
164
+
165
+ expect(callCount).toBe(1);
166
+ });
167
+
168
+ it('should handle issuer with trailing slash', async () => {
169
+ let requestedUrl = '';
170
+ server.use(
171
+ http.get(
172
+ 'https://auth.example.com/.well-known/openid-configuration',
173
+ ({ request }) => {
174
+ requestedUrl = request.url;
175
+ return HttpResponse.json(mockDiscovery);
176
+ },
177
+ ),
178
+ );
179
+
180
+ const verifier = new OidcVerifier({
181
+ issuer: 'https://auth.example.com/',
182
+ audience: 'my-api',
183
+ });
184
+
185
+ await verifier.getDiscovery();
186
+
187
+ expect(requestedUrl).toBe(
188
+ 'https://auth.example.com/.well-known/openid-configuration',
189
+ );
190
+ });
191
+
192
+ it('should throw on failed discovery fetch', async () => {
193
+ server.use(
194
+ http.get(
195
+ 'https://auth.example.com/.well-known/openid-configuration',
196
+ () => {
197
+ return new HttpResponse(null, {
198
+ status: 500,
199
+ statusText: 'Internal Server Error',
200
+ });
201
+ },
202
+ ),
203
+ );
204
+
205
+ const verifier = new OidcVerifier({
206
+ issuer: 'https://auth.example.com',
207
+ audience: 'my-api',
208
+ });
209
+
210
+ await expect(verifier.getDiscovery()).rejects.toThrow(
211
+ 'Failed to fetch OIDC discovery',
212
+ );
213
+ });
214
+ });
215
+
216
+ describe('verify', () => {
217
+ it('should verify a valid token', async () => {
218
+ const verifier = new OidcVerifier({
219
+ issuer: 'https://auth.example.com',
220
+ audience: 'my-api',
221
+ });
222
+ const token = await createTestToken();
223
+
224
+ const claims = await verifier.verify(token);
225
+
226
+ expect(claims.sub).toBe('user-123');
227
+ });
228
+
229
+ it('should throw for invalid token', async () => {
230
+ const verifier = new OidcVerifier({
231
+ issuer: 'https://auth.example.com',
232
+ audience: 'my-api',
233
+ });
234
+
235
+ await expect(verifier.verify('invalid-token')).rejects.toThrow();
236
+ });
237
+
238
+ it('should throw for expired token', async () => {
239
+ const verifier = new OidcVerifier({
240
+ issuer: 'https://auth.example.com',
241
+ audience: 'my-api',
242
+ });
243
+ const token = await createTestToken({}, { expiresIn: '-1h' });
244
+
245
+ await expect(verifier.verify(token)).rejects.toThrow();
246
+ });
247
+
248
+ it('should throw for wrong issuer', async () => {
249
+ const verifier = new OidcVerifier({
250
+ issuer: 'https://other-auth.example.com',
251
+ audience: 'my-api',
252
+ });
253
+ const token = await createTestToken();
254
+
255
+ await expect(verifier.verify(token)).rejects.toThrow();
256
+ });
257
+
258
+ it('should throw for wrong audience', async () => {
259
+ const verifier = new OidcVerifier({
260
+ issuer: 'https://auth.example.com',
261
+ audience: 'other-api',
262
+ });
263
+ const token = await createTestToken();
264
+
265
+ await expect(verifier.verify(token)).rejects.toThrow();
266
+ });
267
+ });
268
+
269
+ describe('verifyOrNull', () => {
270
+ it('should return claims for valid token', async () => {
271
+ const verifier = new OidcVerifier({
272
+ issuer: 'https://auth.example.com',
273
+ audience: 'my-api',
274
+ });
275
+ const token = await createTestToken();
276
+
277
+ const claims = await verifier.verifyOrNull(token);
278
+
279
+ expect(claims).not.toBeNull();
280
+ expect(claims?.sub).toBe('user-123');
281
+ });
282
+
283
+ it('should return null for invalid token', async () => {
284
+ const verifier = new OidcVerifier({
285
+ issuer: 'https://auth.example.com',
286
+ audience: 'my-api',
287
+ });
288
+
289
+ const claims = await verifier.verifyOrNull('invalid-token');
290
+
291
+ expect(claims).toBeNull();
292
+ });
293
+ });
294
+
295
+ describe('fetchUserInfo', () => {
296
+ it('should fetch user info with valid token', async () => {
297
+ const verifier = new OidcVerifier({
298
+ issuer: 'https://auth.example.com',
299
+ audience: 'my-api',
300
+ });
301
+ const token = await createTestToken();
302
+
303
+ const userInfo = await verifier.fetchUserInfo(token);
304
+
305
+ expect(userInfo).not.toBeNull();
306
+ expect(userInfo?.sub).toBe('user-123');
307
+ expect(userInfo?.name).toBe('Test User');
308
+ expect(userInfo?.email).toBe('test@example.com');
309
+ });
310
+
311
+ it('should return null when userinfo endpoint is not available', async () => {
312
+ server.use(
313
+ http.get(
314
+ 'https://auth.example.com/.well-known/openid-configuration',
315
+ () => {
316
+ return HttpResponse.json({
317
+ ...mockDiscovery,
318
+ userinfo_endpoint: undefined,
319
+ });
320
+ },
321
+ ),
322
+ );
323
+
324
+ const verifier = new OidcVerifier({
325
+ issuer: 'https://auth.example.com',
326
+ audience: 'my-api',
327
+ });
328
+
329
+ const userInfo = await verifier.fetchUserInfo('some-token');
330
+
331
+ expect(userInfo).toBeNull();
332
+ });
333
+
334
+ it('should return null on userinfo fetch error', async () => {
335
+ server.use(
336
+ http.get('https://auth.example.com/userinfo', () => {
337
+ return new HttpResponse(null, { status: 401 });
338
+ }),
339
+ );
340
+
341
+ const verifier = new OidcVerifier({
342
+ issuer: 'https://auth.example.com',
343
+ audience: 'my-api',
344
+ });
345
+
346
+ const userInfo = await verifier.fetchUserInfo('invalid-token');
347
+
348
+ expect(userInfo).toBeNull();
349
+ });
350
+ });
351
+
352
+ describe('clearCache', () => {
353
+ it('should clear cached discovery and jwks', async () => {
354
+ let callCount = 0;
355
+ server.use(
356
+ http.get(
357
+ 'https://auth.example.com/.well-known/openid-configuration',
358
+ () => {
359
+ callCount++;
360
+ return HttpResponse.json(mockDiscovery);
361
+ },
362
+ ),
363
+ );
364
+
365
+ const verifier = new OidcVerifier({
366
+ issuer: 'https://auth.example.com',
367
+ audience: 'my-api',
368
+ });
369
+
370
+ await verifier.getDiscovery();
371
+ expect(callCount).toBe(1);
372
+
373
+ verifier.clearCache();
374
+
375
+ await verifier.getDiscovery();
376
+ expect(callCount).toBe(2);
377
+ });
378
+ });
379
+
380
+ describe('cacheDiscovery option', () => {
381
+ it('should not cache when cacheDiscovery is false', async () => {
382
+ let callCount = 0;
383
+ server.use(
384
+ http.get(
385
+ 'https://auth.example.com/.well-known/openid-configuration',
386
+ () => {
387
+ callCount++;
388
+ return HttpResponse.json(mockDiscovery);
389
+ },
390
+ ),
391
+ );
392
+
393
+ const verifier = new OidcVerifier({
394
+ issuer: 'https://auth.example.com',
395
+ audience: 'my-api',
396
+ cacheDiscovery: false,
397
+ });
398
+
399
+ await verifier.getDiscovery();
400
+ await verifier.getDiscovery();
401
+
402
+ expect(callCount).toBe(2);
403
+ });
404
+ });
405
405
  });