-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorizer.ts
52 lines (44 loc) · 1.28 KB
/
authorizer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import {
CustomAuthorizerEvent,
CustomAuthorizerHandler,
CustomAuthorizerResult,
} from 'aws-lambda';
import { passwordsAreSame } from '@app/utils/passwordsAreSame';
import {
generateAllowPolicy,
generateDenyPolicy,
} from '@app/utils/policyFactory';
import middy from '@middy/core';
import inputOutputLogger from '@middy/input-output-logger';
interface Credential {
readonly principalId: string;
readonly accessToken: string;
readonly allowedMethods: string[];
}
const credentialsList: Credential[] = [
{
principalId: 'test-access',
accessToken: 'token',
allowedMethods: ['POST/task'],
},
];
const handler: CustomAuthorizerHandler = async (
event: CustomAuthorizerEvent,
): Promise<CustomAuthorizerResult> => {
const actualToken = event.headers?.Authorization ?? undefined;
if (!actualToken) {
return generateDenyPolicy('unauthorized', event.methodArn);
}
const result = credentialsList.filter((item) => {
return passwordsAreSame(item.accessToken, actualToken);
});
if (result.length !== 1) {
return generateDenyPolicy('unauthorized', event.methodArn);
}
return generateAllowPolicy(
event.methodArn,
result[0].principalId,
result[0].allowedMethods,
);
};
export const authorizer = middy(handler).use(inputOutputLogger());