Skip to content

Commit

Permalink
Merge pull request #182 from Subtletree/master
Browse files Browse the repository at this point in the history
Removed timeFactor config option and fixed refresh time calculation
  • Loading branch information
jpadilla authored Jan 17, 2017
2 parents 26f9c76 + 48f10a2 commit 37ccc3d
Show file tree
Hide file tree
Showing 7 changed files with 5 additions and 55 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ For example, with the following configuration:
};
ENV['ember-simple-auth-token'] = {
refreshAccessTokens: true,
timeFactor: 1,
refreshLeeway: 300 // Refresh the token 5 minutes (300s) before it expires.
};
```
Expand Down Expand Up @@ -193,8 +192,7 @@ For the JWT authenticator (in addition to the Token authenticator fields):
refreshAccessTokens: true,
serverTokenRefreshEndpoint: '/api/token-refresh/',
tokenExpireName: 'exp',
refreshLeeway: 0,
timeFactor: 1 // example - set to "1000" to convert incoming seconds to milliseconds.
refreshLeeway: 0
```

[build-status-image]: https://travis-ci.org/jpadilla/ember-simple-auth-token.svg?branch=master
Expand Down
31 changes: 4 additions & 27 deletions addon/authenticators/jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,6 @@ export default TokenAuthenticator.extend({
*/
tokenExpireName: 'exp',

/**
Default time unit.
@property timeFactor
@type Integer
@default 1 (seconds)
*/
timeFactor: 1,

/**
@method init
@private
Expand All @@ -79,7 +71,6 @@ export default TokenAuthenticator.extend({
this.refreshAccessTokens = Configuration.refreshAccessTokens;
this.refreshLeeway = Configuration.refreshLeeway;
this.tokenExpireName = Configuration.tokenExpireName;
this.timeFactor = Configuration.timeFactor;
this.headers = Configuration.headers;
},

Expand Down Expand Up @@ -107,7 +98,7 @@ export default TokenAuthenticator.extend({
return new Ember.RSVP.Promise((resolve, reject) => {
const now = this.getCurrentTime();
const token = dataObject.get(this.tokenPropertyName);
let expiresAt = this.resolveTime(dataObject.get(this.tokenExpireName));
let expiresAt = dataObject.get(this.tokenExpireName);

if (Ember.isEmpty(token)) {
return reject(new Error('empty token'));
Expand All @@ -118,14 +109,14 @@ export default TokenAuthenticator.extend({
// wasn't included in the data object that was passed in.
const tokenData = this.getTokenData(token);

expiresAt = this.resolveTime(tokenData[this.tokenExpireName]);
expiresAt = tokenData[this.tokenExpireName];
if (Ember.isEmpty(expiresAt)) {
return resolve(data);
}
}

if (expiresAt > now) {
const wait = expiresAt - now - (this.refreshLeeway * 1000);
const wait = (expiresAt - now - this.refreshLeeway) * 1000;

if (wait > 0) {
if (this.refreshAccessTokens) {
Expand Down Expand Up @@ -194,10 +185,9 @@ export default TokenAuthenticator.extend({
*/
scheduleAccessTokenRefresh(expiresAt, token) {
if (this.refreshAccessTokens) {
expiresAt = this.resolveTime(expiresAt);

const now = this.getCurrentTime();
const wait = expiresAt - now - (this.refreshLeeway * 1000);
const wait = (expiresAt - now - this.refreshLeeway) * 1000;

if (!Ember.isEmpty(token) && !Ember.isEmpty(expiresAt) && wait > 0) {
Ember.run.cancel(this._refreshTokenTimeout);
Expand Down Expand Up @@ -340,19 +330,6 @@ export default TokenAuthenticator.extend({
return Math.floor((new Date()).getTime() / 1000);
},

/**
Handles converting between time units for data between different systems.
Default: seconds(1)
@method resolveTime
@private
*/
resolveTime(time) {
if (Ember.isEmpty(time)) {
return time;
}
return new Date(time * this.timeFactor).getTime();
},

/**
Handles authentication response from server, and returns session data
Expand Down
9 changes: 0 additions & 9 deletions addon/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var defaults = {
tokenExpireName: 'exp',
authorizationPrefix: 'Bearer ',
authorizationHeaderName: 'Authorization',
timeFactor: 1,
headers: {}
};

Expand Down Expand Up @@ -115,14 +114,6 @@ export default {
*/
tokenExpireName: defaults.tokenExpireName,

/**
Default time unit.
@property timeFactor
@type Integer
@default 1 (seconds)
*/
timeFactor: 1,

/**
The prefix used in the value of the Authorization header.
Expand Down
2 changes: 0 additions & 2 deletions tests/dummy/app/templates/application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
ENV['ember-simple-auth-token'] = {
serverTokenEndpoint: '/api/token-auth/',
serverTokenRefreshEndpoint: '/api/token-refresh/',
timeFactor: 1000,
refreshLeeway: 5
};
</pre>
Expand All @@ -34,4 +33,3 @@

<p>Token Contents:</p>
<pre>{{tokenData}}</pre>

2 changes: 0 additions & 2 deletions tests/dummy/config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ module.exports = function(environment) {
ENV['ember-simple-auth-token'] = {
serverTokenEndpoint: '/api/token-auth/',
serverTokenRefreshEndpoint: '/api/token-refresh/',
timeFactor: 1000,
refreshLeeway: 5
};
}
Expand Down Expand Up @@ -68,7 +67,6 @@ module.exports = function(environment) {
ENV['ember-simple-auth-token'] = {
serverTokenEndpoint: ENV.API_URL + '/api/token-auth/',
serverTokenRefreshEndpoint: ENV.API_URL + '/api/token-refresh/',
timeFactor: 1000,
refreshLeeway: 5
};
}
Expand Down
8 changes: 0 additions & 8 deletions tests/unit/authenticators/jwt-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@ test('assigns tokenExpireName from the configuration object', assert => {
Configuration.load({}, {});
});

test('assigns timeFactor from the configuration object', assert => {
Configuration.timeFactor = 'timeFactor';

assert.equal(JWT.create().timeFactor, 'timeFactor');

Configuration.load({}, {});
});

test('#restore resolves when the data includes `token` and `expiresAt`', assert => {
assert.expect(1);

Expand Down
4 changes: 0 additions & 4 deletions tests/unit/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,3 @@ test('refreshLeeway', assert => {
test('tokenExpireName', assert => {
assert.equal(Configuration.tokenExpireName, 'exp', 'defaults to "exp"');
});

test('timeFactor', assert => {
assert.equal(Configuration.timeFactor, 1, 'defaults to 1');
});

0 comments on commit 37ccc3d

Please sign in to comment.