@geekmidas/auth 0.0.5 → 0.2.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/README.md +343 -214
  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 +10 -5
  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,174 +1,197 @@
1
1
  import * as jose from 'jose';
2
2
  import { describe, expect, it } from 'vitest';
3
- import { JwtVerifier, decodeJwt } from '../jwt';
3
+ import { decodeJwt, JwtVerifier } from '../jwt';
4
4
 
5
5
  const TEST_SECRET = 'super-secret-key-for-testing-only-32chars';
6
6
 
7
7
  async function createTestToken(
8
- claims: Record<string, unknown> = {},
9
- options: {
10
- secret?: string;
11
- expiresIn?: string;
12
- issuer?: string;
13
- audience?: string;
14
- } = {},
8
+ claims: Record<string, unknown> = {},
9
+ options: {
10
+ secret?: string;
11
+ expiresIn?: string;
12
+ issuer?: string;
13
+ audience?: string;
14
+ } = {},
15
15
  ) {
16
- const secret = new TextEncoder().encode(options.secret ?? TEST_SECRET);
17
- const jwt = new jose.SignJWT({ sub: 'user-123', ...claims })
18
- .setProtectedHeader({ alg: 'HS256' })
19
- .setIssuedAt();
20
-
21
- if (options.expiresIn) {
22
- jwt.setExpirationTime(options.expiresIn);
23
- } else {
24
- jwt.setExpirationTime('1h');
25
- }
26
-
27
- if (options.issuer) {
28
- jwt.setIssuer(options.issuer);
29
- }
30
-
31
- if (options.audience) {
32
- jwt.setAudience(options.audience);
33
- }
34
-
35
- return await jwt.sign(secret);
16
+ const secret = new TextEncoder().encode(options.secret ?? TEST_SECRET);
17
+ const jwt = new jose.SignJWT({ sub: 'user-123', ...claims })
18
+ .setProtectedHeader({ alg: 'HS256' })
19
+ .setIssuedAt();
20
+
21
+ if (options.expiresIn) {
22
+ jwt.setExpirationTime(options.expiresIn);
23
+ } else {
24
+ jwt.setExpirationTime('1h');
25
+ }
26
+
27
+ if (options.issuer) {
28
+ jwt.setIssuer(options.issuer);
29
+ }
30
+
31
+ if (options.audience) {
32
+ jwt.setAudience(options.audience);
33
+ }
34
+
35
+ return await jwt.sign(secret);
36
36
  }
37
37
 
