Skip to content

Commit 75b93ba

Browse files
committed
fix cache TTL
1 parent 02b6c2c commit 75b93ba

File tree

4 files changed

+10
-12
lines changed

4 files changed

+10
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ Optional. Default: `0`.
135135
- `userInfo {Array.<?string>}` — List of properties which should be included in the `request.auth.credentials` object besides `scope` and `sub`.<br/>
136136
Optional. Default: `[]`.<br/>
137137

138-
- `cache {Object|boolean}` — The configuration of the [hapi.js cache](https://hapijs.com/api#servercacheoptions) powered by [catbox][catbox]. If the property `iat` (issued at) or `exp` (expiresAt) is undefined, the plugin uses 60 seconds as default TTL.<br/>
138+
- `cache {Object|boolean}` — The configuration of the [hapi.js cache](https://hapijs.com/api#servercacheoptions) powered by [catbox][catbox]. If the property `exp` (expiresAt) is undefined, the plugin uses 60 seconds as default TTL. Otherwise the cache entry expires as soon as the token itself expires.<br/>
139139
If `false` the cache is disabled. Use `true` or an empty object (`{}`) to use the built-in default cache.<br/>
140140
Optional. Default: `false`.
141141

src/token.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,13 @@ function getRoles (clientId, {
6262
* @private
6363
*
6464
* Get expiration out of token content.
65-
* If `exp` or `iat` is undefined just 60
66-
* seconds as default expiration time.
65+
* If `exp` is undefined just use 60 seconds as default.
6766
*
6867
* @param {number} exp The `expiration` timestamp in seconds
69-
* @param {number} iat The `issued at` timestamp in seconds
7068
* @returns {number} The expiration delta in milliseconds
7169
*/
72-
function getExpiration ({ exp, iat }) {
73-
return [exp, iat].includes(undefined) ? 60 * 1000 : (exp - iat) * 1000
70+
function getExpiration ({ exp }) {
71+
return exp ? (exp * 1000) - Date.now() : 60 * 1000
7472
}
7573

7674
/**

test/fixtures/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ const content = {
127127
noExp: composeContent(true, false),
128128
noScope: composeContent(false, true),
129129
current: composeContent(true, false, {
130-
exp: parseInt(Date.now() / 1000) + 60 * 60,
131-
iat: parseInt(Date.now() / 1000) + 60 * 15
130+
exp: parseInt(Date.now() / 1000) + 60 * 60
132131
}),
133132
rpt: composeContent(true, true, {
134133
authorization: {

test/token.spec.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const test = require('ava')
2+
const _ = require('lodash')
23
const fixtures = require('./fixtures')
34
const token = require('../src/token')
45

@@ -54,7 +55,7 @@ test('get user data of token', (t) => {
5455
const data = token.getData(tkn, { clientId: fixtures.common.clientId })
5556

5657
t.truthy(tkn)
57-
t.is(data.expiresIn, 2700000)
58+
t.truthy(_.inRange(data.expiresIn, 3590000, 3600000))
5859
t.is(data.credentials.sub, fixtures.content.current.sub)
5960
t.falsy(data.credentials.name)
6061
t.deepEqual(data.credentials.scope.sort(), fixtures.targetScope)
@@ -65,7 +66,7 @@ test('get user data of token – rpt', (t) => {
6566
const data = token.getData(tkn, { clientId: fixtures.common.clientId })
6667

6768
t.truthy(tkn)
68-
t.is(data.expiresIn, 4000)
69+
t.truthy(_.inRange(-1 * data.expiresIn, Date.now()))
6970
t.is(data.credentials.sub, fixtures.content.rpt.sub)
7071
t.falsy(data.credentials.name)
7172
t.deepEqual(data.credentials.scope.sort(), [...fixtures.targetScope, 'scope:foo.READ', 'scope:foo.WRITE'])
@@ -79,7 +80,7 @@ test('get user data of token – additional fields', (t) => {
7980
})
8081

8182
t.truthy(tkn)
82-
t.is(data.expiresIn, 2700000)
83+
t.truthy(_.inRange(data.expiresIn, 3590000, 3600000))
8384
t.is(data.credentials.sub, fixtures.content.current.sub)
8485
t.is(data.credentials.name, fixtures.content.current.name)
8586
t.deepEqual(data.credentials.scope.sort(), fixtures.targetScope)
@@ -101,7 +102,7 @@ test('get user data of token – default scopes', (t) => {
101102
const data = token.getData(tkn, { clientId: fixtures.common.clientId })
102103

103104
t.truthy(tkn)
104-
t.is(data.expiresIn, 4000)
105+
t.truthy(_.inRange(-1 * data.expiresIn, Date.now()))
105106
t.is(data.credentials.sub, fixtures.content.expired.sub)
106107
t.falsy(data.credentials.name)
107108
t.deepEqual(data.credentials.scope, [])

0 commit comments

Comments
 (0)