Skip to main content

CDK Lambda TokenAuthorizer JWT

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

View on Construct Hub

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

Install

TypeScript

npm install @cloudy-with-a-chance-of-meatballs/cdk-lambda-token-authorizer-jwt

Python

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

Usage

Notes

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 * as lambda from 'aws-cdk-lib/aws-lambda';

import { TokenAuthorizerJwtFunction } from '@cloudy-with-a-chance-of-meatballs/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', {
      tokenAuthorizerOptions: {
        verificationStrategy: {
          strategyName: 'argument',
          secret: 'mySecret-symmetric-or-asymmetric',
        },
      },
    });

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

    const dummyFn = new lambda.Function(this, 'helloWorldFunction', {
      handler: 'index.handler',
      code: lambda.Code.fromInline(`exports.handler = async (event) => { console.log('event: ', event); return '{"statusCode": 200, "message": "DummyLambdaFunction"}' };`),
      runtime: lambda.Runtime.NODEJS_16_X,
    });

    const dummyLambdaIntegration = new apigateway.LambdaIntegration(
      dummyFn, { passthroughBehavior: apigateway.PassthroughBehavior.WHEN_NO_MATCH }
    );

    const someMethod = api.root.addMethod("GET", dummyLambdaIntegration, {
      authorizer: tokenAuthorizer
    });
  }
}

Validation

import * as cdk from 'aws-cdk-lib';
import { TokenAuthorizerJwtFunction } from '@cloudy-with-a-chance-of-meatballs/cdk-lambda-token-authorizer-jwt';

const app   = new cdk.App();
const stack = new cdk.Stack(app, 'MyStack');

const myValidation = { properties:{ iss: { enum: ['my_trusted_iss'] } }};

new TokenAuthorizerJwtFunction(stack, 'example-stack', {
  tokenAuthorizerOptions: {
    payloadValidationStrategy: {
      strategyName: 'schema',
      schema: JSON.stringify(myValidation)
    },
    verificationStrategy: { strategyName: 'argument',  secret: 'someSecret' }
  }
});

Using JWKS

import * as cdk from 'aws-cdk-lib';
import { TokenAuthorizerJwtFunction } from '@cloudy-with-a-chance-of-meatballs/cdk-lambda-token-authorizer-jwt';

const app   = new cdk.App();
const stack = new cdk.Stack(app, 'MyStack');

new TokenAuthorizerJwtFunction(stack, 'example-stack', {
  tokenAuthorizerOptions: {
    verificationStrategy: {
      strategyName: 'jwksFromUriByKid',
      uri: 'uri',
      kid: 'kid',
    }
  }
});

Using asymmetric algorithms, e.g. public key

import * as cdk from 'aws-cdk-lib';
import { TokenAuthorizerJwtFunction } from '@cloudy-with-a-chance-of-meatballs/cdk-lambda-token-authorizer-jwt';

const app   = new cdk.App();
const stack = new cdk.Stack(app, 'MyStack');

const myPublicKeyOneliner = '-----BEGIN PUBLIC KEY---\nMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKuTfz7kpJHPrmcmgx4Xf4GMoM2kK4mh\nMpSOW3qu1zZA1wfMHV8PS0Kds0nXMB6mmHk/Ke1\Et68aEspQRIn1aLcCAwEAAQ==\n-----END PUBLIC KEY-----';

new TokenAuthorizerJwtFunction(stack, 'example-stack', {
  tokenAuthorizerOptions: {
    verificationStrategy: {
      strategyName: 'argument',
      secret: myPublicKeyOneliner
    }
  }
});

Using symmetric algorithms, same key for sign and verify ⚠️

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

import * as cdk from 'aws-cdk-lib';
import { TokenAuthorizerJwtFunction } from '@cloudy-with-a-chance-of-meatballs/cdk-lambda-token-authorizer-jwt';

const app   = new cdk.App();
const stack = new cdk.Stack(app, 'MyStack');

const mySymmetricSecret = 'sharedSecret';

new TokenAuthorizerJwtFunction(stack, 'example-stack', {
  tokenAuthorizerOptions: {
    verificationStrategy: {
      strategyName: 'argument',
      secret: mySymmetricSecret
    }
  }
});

🍻

Metadata

Release files for cloudy-with-a-chance-of-meatballs.cdk-lambda-token-authorizer-jwt 0.1.24

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cloudy-with-a-chance-of-meatballs.cdk-lambda-token-authorizer-jwt 0.1.24
File Size Uploaded Provenance Actions
cloudy-with-a-chance-of-meatballs.cdk-lambda-token-authorizer-jwt-0.1.24.tar.gz 225.4 kB Without provenance Details for cloudy-with-a-chance-of-meatballs.cdk-lambda-token-authorizer-jwt-0.1.24.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cloudy-with-a-chance-of-meatballs.cdk-lambda-token-authorizer-jwt 0.1.24
File Interpreter ABI Platform Actions
cloudy_with_a_chance_of_meatballs.cdk_lambda_token_authorizer_jwt-0.1.24-py3-none-any.whl Python 3 none any Details for cloudy_with_a_chance_of_meatballs.cdk_lambda_token_authorizer_jwt-0.1.24-py3-none-any.whl

Total release size: 452.1 kB

Release files / cloudy-with-a-chance-of-meatballs.cdk-lambda-token-authorizer-jwt-0.1.24.tar.gz

Download URL cloudy-with-a-chance-of-meatballs.cdk-lambda-token-authorizer-jwt-0.1.24.tar.gz
Size 225.4 kB
Tags Source
SHA-256 checksum
How to use checksums
aecb7c23b0633161b8f7ab600c9cae54fd946fdf3aac29d6df38af79ce5e7270
BLAKE2b-256 checksum
How to use checksums
c71b8469657e3565ad85b5a1f2b97d2c8876ec7fb1b69478145bb9c0f1947903
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.3

Release files / cloudy_with_a_chance_of_meatballs.cdk_lambda_token_authorizer_jwt-0.1.24-py3-none-any.whl

Download URL cloudy_with_a_chance_of_meatballs.cdk_lambda_token_authorizer_jwt-0.1.24-py3-none-any.whl
Size 226.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
258a85c0b444fe67548d5a2c151589de83b5f3f69951261398a61a18d30e8ec6
BLAKE2b-256 checksum
How to use checksums
582d3201def857b79a3be5d1fcb0b1517698ca845b5ff5c050b307975d0cf87a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.3

Security for cloudy-with-a-chance-of-meatballs.cdk-lambda-token-authorizer-jwt 0.1.24

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.24 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page