loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Sliding expiration of access token

Open bajtos opened this issue 12 years ago • 33 comments

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.

bajtos avatar May 14 '14 17:05 bajtos

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.

bajtos avatar Oct 22 '14 16:10 bajtos

Sorry I did not see this before. I think this is very useful. Especially for mobile apps. Please reopen. :+1:

meng-zhang avatar Oct 23 '14 16:10 meng-zhang

@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.token middleware

bajtos avatar Oct 23 '14 16:10 bajtos

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 avatar Nov 05 '15 13:11 diegonetto

@diegonetto It's not implemented. Contribution is welcome!

raymondfeng avatar Nov 05 '15 16:11 raymondfeng

@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?

jamesw6811 avatar Jan 18 '16 17:01 jamesw6811

@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 avatar Feb 05 '16 10:02 tib

@tib thanks a lot!

F3L1X79 avatar Feb 09 '16 15:02 F3L1X79

@bajtos Can this issue be reopened? I wouldn't mind giving it a shot.

seanmangar avatar Jun 15 '16 10:06 seanmangar

@seanmangar Feel free to submit a PR and link to this issue. We can reopen it at that time.

superkhau avatar Jun 15 '16 21:06 superkhau

It would be really useful !

murbanowicz avatar Jun 16 '16 09:06 murbanowicz

+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.

zalos avatar Jun 22 '16 21:06 zalos

This would be super-useful :)

sidewaiise avatar Jul 01 '16 02:07 sidewaiise

Do we have any update on this?

rafaelferreir4 avatar Sep 21 '16 19:09 rafaelferreir4

+1 This feature would be nice to have--configurable from config.json or what have you...

tamitutor avatar Dec 20 '16 10:12 tamitutor

+1 Basing expiration on inactivity is a must for most apps.

ataft avatar Jan 09 '17 23:01 ataft

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).

bajtos avatar Jan 10 '17 14:01 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);
  });

av-k avatar Feb 09 '17 08:02 av-k

@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).

buokae avatar Sep 15 '17 11:09 buokae

Hmm, reminds me I still need to implement this too! I'll just be doing it as @av-k showed I think.

TrevorPage avatar Sep 15 '17 12:09 TrevorPage