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

middleware/token: store the token in current ctx #775

Merged
merged 3 commits into from
Nov 11, 2014
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
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