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:
parent
c944793036
commit
d09c51cc71
|
@ -7,11 +7,7 @@ window.customElements.define('d4s-boot-2', class extends HTMLElement {
|
|||
#clientId = null
|
||||
#redirectUrl = null
|
||||
#audience = null
|
||||
|
||||
// loading attempts nr and timer between attempts
|
||||
//#attempts = 6
|
||||
//#timer = 500
|
||||
|
||||
#authenticated = false
|
||||
#locked = true
|
||||
#queue = []
|
||||
#interval = null
|
||||
|
@ -24,6 +20,11 @@ window.customElements.define('d4s-boot-2', class extends HTMLElement {
|
|||
this.#locked = false
|
||||
}
|
||||
|
||||
fire(etype){
|
||||
const evt = new CustomEvent(etype, { detail : ''})
|
||||
document.dispatchEvent(evt)
|
||||
}
|
||||
|
||||
connectedCallback(){
|
||||
|
||||
this.startStateChecker()
|
||||
|
@ -38,13 +39,24 @@ window.customElements.define('d4s-boot-2', class extends HTMLElement {
|
|||
if(!authenticated) throw "Failed to authenticate";
|
||||
|
||||
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(()=>{
|
||||
|
||||
this.#authorization = new KeycloakAuthorization(this.#keycloak)
|
||||
console.log("Keycloak authorization loaded and initialized", this.#authorization)
|
||||
this.unlock()
|
||||
|
||||
this.#authenticated = true
|
||||
this.unlock()
|
||||
this.fire("authenticated")
|
||||
|
||||
}).catch(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(()=>{
|
||||
if(this.#locked){
|
||||
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){
|
||||
const audience = encodeURIComponent(this.#audience)
|
||||
console.log("Updating token")
|
||||
this.#keycloak.updateToken(30).then(()=>{
|
||||
console.log("Checking entitlement")
|
||||
return this.#authorization.entitlement(audience)
|
||||
if(this.#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(
|
||||
rpt => {
|
||||
token => {
|
||||
console.log("Authorized")
|
||||
//transform all queued requests to fetches
|
||||
console.log("All pending requests to promises")
|
||||
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) )
|
||||
})
|
||||
//clear queue
|
||||
|
@ -216,6 +233,10 @@ window.customElements.define('d4s-boot-2', class extends HTMLElement {
|
|||
}
|
||||
}
|
||||
|
||||
get authenticated(){
|
||||
return this.#authenticated
|
||||
}
|
||||
|
||||
get url() {
|
||||
return this.#url
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue