Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

UI updates: Improve handling of keycloak offline token and reconnect logic (#1340)

Rich Turner815ad2b63bdc
diff --git a/.github/workflows/ci_cd.yml b/.github/workflows/ci_cd.yml
index 713f615b8..5544fbb2a 100644
--- a/.github/workflows/ci_cd.yml
+++ b/.github/workflows/ci_cd.yml
@@ -163,7 +163,13 @@ jobs:
- name: Public IP
id: ip-address
- uses: haythem/[email protected]
+ shell: bash
+ run: |
+ set -e
+ PUBLIC_IPV4=$(curl -sf v4.ident.me 2>/dev/null || true)
+ PUBLIC_IPV6=$(curl -sf v6.ident.me 2>/dev/null || true)
+ echo "ipv4=$PUBLIC_IPV4" >> $GITHUB_OUTPUT
+ echo "ipv6=$PUBLIC_IPV6" >> $GITHUB_OUTPUT
- name: Checkout
uses: actions/checkout@v3
diff --git a/profile/dev-proxy.yml b/profile/dev-proxy.yml
index 249942df0..3cc152474 100644
--- a/profile/dev-proxy.yml
+++ b/profile/dev-proxy.yml
@@ -38,6 +38,7 @@ services:
environment:
KEYCLOAK_ISSUER_BASE_URI: https://localhost/auth
KC_HOSTNAME_STRICT: false
+ KC_HOSTNAME:
depends_on:
postgresql:
condition: service_healthy
diff --git a/ui/component/core/package.json b/ui/component/core/package.json
index c2b7c12e0..0f194dfd7 100644
--- a/ui/component/core/package.json
+++ b/ui/component/core/package.json
@@ -24,7 +24,7 @@
"@openremote/rest": "workspace:*",
"i18next": "^21.5.3",
"i18next-http-backend": "^1.3.1",
- "keycloak-js": "^23.0.6",
+ "keycloak-js": "^25.0.1",
"moment": "^2.29.4",
"platform": "^1.3.6",
"qs": "^6.8.0",
diff --git a/ui/component/core/src/console.ts b/ui/component/core/src/console.ts
index 6fae716ed..2d6e49f1b 100644
--- a/ui/component/core/src/console.ts
+++ b/ui/component/core/src/console.ts
@@ -34,6 +34,9 @@ export interface ProviderEnableResponse extends ProviderMessage {
success: boolean;
}
+/**
+ * Storage provider is a special case that must always be available and doesn't require init/enable logic
+ */
export class Console {
protected _realm: string;
@@ -65,11 +68,7 @@ export class Console {
let consoleProviders = queryParams.get("consoleProviders");
let autoEnableStr = queryParams.get("consoleAutoEnable");
- let requestedProviders = consoleProviders && consoleProviders.length > 0 ? consoleProviders.split(" ") : ["push", "storage"];
-
- if (requestedProviders.indexOf("storage") < 0) {
- requestedProviders.push("storage"); // Storage provider is essential to operation and should always be available
- }
+ let requestedProviders = consoleProviders && consoleProviders.length > 0 ? consoleProviders.split(" ") : ["push"];
this._pendingProviderEnables = requestedProviders;
// Look for existing console registration in local storage or just create a new one
@@ -273,7 +272,7 @@ export class Console {
}
public async sendProviderMessage(message: ProviderMessage, waitForResponse: boolean): Promise<any | null> {
- if (!this._registration.providers!.hasOwnProperty(message.provider)) {
+ if (message.provider !== "storage" && !this._registration.providers!.hasOwnProperty(message.provider)) {
console.debug("Invalid console provider '" + message.provider + "'");
throw new Error("Invalid console provider '" + message.provider + "'");
}
diff --git a/ui/component/core/src/event.ts b/ui/component/core/src/event.ts
index 356151c3f..1eef9c146 100644
--- a/ui/component/core/src/event.ts
+++ b/ui/component/core/src/event.ts
@@ -19,8 +19,7 @@ import {
export enum EventProviderStatus {
DISCONNECTED = "DISCONNECTED",
CONNECTED = "CONNECTED",
- CONNECTING = "CONNECTING",
- RECONNECT_FAILED = "RECONNECT_FAILED"
+ CONNECTING = "CONNECTING"
}
export interface EventProvider {
@@ -114,6 +113,10 @@ abstract class EventProviderImpl implements EventProvider {
}
public connect(): Promise<boolean> {
+ if (this._reconnectTimer) {
+ window.clearTimeout(this._reconnectTimer);
+ this._reconnectTimer = null;
+ }
if (this._status === EventProviderStatus.CONNECTED) {
return Promise.resolve(true);
}
@@ -133,11 +136,6 @@ abstract class EventProviderImpl implements EventProvider {
const deferred = this._connectingDeferred;
this._connectingDeferred = null;
- if (this._reconnectTimer) {
- window.clearTimeout(this._reconnectTimer);
- this._reconnectTimer = null;
- }
-
if (connected) {
console.debug("Connected to event service: " + this.endpointUrl);
this._reconnectDelayMillis = WebSocketEventProvider.MIN_RECONNECT_DELAY;
@@ -148,7 +146,7 @@ abstract class EventProviderImpl implements EventProvider {
}, 0);
} else {
console.debug("Failed to connect to event service: " + this.endpointUrl);
- this._onStatusChanged(EventProviderStatus.DISCONNECTED);
+ this._scheduleReconnect();
}
deferred.resolve(connected);
@@ -537,12 +535,15 @@ abstract class EventProviderImpl implements EventProvider {
}
protected _onDisconnect() {
- this._onStatusChanged(EventProviderStatus.DISCONNECTED);
+ if (this._status === EventProviderStatus.CONNECTED) {
+ this._onStatusChanged(EventProviderStatus.DISCONNECTED);
+ }
if (this._pendingSubscription) {
this._queuedSubscriptions.unshift(this._pendingSubscription);
this._pendingSubscription = null;
}
+ this._onStatusChanged(EventProviderStatus.CONNECTING);
this._scheduleReconnect();
}
@@ -563,15 +564,7 @@ abstract class EventProviderImpl implements EventProvider {
return;
}
- this.connect().then(connected => {
- if (!connected) {
- this._onStatusChanged(EventProviderStatus.RECONNECT_FAILED);
- this._scheduleReconnect();
- }
- }).catch(error => {
- this._onStatusChanged(EventProviderStatus.RECONNECT_FAILED);
- this._scheduleReconnect();
- });
+ this.connect();
}, this._reconnectDelayMillis);
if (this._reconnectDelayMillis < WebSocketEventProvider.MAX_RECONNECT_DELAY) {
@@ -628,10 +621,6 @@ export class WebSocketEventProvider extends EventProviderImpl {
this._webSocket = new WebSocket(authorisedUrl);
this._connectDeferred = new Deferred();
- if(manager.isTokenExpired()) {
- this._connectDeferred.resolve(false);
- }
-
this._webSocket!.onopen = () => {
if (this._connectDeferred) {
const deferred = this._connectDeferred;
diff --git a/ui/component/core/src/index.ts b/ui/component/core/src/index.ts
index fc305e084..496485be1 100644
--- a/ui/component/core/src/index.ts
+++ b/ui/component/core/src/index.ts
@@ -6,7 +6,17 @@ import {EventProvider, EventProviderFactory, EventProviderStatus, WebSocketEvent
import i18next, {InitOptions} from "i18next";
import i18nextBackend from "i18next-http-backend";
import moment from "moment";
-import {AssetModelUtil, Auth, ConsoleAppConfig, EventProviderType, ManagerConfig, MapType, Role, User, UsernamePassword} from "@openremote/model";
+import {
+ AssetModelUtil,
+ Auth,
+ ConsoleAppConfig,
+ EventProviderType,
+ ManagerConfig,
+ MapType,
+ Role,
+ User,
+ UsernamePassword
+} from "@openremote/model";
import * as Util from "./util";
import {createMdiIconSet, createSvgIconSet, IconSets, OrIconSet} from "@openremote/or-icon";
import Keycloak from 'keycloak-js';
@@ -40,11 +50,8 @@ export enum OREvent {
ONLINE = "ONLINE",
OFFLINE = "OFFLINE",
CONNECTING = "CONNECTING",
- RECONNECT_FAILED = "RECONNECT_FAILED",
CONSOLE_INIT = "CONSOLE_INIT",
CONSOLE_READY = "CONSOLE_READY",
- CONSOLE_VISIBLE = "CONSOLE_VISIBLE",
- CONSOLE_HIDDEN = "CONSOLE_HIDDEN",
TRANSLATE_INIT = "TRANSLATE_INIT",
TRANSLATE_LANGUAGE_CHANGED = "TRANSLATE_LANGUAGE_CHANGED",
DISPLAY_REALM_CHANGED = "DISPLAY_REALM_CHANGED"
@@ -256,11 +263,10 @@ export class Manager implements EventProviderFactory {
return this._config.mapType || MapType.VECTOR;
}
+ protected static MAX_RECONNECT_DELAY = 45000;
private _error?: ORError;
private _config!: ManagerConfig;
private _authenticated: boolean = false;
- private _authDisconnected: boolean = false;
- private _authReconnectTimeout?: number;
private _ready: boolean = false;
private _readyCallback?: () => PromiseLike<any>;
private _name: string = "";
@@ -278,7 +284,8 @@ export class Manager implements EventProviderFactory {
private _console!: Console;
private _consoleAppConfig?: ConsoleAppConfig;
private _events?: EventProvider;
- private _eventsDisconnected: boolean = false;
+ private _disconnected: boolean = false;
+ private _reconnectTimer?: number;
private _displayRealm?: string;
public isManagerSameOrigin(): boolean {
@@ -309,13 +316,19 @@ export class Manager implements EventProviderFactory {
public async init(config: ManagerConfig): Promise<boolean> {
if (this._config) {
- console.log("Already initialised");
+ console.debug("Already initialised");
}
this._config = normaliseConfig(config);
let success = await this.loadManagerInfo();
+ // Create console as we need to access storage during auth
+ const orConsole = new Console(this._config.realm!, this._config.consoleAutoEnable!, () => {
+ this._emitEvent(OREvent.CONSOLE_READY);
+ });
+ this._console = orConsole;
+
if (this._config.auth === Auth.BASIC) {
// BASIC auth will likely require UI so lets init translation at least
success = await this.doTranslateInit() && success;
@@ -366,12 +379,16 @@ export class Manager implements EventProviderFactory {
// If failed then we can assume keycloak auth requested but unavailable
if (!success && !this._config.skipFallbackToBasicAuth) {
// Try fallback to BASIC
- console.log("Falling back to basic auth");
+ console.debug("Falling back to basic auth");
this._config.auth = Auth.BASIC;
success = await this.doAuthInit();
}
}
+ if (!success) {
+ return false;
+ }
+
if (success) {
success = this.doRestApiInit();
}
@@ -537,6 +554,10 @@ export class Manager implements EventProviderFactory {
return false;
}
+ if (!success) {
+ return false;
+ }
+
// Add interceptor to inject authorization header on each request
rest.addRequestInterceptor(
(config: AxiosRequestConfig) => {
@@ -580,139 +601,6 @@ export class Manager implements EventProviderFactory {
return connected;
}
- // Public method for reconnecting
- public reconnect(force = false) {
- if(this._authDisconnected || force) {
- this._runAuthReconnectTimer();
- }
- if(this._eventsDisconnected || force) {
- this._tryReconnectEvents();
- }
- }
-
- // A timer that runs that tries 'reconnecting' to the authentication service logic every X milliseconds.
- // This timer increases by 20% with every reconnect attempt, with a maximum of 'timeoutMax' which is 20 seconds by default.
- // Reconnecting in this case means; trying to update the token. See _tryUpdateAccessToken() for more details.
- // It will emit OREvent.RECONNECT_FAILED if it failed to.
- protected _runAuthReconnectTimer(timeout = 5000, timeoutMax = 20000) {
- if(!this._authReconnectTimeout) {
- const reconnectAuthFunc = () => {
- this._tryUpdateAccessToken().then((disconnected) => {
- if(!disconnected) {
- this._finishAuthReconnectTimer(true);
- } else {
- timeout = Math.min(timeoutMax, Math.round(timeout * 1.2)); // gradually increase the timer
- console.debug(`Authentication service reconnect failed. Trying again in ${timeout} milliseconds...`)
- this._authReconnectTimeout = window.setTimeout(reconnectAuthFunc, timeout); // continue timer
- this._emitEvent(OREvent.RECONNECT_FAILED);
- }
-
- }).catch((e) => {
- // It is very unlikely that an exception is thrown here, but we should finish the timer if it does
- console.error(e);
- this._finishAuthReconnectTimer(false);
- })
- };
- // Start reconnect timer by triggering it once
- reconnectAuthFunc();
-
-
- // If a timer is already running, just try to update the access token directly without initiating a timer.
- } else {
- this._tryUpdateAccessToken();
- }
- }
-
-
- // Method that clears the Timeout and sets auth disconnected state.
- protected _finishAuthReconnectTimer(success: boolean) {
- if(this._authReconnectTimeout) {
- clearTimeout(this._authReconnectTimeout);
- delete this._authReconnectTimeout;
- if(success) {
- this._setAuthDisconnected(false);
- }
- }
- }
-
-
- // Checks whether keycloak is reachable using a simple HTTP request.
- // Since the keycloak JS adapter doesn't give us details of the HTTP responses they get, we test it manually using this.
- // Resolves or throws exception.
- protected async isKeycloakReachable(): Promise<void> {
- const controller = new AbortController();
- const timeout = setTimeout(() => controller.abort(), 2000);
- try {
- await fetch(this._config.keycloakUrl! + "/health/ready", {method: 'HEAD', mode: 'no-cors', signal: controller.signal});
- return Promise.resolve();
- } catch (e) {
- return Promise.reject(e);
- } finally {
- clearTimeout(timeout);
- }
- }
-
- // Function that tries to update the token, whilst making sure the authentication service is available.
- // It first checks the connection to keycloak, and then tries to update the access token.
- // Mostly used during "reconnect" logic, since it isn't necessary to 'double check' service availability in most cases.
- // TODO: Currently only applies to Keycloak, not implemented for basic auth.
- protected async _tryUpdateAccessToken(): Promise<boolean> {
- let authOffline = false;
- if(this.isKeycloak()) {
-
- console.debug("Attempting to update keycloak access token...");
- this._emitEvent(OREvent.CONNECTING); // emit event every time a reconnect attempt is made
-
- // Check if keycloak service is available / can be accessed.
- await this.isKeycloakReachable().catch(() => {
- authOffline = true;
- console.error("Could not reach keycloak service! Aborting token update.");
- });
- if(!authOffline) {
-
- // Update token
- await this.updateKeycloakAccessToken().catch((e) => {
- authOffline = true;
- console.error(e);
- });
- }
- }
-
- // Update _authDisconnected respectively
- this._setAuthDisconnected(authOffline);
-
- return authOffline;
- }
-
-
- // Function that tries to reconnect the EventProvider (such as WebSocket)
- protected _tryReconnectEvents() {
- if(this.events?.status !== EventProviderStatus.CONNECTED) {
-
- // If disconnected, proceed in trying to reconnect EventProvider...
- if(this.events?.status === EventProviderStatus.DISCONNECTED) {
- this._connectEvents();
- }
-
- // If already "connecting" the EventProvider, wait for attempt to finish, otherwise execute connectEvents() directly.
- // We subscribe to its status change, and unsubscribe after we get a callback that it got DISCONNECTED. (or a timeout of 4 seconds has gone by)
- else if(this.events?.status === EventProviderStatus.CONNECTING) {
- const callback = (status: EventProviderStatus) => {
- if (status === EventProviderStatus.DISCONNECTED) {
- this.events?.unsubscribeStatusChange(callback);
- this._connectEvents();
- }
- };
- // Subscribe to status change, and add timeout of 4 seconds to automatically unsubscribe.
- this.events.subscribeStatusChange(callback);
- setTimeout(() => {
- this.events?.unsubscribeStatusChange(callback)
- }, 4000);
-
- }
- }
- }
-
// Function that connects the EventProvider.
protected _connectEvents() {
if(this.events?.status === EventProviderStatus.DISCONNECTED) {
@@ -728,29 +616,19 @@ export class Manager implements EventProviderFactory {
protected _onEventProviderStatusChanged(status: EventProviderStatus) {
switch (status) {
case EventProviderStatus.DISCONNECTED:
- this._setEventDisconnected(true);
+ this._onDisconnect();
break;
case EventProviderStatus.CONNECTED:
- this._setEventDisconnected(false);
break;
case EventProviderStatus.CONNECTING:
this._emitEvent(OREvent.CONNECTING);
break;
- case EventProviderStatus.RECONNECT_FAILED:
- this._emitEvent(OREvent.RECONNECT_FAILED);
- break;
}
}
protected async doConsoleInit(): Promise<boolean> {
try {
- const orConsole = new Console(this._config.realm!, this._config.consoleAutoEnable!, () => {
- this._emitEvent(OREvent.CONSOLE_READY);
- });
-
- this._console = orConsole;
-
- await orConsole.initialise();
+ await this.console.initialise();
this._emitEvent(OREvent.CONSOLE_INIT);
return true;
} catch (e) {
@@ -784,10 +662,19 @@ export class Manager implements EventProviderFactory {
}
public logout(redirectUrl?: string) {
+ if (!this._authenticated) {
+ return;
+ }
+ this._authenticated = true;
+
if (this._keycloak) {
- if (this.console.isMobile) {
+ if (this.isMobile()) {
this.console.storeData("REFRESH_TOKEN", null);
}
+ if (this._keycloakUpdateTokenInterval) {
+ window.clearTimeout(this._keycloakUpdateTokenInterval);
+ this._keycloakUpdateTokenInterval = undefined;
+ }
this._keycloak.logout(redirectUrl && redirectUrl !== "" ? {redirectUri: redirectUrl} : undefined);
} else if (this._basicIdentity) {
this._basicIdentity = undefined;
@@ -827,7 +714,7 @@ export class Manager implements EventProviderFactory {
protected async initialiseBasicAuth(): Promise<boolean> {
if (!this.config.basicLoginProvider) {
- console.log("No basicLoginProvider defined on config so cannot display login UI");
+ console.debug("No basicLoginProvider defined on config so cannot display login UI");
return false;
}
@@ -848,8 +735,8 @@ export class Manager implements EventProviderFactory {
}
let result: BasicLoginResult = {
- username: this._config.credentials?.username ? this._config.credentials?.username : "",
- password: this._config.credentials?.password ? this._config.credentials?.password : "",
+ username: this.config.credentials?.username ? this.config.credentials?.username : "",
+ password: this.config.credentials?.password ? this.config.credentials?.password : "",
cancel: false
};
let authenticated = false;
@@ -864,7 +751,7 @@ export class Manager implements EventProviderFactory {
result = await this.config.basicLoginProvider(result.username, result.password);
if (result.cancel) {
- console.log("Basic authentication cancelled by user");
+ console.debug("Basic authentication cancelled by user");
break;
}
@@ -886,7 +773,7 @@ export class Manager implements EventProviderFactory {
if (!success) {
// Undertow incorrectly returns 403 when no authorization header and a 401 when it is set and not valid
if (userResponse.status === 401 || userResponse.status === 403) {
- console.log("Basic authentication invalid credentials, trying again");
+ console.debug("Basic authentication invalid credentials, trying again");
}
}
} catch (e) {
@@ -894,20 +781,22 @@ export class Manager implements EventProviderFactory {
}
if (success) {
- console.log("Basic authentication successful");
+ console.debug("Basic authentication successful");
authenticated = true;
// Get user roles
const rolesResponse = await rest.api.UserResource.getCurrentUserRoles();
this._basicIdentity!.roles = rolesResponse.data;
} else {
- console.log("Unknown response so aborting");
+ console.debug("Unknown response so aborting");
this._basicIdentity = undefined;
break;
}
}
- this._setAuthenticated(authenticated);
+ if (authenticated) {
+ this._onAuthenticated();
+ }
}
public isSuperUser(): boolean {
@@ -959,16 +848,6 @@ export class Manager implements EventProviderFactory {
return this._basicIdentity ? this._basicIdentity.token : undefined;
}
- // Checking (manually) if token is invalid by verifying expiry date.
- // By default, using a margin / minimum validity of 2 seconds
- public isTokenExpired(margin = 2000): boolean {
- if(this.isKeycloak()) {
- return (this._keycloak?.tokenParsed?.exp! <= moment().add(margin, "milliseconds").unix());
- } else {
- return !this._basicIdentity?.token; // TODO: Update this to check validity of JWT token manually
- }
- }
-
public getRealm(): string | undefined {
if (this._config) {
return this._config.realm;
@@ -985,107 +864,85 @@ export class Manager implements EventProviderFactory {
}
protected _onAuthenticated() {
- // If native shell is enabled, we need an offline refresh token
- if (this.console && this.console.isMobile && this.config.auth === Auth.KEYCLOAK) {
+ this._authenticated = true;
- if (this._keycloak && this._keycloak.refreshTokenParsed?.typ === "Offline") {
- console.debug("Storing offline refresh token");
- this.console.storeData("REFRESH_TOKEN", this._keycloak!.refreshToken);
- } else {
- this.login();
- }
+ // TODO: Move events init logic once websocket supports anonymous connections
+ if (!this._events) {
+ this.doEventsSubscriptionInit();
}
}
- // NOTE: The below works with Keycloak 2.x JS API - They made breaking changes in newer versions
- // so this will need updating.
protected async loadAndInitialiseKeycloak(): Promise<boolean> {
-
try {
// Initialise keycloak
this._keycloak = new Keycloak({
clientId: this._config.clientId,
realm: this._config.realm,
- url: this._config.keycloakUrl,
-
+ url: this._config.keycloakUrl
});
- this._keycloak!.onAuthSuccess = () => {
- // clear auth reconnect timer after success
- this._finishAuthReconnectTimer(true);
- };
-
- this._keycloak!.onAuthError = () => {
- this._setAuthenticated(false);
- };
-
- this._keycloak!.onAuthRefreshError = () => {
- console.debug("Keycloak failed to refresh the access token.");
- this._setAuthDisconnected(true);
- }
-
// Try to use a stored offline refresh token if defined
const offlineToken = await this._getNativeOfflineRefreshToken();
- const authenticated = await this._keycloak!.init({
+ // Cannot inject offlineToken here as this adapter is designed for interactive login and even check-sso
+ // does a redirect to keycloak but we can inject the offline token as shown and then update the access token
+ let authenticated = await this._keycloak!.init({
checkLoginIframe: false, // Doesn't work well with offline tokens or periodic token updates
- onLoad: this._config.autoLogin ? "login-required" : "check-sso",
- refreshToken: offlineToken
+ onLoad: "check-sso"
});
- if (authenticated) {
+ if (!authenticated && offlineToken) {
+ this._keycloak.refreshToken = offlineToken;
+ authenticated = await this._updateKeycloakAccessToken();
+ }
+ if (authenticated) {
this._name = this._keycloak.tokenParsed?.name;
this._username = this._keycloak.tokenParsed?.preferred_username;
- // Update the access token every 10s (note keycloak will only update if expiring within configured
- // time period.
- if (this._keycloakUpdateTokenInterval) {
- clearInterval(this._keycloakUpdateTokenInterval);
- delete this._keycloakUpdateTokenInterval;
+ this._createTokenUpdateInterval();
+
+ // If native shell is enabled store offline token
+ if (this.isMobile() && this._keycloak?.refreshTokenParsed?.typ === "Offline") {
+ console.debug("Storing offline refresh token");
+ this.console.storeData("REFRESH_TOKEN", this._keycloak!.refreshToken);
}
- this._keycloakUpdateTokenInterval = window.setInterval(() => {
- // only try to update token when online, otherwise the reconnect logic (this._attemptReconnect()) will try this
- if(!this._authDisconnected) {
- this.updateKeycloakAccessToken().catch(() => {
- console.error("Could not update keycloak access token during regular interval.");
- });
- }
- }, 10000);
this._onAuthenticated();
+ } else if (this.config.autoLogin) {
+ this.login();
+ return false;
}
- this._setAuthenticated(authenticated);
return true;
} catch (error) {
- this._setAuthenticated(false);
+ this._authenticated = false;
this._setError(ORError.AUTH_FAILED);
console.error("Failed to initialise Keycloak: " + error);
return false;
}
}
- protected async updateKeycloakAccessToken(): Promise<boolean | void> {
-
- // Access token must be good for X more seconds, should be half of Constants.ACCESS_TOKEN_LIFESPAN_SECONDS
- // TODO: Improve this, see https://github.com/openremote/openremote/issues/1233
- const promise = this._keycloak!.updateToken(30) as Promise<boolean>;
- promise.then(tokenRefreshed => {
- console.debug("Access token update success, refreshed from server: " + tokenRefreshed);
- }).catch(() => {
- console.error("Access token update failed.");
- this._setAuthDisconnected(true);
- });
+ protected _createTokenUpdateInterval() {
+ if (!this._keycloakUpdateTokenInterval) {
+ this._keycloakUpdateTokenInterval = window.setInterval(async () => {
+ await this._updateKeycloakAccessToken().catch(() => {
+ console.debug("Keycloak failed to refresh the access token");
+ this._onDisconnect();
+ });
+ }, 10000);
+ }
+ }
- // Using Promise.race() with a timeout of 15 seconds
- // Since the timeout will never resolve, we assume only a boolean can be returned.
- let timer: NodeJS.Timeout;
- return Promise.race(
- [promise, new Promise((_r, rej) => timer = setTimeout(rej, 15000))]
- ).finally(() => clearTimeout(timer)) as Promise<boolean | void>;
+ protected async _updateKeycloakAccessToken(): Promise<boolean> {
+ const tokenRefreshed = await this._keycloak!.updateToken(20);
+ console.debug("Access token update success, refreshed from server: " + tokenRefreshed);
+ if (tokenRefreshed) {
+ this._onAuthenticated();
+ }
+ return tokenRefreshed;
}
protected async _getNativeOfflineRefreshToken(): Promise<string | undefined> {
- if (this.console && this.console.isMobile) {
+ if (this.isMobile()) {
return await this.console.retrieveData("REFRESH_TOKEN");
}
}
@@ -1102,79 +959,134 @@ export class Manager implements EventProviderFactory {
protected _setError(error: ORError) {
this._error = error;
this._emitEvent(OREvent.ERROR);
- console.log("Error set: " + error);
+ console.warn("Error set: " + error);
}
- // TODO: Remove events logic once websocket supports anonymous connections
- protected _setAuthenticated(authenticated: boolean) {
- this._authenticated = authenticated;
+ /** Function that clears the `WebView` history of a console. It will not delete the history on regular browsers. */
+ protected _clearWebHistory(): void {
+ this.console?._doSendGenericMessage("CLEAR_WEB_HISTORY", undefined);
+ }
- if (this._events) {
- this._events.disconnect();
+ /**
+ * Disconnect can occur when events status changes to offline and/or keycloak token refresh fails
+ * we just try to reach keycloak and then get a new token as well as wait for the events status to change
+ */
+ protected async _onDisconnect() {
+ if (this._disconnected) {
+ return;
}
- if (!this._events) {
- this.doEventsSubscriptionInit();
+ console.debug("Disconnected");
+ this._disconnected = true;
+
+ // Cancel token refresh timer
+ if (this._keycloakUpdateTokenInterval) {
+ window.clearTimeout(this._keycloakUpdateTokenInterval);
+ this._keycloakUpdateTokenInterval = undefined;
}
+
+ this._emitEvent(OREvent.OFFLINE);
+ this.reconnect();
}
- // When authentication service status changes; in most cases a failed token refresh.
- // Always go OFFLINE if 'failed', only go back ONLINE when all EventProviders are also connected.
- protected _setAuthDisconnected(disconnected: boolean, force = false) {
- if(this._authDisconnected !== disconnected || force) {
- console.debug(`Authentication service status changed: ${disconnected ? 'DISCONNECTED' : 'CONNECTED'}`);
- if(disconnected) {
- this._authDisconnected = true;
- this._runAuthReconnectTimer();
- this._emitEvent(OREvent.OFFLINE);
- } else {
- this._authDisconnected = false;
- if(this._eventsDisconnected) {
- this._tryReconnectEvents();
- } else {
- this._emitEvent(OREvent.ONLINE);
- }
- }
- }
+ protected _onReconnected() {
+ console.debug("Reconnected");
+ this._disconnected = false;
+
+ // Reinstate token update interval
+ this._createTokenUpdateInterval();
+ this._emitEvent(OREvent.ONLINE);
}
- // When EventProvider (such as WebSocket) connection status changes.
- // Always go OFFLINE if disconnected, only go back ONLINE when authentication is also connected.
- protected _setEventDisconnected(disconnected: boolean, force = false) {
- if(this._eventsDisconnected !== disconnected || force) {
- console.debug(`EventProvider status changed: ${disconnected ? 'DISCONNECTED' : 'CONNECTED'}`);
- if(disconnected) {
- this._eventsDisconnected = true;
- this._emitEvent(OREvent.OFFLINE);
-
- // WebSocket might not connect because of token expiry, so we try updating it immediately.
- // Since token expiry is unrelated to ONLINE/OFFLINE state, a successful token update would change _authDisconnected from FALSE to FALSE.
- // So, if the token update is successful, we force update _authDisconnected.
- if(this.isTokenExpired() && !this._authDisconnected) {
- this._tryUpdateAccessToken().then((offline) => {
- if(!offline) {
- this._setAuthDisconnected(false, true);
- }
- });
- }
+ /**
+ * Checks keycloak is available and token is valid otherwise will redirect to login; also checks if event bus is
+ * online.
+ */
+ public async reconnect(reattemptDelayMillis: number = 3000) {
- } else {
- // When EventProvider is back online, check if authentication is disconnected and try to reauthenticate if necessary.
- // Only emit the ONLINE event when both are connected.
- this._eventsDisconnected = false;
- if(this._authDisconnected) {
- this._runAuthReconnectTimer()
- } else {
- this._emitEvent(OREvent.ONLINE);
+ if (!this._disconnected) {
+ return;
+ }
+
+ if (this._reconnectTimer) {
+ window.clearTimeout(this._reconnectTimer);
+ this._reconnectTimer = undefined;
+ }
+
+ const tryReconnect = async () => {
+ console.debug("Attempting reconnect");
+ let keycloakOffline = !await this.isKeycloakReachable();
+
+ if (keycloakOffline) {
+ console.debug("Keycloak is unreachable");
+ return false;
+ }
+ console.debug("Keycloak is reachable");
+
+ // Check if access token can be refreshed
+ console.debug("Checking keycloak access token");
+ try {
+ await this._updateKeycloakAccessToken();
+ } catch (e) {
+ // Try and use offline token if it is available
+ const offlineToken = await this._getNativeOfflineRefreshToken();
+ this._keycloak!.refreshToken = offlineToken;
+
+ try {
+ await this._updateKeycloakAccessToken();
+ } catch (e) {
+ console.debug("Cannot update access token so sending to login");
+ this.login();
+ return;
}
+ console.debug("Keycloak access token is valid");
+ return true;
}
+
+ // Check events
+ const eventsOffline = this.events && this.events.status === EventProviderStatus.CONNECTING;
+ console.debug("If event provider offline then attempting reconnect: offline=" + eventsOffline);
+ // Force reconnect attempt now if needed
+ return !eventsOffline || await this.events?.connect();
+ };
+
+ const connected = await tryReconnect();
+
+ if (connected === undefined) {
+ // Going back to keycloak login so nothing to do
+ return;
}
- }
- /** Function that clears the `WebView` history of a console. It will not delete the history on regular browsers. */
- protected _clearWebHistory(): void {
- this.console?._doSendGenericMessage("CLEAR_WEB_HISTORY", undefined);
+ if (!connected) {
+ // Schedule reconnect again
+ reattemptDelayMillis = Math.min(Manager.MAX_RECONNECT_DELAY, reattemptDelayMillis + 3000);
+ console.debug("Scheduling another reconnect attempt in (ms): " + reattemptDelayMillis);
+ this._reconnectTimer = window.setTimeout(() => this.reconnect(reattemptDelayMillis), reattemptDelayMillis);
+ return;
+ }
+
+ this._onReconnected();
}
+ // Checks whether keycloak is reachable using a simple HTTP HEAD request since the keycloak JS adapter doesn't give
+ // us details of the HTTP responses they get, we test it manually using this.
+ protected async isKeycloakReachable(timeoutMillis: number = 2000) {
+ const controller = new AbortController();
+ const timeout = setTimeout(() => controller.abort(), timeoutMillis);
+ try {
+ // Get the token URL from keycloak as this must already have CORS configured for keycloak to work
+ // Make an OPTIONS request as GET/HEAD requests are not allowed on the token endpoint
+ // Note if keycloak service is unavailable the proxy may respond with 503 and no CORS headers
+ // but this is handled by the catch block
+ // @ts-ignore
+ const tokenUrl = this._keycloak.endpoints.token();
+ const result = await fetch(tokenUrl, {method: 'OPTIONS', signal: controller.signal});
+ return result.status === 200;
+ } catch (e) {
+ return false;
+ } finally {
+ clearTimeout(timeout);
+ }
+ }
}
export const manager = new Manager(); // Needed for webpack bundling
diff --git a/ui/component/or-app/src/index.ts b/ui/component/or-app/src/index.ts
index b3bee39e4..32d037780 100644
--- a/ui/component/or-app/src/index.ts
+++ b/ui/component/or-app/src/index.ts
@@ -82,8 +82,8 @@ export class OrApp<S extends AppStateKeyed> extends LitElement {
@state()
protected _activeMenu?: string;
- protected _onEventBind?: any;
- protected _onVisibilityBind?: any;
+ protected _onEvent = (ev: OREvent) => this._handleEvent(ev);
+ protected _onVisibilityChanged = (ev: Event) => this._handleVisibilityChange(ev);
protected _realms!: Realm[];
protected _offlineFallbackDeferred?: Util.Deferred<void>;
protected _store: Store<S, AnyAction>;
@@ -158,30 +158,35 @@ export class OrApp<S extends AppStateKeyed> extends LitElement {
// Using HTML 'visibilitychange' listener to see whether the Manager is visible for the user.
// TODO; Add an ConsoleProvider that listens to background/foreground changes, and dispatch the respective OREvent. This will improve responsiveness of logic attached to it.
// For example used for triggering reconnecting logic once the UI becomes visible again.
- protected onVisibilityChange(ev: Event) {
+ protected _handleVisibilityChange(ev: Event) {
+
if(document.visibilityState === "visible") {
- this._onEvent(OREvent.CONSOLE_VISIBLE);
+ this._store.dispatch((setVisibility(true)));
+
+ // When the manager appears on Mobile devices, but the connection is OFFLINE,
+ // we reset the timer to the {appConfig.offlineTimeout} seconds. This is because we saw issues with reopening the app,
+ // and seeing a connection interval of 30+ seconds. We now give the user the benefit of the doubt, by resetting the timer.
+ if(manager.console?.isMobile && this._offline) {
+ this._startOfflineFallbackTimer(true);
+ }
+ // Always try reconnecting (just in case we are disconnected)
+ manager.reconnect();
} else {
- this._onEvent(OREvent.CONSOLE_HIDDEN);
+ this._store.dispatch((setVisibility(false)));
}
}
connectedCallback() {
super.connectedCallback();
this._storeUnsubscribe = this._store.subscribe(() => this.stateChanged(this.getState()));
- this._onVisibilityBind = this.onVisibilityChange.bind(this);
- document.addEventListener("visibilitychange", this._onVisibilityBind);
+ document.addEventListener("visibilitychange", this._onVisibilityChanged);
this.stateChanged(this.getState());
}
disconnectedCallback() {
this._storeUnsubscribe();
- if(this._onVisibilityBind) {
- document.removeEventListener("visibilityChange", this._onVisibilityBind);
- }
- if(this._onEventBind) {
- manager.removeListener(this._onEventBind);
- }
+ document.removeEventListener("visibilityChange", this._onVisibilityChanged);
+ manager.removeListener(this._onEvent);
super.disconnectedCallback();
}
@@ -257,8 +262,7 @@ export class OrApp<S extends AppStateKeyed> extends LitElement {
this._initialised = true;
// Register listener to change global state based on certain events
- this._onEventBind = this._onEvent.bind(this);
- manager.addListener(this._onEventBind);
+ manager.addListener(this._onEvent);
// Create route listener to set header active item (this must be done before any routes added)
const headerUpdater = (activeMenu: string | undefined) => {
@@ -420,34 +424,18 @@ export class OrApp<S extends AppStateKeyed> extends LitElement {
this._offline = state.app!.offline;
}
- protected _onEvent(event: OREvent) {
+ protected _handleEvent(event: OREvent) {
if(event === OREvent.OFFLINE) {
if(!this._offline) {
this._store.dispatch((setOffline(true)))
}
+ this._startOfflineFallbackTimer(); // start fallback timer (if not done yet)
} else if(event === OREvent.ONLINE) {
if(this._offline) {
this._showOfflineFallback = false;
this._completeOfflineFallbackTimer(); // complete fallback timer
this._store.dispatch((setOffline(false)));
}
- } else if(event === OREvent.RECONNECT_FAILED) {
- this._startOfflineFallbackTimer(); // start fallback timer (if not done yet)
-
- } else if(event === OREvent.CONSOLE_VISIBLE) {
- this._store.dispatch((setVisibility(true)));
-
- // When the manager appears on Mobile devices, but the connection is OFFLINE,
- // we reset the timer to the {appConfig.offlineTimeout} seconds. This is because we saw issues with reopening the app,
- // and seeing a connection interval of 30+ seconds. We now give the user the benefit of the doubt, by resetting the timer.
- if(manager.console?.isMobile && this._offline) {
- this._startOfflineFallbackTimer(true);
- }
- // Always try reconnecting (if necessary)
- manager.reconnect(true);
-
- } else if(event === OREvent.CONSOLE_HIDDEN) {
- this._store.dispatch((setVisibility(false)));
}
}
diff --git a/yarn.lock b/yarn.lock
index 1f0b1b6f5..3de988fcb 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -2315,7 +2315,7 @@ __metadata:
"@types/qs": "npm:^6.9.7"
i18next: "npm:^21.5.3"
i18next-http-backend: "npm:^1.3.1"
- keycloak-js: "npm:^23.0.6"
+ keycloak-js: "npm:^25.0.1"
moment: "npm:^2.29.4"
platform: "npm:^1.3.6"
qs: "npm:^6.8.0"
@@ -4030,13 +4030,6 @@ __metadata:
languageName: node
linkType: hard
-"base64-js@npm:^1.5.1":
- version: 1.5.1
- resolution: "base64-js@npm:1.5.1"
- checksum: 10c0/f23823513b63173a001030fae4f2dabe283b99a9d324ade3ad3d148e218134676f1ee8568c877cd79ec1c53158dcf2d2ba527a97c606618928ba99dd930102bf
- languageName: node
- linkType: hard
-
"batch@npm:0.6.1":
version: 0.6.1
resolution: "batch@npm:0.6.1"
@@ -6731,10 +6724,10 @@ __metadata:
languageName: node
linkType: hard
-"js-sha256@npm:^0.10.1":
- version: 0.10.1
- resolution: "js-sha256@npm:0.10.1"
- checksum: 10c0/c63119f7c7f8afc24bfa24c1a6b51147c3b562316b6341a375a1cef88569340ec0dbf2cda429ecf472cabfbae47a0b93a0cb82b8730883de066593c3f816c53b
+"js-sha256@npm:^0.11.0":
+ version: 0.11.0
+ resolution: "js-sha256@npm:0.11.0"
+ checksum: 10c0/90980fe01ca01fbd166751fb16c4caa09c1ab997e8bf77c0764cc05c772c6044946f4c1b3bad266ce78357280d2131d3dc0cf2dd7646e78272996bd4d590aa4f
languageName: node
linkType: hard
@@ -6870,14 +6863,13 @@ __metadata:
languageName: node
linkType: hard
-"keycloak-js@npm:^23.0.6":
- version: 23.0.6
- resolution: "keycloak-js@npm:23.0.6"
+"keycloak-js@npm:^25.0.1":
+ version: 25.0.1
+ resolution: "keycloak-js@npm:25.0.1"
dependencies:
- base64-js: "npm:^1.5.1"
- js-sha256: "npm:^0.10.1"
+ js-sha256: "npm:^0.11.0"
jwt-decode: "npm:^4.0.0"
- checksum: 10c0/2201d7c8cbd9bf556b212895afc1bf68fa4fed47e8f1ac64f96dbfff56e29d182e89d6729f5097ee41910ea8e6284192735483cd274deeb2da7e41d7164c4af1
+ checksum: 10c0/891f803e17b8e3759e1e945ad7e8589ddfed754d3b33d2f09e28099efef2638d2e2a42c502b0b87fd1b22198eca3e40b2a3c53f157739022b39533028ab48a5f
languageName: node
linkType: hard