项目作者: vladgolubev

项目描述 :
Lambda Custom Authorizer Middleware for using with AWS Serverless Express and Serverless Offline plugins
高级语言: JavaScript
项目地址: git://github.com/vladgolubev/lambda-custom-authorizer-middleware.git
创建时间: 2017-09-05T19:59:42Z
项目社区:https://github.com/vladgolubev/lambda-custom-authorizer-middleware

开源协议:MIT License

下载


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:

  1. restAP:
  2. handler: lib/handlers/rest-api.handler
  3. events:
  4. - http:
  5. path: v1/{id}/create
  6. method: put
  7. integration: lambda-proxy
  8. authorizer:
  9. arn: arn:aws:lambda:us-east-1:123456789:function:myAuthorizerFunction
  10. 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.

  1. $ 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

  1. import express from 'express';
  2. import awsSlsExpressMiddleware from 'aws-serverless-express/middleware';
  3. import {customLocalLambdaAuthorizer} from 'lambda-custom-authorizer-middleware';
  4. const app = express();
  5. app.use(awsSlsExpressMiddleware.eventContext());
  6. app.use(customLocalLambdaAuthorizer({ // Make sure to add after 'awsSlsExpressMiddleware'
  7. localAuthorizer: {
  8. handlerPath: '../other-project/lambda/auth',
  9. handlerName: 'handler'
  10. }
  11. }));
  12. 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.

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

Lint

  1. $ yarn lint

Build

  1. $ yarn build

Docs

  1. $ yarn docs

Tests

  1. $ yarn test

Coverage

  1. $ yarn coverage