This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
APIGatewayProxyEvent, | |
APIGatewayProxyEventPathParameters, | |
APIGatewayProxyHandler, | |
APIGatewayProxyResult, | |
} from "aws-lambda"; | |
import { | |
hydrateContext, | |
validateCompanyAccessMiddleware, | |
validateTokenUserAccessMiddleware, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda"; | |
import { UserContext } from "../../../types"; | |
import axios from "axios"; | |
import middy from "@middy/core"; | |
// middleware to ensure that the user is part of the given company i.e. permissions | |
export const hydrateContext = (): middy.MiddlewareObj< | |
APIGatewayProxyEvent, | |
APIGatewayProxyResult |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda"; | |
import { UserContext } from "../../../types"; | |
import middy from "@middy/core"; | |
// middleware to ensure that the user is part of the given company i.e. permissions | |
// when getting their order for a specific company, as well as checking their role | |
export const validateCompanyAccessMiddleware = ( | |
role: string | |
): middy.MiddlewareObj<APIGatewayProxyEvent, APIGatewayProxyResult> => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create our lambda layer for our middleware | |
const validationLayer: lambda.LayerVersion = new lambda.LayerVersion( | |
this, | |
"ValidationLayer", | |
{ | |
compatibleRuntimes: [ | |
lambda.Runtime.NODEJS_12_X, | |
lambda.Runtime.NODEJS_14_X, | |
], | |
code: lambda.Code.fromAsset("../layers/validation"), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add the cognito authorizer to our api which validates our tokens using cognito | |
const cognitoAuthorizer: apigw.CfnAuthorizer = new apigw.CfnAuthorizer( | |
this, | |
"APIGatewayAuthorizer", | |
{ | |
name: "sushi-orders-authorizer", | |
identitySource: "method.request.header.Authorization", | |
providerArns: [userPool.userPoolArn], | |
restApiId: ordersAPI.restApiId, | |
type: apigw.AuthorizationType.COGNITO, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create the cognito user pool for auth | |
const authUserPool: cognito.UserPool = new cognito.UserPool( | |
this, | |
"SushiAuthUserPool", | |
{ | |
userPoolName: "SushiUserAuthUserPool", | |
removalPolicy: RemovalPolicy.DESTROY, | |
lambdaTriggers: { | |
// https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-pre-token-generation.html | |
preTokenGeneration, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
PreTokenGenerationTriggerEvent, | |
PreTokenGenerationTriggerHandler, | |
} from "aws-lambda"; | |
import axios from "axios"; | |
// get the users permissions from a separate service using their token sub | |
async function getUserPermissions(userId: string) { | |
const { data: userPermissions } = await axios.request({ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Regexes { | |
[key: string]: any; | |
} | |
import { getLocale } from "../dynamic-imports"; | |
const localeRegexes: Regexes = { | |
global: { | |
firstName: "^[a-z A-Z]+$", | |
surname: "^[a-z A-Z]+$", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { regexes } from "@shared/regexes"; | |
export const schema = { | |
type: "object", | |
required: ["id", "firstName", "surname", "age"], | |
maxProperties: 4, | |
minProperties: 4, | |
properties: { | |
id: { | |
type: "number", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Errors { | |
[key: string]: any; | |
} | |
import { getLocale } from "../dynamic-imports"; | |
// each of these could have different languages or messages | |
// specific to the locale | |
const localeErrorMessages: Errors = { | |
global: { |