From c29ace34042c92cbd5030378e855c6b98f161bc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 10 Nov 2014 18:58:10 +0100 Subject: [PATCH 1/3] Update chai to ^1.10.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c6b3b7c5..2c928bc0 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ }, "devDependencies": { "browserify": "~4.2.3", - "chai": "~1.9.1", + "chai": "^1.10.0", "cookie-parser": "~1.3.3", "errorhandler": "~1.2.0", "es5-shim": "^4.0.3", From 8f5aea3e3bb824db257f74ec830d299a58433054 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 10 Nov 2014 19:12:00 +0100 Subject: [PATCH 2/3] Fix `loopback.getCurrentContext` - ensure the method is always defined - return `null` when the context is not active (we are not inside a request-handling chain) --- lib/loopback.js | 5 +++++ lib/middleware/context.js | 2 +- test/loopback.test.js | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/loopback.js b/lib/loopback.js index b3ee7a6e..ef07bb88 100644 --- a/lib/loopback.js +++ b/lib/loopback.js @@ -185,6 +185,11 @@ loopback.template = function(file) { return ejs.compile(str); }; +loopback.getCurrentContext = function() { + // A placeholder method, see lib/middleware/context.js for the real version + return null; +}; + /*! * Built in models / services */ diff --git a/lib/middleware/context.js b/lib/middleware/context.js index 7b21561d..85b258dc 100644 --- a/lib/middleware/context.js +++ b/lib/middleware/context.js @@ -16,7 +16,7 @@ function createContext(scope) { process.context[scope] = ns; // Set up loopback.getCurrentContext() loopback.getCurrentContext = function() { - return ns; + return ns && ns.active ? ns : null; }; chain(juggler); diff --git a/test/loopback.test.js b/test/loopback.test.js index 426dcc3a..7dd63702 100644 --- a/test/loopback.test.js +++ b/test/loopback.test.js @@ -20,6 +20,10 @@ describe('loopback', function() { expect(require('fs').existsSync(loopback.faviconFile), 'file exists') .to.equal(true); }); + + it.onServer('has `getCurrentContext` method', function() { + expect(loopback.getCurrentContext).to.be.a('function'); + }); }); describe('loopback.createDataSource(options)', function() { From 038c6a454e7d4fdd1ea8e6931d9162b5c2d6c5fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 10 Nov 2014 18:37:35 +0100 Subject: [PATCH 3/3] middleware/token: store the token in current ctx --- lib/middleware/token.js | 2 ++ test/access-token.test.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/middleware/token.js b/lib/middleware/token.js index a8384aa0..235640aa 100644 --- a/lib/middleware/token.js +++ b/lib/middleware/token.js @@ -52,6 +52,8 @@ function token(options) { if (req.accessToken !== undefined) return next(); TokenModel.findForRequest(req, options, function(err, token) { req.accessToken = token || null; + var ctx = loopback.getCurrentContext(); + if (ctx) ctx.set('accessToken', token); next(err); }); }; diff --git a/test/access-token.test.js b/test/access-token.test.js index 093e4dc9..1a604dc2 100644 --- a/test/access-token.test.js +++ b/test/access-token.test.js @@ -143,6 +143,36 @@ describe('app.enableAuth()', function() { .end(done); }); + it('stores token in the context', function(done) { + var TestModel = loopback.createModel('TestModel', { base: 'Model' }); + TestModel.getToken = function(cb) { + cb(null, loopback.getCurrentContext().get('accessToken') || null); + }; + TestModel.remoteMethod('getToken', { + returns: { arg: 'token', type: 'object' }, + http: { verb: 'GET', path: '/token' } + }); + + var app = loopback(); + app.model(TestModel, { dataSource: null }); + + app.enableAuth(); + app.use(loopback.context()); + app.use(loopback.token({ model: Token })); + app.use(loopback.rest()); + + var token = this.token; + request(app) + .get('/TestModels/token?_format=json') + .set('authorization', token.id) + .expect(200) + .expect('Content-Type', /json/) + .end(function(err, res) { + if (err) return done(err); + expect(res.body.token.id).to.eql(token.id); + done(); + }); + }); }); function createTestingToken(done) {