-
Notifications
You must be signed in to change notification settings - Fork 140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refresh token after computer sleep #300
Comments
@ghusse That is the intended behavior. This library can't assume it is safe to wait to invalidate the session while the refresh request is processing because the session could be used by the user during this time. You can extend JWT authenticator and handle this scenario for you specific application. Specifically, you would probably need to extend Let me know if you have any questions about how to extend the authenticator. |
In my application, I have noticed that on a mobile browser, sessions tend to last less that on a desktop browser. In fact, much less than the refresh access token expiration time. I believe that the cause is similar to @ghusse has pointed out here. My impression is that a solution to this problem would involving making the Ember adapter be aware of the possibility that the access token may have expired and cause requests to be held until the refresh query promise finishes. |
I came up with the following solution, which is definitely better than the base implementation if most of the time that the access token expires the refresh token is still valid and if session invalidation already forced a page refresh. If not, this solution may cause most of the time an unwanted extra page refresh.
|
If you set the |
Refresh token request errorI have identified another potential cause for the session being invalidated under circumstances where the client could reasonably recover on its own: The refresh token request resolves in an error other than a HTTP 401 or 403 error (e.g., transient network error). I have come up with a solution for this:
API requests when session is authenticated but access token has expiredThanks @fenichelar for mentioning
My opinion is that 3 is best and it would have to be implemented at the adapter level. In this case, we might need a method for the adapter to be sure that in the token is in brink of being refreshed or to verify that the user has not simply suddenly lost their access altogether (e.g., the user account was inactivated). Another issue could be that the clock of the client is temporarily late with respect to the server's and the adapter should also ask the authorizer to trigger a token refresh asap. I have not yet implemented automatic request retry from the adapter, but at least I have somewhat covered the triggering of a token refresh from the adapter by forcing a
|
@masda70 What do you think of this? 787be61 Note I haven't documented anything yet nor tested it.
As you mentioned, you would then overwrite Note you would still want to set |
I didn't like the use of a class variable to store the number of attempts that had been made. So I changed it to pass the number of attempts around in functions calls. I added some documentation. I also did some testing which caught a bug and I fixed that. Still need to add unit tests though. |
Thank you, 787be61 is definitely a welcome change! However I am not certain I would set The remaining two issues that I am particularly interested in solving, and somewhat presented in my previous post are:
|
@masda70 This sound like a fairly specific use case. I'm not use it makes sense to try an natively support this use case in this library. For the first issue, I agree, handling an inaccessible backend on page load is complex. For many applications, the For the second issue, the intent of the retry logic recently added to this library is more about a transient network issue, not a long term network disconnect. So I don't think I want to add anything to this library. But you could pretty easily do soemthing in your adapter. The access token has an |
We've encountered a similar issue with cross-tab token refreshing, and the current issue was one of the issues I've looked up for inspiration. Sharing my solution in case somebody ever stumbles upon a similar problem. // app/authenticators/jwt.js
import JWT from 'ember-simple-auth-token/authenticators/jwt';
import EmberObject from '@ember/object';
import { inject as service } from '@ember/service';
import { getOwner } from '@ember/application';
export default JWT.extend({
session: service('session'),
get tokenRefreshOffset() {
const min = 1;
const max = this.refreshLeewayOffsetMax;
return Math.floor(Math.random() * (max - min)) + min;
},
init() {
this._super(...arguments);
const owner = getOwner(this);
const environment = owner ? owner.resolveRegistration('config:environment') || {} : {};
const config = environment['ember-simple-auth-token'] || {};
this.refreshLeewayOffsetMax = config.refreshLeewayOffsetMax || 20;
this.refreshLeeway = this.refreshLeeway + this.tokenRefreshOffset;
},
async refreshAccessToken(refreshToken, attempts) {
const parent = this._super;
/* try to restore up-to-date session data from store */
const restoredContent = await this.session.session.store.restore();
const dataObject = EmberObject.create(restoredContent.authenticated);
const now = this.getCurrentTime();
refreshToken = dataObject.get(this.refreshTokenPropertyName);
let expiresAt = dataObject.get(this.tokenExpireName);
if (expiresAt > now) {
const wait = (expiresAt - now - this.refreshLeeway) * 1000;
if (wait > 0) {
this.scheduleAccessTokenRefresh(expiresAt, refreshToken);
} else {
return await parent.call(this, refreshToken, 0);
}
} else {
return await parent.call(this, refreshToken, attempts);
}
},
}); The solution introduces a new option for ESA-token ( |
I experienced an issue with this library, when using refresh tokens.
If the computer goes into sleep mode, and if the authentication token expires during this period, after awakening the computer the user is logged out whereas the refresh token should be refreshed.
I figured out that:
The text was updated successfully, but these errors were encountered: