Giter VIP home page Giter VIP logo

lambda-custom-authorizer-middleware's Introduction

AWS Lambda Local Middleware

Lambda Custom Authorizer Middleware for using with AWS Serverless Express and Serverless Offline plugins

npm npm

Purpose

Let's say you are using aws-serverless-express. Cool, you can write lambdas responding to API Gateway using favorite express.

Let's say you are using serverless-offline to simulate API Gateway for local development. Cool, now you can invoke your lambdas locally.

Let's say you have custom lambda authorizers defined in your serverless.yml file like that:

  restAP:
    handler: lib/handlers/rest-api.handler
    events:
      - http:
          path: v1/{id}/create
          method: put
          integration: lambda-proxy
          authorizer:
            arn: arn:aws:lambda:us-east-1:123456789:function:myAuthorizerFunction
            resultTtlInSeconds: 0

Pretty soon you find this issue saying you cannot use custom non-local authorizers.

And here it comes. With this package you can provide path on local file system to your custom authorizer function which isn't required to be inside the project.

Install

Note, it's installed not in dev deps.

$ yarn add lambda-custom-authorizer-middleware

Usage

Due to the fact this package is meant to be used with serverless-offline it relies on its environment variable IS_OFFLINE to switch on using local Lambda function.

And as for now, it's limited to 1 kind of authorizer function per project.

API

customLocalLambdaAuthorizer

Express middleware function constructor to execute local lambda function as a custom authorizer and attach request context to req object as req.apiGateway.event.requestContext.authorizer (as for usage with aws-serverless-exporess npm package)

Parameters

  • options Object Configuration object (optional, default {})
    • options.identitySourceHeader String Name of HTTP header where auth token is located (optional, default authorization)
    • options.localAuthorizer Object Local authorizer function configuration object (optional, default {})
      • options.localAuthorizer.handlerPath String Path on local file system to the function
      • options.localAuthorizer.handlerName String Name of the exported function in provided path
      • options.handlerPath
      • options.handlerName

Examples

import express from 'express';
import awsSlsExpressMiddleware from 'aws-serverless-express/middleware';
import {customLocalLambdaAuthorizer} from 'lambda-custom-authorizer-middleware';

const app = express();

app.use(awsSlsExpressMiddleware.eventContext());
app.use(customLocalLambdaAuthorizer({ // Make sure to add after 'awsSlsExpressMiddleware'
 localAuthorizer: {
   handlerPath: '../other-project/lambda/auth',
   handlerName: 'handler'
 }
}));

app.get('/', (req, res) => res.json(req.apiGateway.event.requestContext.authorizer));
  • Throws Error Throws when config is not provided

Returns Function Express middleware function. Works only when IS_OFFLINE env var is set.

Development

Debug

This package uses debug library, so set environment variable like that to see the logs.

DEBUG=lambda-custom-authorizer-middleware sls offline start

Lint

$ yarn lint

Build

$ yarn build

Docs

$ yarn docs

Tests

$ yarn test

Coverage

$ yarn coverage

lambda-custom-authorizer-middleware's People

Contributors

vladgolubev avatar vladholubiev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.