Skip to content
Snippets Groups Projects
Commit c1d992e1 authored by Reiter, Christoph's avatar Reiter, Christoph :snake:
Browse files

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.
parent 7f166649
No related branches found
No related tags found
No related merge requests found
...@@ -4,9 +4,20 @@ import {unsafeHTML} from 'lit-html/directives/unsafe-html.js'; ...@@ -4,9 +4,20 @@ import {unsafeHTML} from 'lit-html/directives/unsafe-html.js';
import JSONLD from 'vpu-common/jsonld' import JSONLD from 'vpu-common/jsonld'
import * as commonUtils from 'vpu-common/utils'; import * as commonUtils from 'vpu-common/utils';
import * as commonStyles from 'vpu-common/styles'; import * as commonStyles from 'vpu-common/styles';
import * as events from 'vpu-common/events.js';
import 'vpu-common/vpu-icon.js'; import 'vpu-common/vpu-icon.js';
import VPULitElement from 'vpu-common/vpu-lit-element'; 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 * Keycloak auth web component
* https://www.keycloak.org/docs/latest/securing_apps/index.html#_javascript_adapter * https://www.keycloak.org/docs/latest/securing_apps/index.html#_javascript_adapter
...@@ -36,6 +47,19 @@ class VPUAuth extends VPULitElement { ...@@ -36,6 +47,19 @@ class VPUAuth extends VPULitElement {
this.rememberLogin = false; this.rememberLogin = false;
this.person = null; 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 // Create the events
this.initEvent = new CustomEvent("vpu-auth-init", { "detail": "KeyCloak init event", bubbles: true, composed: true }); 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 }); 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 { ...@@ -45,6 +69,13 @@ class VPUAuth extends VPULitElement {
this.closeDropdown = this.closeDropdown.bind(this); 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 * See: https://lit-element.polymer-project.org/guide/properties#initialize
*/ */
...@@ -76,6 +107,8 @@ class VPUAuth extends VPULitElement { ...@@ -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 // 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)) { if (this.forceLogin || (href.search('[&#]state=') >= 0 && href.search('[&#]session_state=') >= 0)) {
this.loadKeycloak(); this.loadKeycloak();
} else {
this._setLoginStatus(LoginStatus.LOGGED_OUT);
} }
const that = this; const that = this;
...@@ -110,14 +143,13 @@ class VPUAuth extends VPULitElement { ...@@ -110,14 +143,13 @@ class VPUAuth extends VPULitElement {
loadKeycloak() { loadKeycloak() {
const that = this; const that = this;
console.log("loadKeycloak");
if (!this.keyCloakInitCalled) { if (!this.keyCloakInitCalled) {
// inject Keycloak javascript file // inject Keycloak javascript file
const script = document.createElement('script'); const script = document.createElement('script');
script.type = 'text/javascript'; script.type = 'text/javascript';
//script.async = true; //script.async = true;
script.onload = function () { script.onload = () => {
that.keyCloakInitCalled = true; that.keyCloakInitCalled = true;
that._keycloak = Keycloak({ that._keycloak = Keycloak({
...@@ -126,10 +158,11 @@ class VPUAuth extends VPULitElement { ...@@ -126,10 +158,11 @@ class VPUAuth extends VPULitElement {
clientId: that.clientId, clientId: that.clientId,
}); });
this._setLoginStatus(LoginStatus.LOGGING_IN);
// See: https://www.keycloak.org/docs/latest/securing_apps/index.html#_javascript_adapter // See: https://www.keycloak.org/docs/latest/securing_apps/index.html#_javascript_adapter
that._keycloak.init().success((authenticated) => { that._keycloak.init().success((authenticated) => {
console.log(authenticated ? 'authenticated' : 'not authenticated!');
console.log(that._keycloak);
if (!authenticated) { if (!authenticated) {
// set locale of Keycloak login page // set locale of Keycloak login page
...@@ -164,7 +197,8 @@ class VPUAuth extends VPULitElement { ...@@ -164,7 +197,8 @@ class VPUAuth extends VPULitElement {
}, {}, that.lang); }, {}, that.lang);
} }
}).error(function () { }).error(() => {
this._setLoginStatus(LoginStatus.LOGGED_OUT);
console.error('Keycloak failed to initialize!'); console.error('Keycloak failed to initialize!');
}); });
...@@ -196,6 +230,7 @@ class VPUAuth extends VPULitElement { ...@@ -196,6 +230,7 @@ class VPUAuth extends VPULitElement {
} }
logout(e) { logout(e) {
this._setLoginStatus(LoginStatus.LOGGING_OUT);
sessionStorage.removeItem('vpu-logged-in'); sessionStorage.removeItem('vpu-logged-in');
this._keycloak.logout(); this._keycloak.logout();
} }
...@@ -240,8 +275,9 @@ class VPUAuth extends VPULitElement { ...@@ -240,8 +275,9 @@ class VPUAuth extends VPULitElement {
window.VPUUserFullName = this.name; window.VPUUserFullName = this.name;
window.VPUPersonId = this.personId; window.VPUPersonId = this.personId;
console.log("Bearer " + this.token);
this.dispatchKeycloakDataUpdateEvent(); this.dispatchKeycloakDataUpdateEvent();
this._setLoginStatus(LoginStatus.LOGGED_IN, true);
} }
update(changedProperties) { update(changedProperties) {
......
common @ f5502b7a
Subproject commit 833dd224c7ecab1caf509d83f58211c2346e8b19 Subproject commit f5502b7a903ddd9bfbf966fafdae9cc3c4a2ed87
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment