Reformatting code. Replaced alerts. Removed download method.
This commit is contained in:
parent
1321181ad7
commit
c2421ec5ab
130
boot/d4s-boot.js
130
boot/d4s-boot.js
|
@ -1,3 +1,12 @@
|
||||||
|
/**
|
||||||
|
* D4Science boot component that handles keykloak authz
|
||||||
|
* <d4s-boot-2
|
||||||
|
* /@url: keycloak url, for example: https://accounts.d4science.org/auth]
|
||||||
|
* /@realm: is the realm [defaults to: d4science]
|
||||||
|
* /@gateway: is the client id, for example: "pre.d4science.org"
|
||||||
|
* /@context: is the audience, for example: "%2Fpred4s%2Fpreprod%2FpreVRE"
|
||||||
|
* /@redirect-url: where to go on logout
|
||||||
|
*/
|
||||||
window.customElements.define('d4s-boot-2', class extends HTMLElement {
|
window.customElements.define('d4s-boot-2', class extends HTMLElement {
|
||||||
|
|
||||||
#keycloak = null
|
#keycloak = null
|
||||||
|
@ -26,34 +35,28 @@ window.customElements.define('d4s-boot-2', class extends HTMLElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
|
|
||||||
this.startStateChecker()
|
this.startStateChecker()
|
||||||
|
|
||||||
this.loadKeycloak().then(() => {
|
this.loadKeycloak().then(() => {
|
||||||
|
|
||||||
console.log("Keycloak loaded")
|
console.log("Keycloak loaded")
|
||||||
return this.initKeycloak()
|
return this.initKeycloak()
|
||||||
|
|
||||||
}).then((authenticated) => {
|
}).then((authenticated) => {
|
||||||
|
if (!authenticated) {
|
||||||
if(!authenticated) throw "Failed to authenticate";
|
throw "Failed to authenticate"
|
||||||
|
}
|
||||||
console.log("Keycloak initialized and user authenticated")
|
console.log("Keycloak initialized and user authenticated")
|
||||||
|
|
||||||
//if an audience is provided then perform also authorization
|
//if an audience is provided then perform also authorization
|
||||||
if (this.#audience) {
|
if (this.#audience) {
|
||||||
return this.loadKeycloakAuthorization().then(
|
return this.loadKeycloakAuthorization().then(() => {
|
||||||
()=>{
|
|
||||||
this.#authorization = new KeycloakAuthorization(this.#keycloak)
|
this.#authorization = new KeycloakAuthorization(this.#keycloak)
|
||||||
console.log("Keycloak authorization loaded and initialized", this.#authorization)
|
console.log("Keycloak authorization loaded and initialized", this.#authorization)
|
||||||
}
|
})
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
}
|
}
|
||||||
|
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
|
|
||||||
this.#authenticated = true
|
this.#authenticated = true
|
||||||
this.unlock()
|
this.unlock()
|
||||||
this.fire("authenticated")
|
this.fire("authenticated")
|
||||||
|
@ -117,8 +120,8 @@ window.customElements.define('d4s-boot-2', class extends HTMLElement {
|
||||||
} else {
|
} else {
|
||||||
return Promise.resolve(this.#keycloak.token)
|
return Promise.resolve(this.#keycloak.token)
|
||||||
}
|
}
|
||||||
}).then(
|
|
||||||
token => {
|
}).then(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")
|
||||||
|
@ -130,8 +133,8 @@ window.customElements.define('d4s-boot-2', class extends HTMLElement {
|
||||||
this.#queue = []
|
this.#queue = []
|
||||||
console.log("Resolving all fetches")
|
console.log("Resolving all fetches")
|
||||||
return Promise.all(promises)
|
return Promise.all(promises)
|
||||||
}
|
|
||||||
).catch(err => alert("Unable to make calls: " + err))
|
}).catch(err => console.error("Unable to make calls: " + err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, 300)
|
}, 300)
|
||||||
|
@ -180,21 +183,6 @@ window.customElements.define('d4s-boot-2', class extends HTMLElement {
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
download(url, name) {
|
|
||||||
this.secureFetch(url).then(reply=>{
|
|
||||||
if(reply.status !== 200) throw "Unable to download";
|
|
||||||
return reply.blob()
|
|
||||||
}).then(blob=>{
|
|
||||||
const objectURL = URL.createObjectURL(blob)
|
|
||||||
var tmplnk = document.createElement("a")
|
|
||||||
tmplnk.download = name
|
|
||||||
tmplnk.href = objectURL
|
|
||||||
document.body.appendChild(tmplnk)
|
|
||||||
tmplnk.click()
|
|
||||||
document.body.removeChild(tmplnk)
|
|
||||||
}).catch(err=>alert(err))
|
|
||||||
}
|
|
||||||
|
|
||||||
logout() {
|
logout() {
|
||||||
if (this.#keycloak) {
|
if (this.#keycloak) {
|
||||||
if (!this.#redirectUrl) {
|
if (!this.#redirectUrl) {
|
||||||
|
@ -283,85 +271,3 @@ window.customElements.define('d4s-boot-2', class extends HTMLElement {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
/*connectedCallback() {
|
|
||||||
if (typeof Keycloak === 'undefined') {
|
|
||||||
const script = document.createElement('script')
|
|
||||||
script.src = this.#url + '/js/keycloak.js'
|
|
||||||
script.type = 'text/javascript'
|
|
||||||
script.addEventListener('load', () => {
|
|
||||||
this.initKeycloak()
|
|
||||||
})
|
|
||||||
document.head.appendChild(script)
|
|
||||||
} else {
|
|
||||||
this.initKeycloak()
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
|
|
||||||
/*initKeycloak() {
|
|
||||||
this.#keycloak = new Keycloak({
|
|
||||||
url: this.#url,
|
|
||||||
realm: this.#realm,
|
|
||||||
clientId: this.#clientId
|
|
||||||
})
|
|
||||||
|
|
||||||
const keycloak = this.#keycloak
|
|
||||||
const url = this.#url
|
|
||||||
|
|
||||||
keycloak.init({
|
|
||||||
onLoad: 'login-required'
|
|
||||||
, checkLoginIframe: false
|
|
||||||
|
|
||||||
}).then((authenticated) => {
|
|
||||||
console.log(authenticated ? 'Authenticated in ' + this.#realm : 'Not authenticated')
|
|
||||||
if (typeof KeycloakAuthorization === 'undefined') {
|
|
||||||
const authz = document.createElement('script')
|
|
||||||
authz.src = url + '/js/keycloak-authz.js'
|
|
||||||
authz.type = 'text/javascript'
|
|
||||||
authz.addEventListener('load', () => {
|
|
||||||
this.#authorization = new KeycloakAuthorization(keycloak)
|
|
||||||
})
|
|
||||||
document.head.appendChild(authz)
|
|
||||||
} else {
|
|
||||||
this.#authorization = new KeycloakAuthorization(keycloak)
|
|
||||||
}
|
|
||||||
//console.log("expires: " + this.expirationDate(keycloak.tokenParsed.exp))
|
|
||||||
|
|
||||||
}).catch(function(err) {
|
|
||||||
console.error("Failed to initialize d4science boot component")
|
|
||||||
|
|
||||||
})
|
|
||||||
}*/
|
|
||||||
|
|
||||||
/*service(endpoint, method, params, onSuccess, onForbidden) {
|
|
||||||
if (this.#authorization == null) {
|
|
||||||
if (this.#attempts-- > 0) {
|
|
||||||
setTimeout(() => this.service(endpoint, method, params, onSuccess, onForbidden), this.#timer)
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
console.error("Impossible to initialize D4Science authorization component")
|
|
||||||
throw "Fatal error"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.#keycloak.updateToken(30).then(() => {
|
|
||||||
const audience = encodeURIComponent(this.#audience)
|
|
||||||
this.#authorization.entitlement(audience).then(function (rpt) {
|
|
||||||
var req = new XMLHttpRequest()
|
|
||||||
req.open(method, endpoint, true)
|
|
||||||
req.setRequestHeader('Accept', 'application/json')
|
|
||||||
req.setRequestHeader('Authorization', 'Bearer ' + rpt)
|
|
||||||
|
|
||||||
req.onreadystatechange = function () {
|
|
||||||
if (req.readyState == 4) {
|
|
||||||
if (req.status == 200) {
|
|
||||||
onSuccess(req.response)
|
|
||||||
} else if (req.status == 403) {
|
|
||||||
onForbidden(req.statusText)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
req.send(params)
|
|
||||||
})
|
|
||||||
}).catch(function() {
|
|
||||||
onForbidden('Failed to refresh token')
|
|
||||||
})
|
|
||||||
}*/
|
|
||||||
|
|
Loading…
Reference in New Issue