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

@random-guys/sp-auth

v4.5.1

Published

Client for authentication, authorization and workflows

Downloads

28

Readme

sp-auth

Simple library for authentication and authorization for web APIs

Documentation

Overview

This library aims to provide a set of tools for authentication and authorization of express applications.

Features

  • Stateful Sessions
  • Stateless Sessions
  • Encryption and Decryption using tweetnacl
  • One-time tokens
  • Free-form authorization(permission checks)

Installation

You can install sp-auth via a package manager:

yarn

$ yarn add @random-guys/sp-auth`

npm

$ npm install @random-guys/sp-auth`

Usage

Session Management

Make sure to add session typings first

declare namespace Express {
  export interface Request {
    session: any;
    sessionID: string;
  }
}

Setting up authentication for your app.

import express from "express";
import { SessionService } from "@random-guys/sp-auth";

const app = express();
const Auth = new SessionService({
  secret: "some-strong-secret",
  stateful: {
    duration: "10m", // 10 minutes
    store: new RedisStore(ioRedisClient)
  },
  stateless: {
    duration: "1m",
    scheme: "MyAppScheme"
  }
});

// automatically load sessions.
app.use(Auth.autoload());

app.post("/signin", async (req, res) => {
  const user = getUser();
  res.json({ token: await Auth.saveSession(user.id, user) });
});

// allow MyAppScheme and Bearer tokens
app.get("/products/:id", Auth.authCheck, async (req, res) => {
  res.json({
    msg: "This is a protected route",
    session: req.session,
    // this is only available with stateful(bearer) sessions
    sessionID: req.sessionID,
    // create a stateless(MyAppScheme) token
    headless: await Auth.headless(req.session)
  });
});

// only allow MyAppScheme
app.post("/products", Auth.headlessCheck(), (req, res) => {
  res.json({ msg: "This is an internal route" });
});

Free form authorization

This allow to check properties or the session using predicates and throw FailedPredicateError with a message if one of them fails.

const sterlingOnly = because(
  "Only users in 'my-workspace' can access this route",
  req => req.session.workspace == "my-workspace"
);
const permissionCheck = checkRequest(null, sterlingOnly);
app.get("/secure", Auth.authCheck, permissionCheck, handler);

Encryption & Decryption

Encryption is done using xsalsa20-poly1305 from the tweetnacl library.

// secrets must be no less than 32 characters long
const secret = await asyncRandomString(32);
const encrypted = await encrypt(payload, secret);
const decryptedPayload = await decrypt(encrypted, secret);

Single use tokens

Create tokens that are revoked once they've been used once.

// create token that expires in 10 minutes
const token = await sessions.commission(sample, "10m");

// view data without revoking
const peekedData = await sessions.peak(token);

// delay automatic expiry by 2 minutes
const sameData = await sessions.refresh(token, "2M");

// first decomission makes the token unusable.
const data = await sessions.decommission(token);