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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@otosense/jwt-middleware

v4.5.1

Published

Token and certificate validation middleware

Downloads

3

Readme

jwt-middleware

Express middleware that validates a JSON web token for each request and adds a token property to the request object containing the fields from the validated token.

Usage

import accessControl from 'api_access_control';
import express from 'express';

accessControl.setSecret('an RSA public key or secret phrase');
accessControl.setInternalSecret('even more secret phrase');
accessControl.setIssuer('api.otosense.ai');

const app: express.Application = express();

// req.token will have the shape
// { account: string, deviceId: string }
app.use('/device', accessControl.validateJWT(['accountId', 'deviceId', 'channelId']));

// req.token will have the shape
// { account: string, email: string }
app.use('/model', accessControl.validateJWT(['account', 'userId']));

// a token without an 'accessControl' property that contains 'administrator'
// will be rejected and a 403 response returned.
app.use('/admin', accessControl.validateJWT(['accountId', 'userId'], ['administrator']));

// for internal endpoints only
app.use('/internal', accessControl.authenticateInternal);

// enable max age validation
accessControl.checkMaxAge(true);

// disable max age validation
accessControl.checkMaxAge(false);

Exports

  • function setSecret(secret: string): void A secret must be set in order to validate tokens.
  • function setInternalSecret(secret: string): void This secret will be used to authenticate requests for internal endpoints.
  • function setIssuer(issuer: string): void Set the expected issuer value to check at validation (optional).
  • function setMaxAge(maxAge: string | number): void Set max age in milliseconds or parseable string (default is '20m')
  • function validateJWT(tokenFields: string[], requiredAccessLevels: string[] = []): (req, res, next) => void
  • function authenticateInternal(req, res, next): void
  • function checkMaxAge(enable: boolean): void

Notes

Requests must include an Authorization header with the format 'Bearer eyJhbG...'. A 401 Unauthorized response will be returned if:

  • There is no Authorization header
  • The Authorization header does not begin with 'Bearer '
  • The token does not pass signature validation (i.e. it has expired or was generated with a different secret)
  • The token does not contain all of the claims listed in the tokenFields argument to validateJWT

A 403 Forbidden response will be returned if a requiredAccessLevel argument was passed to validateJWT and:

  • The token does not contain an accessLevels claim, or
  • The token's accessLevels value is not an array containing the given requiredAccessLevel

Changelog

Version 1.0.0

  • Changed 'bearer ' to 'Bearer ' to match standard (for compatibility with Postman)
  • Removed propertyName argument from validateJWT function so the property added to req will always be token, for route schema portability
  • Corrected documentation

Version 2.0.0

  • Now expects a base64-encoded secret, which will be decoded before token verification

Version 3.0.0

  • Now expects an unencoded secret

Version 3.1.0

  • Added setInternalSecret and authenticateInternal methods

Version 4.0.0

  • Removed maxAge validation by default and added the checkMaxAge function to enable it if desired.