Skip to content

Commit

Permalink
Merge pull request #775 from strongloop/feature/save-token-in-context
Browse files Browse the repository at this point in the history
middleware/token: store the token in current ctx
  • Loading branch information
bajtos committed Nov 11, 2014
2 parents 6aef836 + 038c6a4 commit 46726f9
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/loopback.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/middleware/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions lib/middleware/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
30 changes: 30 additions & 0 deletions test/access-token.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions test/loopback.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 46726f9

Please sign in to comment.