0.1.11 • Published 11 months ago

@cloudy-with-a-chance-of-meatballs/aws-apigateway-rest-token-authorizer-jwt v0.1.11

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

aws-apigateway-rest-token-authorizer-jwt

A typescript class strongly coupled to aws apigateway and lambda

Usage

npm install @cloudy-with-a-chance-of-meatballs/aws-apigateway-rest-token-authorizer-jwt

Basic

import { AwsApigatewayRestTokenAuthorizerJwt } from '@cloudy-with-a-chance-of-meatballs/aws-apigateway-rest-token-authorizer-jwt';

const authorizer = new AwsApigatewayRestTokenAuthorizerJwt();

Recommended usage inside a Lambda function added to a RestAPI as token authorizer

import { AwsApigatewayRestTokenAuthorizerJwt }          from '@cloudy-with-a-chance-of-meatballs/aws-apigateway-rest-token-authorizer-jwt';
import { AuthResponse, APIGatewayTokenAuthorizerEvent } from "aws-lambda";

const authorizer = new AwsApigatewayRestTokenAuthorizerJwt();

// handle event
export const authHandler = async (event: APIGatewayTokenAuthorizerEvent): Promise<AuthResponse> => {
  const options: ITokenAuthorizerOptions = {
    verificationStrategy: {
      strategyName: "argument", // or jwksFromUriByKid and provide uri and kid
      secret: "foobar",
    },
    payloadValidationStrategy: {
      strategyName: "schema",
      schema: JSON.stringify({
          properties:{ iss: { enum: ['my_trusted_iss'] } } //...
      })
    }
  };
  return authorizer.getAuthResponse(event, options);
};

Token verification

// against asymmetric or symmetric "secret"

authorizer.getAuthResponse(
  { type: 'TOKEN', methodArn: 'methodArn', authorizationToken: 'JWTTOKENSTR' },
  {
    verificationStrategy: {
      strategyName: 'argument',
      secret: 'YOUR-SECRET-OR-PUB-KEY-ONELINER'
    }
  }
);

// against pub key from jwks

authorizer.getAuthResponse(
  { type: 'TOKEN', methodArn: 'methodArn', authorizationToken: 'JWTTOKENSTR' },
  {
    verificationStrategy: {
      strategyName: 'jwksFromUriByKid',
      uri: 'https://example.auth0.com/.well-known/jwks.json',
      kid: 'MN9dzu6gnI4ZZ-tjylYNW'
    }
  }
);

Token validation

// JSONSTR = https://ajv.js.org/json-schema.html

authorizer.getAuthResponse(
  { type: 'TOKEN', methodArn: 'methodArn', authorizationToken: 'JWTTOKENSTR' },
  {
    payloadValidationStrategy: {
      strategyName: 'schema',
      schema: 'JSONSTR',
    }
  }
);
0.1.10

11 months ago

0.1.11

11 months ago

0.1.9

11 months ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.12

3 years ago

0.0.11

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.0

3 years ago