@geekmidas/auth 0.1.0 → 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 (116) hide show
  1. package/dist/{cacheTokenStorage-Ca05vf3T.mjs → cacheTokenStorage-16NE8Cd9.mjs} +3 -3
  2. package/dist/cacheTokenStorage-16NE8Cd9.mjs.map +1 -0
  3. package/dist/{cacheTokenStorage-BzBRJfd-.d.mts → cacheTokenStorage-B7sOoysu.d.cts} +8 -6
  4. package/dist/cacheTokenStorage-B7sOoysu.d.cts.map +1 -0
  5. package/dist/{cacheTokenStorage-CvptztkW.cjs → cacheTokenStorage-CEpMniTi.cjs} +3 -3
  6. package/dist/cacheTokenStorage-CEpMniTi.cjs.map +1 -0
  7. package/dist/{cacheTokenStorage-yNUpDD8W.d.cts → cacheTokenStorage-_tq4Kmhc.d.mts} +8 -6
  8. package/dist/cacheTokenStorage-_tq4Kmhc.d.mts.map +1 -0
  9. package/dist/cacheTokenStorage.cjs +1 -1
  10. package/dist/cacheTokenStorage.d.cts +1 -1
  11. package/dist/cacheTokenStorage.d.mts +1 -1
  12. package/dist/cacheTokenStorage.mjs +1 -1
  13. package/dist/{client-DaiUm0UZ.cjs → client-BPl_mMvb.cjs} +14 -8
  14. package/dist/client-BPl_mMvb.cjs.map +1 -0
  15. package/dist/{client-DjNZS6vZ.mjs → client-eidYNJSH.mjs} +14 -8
  16. package/dist/client-eidYNJSH.mjs.map +1 -0
  17. package/dist/client.cjs +2 -2
  18. package/dist/client.d.cts +1 -1
  19. package/dist/client.d.mts +1 -1
  20. package/dist/client.mjs +2 -2
  21. package/dist/hono/jwt.cjs +1 -1
  22. package/dist/hono/jwt.cjs.map +1 -1
  23. package/dist/hono/jwt.d.cts +2 -1
  24. package/dist/hono/jwt.d.cts.map +1 -0
  25. package/dist/hono/jwt.d.mts +2 -1
  26. package/dist/hono/jwt.d.mts.map +1 -0
  27. package/dist/hono/jwt.mjs +1 -1
  28. package/dist/hono/jwt.mjs.map +1 -1
  29. package/dist/hono/oidc.cjs +1 -1
  30. package/dist/hono/oidc.cjs.map +1 -1
  31. package/dist/hono/oidc.d.cts +3 -2
  32. package/dist/hono/oidc.d.cts.map +1 -0
  33. package/dist/hono/oidc.d.mts +3 -2
  34. package/dist/hono/oidc.d.mts.map +1 -0
  35. package/dist/hono/oidc.mjs +1 -1
  36. package/dist/hono/oidc.mjs.map +1 -1
  37. package/dist/index.cjs +3 -3
  38. package/dist/index.d.cts +2 -2
  39. package/dist/index.d.mts +2 -2
  40. package/dist/index.mjs +3 -3
  41. package/dist/{jwt-DF9kFbmh.d.cts → jwt-3eIuY2dg.d.cts} +2 -1
  42. package/dist/jwt-3eIuY2dg.d.cts.map +1 -0
  43. package/dist/jwt-Bo_eACEi.cjs.map +1 -1
  44. package/dist/{jwt-DH8CJ2xC.d.mts → jwt-BqLlRkw-.d.mts} +2 -1
  45. package/dist/jwt-BqLlRkw-.d.mts.map +1 -0
  46. package/dist/jwt-D88cPJyT.mjs.map +1 -1
  47. package/dist/jwt.d.cts +1 -1
  48. package/dist/jwt.d.mts +1 -1
  49. package/dist/lambda/jwt.cjs +2 -2
  50. package/dist/lambda/jwt.cjs.map +1 -1
  51. package/dist/lambda/jwt.d.cts +2 -1
  52. package/dist/lambda/jwt.d.cts.map +1 -0
  53. package/dist/lambda/jwt.d.mts +2 -1
  54. package/dist/lambda/jwt.d.mts.map +1 -0
  55. package/dist/lambda/jwt.mjs +2 -2
  56. package/dist/lambda/jwt.mjs.map +1 -1
  57. package/dist/lambda/oidc.cjs +2 -2
  58. package/dist/lambda/oidc.cjs.map +1 -1
  59. package/dist/lambda/oidc.d.cts +3 -2
  60. package/dist/lambda/oidc.d.cts.map +1 -0
  61. package/dist/lambda/oidc.d.mts +3 -2
  62. package/dist/lambda/oidc.d.mts.map +1 -0
  63. package/dist/lambda/oidc.mjs +2 -2
  64. package/dist/lambda/oidc.mjs.map +1 -1
  65. package/dist/oidc-C0KyYaAK.cjs.map +1 -1
  66. package/dist/{oidc-CKOGLxjV.d.mts → oidc-ChA17wLB.d.cts} +3 -2
  67. package/dist/oidc-ChA17wLB.d.cts.map +1 -0
  68. package/dist/{oidc-DDLIB5gb.d.cts → oidc-DTRgBBl6.d.mts} +3 -2
  69. package/dist/oidc-DTRgBBl6.d.mts.map +1 -0
  70. package/dist/oidc-KA30gMlx.mjs.map +1 -1
  71. package/dist/oidc.d.cts +2 -2
  72. package/dist/oidc.d.mts +2 -2
  73. package/dist/server.cjs +2 -2
  74. package/dist/server.d.cts +2 -2
  75. package/dist/server.d.mts +2 -2
  76. package/dist/server.mjs +2 -2
  77. package/dist/{tokenManager--Q93eNYr.d.mts → tokenManager-CL3yE06D.d.cts} +2 -1
  78. package/dist/tokenManager-CL3yE06D.d.cts.map +1 -0
  79. package/dist/{tokenManager-BVXxfHRa.mjs → tokenManager-Ci2Tgxp8.mjs} +5 -3
  80. package/dist/tokenManager-Ci2Tgxp8.mjs.map +1 -0
  81. package/dist/{tokenManager-EPmwgai2.cjs → tokenManager-CvEMgxOp.cjs} +5 -3
  82. package/dist/tokenManager-CvEMgxOp.cjs.map +1 -0
  83. package/dist/{tokenManager-H6-lWEah.d.cts → tokenManager-RrwjL0y-.d.mts} +2 -1
  84. package/dist/tokenManager-RrwjL0y-.d.mts.map +1 -0
  85. package/dist/tokenManager.cjs +1 -1
  86. package/dist/tokenManager.d.cts +1 -1
  87. package/dist/tokenManager.d.mts +1 -1
  88. package/dist/tokenManager.mjs +1 -1
  89. package/package.json +3 -3
  90. package/src/__tests__/cacheTokenStorage.spec.ts +148 -148
  91. package/src/__tests__/client.spec.ts +339 -339
  92. package/src/__tests__/hono-jwt.spec.ts +219 -219
  93. package/src/__tests__/hono-oidc.spec.ts +560 -560
  94. package/src/__tests__/jwt.spec.ts +150 -127
  95. package/src/__tests__/lambda-jwt.spec.ts +205 -205
  96. package/src/__tests__/lambda-oidc.spec.ts +580 -579
  97. package/src/__tests__/oidc.spec.ts +365 -365
  98. package/src/__tests__/tokenManager.spec.ts +254 -254
  99. package/src/cacheTokenStorage.ts +32 -32
  100. package/src/client.ts +199 -190
  101. package/src/hono/jwt.ts +111 -111
  102. package/src/hono/oidc.ts +131 -131
  103. package/src/index.ts +2 -2
  104. package/src/jwt.ts +57 -57
  105. package/src/lambda/jwt.ts +170 -170
  106. package/src/lambda/oidc.ts +165 -165
  107. package/src/oidc.ts +121 -121
  108. package/src/server.ts +6 -7
  109. package/src/tokenManager.ts +89 -87
  110. package/tsconfig.json +10 -0
  111. package/dist/cacheTokenStorage-Ca05vf3T.mjs.map +0 -1
  112. package/dist/cacheTokenStorage-CvptztkW.cjs.map +0 -1
  113. package/dist/client-DaiUm0UZ.cjs.map +0 -1
  114. package/dist/client-DjNZS6vZ.mjs.map +0 -1
  115. package/dist/tokenManager-BVXxfHRa.mjs.map +0 -1
  116. package/dist/tokenManager-EPmwgai2.cjs.map +0 -1
