Skip to main content

cdk-lambda-token-authorizer-jwt

Project description

CDK Lambda TokenAuthorizer JWT

Add a lambda function to your project which can be used as a apigateway token authorizer

GitHub GitHub release (latest SemVer) npm (scoped) PyPI Nuget GitHub Workflow Status (branch) Maintainability codecov Gitpod ready-to-code

Install

TypeScript

npm install cdk-lambda-token-authorizer-jwt
yarn add cdk-lambda-token-authorizer-jwt

Python

pip install cloudy-with-a-chance-of-meatballs.cdk-lambda-token-authorizer-jwt

Usage

  • In general the implementation extends lambda.Function and the TokenAuthorizerJwtFunctionOptions extend lambda.FunctionOptions - so you have full flexibillity over the rest of the options beside handler, code. runtime.
  • The token verfification is done via https://github.com/auth0/node-jsonwebtoken, the jwks fetcher is using https://github.com/auth0/node-jwks-rsa. The implementation per default verifies the token and if given the expiration.
  • Any verification of the token payload must be done over injecting a json schema for validation using https://ajv.js.org/json-type-definition.html.
  • Example usage with Rest Apigateway
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as apigateway from 'aws-cdk-lib/aws-apigateway';

import { TokenAuthorizerJwtFunction } from "cdk-lambda-token-authorizer-jwt";

export class HelloworldStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const api               = new apigateway.RestApi(this, 'ApiName', {});
    const tokenAuthFunction = new TokenAuthorizerJwtFunction(this, 'fnName', {...});
    const tokenAuthorizer   = new apigateway.TokenAuthorizer(this, 'fnNameApiGwAuthorizer', {
      handler: tokenAuthFunction // use the TokenAuthorizerJwtFunction
    });

    const someMethod = api.someresource.addMethod("GET", some_target_integration, {
      authorizer: tokenAuthorizer
    });
  }
}
  • Validation
const myValidation = { properties:{ iss: { enum: ['my_trusted_iss'] } }};

new TokenAuthorizerJwtFunction(stack, 'example-stack', { authorizerOptions: {
  tokenPayloadJsonSchema: JSON.stringify(myValidation)
}});
  • Using JWKS (recommended)
new TokenAuthorizerJwtFunction(stack, 'example-stack', { authorizerOptions: {
  jwks: {
    uri: 'https://example.auth0.com/.well-known/jwks.json';
    kid: 'REEyM0FBMDhFQkQ5QjY4Q0YzRjVGNzQ5OTU3RjUzN0FEREFFNzJGMg'
  }
}});
  • Using asymmetric algorithms, e.g. public key (recommended)
const myPublicKeyOneliner = '-----BEGIN PUBLIC KEY---\nMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKuTfz7kpJHPrmcmgx4Xf4GMoM2kK4mh\nMpSOW3qu1zZA1wfMHV8PS0Kds0nXMB6mmHk/Ke1\Et68aEspQRIn1aLcCAwEAAQ==\n-----END PUBLIC KEY-----';

new TokenAuthorizerJwtFunction(stack, 'example-stack', { authorizerOptions: {
  secret: myPublicKeyOneliner
}});
  • Using symmetric algorithms, same key for sign and verify (highly discouraged)

:warning: the key might be exposed during deploy, in the runtime etc.

const myPublicKeyOneliner = 'sharedSecret';

new TokenAuthorizerJwtFunction(stack, 'example-stack', { authorizerOptions: {
  secret: myPublicKeyOneliner
}});

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

File details

Details for the file cloudy-with-a-chance-of-meatballs.cdk-lambda-token-authorizer-jwt-0.0.26.tar.gz.

File metadata

File hashes

Hashes for cloudy-with-a-chance-of-meatballs.cdk-lambda-token-authorizer-jwt-0.0.26.tar.gz
Algorithm Hash digest
SHA256 c9e91710f6c54c28b9839557c991c937ff83ea428c01672d7b8be78fa5da3e6a
MD5 6d9af1f50a80e5eb40fe9dff1693724e
BLAKE2b-256 5fdef3f0336133574f4e76f06bfa6e8cae6481f23eb6f49c58bd243c675f4ae3

See more details on using hashes here.

File details

Details for the file cloudy_with_a_chance_of_meatballs.cdk_lambda_token_authorizer_jwt-0.0.26-py3-none-any.whl.

File metadata

File hashes

Hashes for cloudy_with_a_chance_of_meatballs.cdk_lambda_token_authorizer_jwt-0.0.26-py3-none-any.whl
Algorithm Hash digest
SHA256 a751e0bba9726e0158e033d2fc97b67eb1c83731e2e7534883ff6d0ae221c698
MD5 26d4a96bc8ad9d78fc070e51cdeee68e
BLAKE2b-256 2959756151ef442a6e8cbe5590b94214826c1359b46af194e4f9e8ecc1c07d53

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page