Skip to content

Commit

Permalink
docs: lambda-function-url example asyncify app
Browse files Browse the repository at this point in the history
  • Loading branch information
brettstack committed Apr 14, 2024
1 parent 09e951f commit 4d36259
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 37 deletions.
6 changes: 4 additions & 2 deletions examples/lambda-function-url/packages/api/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -21,20 +22,21 @@ 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
if (!jwtClaims) {
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')
Expand Down
8 changes: 4 additions & 4 deletions examples/lambda-function-url/packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -39,4 +39,4 @@
"ulidx": "^2.2.1",
"winston": "^3.11.0"
}
}
}
29 changes: 1 addition & 28 deletions examples/lambda-function-url/packages/api/utils/cognito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
// }
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion examples/lambda-function-url/scripts/artillery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4d36259

Please sign in to comment.