@@ -6,227 +6,227 @@ import { JwtMiddleware } from '../hono/jwt';
6
6
  const TEST_SECRET = 'super-secret-key-for-testing-only-32chars';
7
7
 
8
8
  async function createTestToken(
9
- claims: Record<string, unknown> = {},
10
- secret = TEST_SECRET,
9
+ claims: Record<string, unknown> = {},
10
+ secret = TEST_SECRET,
11
11
  ) {
12
- const secretKey = new TextEncoder().encode(secret);
13
- return await new jose.SignJWT({ sub: 'user-123', ...claims })
14
- .setProtectedHeader({ alg: 'HS256' })
15
- .setIssuedAt()
16
- .setExpirationTime('1h')
17
- .sign(secretKey);
12
+ const secretKey = new TextEncoder().encode(secret);
13
+ return await new jose.SignJWT({ sub: 'user-123', ...claims })
14
+ .setProtectedHeader({ alg: 'HS256' })
15
+ .setIssuedAt()
16
+ .setExpirationTime('1h')
17
+ .sign(secretKey);
18
18
  }
19
19
 
20
20
  describe('JwtMiddleware', () => {
21
- describe('handler()', () => {
22
- it('should allow request with valid token', async () => {
23
- const app = new Hono();
24
- const jwt = new JwtMiddleware({ config: { secret: TEST_SECRET } });
25
-
26
- app.use('/*', jwt.handler());
27
- app.get('/test', (c) => {
28
- const claims = c.get('jwtClaims');
29
- return c.json({ userId: claims.sub });
30
- });
31
-
32
- const token = await createTestToken();
33
- const res = await app.request('/test', {
34
- headers: { Authorization: `Bearer ${token}` },
35
- });
36
-
37
- expect(res.status).toBe(200);
38
- const body = await res.json();
39
- expect(body.userId).toBe('user-123');
40
- });
41
-
42
- it('should reject request without token', async () => {
43
- const app = new Hono();
44
- const jwt = new JwtMiddleware({ config: { secret: TEST_SECRET } });
45
-
46
- app.use('/*', jwt.handler());
47
- app.get('/test', (c) => c.json({ ok: true }));
48
-
49
- const res = await app.request('/test');
50
-
51
- expect(res.status).toBe(401);
52
- });
53
-
54
- it('should reject request with invalid token', async () => {
55
- const app = new Hono();
56
- const jwt = new JwtMiddleware({ config: { secret: TEST_SECRET } });
57
-
58
- app.use('/*', jwt.handler());
59
- app.get('/test', (c) => c.json({ ok: true }));
60
-
61
- const res = await app.request('/test', {
62
- headers: { Authorization: 'Bearer invalid-token' },
63
- });
64
-
65
- expect(res.status).toBe(401);
66
- });
67
-
68
- it('should extract token from cookie when configured', async () => {
69
- const app = new Hono();
70
- const jwt = new JwtMiddleware({
71
- config: { secret: TEST_SECRET },
72
- extraction: { cookieName: 'auth_token' },
73
- });
74
-
75
- app.use('/*', jwt.handler());
76
- app.get('/test', (c) => {
77
- const claims = c.get('jwtClaims');
78
- return c.json({ userId: claims.sub });
79
- });
80
-
81
- const token = await createTestToken();
82
- const res = await app.request('/test', {
83
- headers: { Cookie: `auth_token=${token}` },
84
- });
85
-
86
- expect(res.status).toBe(200);
87
- });
88
-
89
- it('should use custom context key', async () => {
90
- const app = new Hono();
91
- const jwt = new JwtMiddleware({
92
- config: { secret: TEST_SECRET },
93
- contextKey: 'user',
94
- });
95
-
96
- app.use('/*', jwt.handler());
97
- app.get('/test', (c) => {
98
- const claims = c.get('user');
99
- return c.json({ userId: claims.sub });
100
- });
101
-
102
- const token = await createTestToken();
103
- const res = await app.request('/test', {
104
- headers: { Authorization: `Bearer ${token}` },
105
- });
106
-
107
- expect(res.status).toBe(200);
108
- });
109
-
110
- it('should use custom error handler', async () => {
111
- const app = new Hono();
112
- const jwt = new JwtMiddleware({
113
- config: { secret: TEST_SECRET },
114
- onError: (c) => c.json({ custom: 'error' }, 403),
115
- });
116
-
117
- app.use('/*', jwt.handler());
118
- app.get('/test', (c) => c.json({ ok: true }));
119
-
120
- const res = await app.request('/test');
121
-
122
- expect(res.status).toBe(403);
123
- const body = await res.json();
124
- expect(body.custom).toBe('error');
125
- });
126
-
127
- it('should transform claims', async () => {
128
- const app = new Hono();
129
- const jwt = new JwtMiddleware({
130
- config: { secret: TEST_SECRET },
131
- transformClaims: (claims) => ({
132
- ...claims,
133
- transformed: true,
134
- }),
135
- });
136
-
137
- app.use('/*', jwt.handler());
138
- app.get('/test', (c) => {
139
- const claims = c.get('jwtClaims');
140
- return c.json({ transformed: claims.transformed });
141
- });
142
-
143
- const token = await createTestToken();
144
- const res = await app.request('/test', {
145
- headers: { Authorization: `Bearer ${token}` },
146
- });
147
-
148
- expect(res.status).toBe(200);
149
- const body = await res.json();
150
- expect(body.transformed).toBe(true);
151
- });
152
-
153
- it('should set jwtToken in context', async () => {
154
- const app = new Hono();
155
- const jwt = new JwtMiddleware({ config: { secret: TEST_SECRET } });
156
-
157
- app.use('/*', jwt.handler());
158
- app.get('/test', (c) => {
159
- const token = c.get('jwtToken');
160
- return c.json({ hasToken: !!token });
161
- });
162
-
163
- const token = await createTestToken();
164
- const res = await app.request('/test', {
165
- headers: { Authorization: `Bearer ${token}` },
166
- });
167
-
168
- expect(res.status).toBe(200);
169
- const body = await res.json();
170
- expect(body.hasToken).toBe(true);
171
- });
172
- });
173
-
174
- describe('optional()', () => {
175
- it('should allow request without token', async () => {
176
- const app = new Hono();
177
- const jwt = new JwtMiddleware({ config: { secret: TEST_SECRET } });
178
-
179
- app.use('/*', jwt.optional());
180
- app.get('/test', (c) => {
181
- const claims = c.get('jwtClaims');
182
- return c.json({ authenticated: !!claims });
183
- });
184
-
185
- const res = await app.request('/test');
186
-
187
- expect(res.status).toBe(200);
188
- const body = await res.json();
189
- expect(body.authenticated).toBe(false);
190
- });
191
-
192
- it('should set claims when valid token provided', async () => {
193
- const app = new Hono();
194
- const jwt = new JwtMiddleware({ config: { secret: TEST_SECRET } });
195
-
196
- app.use('/*', jwt.optional());
197
- app.get('/test', (c) => {
198
- const claims = c.get('jwtClaims');
199
- return c.json({ authenticated: !!claims, userId: claims?.sub });
200
- });
201
-
202
- const token = await createTestToken();
203
- const res = await app.request('/test', {
204
- headers: { Authorization: `Bearer ${token}` },
205
- });
206
-
207
- expect(res.status).toBe(200);
208
- const body = await res.json();
209
- expect(body.authenticated).toBe(true);
210
- expect(body.userId).toBe('user-123');
211
- });
212
-
213
- it('should not set claims for invalid token', async () => {
214
- const app = new Hono();
215
- const jwt = new JwtMiddleware({ config: { secret: TEST_SECRET } });
216
-
217
- app.use('/*', jwt.optional());
218
- app.get('/test', (c) => {
219
- const claims = c.get('jwtClaims');
220
- return c.json({ authenticated: !!claims });
221
- });
222
-
223
- const res = await app.request('/test', {
224
- headers: { Authorization: 'Bearer invalid-token' },
225
- });
226
-
227
- expect(res.status).toBe(200);
228
- const body = await res.json();
229
- expect(body.authenticated).toBe(false);
230
- });
231
- });
21
+ describe('handler()', () => {
22
+ it('should allow request with valid token', async () => {
23
+ const app = new Hono();
24
+ const jwt = new JwtMiddleware({ config: { secret: TEST_SECRET } });
25
+
26
+ app.use('/*', jwt.handler());
27
+ app.get('/test', (c) => {
28
+ const claims = c.get('jwtClaims');
29
+ return c.json({ userId: claims.sub });
30
+ });
31
+
32
+ const token = await createTestToken();
33
+ const res = await app.request('/test', {
34
+ headers: { Authorization: `Bearer ${token}` },
35
+ });
36
+
37
+ expect(res.status).toBe(200);
38
+ const body = await res.json();
39
+ expect(body.userId).toBe('user-123');
40
+ });
41
+
42
+ it('should reject request without token', async () => {
43
+ const app = new Hono();
44
+ const jwt = new JwtMiddleware({ config: { secret: TEST_SECRET } });
45
+
46
+ app.use('/*', jwt.handler());
47
+ app.get('/test', (c) => c.json({ ok: true }));
48
+
49
+ const res = await app.request('/test');
50
+
51
+ expect(res.status).toBe(401);
52
+ });
53
+
54
+ it('should reject request with invalid token', async () => {
55
+ const app = new Hono();
56
+ const jwt = new JwtMiddleware({ config: { secret: TEST_SECRET } });
57
+
58
+ app.use('/*', jwt.handler());
59
+ app.get('/test', (c) => c.json({ ok: true }));
60
+
61
+ const res = await app.request('/test', {
62
+ headers: { Authorization: 'Bearer invalid-token' },
63
+ });
64
+
65
+ expect(res.status).toBe(401);
66
+ });
67
+
68
+ it('should extract token from cookie when configured', async () => {
69
+ const app = new Hono();
70
+ const jwt = new JwtMiddleware({
71
+ config: { secret: TEST_SECRET },
72
+ extraction: { cookieName: 'auth_token' },
73
+ });
74
+
75
+ app.use('/*', jwt.handler());
76
+ app.get('/test', (c) => {
77
+ const claims = c.get('jwtClaims');
78
+ return c.json({ userId: claims.sub });
79
+ });
80
+
81
+ const token = await createTestToken();
82
+ const res = await app.request('/test', {
83
+ headers: { Cookie: `auth_token=${token}` },
84
+ });
85
+
86
+ expect(res.status).toBe(200);
87
+ });
88
+
89
+ it('should use custom context key', async () => {
90
+ const app = new Hono();
91
+ const jwt = new JwtMiddleware({
92
+ config: { secret: TEST_SECRET },
93
+ contextKey: 'user',
94
+ });
95
+
96
+ app.use('/*', jwt.handler());
97
+ app.get('/test', (c) => {
98
+ const claims = c.get('user');
99
+ return c.json({ userId: claims.sub });
100
+ });
101
+
102
+ const token = await createTestToken();
103
+ const res = await app.request('/test', {
104
+ headers: { Authorization: `Bearer ${token}` },
105
+ });
106
+
107
+ expect(res.status).toBe(200);
108
+ });
109
+
110
+ it('should use custom error handler', async () => {
111
+ const app = new Hono();
112
+ const jwt = new JwtMiddleware({
113
+ config: { secret: TEST_SECRET },
114
+ onError: (c) => c.json({ custom: 'error' }, 403),
115
+ });
116
+
117
+ app.use('/*', jwt.handler());
118
+ app.get('/test', (c) => c.json({ ok: true }));
119
+
120
+ const res = await app.request('/test');
121
+
122
+ expect(res.status).toBe(403);
123
+ const body = await res.json();
124
+ expect(body.custom).toBe('error');
125
+ });
126
+
127
+ it('should transform claims', async () => {
128
+ const app = new Hono();
129
+ const jwt = new JwtMiddleware({
130
+ config: { secret: TEST_SECRET },
131
+ transformClaims: (claims) => ({
132
+ ...claims,
133
+ transformed: true,
134
+ }),
135
+ });
136
+
137
+ app.use('/*', jwt.handler());
138
+ app.get('/test', (c) => {
139
+ const claims = c.get('jwtClaims');
140
+ return c.json({ transformed: claims.transformed });
141
+ });
142
+
143
+ const token = await createTestToken();
144
+ const res = await app.request('/test', {
145
+ headers: { Authorization: `Bearer ${token}` },
146
+ });
147
+
148
+ expect(res.status).toBe(200);
149
+ const body = await res.json();
150
+ expect(body.transformed).toBe(true);
151
+ });
152
+
153
+ it('should set jwtToken in context', async () => {
154
+ const app = new Hono();
155
+ const jwt = new JwtMiddleware({ config: { secret: TEST_SECRET } });
156
+
157
+ app.use('/*', jwt.handler());
158
+ app.get('/test', (c) => {
159
+ const token = c.get('jwtToken');
160
+ return c.json({ hasToken: !!token });
161
+ });
162
+
163
+ const token = await createTestToken();
164
+ const res = await app.request('/test', {
165
+ headers: { Authorization: `Bearer ${token}` },
166
+ });
167
+
168
+ expect(res.status).toBe(200);
169
+ const body = await res.json();
170
+ expect(body.hasToken).toBe(true);
171
+ });
172
+ });
173
+
174
+ describe('optional()', () => {
175
+ it('should allow request without token', async () => {
176
+ const app = new Hono();
177
+ const jwt = new JwtMiddleware({ config: { secret: TEST_SECRET } });
178
+
179
+ app.use('/*', jwt.optional());
180
+ app.get('/test', (c) => {
181
+ const claims = c.get('jwtClaims');
182
+ return c.json({ authenticated: !!claims });
183
+ });
184
+
185
+ const res = await app.request('/test');
186
+
187
+ expect(res.status).toBe(200);
188
+ const body = await res.json();
189
+ expect(body.authenticated).toBe(false);
190
+ });
191
+
192
+ it('should set claims when valid token provided', async () => {
193
+ const app = new Hono();
194
+ const jwt = new JwtMiddleware({ config: { secret: TEST_SECRET } });
195
+
196
+ app.use('/*', jwt.optional());
197
+ app.get('/test', (c) => {
198
+ const claims = c.get('jwtClaims');
199
+ return c.json({ authenticated: !!claims, userId: claims?.sub });
200
+ });
201
+
202
+ const token = await createTestToken();
203
+ const res = await app.request('/test', {
204
+ headers: { Authorization: `Bearer ${token}` },
205
+ });
206
+
207
+ expect(res.status).toBe(200);
208
+ const body = await res.json();
209
+ expect(body.authenticated).toBe(true);
210
+ expect(body.userId).toBe('user-123');
211
+ });
212
+
213
+ it('should not set claims for invalid token', async () => {
214
+ const app = new Hono();
215
+ const jwt = new JwtMiddleware({ config: { secret: TEST_SECRET } });
216
+
217
+ app.use('/*', jwt.optional());
218
+ app.get('/test', (c) => {
219
+ const claims = c.get('jwtClaims');
220
+ return c.json({ authenticated: !!claims });
221
+ });
222
+
223
+ const res = await app.request('/test', {
224
+ headers: { Authorization: 'Bearer invalid-token' },
225
+ });
226
+
227
+ expect(res.status).toBe(200);
228
+ const body = await res.json();
229
+ expect(body.authenticated).toBe(false);
230
+ });
231
+ });
232
232
  });