Need private packages and team management tools?Check out npm Orgs. »


0.3.0 • Public • Published


This middleware supports server-to-server interactions and not when you authorize on behalf of end user so that consent is not required. JWT (JsonWebToken) is a part of OAuth2 specification and designed to simplify server-to-server flow.

Middleware setup

First of all we encourage you to develop secure services and to use in this particular case private/public RSA (RS256) keys for signing and verifying JWT signatures. So that server would have an access to client's public key and only client has access to his private key. Although middleware supports other algos such as "HS256", "HS384", "HS512", "RS256", "RS384", "RS512", "ES256", "ES384", and "ES512". Check node-jws for more details:

var JwtMiddleware = require('jwt-middleware')
var clientsKeys = {
  '': '[public key in PEM format goes here]'
var auth = new JwtMiddleware({
  ttl: 3600, // access token life time
  store: {
    type: 'encrypted',
    secret: 'very long s3cr3t key'
  getKey: function(payload, cb) {
    // Third argument is optional and can be used to extend session object with server-side params
    cb(null, clientKeys[payload.client_id], {uid: 1});
});'/oauth/token', auth.token.bind(auth));'/method/name', auth.check.bind(auth), function(req, res, next) {
  // Prints consolidated object of JWT's payload and session obj
  console.log(req.session); // -> {uuid: 1, client_id: ''}
  res.send('Protected resource');


Middleware has two prebuilt stores, which keep registry of issued access tokens.

  • encrypted - uses 'cookie-style' way. Basically tokens are not stored anywhere, but they are cryptographically encrypted and contain meta information about session, expiry time etc.
  • momory - uses memory, so it would be error-prone if you're using nodejs cluster or running several children node processes (they do not share memory).

You can create you own custom store (redis/mysql/whatever). Please take a look at the code in lib/store dir for examples. Feel free to post a pull request if you crafted it, so other people can use!

Cilent flow

For the client, simplified OAuth2 flow consists of those steps:

  • Create a JWT, which includes a header, a claim set, and a signature. More information about creating JWT you can find in official spec:

  • Request an access token from the OAuth 2.0 Authorization Server (this middleware).

    POST /oauth/token HTTP/1.1
    Content-Type: application/x-www-form-urlencoded

    where assertion is a JWT generated by a client.

  • Handle the JSON response that the Authorization Server returns. If the response includes an access token, use the access token to call a API. (If the response does not include an access token, your JWT and token request might not be properly formed, or has invalid signature). More details can be found here:

      "access_token": "wdg0icrQWbb-3FmzQ_oOqA2TR76Bu",
      "token_type": "Bearer",
      "expires_in": 1800
  • Call protected resource by either including access token as a query parameter:


    or by using Authorization header:

    GET HTTP/1.1
    Authorization: Bearer wdg0icrQWbb-3FmzQ_oOqA2TR76Bu
  • When the access token expires, client's application generates another JWT, signs it, and requests another access token.


PRs are highly welcome!

You can generate keys using this snippet:

openssl req -x509 -days 365 -newkey rsa:2048 -keyout private.pem -out public.pem -nodes

Developed by


npm i jwt-middleware

Downloadsweekly downloads






last publish


  • avatar
Report a vulnerability