Skip to content

Commit cf98d37

Browse files
committed
fix custom token model in token middleware
Fixing server/middleware/token.js to handle correctly the setup of a custom AccessToken model by name in either middleware.json or using any of : app.use(loopback.token({...})); app.middlewareFromConfig(loopback.token, {...}) app.middleware('auth', loopback.token({...})
1 parent 01ce9b5 commit cf98d37

File tree

3 files changed

+92
-15
lines changed

3 files changed

+92
-15
lines changed

server/middleware/token.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,7 @@ function token(options) {
9595
var app = req.app;
9696
var registry = app.registry;
9797
if (!TokenModel) {
98-
if (registry === loopback.registry) {
99-
TokenModel = options.model || loopback.AccessToken;
100-
} else if (options.model) {
101-
TokenModel = registry.getModel(options.model);
102-
} else {
103-
TokenModel = registry.getModel('AccessToken');
104-
}
98+
TokenModel = registry.getModel(options.model || 'AccessToken');
10599
}
106100

107101
assert(typeof TokenModel === 'function',

test/access-token.test.js

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,107 @@ var extend = require('util')._extend;
1414
var session = require('express-session');
1515
var request = require('supertest');
1616

17-
var Token, ACL;
17+
var Token, ACL, User, TestModel;
1818

1919
describe('loopback.token(options)', function() {
2020
var app;
2121
beforeEach(function(done) {
2222
app = loopback({localRegistry: true, loadBuiltinModels: true});
2323
app.dataSource('db', {connector: 'memory'});
2424

25+
ACL = app.registry.getModel('ACL');
26+
app.model(ACL, {dataSource: 'db'});
27+
28+
User = app.registry.getModel('User');
29+
app.model(User, {dataSource: 'db'});
30+
2531
Token = app.registry.createModel({
2632
name: 'MyToken',
2733
base: 'AccessToken',
2834
});
2935
app.model(Token, {dataSource: 'db'});
3036

31-
ACL = app.registry.getModel('ACL');
37+
TestModel = app.registry.createModel({
38+
name: 'TestModel',
39+
base: 'Model',
40+
});
41+
TestModel.getToken = function(options, cb) {
42+
cb(null, options && options.accessToken || null);
43+
};
44+
TestModel.remoteMethod('getToken', {
45+
accepts: {arg: 'options', type: 'object', http: 'optionsFromRequest'},
46+
returns: {arg: 'token', type: 'object'},
47+
http: {verb: 'GET', path: '/token'},
48+
});
49+
app.model(TestModel, {dataSource: 'db'});
3250

3351
createTestingToken.call(this, done);
3452
});
3553

54+
it('defaults to built-in AccessToken model', function() {
55+
var BuiltInToken = app.registry.getModel('AccessToken');
56+
app.model(BuiltInToken, {dataSource: 'db'});
57+
58+
app.enableAuth({dataSource: 'db'});
59+
app.use(loopback.token());
60+
app.use(loopback.rest());
61+
62+
return BuiltInToken.create({userId: 123}).then(function(token) {
63+
return request(app)
64+
.get('/TestModels/token?_format=json')
65+
.set('authorization', token.id)
66+
.expect(200)
67+
.expect('Content-Type', /json/)
68+
.then(res => {
69+
expect(res.body.token.id).to.eql(token.id);
70+
});
71+
});
72+
});
73+
74+
it('uses correct custom AccessToken model from model class param', function() {
75+
User.hasMany(Token, {
76+
as: 'accessTokens',
77+
options: {disableInclude: true},
78+
});
79+
80+
app.enableAuth();
81+
app.use(loopback.token({model: Token}));
82+
app.use(loopback.rest());
83+
84+
return Token.create({userId: 123}).then(function(token) {
85+
return request(app)
86+
.get('/TestModels/token?_format=json')
87+
.set('authorization', token.id)
88+
.expect(200)
89+
.expect('Content-Type', /json/)
90+
.then(res => {
91+
expect(res.body.token.id).to.eql(token.id);
92+
});
93+
});
94+
});
95+
96+
it('uses correct custom AccessToken model from string param', function() {
97+
User.hasMany(Token, {
98+
as: 'accessTokens',
99+
options: {disableInclude: true},
100+
});
101+
102+
app.enableAuth();
103+
app.use(loopback.token({model: Token.modelName}));
104+
app.use(loopback.rest());
105+
106+
return Token.create({userId: 123}).then(function(token) {
107+
return request(app)
108+
.get('/TestModels/token?_format=json')
109+
.set('authorization', token.id)
110+
.expect(200)
111+
.expect('Content-Type', /json/)
112+
.then(res => {
113+
expect(res.body.token.id).to.eql(token.id);
114+
});
115+
});
116+
});
117+
36118
it('should populate req.token from the query string', function(done) {
37119
createTestAppAndRequest(this.token, done)
38120
.get('/?access_token=' + this.token.id)
@@ -287,7 +369,7 @@ describe('loopback.token(options)', function() {
287369
});
288370

289371
it('should overwrite invalid existing token (is !== undefined and has no "id" property) ' +
290-
' when enableDoubkecheck is true',
372+
' when enableDoublecheck is true',
291373
function(done) {
292374
var token = this.token;
293375
app.use(function(req, res, next) {
@@ -607,9 +689,10 @@ function createTestAppAndRequest(testToken, settings, done) {
607689
}
608690

609691
function createTestApp(testToken, settings, done) {
610-
done = arguments[arguments.length - 1];
611-
if (settings == done) settings = {};
612-
settings = settings || {};
692+
if (!done && typeof settings === 'function') {
693+
done = settings;
694+
settings = {};
695+
}
613696

614697
var appSettings = settings.app || {};
615698
var modelSettings = settings.model || {};

test/fixtures/access-control/common/models/user.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
"principalId": "$everyone"
2121
}
2222
],
23-
"replaceOnPUT": false
24-
}
23+
"replaceOnPUT": false
24+
}

0 commit comments

Comments
 (0)