@geekmidas/auth 0.0.3 → 0.0.5
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.
- package/dist/{cacheTokenStorage-BlpcZwef.d.mts → cacheTokenStorage-BzBRJfd-.d.mts} +2 -1
- package/dist/cacheTokenStorage-Ca05vf3T.mjs +2 -1
- package/dist/cacheTokenStorage-Ca05vf3T.mjs.map +1 -0
- package/dist/cacheTokenStorage-CvptztkW.cjs +2 -1
- package/dist/cacheTokenStorage-CvptztkW.cjs.map +1 -0
- package/dist/cacheTokenStorage-yNUpDD8W.d.cts +2 -1
- package/dist/cacheTokenStorage.d.mts +1 -1
- package/dist/{chunk-DWy1uDak.cjs → chunk-CUT6urMc.cjs} +0 -9
- package/dist/client-DaiUm0UZ.cjs +2 -1
- package/dist/client-DaiUm0UZ.cjs.map +1 -0
- package/dist/client-DjNZS6vZ.mjs +2 -1
- package/dist/client-DjNZS6vZ.mjs.map +1 -0
- package/dist/client.d.mts +1 -1
- package/dist/hono/jwt.cjs +92 -0
- package/dist/hono/jwt.cjs.map +1 -0
- package/dist/hono/jwt.d.cts +45 -0
- package/dist/hono/jwt.d.mts +45 -0
- package/dist/hono/jwt.mjs +91 -0
- package/dist/hono/jwt.mjs.map +1 -0
- package/dist/hono/oidc.cjs +104 -0
- package/dist/hono/oidc.cjs.map +1 -0
- package/dist/hono/oidc.d.cts +48 -0
- package/dist/hono/oidc.d.mts +48 -0
- package/dist/hono/oidc.mjs +103 -0
- package/dist/hono/oidc.mjs.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +2 -2
- package/dist/jwt-Bo_eACEi.cjs +70 -0
- package/dist/jwt-Bo_eACEi.cjs.map +1 -0
- package/dist/jwt-D88cPJyT.mjs +52 -0
- package/dist/jwt-D88cPJyT.mjs.map +1 -0
- package/dist/jwt-DF9kFbmh.d.cts +61 -0
- package/dist/jwt-DH8CJ2xC.d.mts +61 -0
- package/dist/jwt.cjs +5 -0
- package/dist/jwt.d.cts +2 -0
- package/dist/jwt.d.mts +2 -0
- package/dist/jwt.mjs +3 -0
- package/dist/lambda/jwt.cjs +126 -0
- package/dist/lambda/jwt.cjs.map +1 -0
- package/dist/lambda/jwt.d.cts +52 -0
- package/dist/lambda/jwt.d.mts +52 -0
- package/dist/lambda/jwt.mjs +125 -0
- package/dist/lambda/jwt.mjs.map +1 -0
- package/dist/lambda/oidc.cjs +117 -0
- package/dist/lambda/oidc.cjs.map +1 -0
- package/dist/lambda/oidc.d.cts +44 -0
- package/dist/lambda/oidc.d.mts +44 -0
- package/dist/lambda/oidc.mjs +116 -0
- package/dist/lambda/oidc.mjs.map +1 -0
- package/dist/oidc-C0KyYaAK.cjs +68 -0
- package/dist/oidc-C0KyYaAK.cjs.map +1 -0
- package/dist/oidc-CKOGLxjV.d.mts +86 -0
- package/dist/oidc-DDLIB5gb.d.cts +86 -0
- package/dist/oidc-KA30gMlx.mjs +62 -0
- package/dist/oidc-KA30gMlx.mjs.map +1 -0
- package/dist/oidc.cjs +3 -0
- package/dist/oidc.d.cts +3 -0
- package/dist/oidc.d.mts +3 -0
- package/dist/oidc.mjs +3 -0
- package/dist/server.cjs +1 -1
- package/dist/server.d.cts +1 -1
- package/dist/server.d.mts +2 -2
- package/dist/{tokenManager-CCcFk2Py.d.mts → tokenManager--Q93eNYr.d.mts} +2 -1
- package/dist/tokenManager-BVXxfHRa.mjs +2 -1
- package/dist/tokenManager-BVXxfHRa.mjs.map +1 -0
- package/dist/{tokenManager-B60gFnEF.cjs → tokenManager-EPmwgai2.cjs} +3 -2
- package/dist/tokenManager-EPmwgai2.cjs.map +1 -0
- package/dist/{tokenManager-mpzJKLvX.d.cts → tokenManager-H6-lWEah.d.cts} +2 -1
- package/dist/tokenManager.cjs +1 -1
- package/dist/tokenManager.d.cts +1 -1
- package/dist/tokenManager.d.mts +1 -1
- package/package.json +49 -2
- package/src/__tests__/hono-jwt.spec.ts +232 -0
- package/src/__tests__/hono-oidc.spec.ts +605 -0
- package/src/__tests__/jwt.spec.ts +174 -0
- package/src/__tests__/lambda-jwt.spec.ts +278 -0
- package/src/__tests__/lambda-oidc.spec.ts +623 -0
- package/src/__tests__/oidc.spec.ts +405 -0
- package/src/hono/jwt.ts +142 -0
- package/src/hono/oidc.ts +170 -0
- package/src/jwt.ts +106 -0
- package/src/lambda/jwt.ts +210 -0
- package/src/lambda/oidc.ts +203 -0
- package/src/oidc.ts +156 -0
- package/dist/__tests__/cacheTokenStorage.spec.cjs +0 -146
- package/dist/__tests__/cacheTokenStorage.spec.d.cts +0 -1
- package/dist/__tests__/cacheTokenStorage.spec.d.mts +0 -1
- package/dist/__tests__/cacheTokenStorage.spec.mjs +0 -145
- package/dist/__tests__/client.spec.cjs +0 -17281
- package/dist/__tests__/client.spec.d.cts +0 -1
- package/dist/__tests__/client.spec.d.mts +0 -1
- package/dist/__tests__/client.spec.mjs +0 -17308
- package/dist/__tests__/tokenManager.spec.cjs +0 -197
- package/dist/__tests__/tokenManager.spec.d.cts +0 -1
- package/dist/__tests__/tokenManager.spec.d.mts +0 -1
- package/dist/__tests__/tokenManager.spec.mjs +0 -196
- package/dist/playground.cjs +0 -0
- package/dist/playground.d.cts +0 -1
- package/dist/playground.d.mts +0 -1
- package/dist/playground.mjs +0 -0
- package/src/playground.ts +0 -0
|
@@ -0,0 +1,623 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
APIGatewayRequestAuthorizerEvent,
|
|
3
|
+
APIGatewayTokenAuthorizerEvent,
|
|
4
|
+
Context as LambdaContext,
|
|
5
|
+
Statement,
|
|
6
|
+
} from 'aws-lambda';
|
|
7
|
+
|
|
8
|
+
// Helper to access Resource property from Statement union type
|
|
9
|
+
type StatementWithResource = Statement & { Resource: string | string[] };
|
|
10
|
+
import * as jose from 'jose';
|
|
11
|
+
import { http, HttpResponse } from 'msw';
|
|
12
|
+
import { setupServer } from 'msw/node';
|
|
13
|
+
import {
|
|
14
|
+
afterAll,
|
|
15
|
+
afterEach,
|
|
16
|
+
beforeAll,
|
|
17
|
+
describe,
|
|
18
|
+
expect,
|
|
19
|
+
it,
|
|
20
|
+
vi,
|
|
21
|
+
} from 'vitest';
|
|
22
|
+
import { OidcAuthorizer } from '../lambda/oidc';
|
|
23
|
+
|
|
24
|
+
// Mock discovery document
|
|
25
|
+
const mockDiscovery = {
|
|
26
|
+
issuer: 'https://auth.example.com',
|
|
27
|
+
authorization_endpoint: 'https://auth.example.com/authorize',
|
|
28
|
+
token_endpoint: 'https://auth.example.com/token',
|
|
29
|
+
userinfo_endpoint: 'https://auth.example.com/userinfo',
|
|
30
|
+
jwks_uri: 'https://auth.example.com/.well-known/jwks.json',
|
|
31
|
+
scopes_supported: ['openid', 'profile', 'email'],
|
|
32
|
+
response_types_supported: ['code', 'token'],
|
|
33
|
+
claims_supported: ['sub', 'name', 'email'],
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// Generate test keys and tokens - stored in object for mock access
|
|
37
|
+
const testKeys: {
|
|
38
|
+
privateKey: jose.KeyLike | null;
|
|
39
|
+
publicKey: jose.KeyLike | null;
|
|
40
|
+
} = {
|
|
41
|
+
privateKey: null,
|
|
42
|
+
publicKey: null,
|
|
43
|
+
};
|
|
44
|
+
let jwks: jose.JSONWebKeySet;
|
|
45
|
+
|
|
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
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Mock createRemoteJWKSet to return test keys directly.
|
|
57
|
+
// MSW intercepts jose's fetch in standalone scripts but fails in vitest for unknown reasons.
|
|
58
|
+
// TODO: Investigate vitest/MSW/jose interaction and remove this mock if possible.
|
|
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
|
+
};
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
async function createTestToken(
|
|
75
|
+
claims: Record<string, unknown> = {},
|
|
76
|
+
options: { expiresIn?: string } = {},
|
|
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);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// MSW server setup
|
|
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
|
+
);
|
|
99
|
+
|
|
100
|
+
// Mock Lambda context
|
|
101
|
+
const mockContext: LambdaContext = {
|
|
102
|
+
callbackWaitsForEmptyEventLoop: false,
|
|
103
|
+
functionName: 'test-authorizer',
|
|
104
|
+
functionVersion: '1',
|
|
105
|
+
invokedFunctionArn: 'arn:aws:lambda:us-east-1:123456789:function:test',
|
|
106
|
+
memoryLimitInMB: '128',
|
|
107
|
+
awsRequestId: 'test-request-id',
|
|
108
|
+
logGroupName: '/aws/lambda/test',
|
|
109
|
+
logStreamName: 'test-stream',
|
|
110
|
+
getRemainingTimeInMillis: () => 5000,
|
|
111
|
+
done: () => {},
|
|
112
|
+
fail: () => {},
|
|
113
|
+
succeed: () => {},
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
describe('OidcAuthorizer', () => {
|
|
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('tokenHandler', () => {
|
|
131
|
+
it('should allow valid token', async () => {
|
|
132
|
+
const authorizer = new OidcAuthorizer({
|
|
133
|
+
config: {
|
|
134
|
+
issuer: 'https://auth.example.com',
|
|
135
|
+
audience: 'my-api',
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const token = await createTestToken();
|
|
140
|
+
const event: APIGatewayTokenAuthorizerEvent = {
|
|
141
|
+
type: 'TOKEN',
|
|
142
|
+
authorizationToken: `Bearer ${token}`,
|
|
143
|
+
methodArn:
|
|
144
|
+
'arn:aws:execute-api:us-east-1:123456789:api-id/stage/GET/resource',
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const handler = authorizer.tokenHandler();
|
|
148
|
+
const result = await handler(event, mockContext);
|
|
149
|
+
|
|
150
|
+
expect(result.principalId).toBe('user-123');
|
|
151
|
+
expect(result.policyDocument.Statement[0].Effect).toBe('Allow');
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('should deny invalid token', async () => {
|
|
155
|
+
const authorizer = new OidcAuthorizer({
|
|
156
|
+
config: {
|
|
157
|
+
issuer: 'https://auth.example.com',
|
|
158
|
+
audience: 'my-api',
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
const event: APIGatewayTokenAuthorizerEvent = {
|
|
163
|
+
type: 'TOKEN',
|
|
164
|
+
authorizationToken: 'Bearer invalid-token',
|
|
165
|
+
methodArn:
|
|
166
|
+
'arn:aws:execute-api:us-east-1:123456789:api-id/stage/GET/resource',
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const handler = authorizer.tokenHandler();
|
|
170
|
+
const result = await handler(event, mockContext);
|
|
171
|
+
|
|
172
|
+
expect(result.principalId).toBe('unauthorized');
|
|
173
|
+
expect(result.policyDocument.Statement[0].Effect).toBe('Deny');
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('should handle token without Bearer prefix', async () => {
|
|
177
|
+
const authorizer = new OidcAuthorizer({
|
|
178
|
+
config: {
|
|
179
|
+
issuer: 'https://auth.example.com',
|
|
180
|
+
audience: 'my-api',
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
const token = await createTestToken();
|
|
185
|
+
const event: APIGatewayTokenAuthorizerEvent = {
|
|
186
|
+
type: 'TOKEN',
|
|
187
|
+
authorizationToken: token,
|
|
188
|
+
methodArn:
|
|
189
|
+
'arn:aws:execute-api:us-east-1:123456789:api-id/stage/GET/resource',
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
const handler = authorizer.tokenHandler();
|
|
193
|
+
const result = await handler(event, mockContext);
|
|
194
|
+
|
|
195
|
+
expect(result.principalId).toBe('user-123');
|
|
196
|
+
expect(result.policyDocument.Statement[0].Effect).toBe('Allow');
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('should use wildcard resource by default', async () => {
|
|
200
|
+
const authorizer = new OidcAuthorizer({
|
|
201
|
+
config: {
|
|
202
|
+
issuer: 'https://auth.example.com',
|
|
203
|
+
audience: 'my-api',
|
|
204
|
+
},
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
const token = await createTestToken();
|
|
208
|
+
const event: APIGatewayTokenAuthorizerEvent = {
|
|
209
|
+
type: 'TOKEN',
|
|
210
|
+
authorizationToken: `Bearer ${token}`,
|
|
211
|
+
methodArn:
|
|
212
|
+
'arn:aws:execute-api:us-east-1:123456789:api-id/stage/GET/resource',
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const handler = authorizer.tokenHandler();
|
|
216
|
+
const result = await handler(event, mockContext);
|
|
217
|
+
|
|
218
|
+
const statement = result.policyDocument
|
|
219
|
+
.Statement[0] as StatementWithResource;
|
|
220
|
+
expect(statement.Resource).toBe(
|
|
221
|
+
'arn:aws:execute-api:us-east-1:123456789:api-id/stage/*',
|
|
222
|
+
);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('should use exact resource when wildcardResource is false', async () => {
|
|
226
|
+
const authorizer = new OidcAuthorizer({
|
|
227
|
+
config: {
|
|
228
|
+
issuer: 'https://auth.example.com',
|
|
229
|
+
audience: 'my-api',
|
|
230
|
+
},
|
|
231
|
+
wildcardResource: false,
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
const token = await createTestToken();
|
|
235
|
+
const event: APIGatewayTokenAuthorizerEvent = {
|
|
236
|
+
type: 'TOKEN',
|
|
237
|
+
authorizationToken: `Bearer ${token}`,
|
|
238
|
+
methodArn:
|
|
239
|
+
'arn:aws:execute-api:us-east-1:123456789:api-id/stage/GET/resource',
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const handler = authorizer.tokenHandler();
|
|
243
|
+
const result = await handler(event, mockContext);
|
|
244
|
+
|
|
245
|
+
const statement = result.policyDocument
|
|
246
|
+
.Statement[0] as StatementWithResource;
|
|
247
|
+
expect(statement.Resource).toBe(
|
|
248
|
+
'arn:aws:execute-api:us-east-1:123456789:api-id/stage/GET/resource',
|
|
249
|
+
);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it('should use custom getPrincipalId', async () => {
|
|
253
|
+
const authorizer = new OidcAuthorizer({
|
|
254
|
+
config: {
|
|
255
|
+
issuer: 'https://auth.example.com',
|
|
256
|
+
audience: 'my-api',
|
|
257
|
+
},
|
|
258
|
+
getPrincipalId: (claims) => `custom-${claims.sub}`,
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
const token = await createTestToken();
|
|
262
|
+
const event: APIGatewayTokenAuthorizerEvent = {
|
|
263
|
+
type: 'TOKEN',
|
|
264
|
+
authorizationToken: `Bearer ${token}`,
|
|
265
|
+
methodArn:
|
|
266
|
+
'arn:aws:execute-api:us-east-1:123456789:api-id/stage/GET/resource',
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
const handler = authorizer.tokenHandler();
|
|
270
|
+
const result = await handler(event, mockContext);
|
|
271
|
+
|
|
272
|
+
expect(result.principalId).toBe('custom-user-123');
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it('should include context from getContext', async () => {
|
|
276
|
+
const authorizer = new OidcAuthorizer({
|
|
277
|
+
config: {
|
|
278
|
+
issuer: 'https://auth.example.com',
|
|
279
|
+
audience: 'my-api',
|
|
280
|
+
},
|
|
281
|
+
getContext: (claims) => ({
|
|
282
|
+
userId: claims.sub!,
|
|
283
|
+
role: 'admin',
|
|
284
|
+
}),
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
const token = await createTestToken();
|
|
288
|
+
const event: APIGatewayTokenAuthorizerEvent = {
|
|
289
|
+
type: 'TOKEN',
|
|
290
|
+
authorizationToken: `Bearer ${token}`,
|
|
291
|
+
methodArn:
|
|
292
|
+
'arn:aws:execute-api:us-east-1:123456789:api-id/stage/GET/resource',
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
const handler = authorizer.tokenHandler();
|
|
296
|
+
const result = await handler(event, mockContext);
|
|
297
|
+
|
|
298
|
+
expect(result.context).toEqual({
|
|
299
|
+
userId: 'user-123',
|
|
300
|
+
role: 'admin',
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it('should deny when authorize callback returns false', async () => {
|
|
305
|
+
const authorizer = new OidcAuthorizer({
|
|
306
|
+
config: {
|
|
307
|
+
issuer: 'https://auth.example.com',
|
|
308
|
+
audience: 'my-api',
|
|
309
|
+
},
|
|
310
|
+
authorize: () => false,
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
const token = await createTestToken();
|
|
314
|
+
const event: APIGatewayTokenAuthorizerEvent = {
|
|
315
|
+
type: 'TOKEN',
|
|
316
|
+
authorizationToken: `Bearer ${token}`,
|
|
317
|
+
methodArn:
|
|
318
|
+
'arn:aws:execute-api:us-east-1:123456789:api-id/stage/GET/resource',
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
const handler = authorizer.tokenHandler();
|
|
322
|
+
const result = await handler(event, mockContext);
|
|
323
|
+
|
|
324
|
+
expect(result.principalId).toBe('unauthorized');
|
|
325
|
+
expect(result.policyDocument.Statement[0].Effect).toBe('Deny');
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
it('should allow when authorize callback returns true', async () => {
|
|
329
|
+
const authorizer = new OidcAuthorizer({
|
|
330
|
+
config: {
|
|
331
|
+
issuer: 'https://auth.example.com',
|
|
332
|
+
audience: 'my-api',
|
|
333
|
+
},
|
|
334
|
+
authorize: (claims) => claims.sub === 'user-123',
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
const token = await createTestToken();
|
|
338
|
+
const event: APIGatewayTokenAuthorizerEvent = {
|
|
339
|
+
type: 'TOKEN',
|
|
340
|
+
authorizationToken: `Bearer ${token}`,
|
|
341
|
+
methodArn:
|
|
342
|
+
'arn:aws:execute-api:us-east-1:123456789:api-id/stage/GET/resource',
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
const handler = authorizer.tokenHandler();
|
|
346
|
+
const result = await handler(event, mockContext);
|
|
347
|
+
|
|
348
|
+
expect(result.principalId).toBe('user-123');
|
|
349
|
+
expect(result.policyDocument.Statement[0].Effect).toBe('Allow');
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
it('should support async authorize callback', async () => {
|
|
353
|
+
const authorizer = new OidcAuthorizer({
|
|
354
|
+
config: {
|
|
355
|
+
issuer: 'https://auth.example.com',
|
|
356
|
+
audience: 'my-api',
|
|
357
|
+
},
|
|
358
|
+
authorize: async (claims) => {
|
|
359
|
+
await Promise.resolve();
|
|
360
|
+
return claims.sub === 'user-123';
|
|
361
|
+
},
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
const token = await createTestToken();
|
|
365
|
+
const event: APIGatewayTokenAuthorizerEvent = {
|
|
366
|
+
type: 'TOKEN',
|
|
367
|
+
authorizationToken: `Bearer ${token}`,
|
|
368
|
+
methodArn:
|
|
369
|
+
'arn:aws:execute-api:us-east-1:123456789:api-id/stage/GET/resource',
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
const handler = authorizer.tokenHandler();
|
|
373
|
+
const result = await handler(event, mockContext);
|
|
374
|
+
|
|
375
|
+
expect(result.principalId).toBe('user-123');
|
|
376
|
+
expect(result.policyDocument.Statement[0].Effect).toBe('Allow');
|
|
377
|
+
});
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
describe('requestHandler', () => {
|
|
381
|
+
function createRequestEvent(
|
|
382
|
+
headers: Record<string, string | undefined>,
|
|
383
|
+
): APIGatewayRequestAuthorizerEvent {
|
|
384
|
+
return {
|
|
385
|
+
type: 'REQUEST',
|
|
386
|
+
methodArn:
|
|
387
|
+
'arn:aws:execute-api:us-east-1:123456789:api-id/stage/GET/resource',
|
|
388
|
+
headers,
|
|
389
|
+
multiValueHeaders: null,
|
|
390
|
+
pathParameters: null,
|
|
391
|
+
queryStringParameters: null,
|
|
392
|
+
multiValueQueryStringParameters: null,
|
|
393
|
+
stageVariables: null,
|
|
394
|
+
requestContext: {} as any,
|
|
395
|
+
resource: '/resource',
|
|
396
|
+
path: '/resource',
|
|
397
|
+
httpMethod: 'GET',
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
it('should allow valid token from authorization header', async () => {
|
|
402
|
+
const authorizer = new OidcAuthorizer({
|
|
403
|
+
config: {
|
|
404
|
+
issuer: 'https://auth.example.com',
|
|
405
|
+
audience: 'my-api',
|
|
406
|
+
},
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
const token = await createTestToken();
|
|
410
|
+
const event = createRequestEvent({
|
|
411
|
+
authorization: `Bearer ${token}`,
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
const handler = authorizer.requestHandler();
|
|
415
|
+
const result = await handler(event, mockContext);
|
|
416
|
+
|
|
417
|
+
expect(result.principalId).toBe('user-123');
|
|
418
|
+
expect(result.policyDocument.Statement[0].Effect).toBe('Allow');
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
it('should deny when no token is provided', async () => {
|
|
422
|
+
const authorizer = new OidcAuthorizer({
|
|
423
|
+
config: {
|
|
424
|
+
issuer: 'https://auth.example.com',
|
|
425
|
+
audience: 'my-api',
|
|
426
|
+
},
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
const event = createRequestEvent({});
|
|
430
|
+
|
|
431
|
+
const handler = authorizer.requestHandler();
|
|
432
|
+
const result = await handler(event, mockContext);
|
|
433
|
+
|
|
434
|
+
expect(result.principalId).toBe('unauthorized');
|
|
435
|
+
expect(result.policyDocument.Statement[0].Effect).toBe('Deny');
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
it('should extract token from cookie', async () => {
|
|
439
|
+
const authorizer = new OidcAuthorizer({
|
|
440
|
+
config: {
|
|
441
|
+
issuer: 'https://auth.example.com',
|
|
442
|
+
audience: 'my-api',
|
|
443
|
+
},
|
|
444
|
+
extraction: {
|
|
445
|
+
cookieName: 'auth_token',
|
|
446
|
+
},
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
const token = await createTestToken();
|
|
450
|
+
const event = createRequestEvent({
|
|
451
|
+
cookie: `auth_token=${token}; other=value`,
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
const handler = authorizer.requestHandler();
|
|
455
|
+
const result = await handler(event, mockContext);
|
|
456
|
+
|
|
457
|
+
expect(result.principalId).toBe('user-123');
|
|
458
|
+
expect(result.policyDocument.Statement[0].Effect).toBe('Allow');
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
it('should handle case-insensitive header names', async () => {
|
|
462
|
+
const authorizer = new OidcAuthorizer({
|
|
463
|
+
config: {
|
|
464
|
+
issuer: 'https://auth.example.com',
|
|
465
|
+
audience: 'my-api',
|
|
466
|
+
},
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
const token = await createTestToken();
|
|
470
|
+
const event = createRequestEvent({
|
|
471
|
+
Authorization: `Bearer ${token}`,
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
const handler = authorizer.requestHandler();
|
|
475
|
+
const result = await handler(event, mockContext);
|
|
476
|
+
|
|
477
|
+
expect(result.principalId).toBe('user-123');
|
|
478
|
+
expect(result.policyDocument.Statement[0].Effect).toBe('Allow');
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
it('should extract token without prefix when configured', async () => {
|
|
482
|
+
const authorizer = new OidcAuthorizer({
|
|
483
|
+
config: {
|
|
484
|
+
issuer: 'https://auth.example.com',
|
|
485
|
+
audience: 'my-api',
|
|
486
|
+
},
|
|
487
|
+
extraction: {
|
|
488
|
+
headerName: 'x-auth-token',
|
|
489
|
+
tokenPrefix: '',
|
|
490
|
+
},
|
|
491
|
+
});
|
|
492
|
+
|
|
493
|
+
const token = await createTestToken();
|
|
494
|
+
const event = createRequestEvent({
|
|
495
|
+
'x-auth-token': token,
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
const handler = authorizer.requestHandler();
|
|
499
|
+
const result = await handler(event, mockContext);
|
|
500
|
+
|
|
501
|
+
expect(result.principalId).toBe('user-123');
|
|
502
|
+
expect(result.policyDocument.Statement[0].Effect).toBe('Allow');
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
it('should deny invalid token', async () => {
|
|
506
|
+
const authorizer = new OidcAuthorizer({
|
|
507
|
+
config: {
|
|
508
|
+
issuer: 'https://auth.example.com',
|
|
509
|
+
audience: 'my-api',
|
|
510
|
+
},
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
const event = createRequestEvent({
|
|
514
|
+
authorization: 'Bearer invalid-token',
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
const handler = authorizer.requestHandler();
|
|
518
|
+
const result = await handler(event, mockContext);
|
|
519
|
+
|
|
520
|
+
expect(result.principalId).toBe('unauthorized');
|
|
521
|
+
expect(result.policyDocument.Statement[0].Effect).toBe('Deny');
|
|
522
|
+
});
|
|
523
|
+
|
|
524
|
+
it('should use wildcard resource by default', async () => {
|
|
525
|
+
const authorizer = new OidcAuthorizer({
|
|
526
|
+
config: {
|
|
527
|
+
issuer: 'https://auth.example.com',
|
|
528
|
+
audience: 'my-api',
|
|
529
|
+
},
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
const token = await createTestToken();
|
|
533
|
+
const event = createRequestEvent({
|
|
534
|
+
authorization: `Bearer ${token}`,
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
const handler = authorizer.requestHandler();
|
|
538
|
+
const result = await handler(event, mockContext);
|
|
539
|
+
|
|
540
|
+
const statement = result.policyDocument
|
|
541
|
+
.Statement[0] as StatementWithResource;
|
|
542
|
+
expect(statement.Resource).toBe(
|
|
543
|
+
'arn:aws:execute-api:us-east-1:123456789:api-id/stage/*',
|
|
544
|
+
);
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
it('should include context from getContext', async () => {
|
|
548
|
+
const authorizer = new OidcAuthorizer({
|
|
549
|
+
config: {
|
|
550
|
+
issuer: 'https://auth.example.com',
|
|
551
|
+
audience: 'my-api',
|
|
552
|
+
},
|
|
553
|
+
getContext: (claims) => ({
|
|
554
|
+
userId: claims.sub!,
|
|
555
|
+
}),
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
const token = await createTestToken();
|
|
559
|
+
const event = createRequestEvent({
|
|
560
|
+
authorization: `Bearer ${token}`,
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
const handler = authorizer.requestHandler();
|
|
564
|
+
const result = await handler(event, mockContext);
|
|
565
|
+
|
|
566
|
+
expect(result.context).toEqual({
|
|
567
|
+
userId: 'user-123',
|
|
568
|
+
});
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
it('should deny when authorize callback returns false', async () => {
|
|
572
|
+
const authorizer = new OidcAuthorizer({
|
|
573
|
+
config: {
|
|
574
|
+
issuer: 'https://auth.example.com',
|
|
575
|
+
audience: 'my-api',
|
|
576
|
+
},
|
|
577
|
+
authorize: () => false,
|
|
578
|
+
});
|
|
579
|
+
|
|
580
|
+
const token = await createTestToken();
|
|
581
|
+
const event = createRequestEvent({
|
|
582
|
+
authorization: `Bearer ${token}`,
|
|
583
|
+
});
|
|
584
|
+
|
|
585
|
+
const handler = authorizer.requestHandler();
|
|
586
|
+
const result = await handler(event, mockContext);
|
|
587
|
+
|
|
588
|
+
expect(result.principalId).toBe('unauthorized');
|
|
589
|
+
expect(result.policyDocument.Statement[0].Effect).toBe('Deny');
|
|
590
|
+
});
|
|
591
|
+
|
|
592
|
+
it('should handle null headers gracefully', async () => {
|
|
593
|
+
const authorizer = new OidcAuthorizer({
|
|
594
|
+
config: {
|
|
595
|
+
issuer: 'https://auth.example.com',
|
|
596
|
+
audience: 'my-api',
|
|
597
|
+
},
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
const event: APIGatewayRequestAuthorizerEvent = {
|
|
601
|
+
type: 'REQUEST',
|
|
602
|
+
methodArn:
|
|
603
|
+
'arn:aws:execute-api:us-east-1:123456789:api-id/stage/GET/resource',
|
|
604
|
+
headers: null,
|
|
605
|
+
multiValueHeaders: null,
|
|
606
|
+
pathParameters: null,
|
|
607
|
+
queryStringParameters: null,
|
|
608
|
+
multiValueQueryStringParameters: null,
|
|
609
|
+
stageVariables: null,
|
|
610
|
+
requestContext: {} as any,
|
|
611
|
+
resource: '/resource',
|
|
612
|
+
path: '/resource',
|
|
613
|
+
httpMethod: 'GET',
|
|
614
|
+
};
|
|
615
|
+
|
|
616
|
+
const handler = authorizer.requestHandler();
|
|
617
|
+
const result = await handler(event, mockContext);
|
|
618
|
+
|
|
619
|
+
expect(result.principalId).toBe('unauthorized');
|
|
620
|
+
expect(result.policyDocument.Statement[0].Effect).toBe('Deny');
|
|
621
|
+
});
|
|
622
|
+
});
|
|
623
|
+
});
|