Skip to content

Commit aa32980

Browse files
committed
Use the token's exp as the timeout in token storage
1 parent aca2056 commit aa32980

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

models/jwt/JwtService.cfc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,8 +806,12 @@ component accessors="true" singleton threadsafe {
806806
getTokenStorage().set(
807807
key = payload.jti,
808808
token = jwtToken,
809-
expiration = variables.settings.jwt.expiration,
810-
payload = payload
809+
expiration = dateDiff(
810+
"n",
811+
fromEpoch( payload.iat ),
812+
fromEpoch( payload.exp )
813+
),
814+
payload = payload
811815
);
812816
}
813817

test-harness/tests/specs/integration/JWTSpec.cfc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,36 @@ component extends="coldbox.system.testing.BaseTestCase" appMapping="/root" {
303303
} );
304304
} );
305305

306+
given( "a valid jwt token put in to storage", function(){
307+
then( "it should use the exp on the token for the storage timeout", function(){
308+
var originalTokenStorage = duplicate( variables.jwtService.getTokenStorage() );
309+
try {
310+
variables.jwtService.getTokenStorage().clearAll();
311+
var tokenStorageMock = prepareMock( variables.jwtService.getTokenStorage() );
312+
tokenStorageMock.$( "set", tokenStorageMock );
313+
var expirationSeconds = 100;
314+
var expirationTime = variables.jwtService.toEpoch(
315+
dateAdd( "n", expirationSeconds, now() )
316+
);
317+
var thisToken = variables.jwtService.attempt(
318+
"test",
319+
"test",
320+
{ "exp" : expirationTime }
321+
);
322+
var tokenStorageSetCallLog = tokenStorageMock.$callLog().set;
323+
expect( tokenStorageSetCallLog ).toBeArray();
324+
expect( tokenStorageSetCallLog ).toHaveLength( 1 );
325+
expect( tokenStorageSetCallLog[ 1 ] ).toHaveKey( "expiration" );
326+
expect( tokenStorageSetCallLog[ 1 ].expiration ).toBeCloseTo(
327+
expirationSeconds,
328+
1
329+
);
330+
} finally {
331+
variables.jwtService.setTokenStorage( originalTokenStorage );
332+
}
333+
} );
334+
} );
335+
306336
given( "a valid jwt token but it is not in the storage", function(){
307337
then( "it should block with no authorization", function(){
308338
var thisToken = variables.jwtService.attempt( "test", "test" );

0 commit comments

Comments
 (0)