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-api-auth

v1.0.0

Published

A middleware to expose authentication logic for product API inside a serverless lambda

Downloads

2

Readme

lambda-api-auth

A middleware that can be used inside a lambda to add an authorization layer for product API calls.

Table of Contents

Prerequisites

  • The project is compatible with the following library: lambda-api
  • The environment variable PRODUCT_API_HOST must be set (eg. api.rebrandly.com)

Install

npm install -S lambda-api-auth

Usage

import API from 'lambda-api';
import { lambdaApiAuth } from 'lambda-api-auth';

const api = API({
    logger: {
        access: true,
        stack: true
    }
});

api.use(lambdaApiAuth);

// [...]

Authentication

Authentication requires at least a valid apikey or a non-expired oauth token in the request headers. It is also possible to validate a workspace and/or a domain associated with the account. To validate a workspace, pass the value of the public_workspace_id in the headers with the key workspace or in the query params with the key workspaceId. To validate a domain, pass the value of the domain_public_id in the query params with the key domainId. Workspace and domain validation is enabled by default, you can esplicitly enable/disable it using the environment variables VALIDATE_WORKSPACE and VALIDATE_DOMAIN (allowed values are true or false).

Request enrichment

After a successful authorization, the middleware will enrich the request object with account data retrieved from the product API. You can access it easily under request.account:

api.get('/my/path', async (req, res) => {
    console.log(req.account.id);
    console.log(req.account.createdAt);
};

If workspace and/or domain validation is enabled, request object will be also enriched with workspace and/or domain data retrieved from product API:

api.get('/my/path', async (req, res) => {
    console.log(req.workspace.id);
    console.log(req.domain.id);
};

A note about headers

Mind that if your lambda is under an API Gateway, you have to explicitly enable the following headers in your lambda-api options and on the API Gateway itself:

index.ts

import API from 'lambda-api';

const api = API({
    logger: {
        access: true,
        stack: true
    }
});

api.options('/*', (req, res) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With, apikey, workspace');
    res.status(200).send({});
});

serverless.yml

# [...]

functions:
    your_lambda:
      # [...]
      events:
        - http:
              path: /my/path
              method: GET
              cors:
                origin: '*'
                headers:
                  - Authorization
                  - Content-Length
                  - Content-Type
                  - X-Requested-With
                  - apikey
                  - workspace
                allowCredentials: true
        # [...]