38
38
  describe('JwtVerifier', () => {
39
- describe('with secret', () => {
40
- it('should verify a valid token', async () => {
41
- const verifier = new JwtVerifier({ secret: TEST_SECRET });
42
- const token = await createTestToken();
39
+ describe('with secret', () => {
40
+ it('should verify a valid token', async () => {
41
+ const verifier = new JwtVerifier({ secret: TEST_SECRET });
42
+ const token = await createTestToken();
43
43
 
44
- const claims = await verifier.verify(token);
44
+ const claims = await verifier.verify(token);
45
45
 
46
- expect(claims.sub).toBe('user-123');
47
- });
46
+ expect(claims.sub).toBe('user-123');
47
+ });
48
48
 
49
- it('should throw for invalid token', async () => {
50
- const verifier = new JwtVerifier({ secret: TEST_SECRET });
49
+ it('should throw for invalid token', async () => {
50
+ const verifier = new JwtVerifier({ secret: TEST_SECRET });
51
51
 
52
- await expect(verifier.verify('invalid-token')).rejects.toThrow();
53
- });
52
+ await expect(verifier.verify('invalid-token')).rejects.toThrow();
53
+ });
54
54
 
55
- it('should throw for wrong secret', async () => {
56
- const verifier = new JwtVerifier({ secret: TEST_SECRET });
57
- const token = await createTestToken(
58
- {},
59
- { secret: 'wrong-secret-key-32-chars-long!!' },
60
- );
55
+ it('should throw for wrong secret', async () => {
56
+ const verifier = new JwtVerifier({ secret: TEST_SECRET });
57
+ const token = await createTestToken(
58
+ {},
59
+ { secret: 'wrong-secret-key-32-chars-long!!' },
60
+ );
61
61
 
62
- await expect(verifier.verify(token)).rejects.toThrow();
63
- });
62
+ await expect(verifier.verify(token)).rejects.toThrow();
63
+ });
64
64
 
65
- it('should throw for expired token', async () => {
66
- const verifier = new JwtVerifier({ secret: TEST_SECRET });
67
- const token = await createTestToken({}, { expiresIn: '-1h' });
65
+ it('should throw for expired token', async () => {
66
+ const verifier = new JwtVerifier({ secret: TEST_SECRET });
67
+ const token = await createTestToken({}, { expiresIn: '-1h' });
68
68
 
69
- await expect(verifier.verify(token)).rejects.toThrow();
70
- });
69
+ await expect(verifier.verify(token)).rejects.toThrow();
70
+ });
71
71
 
72
- it('should validate issuer when configured', async () => {
73
- const verifier = new JwtVerifier({
74
- secret: TEST_SECRET,
75
- issuer: 'my-app',
76
- });
72
+ it('should validate issuer when configured', async () => {
73
+ const verifier = new JwtVerifier({
74
+ secret: TEST_SECRET,
75
+ issuer: 'my-app',
76
+ });
77
77
 
78
- const validToken = await createTestToken({}, { issuer: 'my-app' });
79
- const invalidToken = await createTestToken({}, { issuer: 'other-app' });
78
+ const validToken = await createTestToken({}, { issuer: 'my-app' });
79
+ const invalidToken = await createTestToken({}, { issuer: 'other-app' });
80
80
 
81
- await expect(verifier.verify(validToken)).resolves.toBeDefined();
82
- await expect(verifier.verify(invalidToken)).rejects.toThrow();
83
- });
81
+ await expect(verifier.verify(validToken)).resolves.toBeDefined();
82
+ await expect(verifier.verify(invalidToken)).rejects.toThrow();
83
+ });
84
84
 
85
- it('should validate audience when configured', async () => {
86
- const verifier = new JwtVerifier({
87
- secret: TEST_SECRET,
88
- audience: 'my-api',
89
- });
85
+ it('should validate audience when configured', async () => {
86
+ const verifier = new JwtVerifier({
87
+ secret: TEST_SECRET,
88
+ audience: 'my-api',
89
+ });
90
90
 
91
- const validToken = await createTestToken({}, { audience: 'my-api' });
92
- const invalidToken = await createTestToken({}, { audience: 'other-api' });
91
+ const validToken = await createTestToken({}, { audience: 'my-api' });
92
+ const invalidToken = await createTestToken({}, { audience: 'other-api' });
93
93
 
94
- await expect(verifier.verify(validToken)).resolves.toBeDefined();
95
- await expect(verifier.verify(invalidToken)).rejects.toThrow();
96
- });
97
- });
94
+ await expect(verifier.verify(validToken)).resolves.toBeDefined();
95
+ await expect(verifier.verify(invalidToken)).rejects.toThrow();
96
+ });
97
+ });
98
98
 
99
- describe('verifyOrNull', () => {
100
- it('should return claims for valid token', async () => {
101
- const verifier = new JwtVerifier({ secret: TEST_SECRET });
102
- const token = await createTestToken();
99
+ describe('verifyOrNull', () => {
100
+ it('should return claims for valid token', async () => {
101
+ const verifier = new JwtVerifier({ secret: TEST_SECRET });
102
+ const token = await createTestToken();
103
103
 
104
- const claims = await verifier.verifyOrNull(token);
104
+ const claims = await verifier.verifyOrNull(token);
105
105
 
106
- expect(claims).not.toBeNull();
107
- expect(claims?.sub).toBe('user-123');
108
- });
106
+ expect(claims).not.toBeNull();
107
+ expect(claims?.sub).toBe('user-123');
108
+ });
109
109
 
110
- it('should return null for invalid token', async () => {
111
- const verifier = new JwtVerifier({ secret: TEST_SECRET });
110
+ it('should return null for invalid token', async () => {
111
+ const verifier = new JwtVerifier({ secret: TEST_SECRET });
112
112
 
113
- const claims = await verifier.verifyOrNull('invalid-token');
113
+ const claims = await verifier.verifyOrNull('invalid-token');
114
114
 
115
- expect(claims).toBeNull();
116
- });
115
+ expect(claims).toBeNull();
116
+ });
117
117
 
118
- it('should return null for expired token', async () => {
119
- const verifier = new JwtVerifier({ secret: TEST_SECRET });
120
- const token = await createTestToken({}, { expiresIn: '-1h' });
118
+ it('should return null for expired token', async () => {
119
+ const verifier = new JwtVerifier({ secret: TEST_SECRET });
120
+ const token = await createTestToken({}, { expiresIn: '-1h' });
121
121
 
122
- const claims = await verifier.verifyOrNull(token);
122
+ const claims = await verifier.verifyOrNull(token);
123
123
 
124
- expect(claims).toBeNull();
125
- });
126
- });
124
+ expect(claims).toBeNull();
125
+ });
126
+ });
127
127
 
128
- describe('custom claims', () => {
129
- interface CustomClaims {
130
- sub?: string;
131
- role: string;
132
- permissions: string[];
133
- }
128
+ describe('custom claims', () => {
129
+ interface CustomClaims {
130
+ sub?: string;
131
+ role: string;
132
+ permissions: string[];
133
+ }
134
134
 
135
- it('should return typed claims', async () => {
136
- const verifier = new JwtVerifier<CustomClaims>({ secret: TEST_SECRET });
137
- const token = await createTestToken({
138
- role: 'admin',
139
- permissions: ['read', 'write'],
140
- });
135
+ it('should return typed claims', async () => {
136
+ const verifier = new JwtVerifier<CustomClaims>({ secret: TEST_SECRET });
137
+ const token = await createTestToken({
138
+ role: 'admin',
139
+ permissions: ['read', 'write'],
140
+ });
141
141
 
142
- const claims = await verifier.verify(token);
142
+ const claims = await verifier.verify(token);
143
143
 
144
- expect(claims.role).toBe('admin');
145
- expect(claims.permissions).toEqual(['read', 'write']);
146
- });
147
- });
144
+ expect(claims.role).toBe('admin');
145
+ expect(claims.permissions).toEqual(['read', 'write']);
146
+ });
147
+ });
148
+ });
149
+
150
+ describe('clearCache', () => {
151
+ it('should clear the JWKS cache', () => {
152
+ const verifier = new JwtVerifier({ secret: TEST_SECRET });
153
+
154
+ // Call clearCache - should not throw
155
+ verifier.clearCache();
156
+
157
+ // Verify the method exists and can be called
158
+ expect(verifier.clearCache).toBeDefined();
159
+ });
160
+
161
+ it('should allow re-verification after clearing cache', async () => {
162
+ const verifier = new JwtVerifier({ secret: TEST_SECRET });
163
+ const token = await createTestToken();
164
+
165
+ const claims1 = await verifier.verify(token);
166
+ verifier.clearCache();
167
+ const claims2 = await verifier.verify(token);
168
+
169
+ expect(claims1.sub).toBe(claims2.sub);
170
+ });
148
171
  });
