JSPM

@entespotify/express-token-verifier

0.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q33255F
  • License MIT

Express middleware + utilities to validate JWTs (with JWKs caching)

Package Exports

  • @entespotify/express-token-verifier
  • @entespotify/express-token-verifier/dist/index.cjs
  • @entespotify/express-token-verifier/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@entespotify/express-token-verifier) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@entespotify/express-token-verifier

npm version License: MIT Node.js CI

Beta Release - This library is currently in beta. Use with caution in production environments.


๐Ÿงฉ Overview

@entespotify/express-token-verifier is an Express middleware and utility library for validating JSON Web Tokens (JWTs) using JSON Web Key Sets (JWKS) with caching support.
It simplifies the process of securing your Express APIs by verifying JWTs against a trusted JWKS endpoint.


โœจ Features

  • ๐Ÿš€ Express Middleware โ€” Easily integrate JWT validation into your Express routes.
  • ๐Ÿง  JWKS Caching โ€” Reduces network overhead by caching JWKS responses.
  • โš™๏ธ Customizable โ€” Supports issuer, audience validation, and configurable cache TTL.
  • ๐Ÿง‘โ€๐Ÿ’ป TypeScript Support โ€” Fully typed for a great developer experience.

๐Ÿ“ฆ Installation

npm install @entespotify/express-token-verifier

๐Ÿงฑ Usage

Middleware Example

import express from 'express';
import { createAuthMiddleware } from '@entespotify/express-token-verifier';

const app = express();

const authConfig = {
  issuer: 'https://example.com',
  jwksUri: 'https://example.com/.well-known/jwks.json',
  audience: 'your-audience',
};

const authMiddleware = createAuthMiddleware(authConfig);

app.use(authMiddleware);

app.get('/protected', (req, res) => {
  res.json({ message: 'You have access!', user: (req as any).user });
});

app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});

Verifying JWTs Manually

If you need to verify JWTs outside of middleware, you can use the verifyJwt function:

import { verifyJwt } from '@entespotify/express-token-verifier';

const token = 'your-jwt-token';
const config = {
  issuer: 'https://example.com',
  jwksUri: 'https://example.com/.well-known/jwks.json',
  audience: 'your-audience',
};

verifyJwt(token, config)
  .then((payload) => {
    console.log('Token payload:', payload);
  })
  .catch((err) => {
    console.error('Token verification failed:', err.message);
  });

๐Ÿ“˜ API Reference

createAuthMiddleware(config: AuthConfig)

Creates an Express middleware for JWT validation.

Parameters:

  • config (AuthConfig): Configuration object for JWT validation.

Returns:
Express middleware function.


verifyJwt(token: string, config: AuthConfig)

Verifies a JWT against the provided configuration.

Parameters:

  • token (string): The JWT to verify.
  • config (AuthConfig): Configuration object for JWT validation.

Returns:
A promise that resolves with the decoded token payload.


JwksCache

A utility class for managing JWKS caching.

Methods:

  • getKeyByKid(kid: string): Retrieves a JWK by its key ID (kid).
  • refresh(): Refreshes the JWKS cache.

AuthConfig

Configuration interface for JWT validation.

Property Type Description
issuer string The expected issuer of the JWT.
jwksUri string The URI of the JWKS endpoint.
audience string or string[] The expected audience(s) of the JWT.
jwksCacheTtl number Cache TTL in seconds (default: 300).
clockSkew number Clock skew tolerance in seconds (default: 60).

โš™๏ธ Implementation Details

  • JWKS Caching: The JwksCache class fetches and caches JWKS responses to minimize network requests.
  • JWT Validation: The verifyJwt function uses the jose library to validate JWTs against the cached JWKs.
  • Error Handling: Middleware and utilities throw meaningful errors for invalid or expired tokens.

๐Ÿงพ License

This library is licensed under the MIT License.
See the LICENSE file for details.


๐Ÿค Contributing

Contributions, issues, and feature requests are welcome!
Feel free to check the issues page.


๐Ÿ’ฌ Support

If you find this library helpful, please โญ๏ธ the repository to show support!
For questions or suggestions, open an issue or start a discussion on GitHub.