Skip to content
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

Fixes jwt authenticator restore method to schedule access token refresh after page reload #44

Merged
merged 5 commits into from
Apr 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions addon/authenticators/jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,21 @@ export default TokenAuthenticator.extend({
dataObject = Ember.Object.create(data);

return new Ember.RSVP.Promise(function(resolve, reject) {
var now = (new Date()).getTime(),
expiresAt = _this.resolveTime(dataObject.get('expiresAt')),
token = dataObject.get(_this.tokenPropertyName);
var now = (new Date()).getTime();
var expiresAt = _this.resolveTime(dataObject.get(_this.tokenExpireName));
var token = dataObject.get(_this.tokenPropertyName);

if (Ember.isEmpty(token)) {
return reject(new Error('empty token'));
}

if (Ember.isEmpty(expiresAt)) {
if (Ember.isEmpty(expiresAt)) {
// Fetch the expire time from the token data since `expiresAt`
// wasn't included in the data object that was passed in.
var tokenData = _this.getTokenData(data[_this.tokenPropertyName]);
expiresAt = _this.resolveTime(tokenData[_this.tokenExpireName]);
if (Ember.isEmpty(expiresAt)) {
return resolve(data);
}

}
if (expiresAt !== expiresAt) {
return reject(new Error('invalid expiration'));
Expand All @@ -127,11 +126,12 @@ export default TokenAuthenticator.extend({
var wait = expiresAt - now - (_this.refreshLeeway * 1000);
if (wait > 0) {
if (_this.refreshAccessTokens) {
_this.scheduleAccessTokenRefresh(expiresAt, token);
_this.scheduleAccessTokenRefresh(dataObject.get(_this.tokenExpireName), token);
}
resolve(data);
} else if (_this.refreshAccessTokens) {
resolve(_this.refreshAccessToken(data.token).then(function () {
resolve(_this.refreshAccessToken(
dataObject.get(_this.tokenPropertyName)).then(function () {
return data;
}));
} else {
Expand Down Expand Up @@ -240,14 +240,15 @@ export default TokenAuthenticator.extend({
return new Ember.RSVP.Promise(function(resolve, reject) {
_this.makeRequest(_this.serverTokenRefreshEndpoint, data).then(function(response) {
Ember.run(function() {
var tokenData = _this.getTokenData(response[_this.tokenPropertyName]),
expiresAt = tokenData[_this.tokenExpireName],
data = Ember.merge(response, {
expiresAt: expiresAt
});
var tokenData = _this.getTokenData(response[_this.tokenPropertyName]);
var expiresAt = tokenData[_this.tokenExpireName];
var tokenExpireData = {};

_this.scheduleAccessTokenRefresh(expiresAt, response.token);
tokenExpireData[_this.tokenExpireName] = expiresAt;

data = Ember.merge(response, tokenExpireData);

_this.scheduleAccessTokenRefresh(expiresAt, response.token);
_this.trigger('sessionDataUpdated', data);

resolve(response);
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/authenticators/jwt-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ test('#restore schedules a token refresh when `refreshAccessTokens` is true.', f
data[jwt.tokenPropertyName] = token;
data[jwt.tokenExpireName] = expiresAt;

// TODO: find out of there is another way besides setting Ember.testing.
// TODO: find out if there is another way besides setting Ember.testing.
Ember.testing = false;

Ember.run(function() {
Expand Down Expand Up @@ -506,8 +506,8 @@ test('#refreshAccessToken triggers the `sessionDataUpdated` event on successful
App.authenticator.refreshAccessToken(token);

App.authenticator.one('sessionDataUpdated', function(data) {
ok(data.expiresAt, 'Verify expiresAt was added to response');
ok(data.expiresAt > (new Date()).getTime(), 'Verify is greater than now');
ok(data[jwt.tokenExpireName], 'Verify expiresAt was added to response');
ok(data[jwt.tokenExpireName] > (new Date()).getTime(), 'Verify is greater than now');
deepEqual(data.token, token);
});
});
Expand Down