Support for OIDC flow only, Raise authenticated event when auth procedure completes, added authenticated variable to check if boot has completed authentication procedure

This commit is contained in:
root 2022-04-15 13:05:33 +02:00
parent c944793036
commit d09c51cc71
1 changed files with 38 additions and 17 deletions

View File

@ -7,11 +7,7 @@ window.customElements.define('d4s-boot-2', class extends HTMLElement {
#clientId = null #clientId = null
#redirectUrl = null #redirectUrl = null
#audience = null #audience = null
#authenticated = false
// loading attempts nr and timer between attempts
//#attempts = 6
//#timer = 500
#locked = true #locked = true
#queue = [] #queue = []
#interval = null #interval = null
@ -24,6 +20,11 @@ window.customElements.define('d4s-boot-2', class extends HTMLElement {
this.#locked = false this.#locked = false
} }
fire(etype){
const evt = new CustomEvent(etype, { detail : ''})
document.dispatchEvent(evt)
}
connectedCallback(){ connectedCallback(){
this.startStateChecker() this.startStateChecker()
@ -38,13 +39,24 @@ window.customElements.define('d4s-boot-2', class extends HTMLElement {
if(!authenticated) throw "Failed to authenticate"; if(!authenticated) throw "Failed to authenticate";
console.log("Keycloak initialized and user authenticated") console.log("Keycloak initialized and user authenticated")
return this.loadKeycloakAuthorization()
//if an audience is provided then perform also authorization
if(this.#audience){
return this.loadKeycloakAuthorization().then(
()=>{
this.#authorization = new KeycloakAuthorization(this.#keycloak)
console.log("Keycloak authorization loaded and initialized", this.#authorization)
}
)
}else{
return Promise.resolve()
}
}).then(()=>{ }).then(()=>{
this.#authorization = new KeycloakAuthorization(this.#keycloak) this.#authenticated = true
console.log("Keycloak authorization loaded and initialized", this.#authorization) this.unlock()
this.unlock() this.fire("authenticated")
}).catch(err=>{ }).catch(err=>{
console.error("Unable to initialize Keycloak",err) console.error("Unable to initialize Keycloak",err)
@ -93,20 +105,25 @@ window.customElements.define('d4s-boot-2', class extends HTMLElement {
this.#interval = window.setInterval(()=>{ this.#interval = window.setInterval(()=>{
if(this.#locked){ if(this.#locked){
console.log("Still locked. Currently has " + this.#queue.length + " pending requests.") console.log("Still locked. Currently has " + this.#queue.length + " pending requests.")
}else{ }else if (!this.authenticated){
window.alert("Not authorized!")
}else{
if(this.#queue.length > 0){ if(this.#queue.length > 0){
const audience = encodeURIComponent(this.#audience)
console.log("Updating token")
this.#keycloak.updateToken(30).then(()=>{ this.#keycloak.updateToken(30).then(()=>{
console.log("Checking entitlement") if(this.#audience){
return this.#authorization.entitlement(audience) console.log("Checking entitlement for audience", this.#audience)
const audience = encodeURIComponent(this.#audience)
return this.#authorization.entitlement(audience)
} else {
return Promise.resolve(this.#keycloak.token)
}
}).then( }).then(
rpt => { token => {
console.log("Authorized") console.log("Authorized")
//transform all queued requests to fetches //transform all queued requests to fetches
console.log("All pending requests to promises") console.log("All pending requests to promises")
let promises = this.#queue.map(r => { let promises = this.#queue.map(r => {
r.request.headers["Authorization"] = "Bearer " + rpt r.request.headers["Authorization"] = "Bearer " + token
return r.resolve( fetch(r.url, r.request) ) return r.resolve( fetch(r.url, r.request) )
}) })
//clear queue //clear queue
@ -216,6 +233,10 @@ window.customElements.define('d4s-boot-2', class extends HTMLElement {
} }
} }
get authenticated(){
return this.#authenticated
}
get url() { get url() {
return this.#url return this.#url
} }