Skip to content

Commit ee8a9e7

Browse files
committed
Set default timeSkew to 60 seconds to avoid forced token refresh (#52)
Closes #52 Signed-off-by: Janek Beck <[email protected]>
1 parent 21fc968 commit ee8a9e7

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

lib/keycloak.d.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,10 @@ export interface KeycloakInitOptions {
112112
idToken?: string;
113113

114114
/**
115-
* Set an initial value for skew between local time and Keycloak server in
116-
* seconds (only together with `token` or `refreshToken`).
115+
* Set an initial value for skew between local and Keycloak server time (in
116+
* seconds). This is also used to determine whether the initial
117+
* token is still valid (only together with `token` or `refreshToken`).
118+
* @default 60
117119
*/
118120
timeSkew?: number;
119121

@@ -433,11 +435,11 @@ declare class Keycloak {
433435
idTokenParsed?: KeycloakTokenParsed;
434436

435437
/**
436-
* The estimated time difference between the browser time and the Keycloak
437-
* server in seconds. This value is just an estimation, but is accurate
438+
* The estimated time difference between the browser and the Keycloak
439+
* server time (in seconds). This value is just an estimation, but is accurate
438440
* enough when determining if a token is expired or not.
439441
*/
440-
timeSkew?: number;
442+
timeSkew: number;
441443

442444
/**
443445
* Whether the instance has been initialized by calling `.init()`.

lib/keycloak.js

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,7 @@ function Keycloak (config) {
129129
kc.flow = initOptions.flow;
130130
}
131131

132-
if (initOptions.timeSkew != null) {
133-
kc.timeSkew = initOptions.timeSkew;
134-
}
132+
kc.timeSkew = initOptions.timeSkew ?? 60;
135133

136134
if(initOptions.redirectUri) {
137135
kc.redirectUri = initOptions.redirectUri;
@@ -299,7 +297,7 @@ function Keycloak (config) {
299297
});
300298
});
301299
} else {
302-
kc.updateToken(-1).then(function() {
300+
kc.updateToken().then(function() {
303301
kc.onAuthSuccess && kc.onAuthSuccess();
304302
initPromise.setSuccess();
305303
}).catch(function(error) {
@@ -603,11 +601,6 @@ function Keycloak (config) {
603601
throw 'Not authenticated';
604602
}
605603

606-
if (kc.timeSkew == null) {
607-
logInfo('[KEYCLOAK] Unable to determine if token is expired as timeskew is not set');
608-
return true;
609-
}
610-
611604
var expiresIn = kc.tokenParsed['exp'] - Math.ceil(new Date().getTime() / 1000) + kc.timeSkew;
612605
if (minValidity) {
613606
if (isNaN(minValidity)) {
@@ -987,17 +980,15 @@ function Keycloak (config) {
987980
kc.timeSkew = Math.floor(timeLocal / 1000) - kc.tokenParsed.iat;
988981
}
989982

990-
if (kc.timeSkew != null) {
991-
logInfo('[KEYCLOAK] Estimated time difference between browser and server is ' + kc.timeSkew + ' seconds');
983+
logInfo('[KEYCLOAK] Estimated time difference between browser and server is ' + kc.timeSkew + ' seconds');
992984

993-
if (kc.onTokenExpired) {
994-
var expiresIn = (kc.tokenParsed['exp'] - (new Date().getTime() / 1000) + kc.timeSkew) * 1000;
995-
logInfo('[KEYCLOAK] Token expires in ' + Math.round(expiresIn / 1000) + ' s');
996-
if (expiresIn <= 0) {
997-
kc.onTokenExpired();
998-
} else {
999-
kc.tokenTimeoutHandle = setTimeout(kc.onTokenExpired, expiresIn);
1000-
}
985+
if (kc.onTokenExpired) {
986+
var expiresIn = (kc.tokenParsed['exp'] - (new Date().getTime() / 1000) + kc.timeSkew) * 1000;
987+
logInfo('[KEYCLOAK] Token expires in ' + Math.round(expiresIn / 1000) + ' s');
988+
if (expiresIn <= 0) {
989+
kc.onTokenExpired();
990+
} else {
991+
kc.tokenTimeoutHandle = setTimeout(kc.onTokenExpired, expiresIn);
1001992
}
1002993
}
1003994
} else {

0 commit comments

Comments
 (0)