UNPKG

@dunite/check-access

Version:

Handles authentication for the corresponding AuthMiddle-nuget

113 lines (91 loc) 3.82 kB
import { bindable, inject, LogManager, NewInstance } from "aurelia-framework"; import { HttpClient } from "aurelia-fetch-client"; import { EventAggregator } from "aurelia-event-aggregator"; import { MessageBar } from "./message-bar"; @inject(EventAggregator,"CheckAccessHttpClient") export class CheckAccess { @bindable public ProjectId: string; @bindable public CheckAccessUrl: string; private log: any = LogManager.getLogger('dualite'); private messageBar: MessageBar; private eventAggregator: EventAggregator; private httpClient: HttpClient; public cacheKey: string = "NoCacheKey"; public src = ""; constructor(eventAggregator: EventAggregator, httpClient:HttpClient) { if(typeof httpClient == 'string') { throw Error("httpClient is null in CheckAccess. Make sure the plugin is initiated. If under test call aurelia.container.autoRegister('CheckAccessHttpClient', with proper mock."); } this.httpClient = httpClient; this.eventAggregator = eventAggregator; this.log.info("CheckAccess constructor"); window.addEventListener("message",this.messageListener); } public messageListener: EventListenerOrEventListenerObject = (event: MessageEvent) => { this.log.info("in message from Host. origin=" + event.origin + " appdomain=" + this.CheckAccessUrl); if ((this.CheckAccessUrl.indexOf(event.origin) >= 0)) { this.log.info("origin ok, message:" + JSON.stringify(event.data)); if (event.data.status === "OK") { try { window.localStorage.setItem("cacheKey" + this.ProjectId, event.data.cacheKey); this.messageBar.show("Du är nu inloggad", "", "info"); setTimeout(() => { console.log(this); this.messageBar.hide(); }, 5000); this.PublishUserIsLoggedIn(); } catch (error) { this.log.info("Localstorage could not be used"); } window.removeEventListener("message", this.messageListener, false); } else { this.messageBar.show("Det gick inte att logga in. Kontakta administratör.", "", "error"); } } } public showAccessDenied(url: string) { this.log.info("in showAccessDenied"); this.messageBar.show("Du loggas nu in mot Dualite", "Strax klart", "info"); this.src = url; } public async attached() { this.log.info("check-access attached"); this.cacheKey = window.localStorage.getItem("cacheKey" + this.ProjectId); this.log.info("cacheKey from storage: " + this.cacheKey); this.httpClient.configure(config => { config .withBaseUrl(this.CheckAccessUrl) .withDefaults({ headers: { "Accept": "application/json", "CacheKey": this.cacheKey == null ? "" : this.cacheKey, "ProjectID": this.ProjectId } }); }); try { var response = await this.httpClient.post("api/token"); this.log.info("fetch('response')" + JSON.stringify(response)); if (response.ok) { var data = await response.json(); if(data.ErrorMessage != null || data.ErrorMessage === "") { this.log.info("checkaccess failed, will redirect"); this.showAccessDenied(data.RedirectUrl); } else { this.log.info("fetch('checkaccess succeed')"); this.PublishUserIsLoggedIn(); } } else { this.log.info("checkaccess failed, can not redirect"); } } catch (error) { this.log.info("fetch('checkaccess failed')" + JSON.stringify(error)); this.messageBar.show("Det går inte att logga in. Kontakta administratör.", "Får inte kontakt svar på servern.", "error"); } } public PublishUserIsLoggedIn(): void { this.eventAggregator.publish("UserIsLoggedIn", { cacheKey: this.cacheKey }); } }