express-jwt Not respecting unprotected paths

2019-07-14 01:17发布

Information on the express-jwt module can be found here:

In my main.js server file, I have the following:

import ExpressJwt from 'express-jwt';
// import other crap...

let token = ExpressJwt({
    secret: 'whatever',
    audience: 'whatever',
    issuer: 'whatever'
});

app.all('/apiv1', token.unless({ path: ['apiv1/user/create', '/apiv1/auth/login']}));

app.use('/apiv1/user', user);
app.use('/apiv1/auth', auth);

Where user and auth are the middlewares that handle my routes. What I want to do is obvious; deny API access to all unauthenticated users, except when they attempt to create a new user via apiv1/user/create and/or login via apiv1/auth/login.

Any time I try to make a request to the aforementioned unprotected paths however, I get the error:

UnauthorizedError: No authorization token was found

It's still protecting the routes I specified to be unprotected! I also tried:

app.use('/apiv1/user', token.unless({ path: ['/apiv1/user/create'] }), user);
app.use('/apiv1/auth', token.unless({ path: ['/apiv1/auth/login'] }), auth);

But that didn't work. I also tried using regex for the unless paths, but that didn't work either.

I arrived at app.all('/apiv1', token...) via this answer, but that solution does not yield me the desired functionality.

1条回答
\"骚年 ilove
2楼-- · 2019-07-14 01:40

Instead of using all:

app.all('/apiv1', token.unless({ path: ['apiv1/user/create', '/apiv1/auth/login']}));

Try using use and adding in the path route a slash / at the beginning:

app.use('/apiv1', token.unless({ path: ['/apiv1/user/create', '/apiv1/auth/login']}));

Here it is an example that is working:

app.js:

var express = require('express');
var app = express();

var expressJwt = require('express-jwt');
var jwt = require('jsonwebtoken');
var secret = 'secret';

app.use('/api', expressJwt({secret: secret}).unless({path: ['/api/token']}));

app.get('/api/token', function(req, res) {
  var token = jwt.sign({foo: 'bar'}, secret);
  res.send({token: token});
});

app.get('/api/protected', function(req, res) {
  res.send('hello from /api/protected route.');
});

app.use(function(err, req, res, next) {
  res.status(err.status || 500).send(err);
});

app.listen(4040, function() {
  console.log('server up and running at 4040 port');
});

module.exports = app;

test.js:

var request = require('supertest');
var app = require('./app.js');

describe('Test API', function() {
  var token = '';

  before(function(done) {
    request(app)
      .get('/api/token')
      .end(function(err, response) {
        if (err) { return done(err); }
        var result = JSON.parse(response.text);
        token = result.token;
        done();
      });
  });

  it('should not be able to consume /api/protected since no token was sent', function(done) {
    request(app)
      .get('/api/protected')
      .expect(401, done);
  });

  it('should be able to consume /api/protected since token was sent', function(done) {
    request(app)
      .get('/api/protected')
      .set('Authorization', 'Bearer ' + token)
      .expect(200, done);
  });
});
查看更多
登录 后发表回答