express-aad-jwt

0.1.2 • Public • Published

express-aad-jwt

Build

Middleware that validates JsonWebTokens issued from Azure Active Directory and sets req.user.

This module lets you authenticate HTTP requests using JWT tokens issued from Azure Active Directory in your Node.js applications. JWTs are typically used to protect API endpoints, and are often issued using OpenID Connect.

Install

$ npm install express-aad-jwt

Usage

The JWT authentication middleware authenticates callers using a JWT. If the token is valid, req.user will be set with the JSON object decoded to be used by later middleware for authorization and access control.

For example,

var jwt = require('express-aad-jwt');
 
app.get('/protected',
  jwt(),
  function(req, res) {
    if (!req.user.admin) return res.send(401);
    res.send(200);
  });

You can specify audience as well:

jwt(audience: 'https://graph.windows.net'})

If the JWT has an expiration (exp), it will be checked.

Optionally you can make some paths unprotected as follows:

app.use(jwt().unless({path: ['/token']}));

This is especially useful when applying to multiple routes.

By default, the decoded token is attached to req.user but can be configured with the requestProperty option.

jwt({ requestProperty: 'auth' });

Error handling

The default behavior is to throw an error when the token is invalid, so you can add your custom logic to manage unauthorized access as follows:

app.use(function (err, req, res, next) {
  if (err.name === 'UnauthorizedError') {
    res.send(401, 'invalid token...');
  }
});

You might want to use this module to identify registered users without preventing unregistered clients to access to some data, you can do it using the option credentialsRequired:

app.use(jwt({
  credentialsRequired: false
}));

Related Modules

Issue Reporting

If you have found a bug or if you have a feature request, please report them at this repository issues section. Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.

Tests

$ npm install
$ npm test

Contributors

Check them our here

License

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

Dependents (0)

Package Sidebar

Install

npm i express-aad-jwt

Weekly Downloads

16

Version

0.1.2

License

none

Last publish

Collaborators

  • dei79