express-oauth2-jwt-bearer
TypeScript icon, indicating that this package has built-in type declarations

1.6.0 • Public • Published

Authentication middleware for Express.js that validates JWT Bearer Access Tokens

npm codecov Downloads License CircleCI

📚 Documentation - 🚀 Getting Started - 💻 API Reference - 💬 Feedback

Documentation

  • Docs Site - explore our Docs site and learn more about Auth0.

Getting started

Requirements

This package supports the following tooling versions:

  • Node.js: ^12.19.0 || ^14.15.0 || ^16.13.0 || ^18.12.0 || ^20.2.0

Installation

Using npm in your project directory run the following command:

npm install express-oauth2-jwt-bearer

Getting started

Configure the SDK

The library requires issuerBaseURL and audience.

Environment Variables

ISSUER_BASE_URL=https://YOUR_ISSUER_DOMAIN
AUDIENCE=https://my-api.com
const { auth } = require('express-oauth2-jwt-bearer');
app.use(auth());

Library Initialization

const { auth } = require('express-oauth2-jwt-bearer');
app.use(
  auth({
    issuerBaseURL: 'https://YOUR_ISSUER_DOMAIN',
    audience: 'https://my-api.com',
  })
);

JWTs signed with symmetric algorithms (eg HS256)

const { auth } = require('express-oauth2-jwt-bearer');
app.use(
  auth({
    issuer: 'https://YOUR_ISSUER_DOMAIN',
    audience: 'https://my-api.com',
    secret: 'YOUR SECRET',
    tokenSigningAlg: 'HS256',
  })
);

With this configuration, your api will require a valid Access Token JWT bearer token for all routes.

Successful requests will have the following properties added to them:

app.get('/api/messages', (req, res, next) => {
  const auth = req.auth;
  auth.header; // The decoded JWT header.
  auth.payload; // The decoded JWT payload.
  auth.token; // The raw JWT token.
});

Security Headers

Along with the other security best practices in the Express.js documentation, we recommend you use helmet in addition to this middleware which can help protect your app from some well-known web vulnerabilities by setting default security HTTP headers.

Error Handling

This SDK raises errors with err.status and err.headers according to rfc6750. The Express.js default error handler will set the error response with:

  • res.statusCode set from err.status
  • res.statusMessage set according to the status code.
  • The body will be the HTML of the status code message when in production environment, otherwise will be err.stack.
  • Any headers specified in an err.headers object.

The error_description in the WWW-Authenticate header will contain useful information about the error, which you may not want to disclose in Production.

See the Express.js docs on error handling for more information on writing custom error handlers.

API Reference

  • auth - Middleware that will return a 401 if a valid Access token JWT bearer token is not provided in the request.
  • AuthResult - The properties added to req.auth upon successful authorization.
  • requiredScopes - Check a token's scope claim to include a number of given scopes, raises a 403 insufficient_scope error if the value of the scope claim does not include all the given scopes.
  • claimEquals - Check a token's claim to be equal a given JSONPrimitive (string, number, boolean or null) raises a 401 invalid_token error if the value of the claim does not match.
  • claimIncludes - Check a token's claim to include a number of given JSONPrimitives (string, number, boolean or null) raises a 401 invalid_token error if the value of the claim does not include all the given values.
  • claimCheck - Check the token's claims using a custom method that receives the JWT Payload and should return true if the token is valid. Raises a 401 invalid_token error if the function returns false.

Feedback

Contributing

We appreciate feedback and contribution to this repo! Before you get started, please see the following:

Raise an issue

To provide feedback or report a bug, please raise an issue on our issue tracker.

Vulnerability Reporting

Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.

What is Auth0?

Auth0 Logo

Auth0 is an easy to implement, adaptable authentication and authorization platform. To learn more checkout Why Auth0?

This project is licensed under the MIT license. See the LICENSE file for more info.

Readme

Keywords

none

Package Sidebar

Install

npm i express-oauth2-jwt-bearer

Weekly Downloads

76,113

Version

1.6.0

License

MIT

Unpacked Size

31.8 kB

Total Files

5

Last publish

Collaborators

  • npirani_okta
  • soumya.bodavula
  • oktajeffoktajeff
  • amandamdacruz0
  • rithuc23
  • jfromaniello
  • edgarchirivella-okta
  • sanjay.manikandhan
  • ncluer
  • vic-dev
  • enriquepina
  • ece-okta
  • pubalokta
  • dougmiller-okta
  • zak.nour
  • stheller
  • jamescgarrett-okta
  • madhuri.rm23
  • david.renaud.okta
  • auth0-oss
  • codepete
  • ziluvatar
  • iaco
  • auth0npm
  • auth0brokkr
  • hzalaz
  • aaguiarz
  • charlesrea
  • julien.wollscheid
  • cristiandouce
  • sambego
  • sandrinodimattia
  • lzychowski
  • davidpatrick0
  • sergii.biienko
  • tomauth0
  • jpadilla
  • jessele
  • rhamzeh_auth0
  • adamjmcgrath
  • rob.coles