UNPKG

@geogirafe/lib-geoportal

Version:

GeoGirafe is a flexible application to build online geoportals.

90 lines (89 loc) 3.33 kB
export default class AbstractConnectManager { storagePath = 'oAuth'; context; constructor(context) { this.context = context; } get state() { return this.context.stateManager.state; } loggedOutFromBackend() { this.context.errorManager.pushMessage('You were logged out', 'Your identity is not recognized any more by the backend. Please try to login again, or contact an administrator.', 'warning'); this.loggedOut(); } handleErrorFromIssuer() { this.context.errorManager.pushMessage('Login Failed', this.state.oauth.error ?? 'Unknown error', 'error'); this.loggedOut(); } handleUnknownError(error) { this.state.oauth.error = error.message; this.state.oauth.status = 'loginFailed'; } loggedOut() { this.context.sessionManager.saveStateToSession(); this.state.oauth.status = 'loggedOut'; this.state.oauth.tokens = undefined; this.state.oauth.userInfo = undefined; this.state.oauth.audience = []; } /** * The RedirectUrl has to be the same for all call to the issuer * Because the authentication is given for a specific redirectUrl. * Therefore, we keep it in localStorage. */ get redirectUrl() { return this.loadFromLocalStorage('redirectUrl'); } set redirectUrl(value) { this.saveToLocalStorage('redirectUrl', value); } getLoginRedirectUrl(silent) { let url; if (silent) { url = new URL(this.context.urlManager.getRootUrl()); url.pathname += 'silentlogincallback.html'; } else { url = new URL(this.context.urlManager.getBaseUrlPath()); } url.searchParams.append('authentified', 'true'); url.hash = this.context.urlManager.getHash() ?? ''; return url.toString(); } getLogoutRedirectUrl() { const url = new URL(this.context.urlManager.getBaseUrlPath()); url.searchParams.append('authentified', 'false'); url.hash = this.context.urlManager.getHash() ?? ''; return url.toString(); } loadFromLocalStorage(path) { return this.context.userDataManager.getUserData(`${this.storagePath}.${path}`, true) ?? ''; } saveToLocalStorage(path, value) { return this.context.userDataManager.saveUserData(`${this.storagePath}.${path}`, value, true); } resetUrl() { const url = new URL(this.context.urlManager.getBaseUrlPath()); url.hash = this.context.urlManager.getHash() ?? ''; this.context.urlManager.updateUrl(url); } finalizeLoginWorkflow() { switch (this.state.oauth.status) { case 'loggedIn': this.resetUrl(); break; case 'loggedOut': this.resetUrl(); break; case 'not-initialized': case 'backend.loggedOut': case 'issuer.loggedIn': case 'loginFailed': case 'logoutFailed': throw new Error('We should only finalize the login workflow when the user is either loggedIn or loggedOut'); default: throw new Error('Unmanaged login state'); } this.state.application.isAuthInitialized = true; } }