Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix token middleware crash #2650

Merged
merged 2 commits into from
Aug 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"cookie-parser": "^1.3.4",
"es5-shim": "^4.1.0",
"eslint-config-loopback": "^1.0.0",
"express-session": "^1.14.0",
"grunt": "^1.0.1",
"grunt-browserify": "^5.0.0",
"grunt-cli": "^1.2.0",
Expand Down
2 changes: 2 additions & 0 deletions server/middleware/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

var g = require('strong-globalize')();

module.exports = function() {
throw new Error(g.f(
'%s middleware was removed in version 3.0. See %s for more details.',
Expand Down
2 changes: 1 addition & 1 deletion server/middleware/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function token(options) {
req.accessToken = token || null;
rewriteUserLiteral(req, currentUserLiteral);
var ctx = req.loopbackContext;
if (ctx) ctx.set('accessToken', token);
if (ctx && ctx.active) ctx.set('accessToken', token);
next(err);
});
};
Expand Down
26 changes: 26 additions & 0 deletions test/access-token.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var LoopBackContext = require('loopback-context');
var contextMiddleware = require('loopback-context').perRequest;
var loopback = require('../');
var extend = require('util')._extend;
var session = require('express-session');

var Token = loopback.AccessToken.extend('MyToken');
var ds = loopback.createDataSource({ connector: loopback.Memory });
Token.attachTo(ds);
Expand Down Expand Up @@ -509,6 +511,30 @@ describe('app.enableAuth()', function() {
done();
});
});

// See https://github.com/strongloop/loopback-context/issues/6
it('checks whether context is active', function(done) {
var app = loopback();

app.enableAuth();
app.use(contextMiddleware());
app.use(session({
secret: 'kitty',
saveUninitialized: true,
resave: true,
}));
app.use(loopback.token({ model: Token }));
app.get('/', function(req, res) { res.send('OK'); });
app.use(loopback.rest());

request(app)
.get('/')
.set('authorization', this.token.id)
.set('cookie', 'connect.sid=s%3AFTyno9_MbGTJuOwdh9bxsYCVxlhlulTZ.' +
'PZvp85jzLXZBCBkhCsSfuUjhij%2Fb0B1K2RYZdxSQU0c')
.expect(200, 'OK')
.end(done);
});
});

function createTestingToken(done) {
Expand Down