+
Skip to content

Set default timeSkew to 60 seconds to avoid forced token refresh (#52) #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/keycloak.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,10 @@ export interface KeycloakInitOptions {
idToken?: string;

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

Expand Down Expand Up @@ -472,8 +474,8 @@ declare class Keycloak {
idTokenParsed?: KeycloakTokenParsed;

/**
* The estimated time difference between the browser time and the Keycloak
* server in seconds. This value is just an estimation, but is accurate
* The estimated time difference between the browser and the Keycloak
* server time (in seconds). This value is just an estimation, but is accurate
* enough when determining if a token is expired or not.
*/
timeSkew: number | null;
Expand Down
33 changes: 12 additions & 21 deletions lib/keycloak.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ export default class Keycloak {
responseType = 'code';
/** @type {KeycloakFlow} */
flow = 'standard';
/** @type {number?} */
timeSkew = null;
/** @type {number} */
timeSkew = 60;
/** @type {string=} */
redirectUri;
/** @type {string=} */
Expand Down Expand Up @@ -865,7 +865,7 @@ export default class Keycloak {
}
} else {
try {
await this.updateToken(-1);
await this.updateToken();
this.onAuthSuccess?.();
} catch (error) {
this.onAuthError?.();
Expand Down Expand Up @@ -1437,11 +1437,6 @@ export default class Keycloak {
throw 'Not authenticated';
}

if (this.timeSkew == null) {
this.#logInfo('[KEYCLOAK] Unable to determine if token is expired as timeskew is not set');
return true;
}

if (typeof this.tokenParsed.exp !== 'number') {
return false;
}
Expand All @@ -1460,13 +1455,11 @@ export default class Keycloak {
* @param {number} minValidity
* @returns {Promise<boolean>}
*/
async updateToken(minValidity) {
async updateToken(minValidity = 5) {
if (!this.refreshToken) {
throw new Error('Unable to update token, no refresh token available.');
}

minValidity = minValidity || 5;

if (this.#loginIframe.enable) {
await this.#checkLoginIframe();
}
Expand Down Expand Up @@ -1574,17 +1567,15 @@ export default class Keycloak {
this.timeSkew = Math.floor(timeLocal / 1000) - this.tokenParsed.iat;
}

if (this.timeSkew !== null) {
this.#logInfo('[KEYCLOAK] Estimated time difference between browser and server is ' + this.timeSkew + ' seconds');
this.#logInfo('[KEYCLOAK] Estimated time difference between browser and server is ' + this.timeSkew + ' seconds');

if (this.onTokenExpired) {
var expiresIn = (this.tokenParsed.exp - (new Date().getTime() / 1000) + this.timeSkew) * 1000;
this.#logInfo('[KEYCLOAK] Token expires in ' + Math.round(expiresIn / 1000) + ' s');
if (expiresIn <= 0) {
this.onTokenExpired();
} else {
this.tokenTimeoutHandle = window.setTimeout(this.onTokenExpired, expiresIn);
}
if (this.onTokenExpired) {
var expiresIn = (this.tokenParsed.exp - (new Date().getTime() / 1000) + this.timeSkew) * 1000;
this.#logInfo('[KEYCLOAK] Token expires in ' + Math.round(expiresIn / 1000) + ' s');
if (expiresIn <= 0) {
this.onTokenExpired();
} else {
this.tokenTimeoutHandle = window.setTimeout(this.onTokenExpired, expiresIn);
}
}
} else {
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载