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

@webauthz/sdk-token-core-node-js

v1.0.1

Published

SDK for resource or authorization servers to manage access to a resource via Webauthz

Downloads

5

Readme

sdk-token-core-node-js

Generate and validate tokens.

This library integrates into the resource or authorization server back-end JavaScript using NodeJS.

Use this library for all token types: 'access', 'client', 'grant', and 'refresh'.

Usage

To integrate this library with a resource or authorization server you will need two imports. One is for this library, the other one is for a data driver that manages the persistent data storage for this library. In this example, we use an in-memory storage driver:

const { WebauthzTokenMemoryDatabase } = require('@webauthz/sdk-token-data-memory-js');
const { WebauthzToken } = require('@webauthz/sdk-token-core-node-js');

Then, create an instance of the Webauthz class and configure it:

// webauthz token manager with in-memory database
const webauthzToken = new WebauthzToken({
    database: new WebauthzTokenMemoryDatabase(),
});

Generate access tokens:

const access_token = await webauthzToken.generateToken({
    type: 'access',
    client_id,
    realm,
    scope,
    path,
    not_after,
    user_id
});

Generate client tokens:

const client_token = await webauthzToken.generateToken({
    type: 'client',
    client_id,
    realm: 'Webauthz',
    scope: 'webauthz:client',
    path: '/api/webauthz',
});

Generate grant tokens:

const grant_token = await webauthzPlugin.generateToken({
    type: 'grant',
    client_id,
    request_id
});

Generate refresh tokens:

const refresh_token = await webauthzPlugin.generateToken({
    type: 'refresh',
    client_id,
    request_id
});

The next place to integrate the library is wherever you initialize middleware such as WebauthzExpress. The middleware uses the checkToken method of this library to validate incoming bearer tokens:

const authorizationHeader = req.header('Authorization');
const tokenInput = authorizationHeader.substr('bearer '.length).trim();
const tokenInfo = await webauthzToken.checkToken(tokenInput);

API

An application will need to use the following functions:

  • generateToken
  • checkToken

generateToken

Example usage:

const access_token = await webauthzToken.generateToken({
    type: 'access',
    client_id,
    realm,
    scope,
    path,
    not_after,
    user_id
});

Generate a random token, compute the hash of the token, and store the hash associated to the specified token info.

Parameters:

  • param0 (object, required) is an object with the properties type, client_id, and other type-specific properties.

Properties of param0:

  • type (string, required) the token type; one of 'access', 'client', 'grant', or 'refresh'
  • client_id (string, required) the unique id of the client for which the token is generated

Return value: the token value to return to the client

checkToken

Example usage:

const tokenInfo = await webauthzPlugin.checkToken(bearerToken);

Validate a token.

Parameters:

  • param0 (string, required) the bearer token value

Return value: If successful, returns the validated token info. Otherwise, throws an exception.

Storage

The library uses an abstract database object to store and lookup information in pesistent storage. The application must provide an object that implements the storage interface documented here.

See also sdk-token-data-memory-js for an example in-memory implementation that is useful for testing.

createToken

Example usage:

const isCreated = await database.createToken(id, record);

Create new record, or replace existing record. Should not throw exception unless there is a write error.

The record object is stored using the id as the primary key.

Parameters:

  • param0 (string, required) the value of id
  • param1 (object, required) is an object with the token info to store

Returns a boolean indicating whether the write operation was successful.

fetchToken

Example usage:

const record = await database.fetchToken(id);

Fetch an existing token record from storage.

Parameters:

  • param0 (string, required) the value of id

Return the token record object, or null if it is not found.

Build

npm run lint
npm run build