├── .gitignore ├── LICENSE ├── README.md ├── deploy.zip ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | .DS_Store 3 | package-lock.json -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Chris Concannon 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JWT Token Lambda Authorizer 2 | ## Overview 3 | This function uses the `jwks-rsa` and `jsonwebtoken` npm packages to implement token validation of JSON Web Tokens (JWTs). These tokens are granted by ID Providers using the OAuth2 protocol. 4 | 5 | The authorizer expects to find a JWT in the Authorization header. 6 | 7 | The public RSA256 key(s) from the Identity Provider are fetched and cached. The JWT is then validated with the public RSA key without further HTTP calls. This means that there is no token introspection performed at the ID Provider server. This allows the authorizer to perform authorization based on signed, unexpired tokens that contain the required issuer and audience credentials per OIDC spec. This also enables extremely low latency times for invoking the AWS API Gateway calls to protected resources. 8 | 9 | ## Deployment 10 | Upload the .zip file to AWS Lambda in the same region as the API Gateway resources you intend to protect with this authorizer. 11 | 12 | ## Configuration 13 | Two environment variables must be set when you deploy the function to AWS: 14 | - RESOURCE 15 | - the AWS arn for the API Gateway endpoint(s) you intend to secure with this Lambda Authorizer 16 | - JWKS_URI 17 | - the uri to retrieve the public signing keys at your Identity Provider (this can usually be found at the OAuth2/OIDC server discovery endpoint) 18 | 19 | ## Additional Security 20 | The function as-is will validate the JWT claims by checking the JWT signature against the IdP public RSA key. This provides assurance that the claims in the JWT can be trusted, but there is no logic that restricts access based on the JWT claims. 21 | 22 | **As-is, this function will allow access to the specified arn resource to any bearer of a valid JWT.** 23 | 24 | **As-is, this function allows access to all users regardless of issuer, scope, and intended audience claims.** 25 | 26 | It is best practice to implement fine-grained authorization to access protected resources based on these claims. 27 | 28 | Refer to [AWS API Gateway Lambda Authorizers Documentation](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html) for more in-depth documentation about use of Lambda Authorizers. 29 | -------------------------------------------------------------------------------- /deploy.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cconcannon/lambda-authorizer-jwt/3d2d8e03e5ab4948c757b0c51172f7ae9788ba47/deploy.zip -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const jwt = require('jsonwebtoken'); 2 | const jwksClient = require('jwks-rsa'); 3 | 4 | const keyClient = jwksClient({ 5 | cache: true, 6 | cacheMaxAge: 86400000, //value in ms 7 | rateLimit: true, 8 | jwksRequestsPerMinute: 10, 9 | strictSsl: true, 10 | jwksUri: process.env.JWKS_URI 11 | }) 12 | 13 | const verificationOptions = { 14 | // verify claims, e.g. 15 | // "audience": "urn:audience" 16 | "algorithms": "RS256" 17 | } 18 | 19 | const allow = { 20 | "principalId": "user", 21 | "policyDocument": { 22 | "Version": "2012-10-17", 23 | "Statement": [ 24 | { 25 | "Action": "execute-api:Invoke", 26 | "Effect": "Allow", 27 | "Resource": process.env.RESOURCE 28 | } 29 | ] 30 | } 31 | } 32 | 33 | function getSigningKey (header = decoded.header, callback) { 34 | keyClient.getSigningKey(header.kid, function(err, key) { 35 | const signingKey = key.publicKey || key.rsaPublicKey; 36 | callback(null, signingKey); 37 | }) 38 | } 39 | 40 | function extractTokenFromHeader(e) { 41 | if (e.authorizationToken && e.authorizationToken.split(' ')[0] === 'Bearer') { 42 | return e.authorizationToken.split(' ')[1]; 43 | } else { 44 | return e.authorizationToken; 45 | } 46 | } 47 | 48 | function validateToken(token, callback) { 49 | jwt.verify(token, getSigningKey, verificationOptions, function (error) { 50 | if (error) { 51 | callback("Unauthorized") 52 | } else { 53 | callback(null, allow) 54 | } 55 | }) 56 | } 57 | 58 | exports.handler = (event, context, callback) => { 59 | let token = extractTokenFromHeader(event) || ''; 60 | validateToken(token, callback); 61 | } 62 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "lambda-authorizer-jwt", 3 | "version": "1.0.0", 4 | "description": "A Lambda Authorizer in NodeJS for JWT access tokens", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/cconcannon/lambda-authorizer-jwt.git" 12 | }, 13 | "author": "Chris Concannon", 14 | "license": "ISC", 15 | "bugs": { 16 | "url": "https://github.com/cconcannon/lambda-authorizer-jwt/issues" 17 | }, 18 | "homepage": "https://github.com/cconcannon/lambda-authorizer-jwt#readme" 19 | } 20 | --------------------------------------------------------------------------------