From 4d36259531f8640abc29237236bc369208975950 Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 14 Apr 2024 21:16:31 +0930 Subject: [PATCH] docs: lambda-function-url example asyncify app --- .../lambda-function-url/packages/api/app.ts | 6 ++-- .../packages/api/package.json | 8 ++--- .../packages/api/utils/cognito.ts | 29 +------------------ .../packages/ui/.env/.env.development | 2 +- .../packages/ui/.env/.env.production | 2 +- .../lambda-function-url/scripts/artillery.yml | 2 +- 6 files changed, 12 insertions(+), 37 deletions(-) diff --git a/examples/lambda-function-url/packages/api/app.ts b/examples/lambda-function-url/packages/api/app.ts index 336d1253..a5a69ced 100644 --- a/examples/lambda-function-url/packages/api/app.ts +++ b/examples/lambda-function-url/packages/api/app.ts @@ -11,8 +11,9 @@ import { NotFoundError, UnauthenticatedError, UserInputError, BadRequestError } import { log } from './utils/logger' import { IS_PRODUCTION } from './config' import { idTokenVerifier } from './utils/cognito' +import asyncify from 'express-asyncify' -const app = express() +const app = asyncify(express()) app.use( cors({ maxAge: 86400, @@ -21,7 +22,6 @@ app.use( app.use(json()) app.use(async (req, res, next) => { const { event = {} } = getCurrentInvoke() - // NOTE: APIGW sets event.requestContext.authorizer when using an Authorizer // If one isn't set, this function is either being invoked locally or through Lambda Function URL let jwtClaims = event.requestContext?.authorizer?.claims @@ -29,12 +29,14 @@ app.use(async (req, res, next) => { console.time('time_to_validate_jwt') if (!req.headers.authorization) { console.error('Missing Authorization header') + console.timeEnd('time_to_validate_jwt') throw new UnauthenticatedError() } try { jwtClaims = await idTokenVerifier.verify(req.headers.authorization) } catch (error) { console.error('error while validating token', error) + console.timeEnd('time_to_validate_jwt') throw new UnauthenticatedError() } console.timeEnd('time_to_validate_jwt') diff --git a/examples/lambda-function-url/packages/api/package.json b/examples/lambda-function-url/packages/api/package.json index e9a82cc4..ad699767 100644 --- a/examples/lambda-function-url/packages/api/package.json +++ b/examples/lambda-function-url/packages/api/package.json @@ -6,9 +6,9 @@ "type": "module", "exports": "./app.ts", "scripts": { - "start-api:dev": "ENVIRONMENT=development IS_LOCAL=1 dotenv -e .env.development.local -e .env.development -- tsx -r tsconfig-paths/register ./app.local", - "start-api:staging": "ENVIRONMENT=staging IS_LOCAL=1 dotenv -e .env.staging.local -e .env.staging -- tsx -r tsconfig-paths/register ./app.local", - "start-api:prod": "ENVIRONMENT=production IS_LOCAL=1 dotenv -e .env.production.local -e .env.production -- tsx -r tsconfig-paths/register ./app.local", + "start-api:dev": "ENVIRONMENT=development IS_LOCAL=1 dotenv -e .env.development.local -e .env.development -- tsx watch -r tsconfig-paths/register ./app.local", + "start-api:staging": "ENVIRONMENT=staging IS_LOCAL=1 dotenv -e .env.staging.local -e .env.staging -- tsx watch -r tsconfig-paths/register ./app.local", + "start-api:prod": "ENVIRONMENT=production IS_LOCAL=1 dotenv -e .env.production.local -e .env.production -- tsx watch -r tsconfig-paths/register ./app.local", "test": "jest", "test:watch": "npm run test -- --watch" }, @@ -39,4 +39,4 @@ "ulidx": "^2.2.1", "winston": "^3.11.0" } -} +} \ No newline at end of file diff --git a/examples/lambda-function-url/packages/api/utils/cognito.ts b/examples/lambda-function-url/packages/api/utils/cognito.ts index 22a02aab..8c244539 100644 --- a/examples/lambda-function-url/packages/api/utils/cognito.ts +++ b/examples/lambda-function-url/packages/api/utils/cognito.ts @@ -4,36 +4,9 @@ import { COGNITO_USER_POOL_ID, COGNITO_USER_POOL_CLIENT_ID } from '../config' import { assertHasRequiredEnvVars } from '@/common/required-env-vars' assertHasRequiredEnvVars(['COGNITO_USER_POOL_ID', 'COGNITO_USER_POOL_CLIENT_ID']) + export const idTokenVerifier = CognitoJwtVerifier.create({ userPoolId: COGNITO_USER_POOL_ID, tokenUse: 'id', clientId: COGNITO_USER_POOL_CLIENT_ID, }) - -// const COGNITO_USER_POOL_JWKS_ENDPOINT = `https://cognito-idp.us-west-2.amazonaws.com/${COGNITO_USER_POOL_ID}/.well-known/jwks.json` -// const COGNITO_USER_POOL_JWKS = { -// keys: [ -// { -// alg: 'RS256', -// e: 'AQAB', -// kid: 'RQ0/M1soNZXXA3k3E57kGC0mLlIiPCiLPKTTmT/Fb/Q=', -// kty: 'RSA', -// n: 'yip8NDEpPVcKT3lc2GEZk12VdZFPHJwA1lOqqhNmW8DR11z0Di0f90pc-Upu5dKVGgWgFd6oMEOPBmfzIaFZQm30I0v_6UXhoNsAglTAcXKBRB5OEZ5TgcLM5JcFmndzi9WL4p55fA92OTS6-y-GOIc2t8ppwngtoBvwpbTeCqbOflWS41mFTf6pQ-qoaIaKer8Itr6pDGGm-JdsWNGoDaIIA7fmu_RfP_r8f0aa0ZLImT3kaMQZ-S0hTJo3FyE0Dpo8AWDfOVnYAolYAS97e7SZ_Lo5mPrCzzrh4Iic1MoeAeu6uWAhFhvambOXefBYzpxiGWe3n-cfhVebUlciiw', -// use: 'sig', -// }, -// { -// alg: 'RS256', -// e: 'AQAB', -// kid: 'L1k6dj0o/kOmg0xQ66FWZyNofi28rG6UCiOBB9wHS4k=', -// kty: 'RSA', -// n: 'rK3HqRyVPndtnN9fTCnOnXj0GNIMjtow0bqezmnh--S8JZQopTcvBnb3ZI6j07IMmlngd_DymuRXTffP0QueqhYBXwKscj2mhTr-Wn1SnDNKiHVAZvFvIrIXQy5tLydIYPg6o02T5v-XXYCGvrFpQwFLx6Xj-MAicyBqE4U0Z-cA4PJJqCLhuKMXfFsDFolmY3gpvT44Z7_pehw7C8jKMlLRb8qMkcz1-Pg_caQLGUFGqUu7t-5hGRHiHmE4uPMTRODOLNbmrmsYjuCiAUo89UBo06WSAilBesteAXKKpQaJs93mxXyrfia91sw4Wvy9_nKcpmx12uxXRpHsXTWupw', -// use: 'sig', -// }, -// ], -// } -// const jwks = JSON.parse(readFileSync('jwks.json', { encoding: 'utf-8' })) -// idTokenVerifier.cacheJwks(COGNITO_USER_POOL_JWKS) - -// export async function getCognitoUserPoolJwks() { -// return COGNITO_USER_POOL_JWKS -// } diff --git a/examples/lambda-function-url/packages/ui/.env/.env.development b/examples/lambda-function-url/packages/ui/.env/.env.development index d1738565..046f958f 100644 --- a/examples/lambda-function-url/packages/ui/.env/.env.development +++ b/examples/lambda-function-url/packages/ui/.env/.env.development @@ -3,7 +3,7 @@ NEXT_PUBLIC_AUTO_VERIFY_USERS=1 NEXT_PUBLIC_ApiGatewayUrl="https://0c8qrs3t4l.execute-api.us-west-2.amazonaws.com" NEXT_PUBLIC_LambdaFunctionUrl="https://d4nnw44cddanflk7kv3r3xbbke0uvnis.lambda-url.us-west-2.on.aws/" NEXT_PUBLIC_CloudFrontDistributionUrl="https://dthdcw7dsiu7v.cloudfront.net" -NEXT_PUBLIC_ApiEndpoint="https://0c8qrs3t4l.execute-api.us-west-2.amazonaws.com" +NEXT_PUBLIC_ApiEndpoint="https://dthdcw7dsiu7v.cloudfront.net" NEXT_PUBLIC_CognitoUserPoolId="us-west-2_MIQhmcA5v" NEXT_PUBLIC_CognitoUserPoolClientId="7covdgpck9g0qlj93fm93n0pnj" NEXT_PUBLIC_Region="us-west-2" diff --git a/examples/lambda-function-url/packages/ui/.env/.env.production b/examples/lambda-function-url/packages/ui/.env/.env.production index 66314005..4110ad0c 100644 --- a/examples/lambda-function-url/packages/ui/.env/.env.production +++ b/examples/lambda-function-url/packages/ui/.env/.env.production @@ -2,7 +2,7 @@ NEXT_PUBLIC_ApiGatewayUrl="https://1cql2qj0ki.execute-api.us-west-2.amazonaws.com" NEXT_PUBLIC_LambdaFunctionUrl="https://dbnmtkykig3vobqu6f73dbgiam0qxeot.lambda-url.us-west-2.on.aws/" NEXT_PUBLIC_CloudFrontDistributionUrl="https://d17bj8zz01o4vw.cloudfront.net" -NEXT_PUBLIC_ApiEndpoint="https://1cql2qj0ki.execute-api.us-west-2.amazonaws.com" +NEXT_PUBLIC_ApiEndpoint="https://d17bj8zz01o4vw.cloudfront.net" NEXT_PUBLIC_CognitoUserPoolId="us-west-2_6bXjvA4Qc" NEXT_PUBLIC_CognitoUserPoolClientId="5d5ahohds4882ua15nqn3vscr8" NEXT_PUBLIC_Region="us-west-2" diff --git a/examples/lambda-function-url/scripts/artillery.yml b/examples/lambda-function-url/scripts/artillery.yml index 7cfd3db7..f34c311b 100644 --- a/examples/lambda-function-url/scripts/artillery.yml +++ b/examples/lambda-function-url/scripts/artillery.yml @@ -35,4 +35,4 @@ scenarios: - get: name: API Gateway HTTP API + Cognito Authorizer url: 'https://1cql2qj0ki.execute-api.us-west-2.amazonaws.com/todo-lists' - count: 100 + count: 10