Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: auth0/express-jwt
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 5766a24aeb7db15b8a183c59b4a9145552702f0e
Choose a base ref
...
head repository: auth0/express-jwt
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 678f3b0e812d86b399b925f069105fc37eecde5b
Choose a head ref
  • 11 commits
  • 9 files changed
  • 5 contributors

Commits on Apr 7, 2020

  1. Copy the full SHA
    1789282 View commit details
  2. fix license field

    jfromaniello committed Apr 7, 2020
    Copy the full SHA
    f4f4d1d View commit details
  3. 5.3.2

    jfromaniello committed Apr 7, 2020
    Copy the full SHA
    6591014 View commit details
  4. Copy the full SHA
    888f0e9 View commit details
  5. Copy the full SHA
    c5d8419 View commit details
  6. Update README.md

    Co-Authored-By: Filip Skokan <panva.ip@gmail.com>
    2 people authored and jfromaniello committed Apr 7, 2020
    Copy the full SHA
    d3e86bf View commit details
  7. Copy the full SHA
    8662579 View commit details
  8. 5.3.3

    jfromaniello committed Apr 7, 2020
    Copy the full SHA
    e9ed6d2 View commit details

Commits on Jun 25, 2020

  1. Made algorithms mandatory

    Marcos Castany committed Jun 25, 2020
    Copy the full SHA
    304a1c5 View commit details

Commits on Jun 29, 2020

  1. Merge pull request from GHSA-6g6m-m6h5-w9gf

    Made algorithms mandatory
    Marcos Castany authored Jun 29, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    7ecab5f View commit details
  2. 6.0.0

    Marcos Castany committed Jun 29, 2020
    Copy the full SHA
    678f3b0 View commit details
Showing with 2,699 additions and 66 deletions.
  1. +3 −2 .travis.yml
  2. +34 −23 README.md
  3. +3 −0 lib/index.js
  4. +2,602 −0 package-lock.json
  5. +4 −9 package.json
  6. +46 −29 test/jwt.test.js
  7. +4 −2 test/multitenancy.test.js
  8. +2 −0 test/revocation.test.js
  9. +1 −1 test/string_token.test.js
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: "node_js"
before_install: npm i -g npm@2
node_js:
- "0.8"
- "0.10"
- 8
- 10
- 12
57 changes: 34 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -2,41 +2,43 @@

[![Build](https://travis-ci.org/auth0/express-jwt.png)](http://travis-ci.org/auth0/express-jwt)

Middleware that validates JsonWebTokens and sets `req.user`.

This module lets you authenticate HTTP requests using JWT tokens in your Node.js
applications. JWTs are typically used to protect API endpoints, and are
often issued using OpenID Connect.
This module provides Express middleware for validating JWTs ([JSON Web Tokens](https://jwt.io)) through the [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken/) module. The decoded JWT payload is available on the request object.

## Install

$ npm install express-jwt
```
$ npm install express-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,
Basic usage using an HS256 secret:

```javascript
var jwt = require('express-jwt');

app.get('/protected',
jwt({secret: 'shhhhhhared-secret'}),
jwt({ secret: 'shhhhhhared-secret' }),
function(req, res) {
if (!req.user.admin) return res.sendStatus(401);
res.sendStatus(200);
});
```

The decoded JWT payload is available on the request via the `user` property. This can be configured using the `requestProperty` option ([see below](#retrieving-the-decoded-payload)).

> The default behavior of the module is to extract the JWT from the `Authorization` header as an [OAuth2 Bearer token](https://oauth.net/2/bearer-tokens/).
### Additional Options

You can specify audience and/or issuer as well:

```javascript
jwt({ secret: 'shhhhhhared-secret',
jwt({
secret: 'shhhhhhared-secret',
audience: 'http://myapi/protected',
issuer: 'http://issuer' })
issuer: 'http://issuer'
})
```

> If the JWT has an expiration (`exp`), it will be checked.
@@ -64,6 +66,8 @@ var publicKey = fs.readFileSync('/path/to/public.pub');
jwt({ secret: publicKey });
```

### Retrieving the Decoded Payload

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


@@ -79,6 +83,8 @@ jwt({ secret: publicKey, resultProperty: 'locals.user' });

Both `resultProperty` and `requestProperty` utilize [lodash.set](https://lodash.com/docs/4.17.2#set) and will accept nested property paths.

### Customizing Token Location

A custom function for extracting the token from a request can be specified with
the `getToken` option. This is useful if you need to pass the token through a
query parameter or a cookie. You can throw an error in this function and it will
@@ -100,6 +106,7 @@ app.use(jwt({
```

### Multi-tenancy

If you are developing an application in which the secret used to sign tokens is not static, you can provide a callback function as the `secret` parameter. The function has the signature: `function(req, payload, done)`:
* `req` (`Object`) - The express `request` object.
* `payload` (`Object`) - An object with the JWT claims.
@@ -108,6 +115,7 @@ If you are developing an application in which the secret used to sign tokens is
* `secret` (`String`) - The secret to use to verify the JWT.

For example, if the secret varies based on the [JWT issuer](http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html#issDef):

```javascript
var jwt = require('express-jwt');
var data = require('./data');
@@ -126,7 +134,7 @@ var secretCallback = function(req, payload, done){
};

app.get('/protected',
jwt({secret: secretCallback}),
jwt({ secret: secretCallback }),
function(req, res) {
if (!req.user.admin) return res.sendStatus(401);
res.sendStatus(200);
@@ -158,19 +166,21 @@ var isRevokedCallback = function(req, payload, done){
};

app.get('/protected',
jwt({secret: 'shhhhhhared-secret',
isRevoked: isRevokedCallback}),
jwt({
secret: 'shhhhhhared-secret',
isRevoked: isRevokedCallback
}),
function(req, res) {
if (!req.user.admin) return res.sendStatus(401);
res.sendStatus(200);
});
}
);
```

### 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:


```javascript
app.use(function (err, req, res, next) {
if (err.name === 'UnauthorizedError') {
@@ -179,8 +189,7 @@ app.use(function (err, req, res, next) {
});
```

You might want to use this module to identify registered users while still providing access to unregistered users. You
can do this by using the option _credentialsRequired_:
You might want to use this module to identify registered users while still providing access to unregistered users. You can do this by using the option `credentialsRequired`:

```javascript
app.use(jwt({
@@ -196,8 +205,10 @@ app.use(jwt({

## Tests

$ npm install
$ npm test
```
$ npm install
$ npm test
```

## Contributors
Check them out [here](https://github.com/auth0/express-jwt/graphs/contributors)
3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -19,6 +19,9 @@ function wrapStaticSecretInCallback(secret){
module.exports = function(options) {
if (!options || !options.secret) throw new Error('secret should be set');

if (!options.algorithms) throw new Error('algorithms should be set');
if (!Array.isArray(options.algorithms)) throw new Error('algorithms must be an array');

var secretCallback = options.secret;

if (!isFunction(secretCallback)){
Loading