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

koas-security

v0.7.0

Published

Koas security checks if a request matches the security requirement of an operation. For example, given the following partial OpenAPI document:

Downloads

5,029

Readme

Koas Security

Koas security checks if a request matches the security requirement of an operation. For example, given the following partial OpenAPI document:

components:
  securitySchemes:
    basicAuth:
      type: http
      scheme: basic
    cookieAuth:
      type: apiKey
      in: cookie
      name: key
    oauth2:
      type: oauth2
      scopes:
        email: Read the user’s email address.
        profile: Get the user’s profile.
paths:
  /me:
    get:
      security:
        - basicAuth: []
          cookieAuth: []
        - oauth2:
            - profile

Koas security will make sure the user is either authenticated using HTTP basic authentication and a cookie named key, or using OAuth2. The first authentication requirement specified in the document wins.

Installation

npm install koa koas-core koas-security

Usage

const Koa = require('koa');
const { koas } = require('koas-core');
const { security } = require('koas-security');

const api = require('./api.json');

const app = new Koa();
app.use(
  koas(api, [
    security({
      // User getters
    }),
  ]),
);

This middleware sets the users and clients variables in the Koa context. These are mappings of security requirements to authenticated users and OAuth2 clients respectively. Also user and client are set to match a single user and client. Since most API calls only require a single authentication method at a time, this can be used for convenience.

Options

Koas security takes a mapping of security requirement keys to user get functions. The signature depends on the type of authentication.

apiKey

The function accepts the API key, and should return the user that is authenticated using that key.

http

| scheme | Description | | -------- | ---------------------------------------------------------------------------------------------------------- | | basic | The function accepts the username and password, and should return the user that matches these credentials. | | bearer | The function accepts the bearer access token, and should return the user that matches these credentials. |

oauth2 / openIdConnect

The function accepts the access token, and should return a tuple which consists of the user and the authenticated OAuth2 client.

Declaring clients and users

When using TypeScript, the Clients and Users interfaces can be augmented to map security scheme to custom types. For example:

declare module 'koas-security' {
  interface Users {
    basicAuth: User;
    cookieAuth: User;
    oauth2: User;
  }

  interface Clients {
    oauth2: {
      type: 'oauth2';
      scopes: ('email' | 'profile')[];
    };
  }
}