Skip to content

Commit 3229fdb

Browse files
authored
Merge pull request #2841 from strongloop/feature/allow-eternal-access-tokens
Allow tokens with eternal TTL (value -1)
2 parents 953cfa9 + 6808159 commit 3229fdb

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
@@ -147,11 +147,19 @@ module.exports = function(AccessToken) {
147147
assert(this.ttl, 'token.ttl must exist');
148148
assert(this.ttl >= -1, 'token.ttl must be >= -1');
149149

150+
var AccessToken = this.constructor;
151+
var userRelation = AccessToken.relations.user; // may not be set up
152+
var User = userRelation && userRelation.modelTo;
153+
150154
var now = Date.now();
151155
var created = this.created.getTime();
152156
var elapsedSeconds = (now - created) / 1000;
153157
var secondsToLive = this.ttl;
154-
var isValid = elapsedSeconds < secondsToLive;
158+
var eternalTokensAllowed = !!(User && User.settings.allowEternalTokens);
159+
var isEternalToken = secondsToLive === -1;
160+
var isValid = isEternalToken ?
161+
eternalTokensAllowed :
162+
elapsedSeconds < secondsToLive;
155163

156164
if (isValid) {
157165
cb(null, isValid);

test/access-token.test.js

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

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

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

@@ -626,3 +653,16 @@ function createTestApp(testToken, settings, done) {
626653

627654
return app;
628655
}
656+
657+
function givenLocalTokenModel() {
658+
var app = loopback({ localRegistry: true, loadBuiltinModels: true });
659+
app.dataSource('db', { connector: 'memory' });
660+
661+
var User = app.registry.getModel('User');
662+
app.model(User, { dataSource: 'db' });
663+
664+
var Token = app.registry.getModel('AccessToken');
665+
app.model(Token, { dataSource: 'db' });
666+
667+
return Token;
668+
}

0 commit comments

Comments
 (0)