Skip to content

Instantly share code, notes, and snippets.

@cal0610
Last active November 2, 2022 03:21
Show Gist options
  • Save cal0610/7b8b394c5d930340143f3b1c1bf4523b to your computer and use it in GitHub Desktop.
Save cal0610/7b8b394c5d930340143f3b1c1bf4523b to your computer and use it in GitHub Desktop.
token authorizer
import { APIGatewayTokenAuthorizerHandler } from 'aws-lambda';
export const authorizer: APIGatewayTokenAuthorizerHandler = async (
event: any
) => {
const token = event.authorizationToken;
let effect = 'Deny';
if (
compareTokenWithCredentials(
token,
process.env.medium_username as string, // environment variable from ssm in myStack.ts
process.env.medium_password as string
)
) {
effect = 'Allow';
}
return {
principalId: 'user',
policyDocument: {
Version: '2012-10-17',
Statement: [
{
Action: 'execute-api:Invoke',
Effect: effect,
Resource: '*', // best practice is to limit this to your api gateway arn
},
],
},
};
};
const btoa = (str: string) => Buffer.from(str).toString('base64');
const compareTokenWithCredentials = (
token: string,
user: string,
pass: string
) => token === `Basic ${btoa(`${user}:${pass}`)}`;
export default authorizer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment