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

passport-jwt-async

v3.1.0

Published

Passport authentication strategy using JSON Web Tokens

Downloads

21

Readme

passport-jwt-async

A Passport strategy for authenticating with a JSON Web Token.

This module lets you authenticate endpoints using a JSON web token. It is intended to be used to secure RESTful endpoints without sessions.

Install

npm install passport-jwt-async

Usage

Configure Strategy

The JWT authentication strategy is constructed as follows:

new JwtStrategy(options, verify)

options is an object literal containing options to control how the token is extracted from the request and verified.

  • secretOrKey is a string or buffer containing the secret (symmetric) or PEM-encoded public key (asymmetric) for verifying the token's signature.
  • secretOrKeyProvider(request: Request, rawJwtToken: string): Promise<string | Buffer | undefined> is a function which should return a secret or PEM-encoded public key (asymmetric) for the given key and request combination. Note it is up to the implementer to decode rawJwtToken. Owerrides secretOrKey.
  • extractToken(request: Request): string | null Required. Function that accepts a request as the only parameter and returns either the JWT as a string or null. See Extracting the JWT from the request for more details.
  • verifyJwt({token: string, secretOrKey: string | Buffer, options: T}): Promise<JwtPayload>: JWT verifying function. Library contains default imlementation using jsonwebtoken.
  • verifyJwtOptions: Contains additional options for verifyJwt For jsonWebTokenVerifier pass here an options object for any other option you can pass the jsonwebtoken verifier. (i.e maxAge)
  • passReqToCallback: If true the request will be passed to the verify callback. i.e. verify(request, jwtPayload, doneCallback).

verify is a function with the parameters verify(jwtPayload, done)

  • request is an Express request.
  • jwtPayload is an object literal containing the decoded JWT payload.
  • done is a passport error first callback accepting arguments done(error, user, info)

An example configuration which reads the JWT from the http Authorization header with the scheme 'bearer':

import {
  Strategy as JwtStrategy,
  fromAuthHeaderAsBearerToken,
  jsonWebTokenVerifier,
} from 'passport-jwt-async';

passport.use(
  new JwtStrategy(
    {
      extractToken: fromAuthHeaderAsBearerToken(),
      secretOrKey: 'secret',
      verifyJwt: jsonWebTokenVerifier,
      verifyJwtOptions: {
        issuer: 'accounts.examplesoft.com',
        audience: 'yoursite.net',
      },
    },
    function ({ payload: { sub } }, done) {
      User.findOne({ id: sub }, function (err, user) {
        if (err) {
          return done(err, false);
        }
        if (user) {
          return done(null, user);
        } else {
          return done(null, false);
          // or you could create a new account
        }
      });
    }
  )
);

Extracting the JWT from the request

There are a number of ways the JWT may be included in a request. In order to remain as flexible as possible the JWT is parsed from the request by a user-supplied callback passed in as the extractToken parameter. This callback, from now on referred to as an extractor, accepts a request object as an argument and returns the encoded JWT string or null.

Included extractors

A number of extractor factory functions are provided in passport-jwt.ExtractJwt. These factory functions return a new extractor configured with the given parameters.

  • fromHeader(headerName: string) creates a new extractor that looks for the JWT in the given http header
  • fromBodyField(fieldName: string) creates a new extractor that looks for the JWT in the given body field. You must have a body parser configured in order to use this method.
  • fromUrlQueryParameter(paramName: string) creates a new extractor that looks for the JWT in the given URL query parameter.
  • fromAuthHeaderWithScheme(authScheme: string) creates a new extractor that looks for the JWT in the authorization header, expecting the scheme to match auth_scheme.
  • fromAuthHeaderAsBearerToken() creates a new extractor that looks for the JWT in the authorization header with the scheme 'bearer'
  • fromExtractors(extractors: TokenExtractor[]) creates a new extractor using an array of extractors provided. Each extractor is attempted in order until one returns a token.

Writing a custom extractor function

If the supplied extractors don't meet your needs you can easily provide your own callback. For example, if you are using the cookie-parser middleware and want to extract the JWT in a cookie you could use the following function as the argument to the extractToken option:

const cookieExtractor = (req) => {
    const token = null;
    if (req && req.cookies) {
        token = req.cookies['jwt'];
    }
    return token;
};

Authenticate requests

Use passport.authenticate() specifying 'jwt' as the strategy.

app.post('/profile', passport.authenticate('jwt', { session: false }),
    function(req, res) {
        res.send(req.user.profile);
    }
);

Include the JWT in requests

The method of including a JWT in a request depends entirely on the extractor function you choose. For example, if you use the fromAuthHeaderAsBearerToken extractor, you would include an Authorization header in your request with the scheme set to bearer. e.g.

Authorization: bearer JSON_WEB_TOKEN_STRING.....

Tests

npm install
npm test

To generate test-coverage reports:

npm install -g istanbul
npm run-script testcov
istanbul report

License

The MIT License