A lambda authorizer provides custom authorization for API Endpoints exposed through Amazon API Gateway. This authorizer makes sure that API requests:
- Contain a Bearer token (JWT) generated by your Custom Authorization Server
- And the "uid" (user id claim) in the JWT matches the API request /api/v1/users/{userid} parameter. This ensures that users can only update their own profile and settings. This is done by explicitly whitelisting allowed methods:
// allow update own profile
policy.allowMethod(AuthPolicy.HttpVerb.POST, '/api/v1/users/' + uid);
// allow change own password
policy.allowMethod(AuthPolicy.HttpVerb.POST, '/api/v1/users/' + uid + '/credentials/*');
We use the library Okta JWT Verifier for Node.js to validate the token. And upon validation generate the aws-auth-policy for Amazon API Gateway.
- Clone this repository and
cdinto/api/byob-api-proxy-lambda-authorizer-sample/ rm -rf node_modules/ && npm install --productionnpm run zipwill create a source bundle and place it in the/distfolder. You will need this zip file in the next steps, below.
- Click Create Function
- Select Author from scratch
- Provide a value for Function name
- Runtime = Node.js 10.x
- Click Create Function (This will automatically create an AMI role. But you can also select an existing one with the correct permissions if you have one)
- In the Code entry type field, select Upload a .zip file and upload the previously generated zip file
- Add Environment variables:
| NAME | VALUE |
|---|---|
| AUD | api://default (or whichever value you configured as "Audience" in your AuthorizationServer) |
| CLIENT_ID | (the client_id of your application configured in Okta) |
| ISSUER | The "issuer" value of your AuthorizationServer (e.g. https://acme.okta.com/oauth2/default) |
Create an Authorizer for your API (Amazon API Gateway) using this Lambda function