From c1d992e13684037e30fef0baad08dd237539bb96 Mon Sep 17 00:00:00 2001 From: Christoph Reiter <reiter.christoph@gmail.com> Date: Mon, 18 Nov 2019 12:05:33 +0100 Subject: [PATCH] Add a new vpu-auth-update event which provides the login status and the token. This is in parallel to the existing events to avoid breaking things. The goal here is to have one event which contains all the auth related information to which everyone interessted can subscribe to. The vpu-auth-update-request event can be emitted to trigger an update. --- packages/auth/src/vpu-auth.js | 48 ++++++++++++++++++++++++++++++----- packages/auth/vendor/common | 2 +- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/packages/auth/src/vpu-auth.js b/packages/auth/src/vpu-auth.js index d3529b6f..f6e90090 100644 --- a/packages/auth/src/vpu-auth.js +++ b/packages/auth/src/vpu-auth.js @@ -4,9 +4,20 @@ import {unsafeHTML} from 'lit-html/directives/unsafe-html.js'; import JSONLD from 'vpu-common/jsonld' import * as commonUtils from 'vpu-common/utils'; import * as commonStyles from 'vpu-common/styles'; +import * as events from 'vpu-common/events.js'; import 'vpu-common/vpu-icon.js'; import VPULitElement from 'vpu-common/vpu-lit-element'; + +const LoginStatus = Object.freeze({ + UNKNOWN: 'unknown', + LOGGING_IN: 'logging-in', + LOGGED_IN: 'logged-in', + LOGGING_OUT: 'logging-out', + LOGGED_OUT: 'logged-out', +}); + + /** * Keycloak auth web component * https://www.keycloak.org/docs/latest/securing_apps/index.html#_javascript_adapter @@ -36,6 +47,19 @@ class VPUAuth extends VPULitElement { this.rememberLogin = false; this.person = null; + const _getLoginData = () => { + const message = { + status: this._loginStatus, + token: this.token, + }; + console.log('Login status: ' + this._loginStatus); + return message; + }; + + this._loginStatus = LoginStatus.UNKNOWN; + this._emitter = new events.EventEmitter('vpu-auth-update', 'vpu-auth-update-request'); + this._emitter.registerCallback(_getLoginData); + // Create the events this.initEvent = new CustomEvent("vpu-auth-init", { "detail": "KeyCloak init event", bubbles: true, composed: true }); this.personInitEvent = new CustomEvent("vpu-auth-person-init", { "detail": "KeyCloak person init event", bubbles: true, composed: true }); @@ -45,6 +69,13 @@ class VPUAuth extends VPULitElement { this.closeDropdown = this.closeDropdown.bind(this); } + _setLoginStatus(status, force) { + if (this._loginStatus === status && !force) + return; + this._loginStatus = status; + this._emitter.emit(); + } + /** * See: https://lit-element.polymer-project.org/guide/properties#initialize */ @@ -76,6 +107,8 @@ class VPUAuth extends VPULitElement { // load Keycloak if we want to force the login or if we were redirected from the Keycloak login page if (this.forceLogin || (href.search('[&#]state=') >= 0 && href.search('[&#]session_state=') >= 0)) { this.loadKeycloak(); + } else { + this._setLoginStatus(LoginStatus.LOGGED_OUT); } const that = this; @@ -110,14 +143,13 @@ class VPUAuth extends VPULitElement { loadKeycloak() { const that = this; - console.log("loadKeycloak"); if (!this.keyCloakInitCalled) { // inject Keycloak javascript file const script = document.createElement('script'); script.type = 'text/javascript'; //script.async = true; - script.onload = function () { + script.onload = () => { that.keyCloakInitCalled = true; that._keycloak = Keycloak({ @@ -126,10 +158,11 @@ class VPUAuth extends VPULitElement { clientId: that.clientId, }); + this._setLoginStatus(LoginStatus.LOGGING_IN); + // See: https://www.keycloak.org/docs/latest/securing_apps/index.html#_javascript_adapter that._keycloak.init().success((authenticated) => { - console.log(authenticated ? 'authenticated' : 'not authenticated!'); - console.log(that._keycloak); + if (!authenticated) { // set locale of Keycloak login page @@ -164,7 +197,8 @@ class VPUAuth extends VPULitElement { }, {}, that.lang); } - }).error(function () { + }).error(() => { + this._setLoginStatus(LoginStatus.LOGGED_OUT); console.error('Keycloak failed to initialize!'); }); @@ -196,6 +230,7 @@ class VPUAuth extends VPULitElement { } logout(e) { + this._setLoginStatus(LoginStatus.LOGGING_OUT); sessionStorage.removeItem('vpu-logged-in'); this._keycloak.logout(); } @@ -240,8 +275,9 @@ class VPUAuth extends VPULitElement { window.VPUUserFullName = this.name; window.VPUPersonId = this.personId; - console.log("Bearer " + this.token); this.dispatchKeycloakDataUpdateEvent(); + + this._setLoginStatus(LoginStatus.LOGGED_IN, true); } update(changedProperties) { diff --git a/packages/auth/vendor/common b/packages/auth/vendor/common index 833dd224..f5502b7a 160000 --- a/packages/auth/vendor/common +++ b/packages/auth/vendor/common @@ -1 +1 @@ -Subproject commit 833dd224c7ecab1caf509d83f58211c2346e8b19 +Subproject commit f5502b7a903ddd9bfbf966fafdae9cc3c4a2ed87 -- GitLab