2018-06-19 21:59:50 +00:00
|
|
|
if (typeof Oidc == "undefined") {
|
|
|
|
throw new Error("You are missing the `oidc-client-js` lib");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Var declarations
|
|
|
|
var sib = {};
|
|
|
|
sib.oidc = {};
|
|
|
|
|
|
|
|
|
|
|
|
// Setup User Manager
|
|
|
|
sib.oidc._setup = function(settings) {
|
|
|
|
if (typeof sib.oidc._manager == "undefined") {
|
|
|
|
sib.oidc._manager = new Oidc.UserManager(settings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-21 07:32:07 +00:00
|
|
|
// Retrieve User from sessionStorage, or fetch it
|
|
|
|
sib.oidc._getUser = function() {
|
|
|
|
sib.oidc._manager.getUser()
|
|
|
|
.then(function(user) {
|
|
|
|
if (user == null) {
|
|
|
|
sib.oidc._manager.signinRedirectCallback()
|
|
|
|
.then()
|
|
|
|
.catch(function(error) {
|
|
|
|
sib.oidc._manager.signinRedirect({state: "dummy data"})
|
|
|
|
.then()
|
|
|
|
.catch(function(error) {
|
|
|
|
console.log(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sib.oidc.user = user;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(function(error) {
|
|
|
|
console.log(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-19 21:59:50 +00:00
|
|
|
// Connect to OIDC provider
|
2018-06-21 07:32:07 +00:00
|
|
|
sib.oidc._connect = function(settings) {
|
2018-06-19 21:59:50 +00:00
|
|
|
sib.oidc._setup(settings);
|
2018-06-21 07:32:07 +00:00
|
|
|
sib.oidc._getUser();
|
2018-06-19 21:59:50 +00:00
|
|
|
|
|
|
|
|
2018-06-21 07:32:07 +00:00
|
|
|
// When user loaded...
|
|
|
|
sib.oidc._manager.events.addUserLoaded(function(user) {
|
|
|
|
sib.oidc._getUser();
|
2018-06-19 21:59:50 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Entry point
|
|
|
|
fetch("./oidc-client-config.json")
|
|
|
|
.then(response => response.json())
|
2018-06-21 07:32:07 +00:00
|
|
|
.then(settings => sib.oidc._connect(settings));
|
2018-06-19 21:59:50 +00:00
|
|
|
|