express-jwt Not respecting unprotected paths

混江龙づ霸主 提交于 2019-12-14 01:32:34

问题


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

  • https://github.com/auth0/express-jwt
  • https://www.npmjs.com/package/express-jwt

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:


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);
  });
});


来源:https://stackoverflow.com/questions/31496439/express-jwt-not-respecting-unprotected-paths

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!