149
172
 
150
173
  describe('decodeJwt', () => {
151
- it('should decode token without verification', async () => {
152
- const token = await createTestToken({ custom: 'data' });
174
+ it('should decode token without verification', async () => {
175
+ const token = await createTestToken({ custom: 'data' });
153
176
 
154
- const claims = decodeJwt(token);
177
+ const claims = decodeJwt(token);
155
178
 
156
- expect(claims.sub).toBe('user-123');
157
- expect(claims).toHaveProperty('custom', 'data');
158
- });
179
+ expect(claims.sub).toBe('user-123');
180
+ expect(claims).toHaveProperty('custom', 'data');
181
+ });
159
182
 
160
- it('should decode token even with wrong secret', async () => {
161
- const token = await createTestToken(
162
- {},
163
- { secret: 'any-secret-32-characters-long!!' },
164
- );
183
+ it('should decode token even with wrong secret', async () => {
184
+ const token = await createTestToken(
185
+ {},
186
+ { secret: 'any-secret-32-characters-long!!' },
187
+ );
165
188
 
166
- const claims = decodeJwt(token);
189
+ const claims = decodeJwt(token);
167
190
 
168
- expect(claims.sub).toBe('user-123');
169
- });
191
+ expect(claims.sub).toBe('user-123');
192
+ });
170
193
 
171
- it('should throw for malformed token', () => {
172
- expect(() => decodeJwt('not.a.valid.jwt')).toThrow();
173
- });
194
+ it('should throw for malformed token', () => {
195
+ expect(() => decodeJwt('not.a.valid.jwt')).toThrow();
196
+ });
174
197
  });