Git Product home page Git Product logo

serverless-offline-local-authorizers-plugin's Introduction

serverless-offline-local-authorizers-plugin

Serverless plugin for adding authorizers when developing and testing functions locally with serverless-offline.

Serverless npm npm

This plugin allows you to add local authorizer functions to your serverless projects. These authorizers are added dynamically in a way they can be called by serverless-offline but don't interfer with your deployment and your shared authorizer functions. This helps when you have shared API Gateway authorizers and developing and testing locally with serverless-offline.

โš ๏ธ If you are using this plugin and get schema validation errors: Please check indentation of localAuthorizer: config property! See example below...

Installation

Installing using npm:

npm i serverless-offline-local-authorizers-plugin --save-dev

Usage

Step 1: Define your authorizer functions in a file of your choice (.js, .mjs, .ts and others) and later you will inform the path and name or file default called local-authorizers.js and put it into your project root (that's where your serverless.yml lives).

If you want the local function to call your deployed shared authorizer it could look something like this:

const AWS = require("aws-sdk");
const mylocalAuthProxyFn = async (event, context) => {

  const lambda = new AWS.Lambda();
  const result = await lambda.invoke({
    FunctionName: "my-shared-lambda-authorizer",
    InvocationType: "RequestResponse",
    Payload: JSON.stringify(event),
  }).promise();

  if (result.StatusCode === 200) {
    return JSON.parse(result.Payload);
  }

  throw Error("Authorizer error");
};

module.exports = { mylocalAuthProxyFn };

Of course you could also just return a mocked response, call Cognito to mock your Cognito Authorizer or whatever suits your needs. You can also define multiple authorizer functions if you need to.

Step 2: In your serverless.yml, add the localAuthorizer property to your http events. This will not interfere with your "real" authorizers and will be ignored upon deployment.

functions:
  myFunction:
    handler: myFunction.handler
    events:
      - http:
          path: /my/api/path
          method: GET
          authorizer:
            type: CUSTOM
            authorizerId: abcjfk
          localAuthorizer:
            name: "mylocalAuthProxyFn"
            pathFile: "local-authorizers.js" # Optional
            type: "request"

Step 3: Add the plugin to the plugins sections in serverless.yml:

plugins:
  - serverless-offline-local-authorizers-plugin
  - serverless-offline

Step 4: Fire up serverless offline with the local-authorizers option:

$ sls offline local-authorizers --stage dev --region eu-central-1

License

MIT

serverless-offline-local-authorizers-plugin's People

Contributors

dependabot[bot] avatar dimabory avatar dion-conqa avatar luisdemarchi avatar nlang avatar scmilee 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.