Skip to content

Commit 765e530

Browse files
authored
Merge pull request #2845 from strongloop/feature/allow-eternal-access-tokens-2x
Allow tokens with eternal TTL (value -1)
2 parents b445d80 + b3497c6 commit 765e530

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

common/models/access-token.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,19 @@ module.exports = function(AccessToken) {
149149
assert(this.ttl, 'token.ttl must exist');
150150
assert(this.ttl >= -1, 'token.ttl must be >= -1');
151151

152+
var AccessToken = this.constructor;
153+
var userRelation = AccessToken.relations.user; // may not be set up
154+
var User = userRelation && userRelation.modelTo;
155+
152156
var now = Date.now();
153157
var created = this.created.getTime();
154158
var elapsedSeconds = (now - created) / 1000;
155159
var secondsToLive = this.ttl;
156-
var isValid = elapsedSeconds < secondsToLive;
160+
var eternalTokensAllowed = !!(User && User.settings.allowEternalTokens);
161+
var isEternalToken = secondsToLive === -1;
162+
var isValid = isEternalToken ?
163+
eternalTokensAllowed :
164+
elapsedSeconds < secondsToLive;
157165

158166
if (isValid) {
159167
cb(null, isValid);

test/access-token.test.js

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,38 @@ describe('AccessToken', function() {
355355
assert(Object.prototype.toString.call(this.token.created), '[object Date]');
356356
});
357357

358-
it('should be validateable', function(done) {
359-
this.token.validate(function(err, isValid) {
360-
assert(isValid);
358+
describe('.validate()', function() {
359+
it('accepts valid tokens', function(done) {
360+
this.token.validate(function(err, isValid) {
361+
assert(isValid);
362+
done();
363+
});
364+
});
361365

362-
done();
366+
it('rejects eternal TTL by default', function(done) {
367+
this.token.ttl = -1;
368+
this.token.validate(function(err, isValid) {
369+
if (err) return done(err);
370+
expect(isValid, 'isValid').to.equal(false);
371+
done();
372+
});
373+
});
374+
375+
it('allows eternal tokens when enabled by User.allowEternalTokens',
376+
function(done) {
377+
var Token = givenLocalTokenModel();
378+
379+
// Overwrite User settings - enable eternal tokens
380+
Token.app.models.User.settings.allowEternalTokens = true;
381+
382+
Token.create({ userId: '123', ttl: -1 }, function(err, token) {
383+
if (err) return done(err);
384+
token.validate(function(err, isValid) {
385+
if (err) return done(err);
386+
expect(isValid, 'isValid').to.equal(true);
387+
done();
388+
});
389+
});
363390
});
364391
});
365392

@@ -622,3 +649,16 @@ function createTestApp(testToken, settings, done) {
622649

623650
return app;
624651
}
652+
653+
function givenLocalTokenModel() {
654+
var app = loopback({ localRegistry: true, loadBuiltinModels: true });
655+
app.dataSource('db', { connector: 'memory' });
656+
657+
var User = app.registry.getModel('User');
658+
app.model(User, { dataSource: 'db' });
659+
660+
var Token = app.registry.getModel('AccessToken');
661+
app.model(Token, { dataSource: 'db' });
662+
663+
return Token;
664+
}

0 commit comments

Comments
 (0)