Skip to content

Commit

Permalink
changed token object to store expiresAt instead of timeout property
Browse files Browse the repository at this point in the history
  • Loading branch information
vkai authored and gr2m committed Dec 1, 2016
1 parent c9c9630 commit 23ff166
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 46 deletions.
4 changes: 0 additions & 4 deletions lib/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ function account (setupPromise, state, findAccountOptions) {
return Promise.reject(errors.TOKEN_TYPE_INVALID)
}

if (!tokenOptions.hasOwnProperty('timeout')) {
tokenOptions.timeout = 7200
}

return setupPromise

.then(function () {
Expand Down
6 changes: 5 additions & 1 deletion lib/utils/add-token-to-user-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ function addTokenToUserDoc (db, account, token) {
var id = token.id || uuid.v4()
delete token.id

token.createdAt = new Date().toISOString()
var timeout = token.hasOwnProperty('timeout') ? token.timeout : 7200
delete token.timeout
var now = Date.now()
token.createdAt = new Date(now).toISOString()
token.expiresAt = new Date(now + (timeout * 1000)).toISOString()

userDoc.tokens[id] = token

Expand Down
5 changes: 1 addition & 4 deletions lib/utils/is-token-expired.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
module.exports = isTokenExpired

function isTokenExpired (token) {
var timeOutInMs = token.timeout * 1000
var createdAtInMs = Date.parse(token.createdAt)

return createdAtInMs + timeOutInMs < Date.now()
return Date.parse(token.expiresAt) <= Date.now()
}
2 changes: 1 addition & 1 deletion test/integration/token-rejection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test('token rejection', function (t) {
})

.then(function (token) {
t.is(token.timeout, 0, 'token created with timeout 0s')
t.is(token.createdAt, token.expiresAt, 'token created with createdAt and expiresAt same')

return api.sessions.add({
account: {
Expand Down
36 changes: 0 additions & 36 deletions test/unit/account/tokens-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,41 +40,5 @@ test('add', function (group) {
})
})

group.test('with timeout set', function (t) {
t.plan(1)

var timeout = 5
var token = {
id: 'testtoken',
type: 'testtype',
timeout: timeout
}

account(Promise.reject('mock')).tokens.add(token)

.catch(function () {
t.is(token.timeout, timeout)
})
})

group.test('without timeout', function (t) {
t.plan(1)

var compareToken = {
id: 'testtoken',
type: 'testtype',
timeout: 7200
}
var tokenWithoutTimeout = {
id: 'testtoken',
type: 'testtype'
}
account(Promise.reject('mock')).tokens.add(tokenWithoutTimeout)

.catch(function () {
t.deepEqual(tokenWithoutTimeout, compareToken)
})
})

group.end()
})

0 comments on commit 23ff166

Please sign in to comment.