npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lambda-custom-authorizer-middleware

v1.0.3

Published

Adds support of custom lambda authorizers for local offline usage, e.g. with serverless-offlin

Downloads

10

Readme

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', // NOTE: path is relative to the package inside node_modules/
   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