Sliding expiration of access token
It should be possible to configure LoopBack application in such way that the access token expires after a certain time of inactivity. The current implementation supports only the expiration after a fixed time since login.
See strongloop/loopback-angular#39 for a possible solution:
// ...
app.use(loopback.token({/* config */});
app.use(function(req, res, next) {
var token = req.accessToken;
if (!token) return next();
var now = new Date();
// performance optimization:
// do not update the token more often than once per second
if (now.getTime() - token.created.getTime() < 1000) return;
// update the token and save the changes
req.accessToken.created = now;
req.accessToken.ttl = 60; /* session timeout in seconds */
req.accessToken.save(next);
});
// register other middleware, etc.
I am closing this issue, since no user was asking for this enhancement and it clearly was not a priority for us so far. We can reopen it again later when there is more demand.
Sorry I did not see this before. I think this is very useful. Especially for mobile apps. Please reopen. :+1:
@meng-zhang I am afraid we don't have bandwidth to implement this anytime soon. Would you mind submitting a pull request?
- add a configuration option for enabling the sliding expiration, probably via
AccessToken.settings.slidingExpiration - implement sliding expiration in
loopback.tokenmiddleware
I didn't see any reference of this in the docs, so I assume it still hasn't been implemented?
If so I'd like to take a stab at it.
@diegonetto It's not implemented. Contribution is welcome!
@diegonetto I would definitely use this as well
Perhaps the onus could be on the client-side? Generate a new accessToken using the current accessToken?
@bajtos I think you should call next() every time before you return. :) This is how it should look like if you want to keep alive the token for a week.
server.js
app.use(loopback.token());
app.use(function(req, res, next) {
var token = req.accessToken;
if (!token) {
return next();
}
var now = new Date();
if ( now.getTime() - token.created.getTime() < 1000 ) {
return next();
}
req.accessToken.created = now;
req.accessToken.ttl = 604800; //one week
req.accessToken.save(next);
});
...
app.start...
@tib thanks a lot!
@bajtos Can this issue be reopened? I wouldn't mind giving it a shot.
@seanmangar Feel free to submit a PR and link to this issue. We can reopen it at that time.
It would be really useful !
+1
This would be very handy. We have a need to have an inactivity be the cause of logout, as well as the database access for this would be slow, hooking to memcache or redis would be way faster.
This would be super-useful :)
Do we have any update on this?
+1 This feature would be nice to have--configurable from config.json or what have you...
+1 Basing expiration on inactivity is a must for most apps.
Hello, the status of this feature request remains the same - it's not a priority for us right now. However, we welcome community contributions and if you decide to implement this feature, then I am happy to help you along the way. Just open a pull request and mention my handle (@bajtos).
Prolongation by each request (maximum one update per day)
/* Access token live time prolongation */
app.use(function accessTokenProlongation(req, res, next) {
var token = req.accessToken,
now = new Date(),
createTS = token ? token.created.getTime() : 0,
expiredTS = token ? req.accessToken.ttl * 1000 : 0,
dayTS = 24 * 60 * 60 * 1000;
if (!token || (token && (createTS + expiredTS < now))) {
return next();
}
if (now.getTime() - createTS < dayTS) {
return next();
}
token.updateAttribute('created', now, next);
});
@bajtos I'm trying to figure out where to implement this, because it's really a thing lb needs imho. I'm currently thinking about putting the middleware bit in loopback/common/models/access-token.js, in the AccessToken.prototype.validate function. Also would love to know how/where to add a configuration option for enabling the sliding expiration, you mentioned doing it via AccessToken.settings.slidingExpiration but I have no clue where that is (which file).
Hmm, reminds me I still need to implement this too! I'll just be doing it as @av-k showed I think.