Commit 8f65a140 authored by Nicolas Mérigot's avatar Nicolas Mérigot

Merge branch 'refactor' into 'master'

Refactor

See merge request !1
parents b4525d7f 84ccf4a7
......@@ -5,20 +5,32 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>SIB-OIDC test</title>
<script src="/node_modules/oidc-client/dist/oidc-client.js"></script>
<script
<script src="./node_modules/oidc-client/dist/oidc-client.js"></script>
<script
src="sib-oidc.js"
data-authority="https://test-paris.happy-dev.fr/openid/"
data-client_id="833925"
data-response_type="id_token token"
data-scope="openid profile email"
data-automaticSilentRenew="true"
data-loadUserInfo="true"
></script>
</head>
<body>
<h1>sib-oidc</h1>
<button id="reset">clear storage and set new URL</button>
<button id="reload">reload</button>
<script>
reset.onclick = () => {
window.sessionStorage.clear();
window.localStorage.clear();
history.pushState({}, '', '.?time=' + new Date().toLocaleTimeString());
};
reload.onclick = () => location.reload();
</script>
<pre id="output"></pre>
<script>
sib.oidc.getUser().then(user => {
output.textContent = JSON.stringify(user, null, 2);
});
</script>
</body>
</html>
\ No newline at end of file
</html>
if (typeof Oidc == "undefined") {
throw new Error("You are missing the `oidc-client-js` lib");
}
// Var declarations
var sib = {};
sib.oidc = {};
sib.oidc._applyBindUser = function() {
const id = sib.oidc.user.profile.website
const elements = document.querySelectorAll(`[bind-user]:not([data-src="${id}"])`)
for (const element of elements) {
element.setAttribute('data-src', id)
}
}
// Setup User Manager
sib.oidc._setup = function(settings) {
if (typeof sib.oidc._manager == "undefined") {
sib.oidc._manager = new Oidc.UserManager(settings);
}
}
// 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(message => {
location.href = message.state
})
.catch(function(error) {
sib.oidc._manager.signinRedirect({
state: location.href,
})
.then()
.catch(function(error) {
console.log(error);
});
});
}
else {
sib.oidc.user = user;
sib.oidc._applyBindUser();
}
})
.catch(function(error) {
console.log(error);
});
}
// Connect to OIDC provider
sib.oidc._connect = function(settings) {
sib.oidc._setup(settings);
window.addEventListener('DOMContentLoaded', () => sib.oidc._getUser());
// When user loaded...
sib.oidc._manager.events.addUserLoaded(function(user) {
sib.oidc._getUser();
{
const defaultSettings = {
redirect_uri: location.origin,
// authority: 'https://test-paris.happy-dev.fr/openid/',
// client_id: '833925',
response_type: 'id_token token',
scope: 'openid profile email',
automaticsilentrenew: 'true',
loaduserinfo: 'true',
};
const settings = Object.assign(
{},
defaultSettings,
document.currentScript.dataset,
);
const manager = new Oidc.UserManager(settings);
const promise = tryToGetUserOrConnect();
const sib = {
oidc: {
getUser: () => promise,
user: null,
},
};
window.sib = sib;
sib.oidc.getUser().then(user => {
sib.oidc.user = user;
});
}
document.addEventListener('DOMContentLoaded', async () => {
const user = await sib.oidc.getUser();
const id = user.profile.website;
const selector = `[bind-user]:not([data-src="${id}"])`;
const elements = document.querySelectorAll(selector);
for (const element of elements) {
element.setAttribute('data-src', id);
}
});
// Entry point
const default_settings = {
redirect_uri: document.location.origin
async function tryToGetUserOrConnect() {
const user = await manager.getUser();
if (user) {
return user;
}
try {
const user = await manager.signinRedirectCallback();
location.href = user.state;
return user;
} catch (e) {
manager.signinRedirect({
state: location.href,
});
throw `Can't get user, redirect to OIDC authority ${settings.authority}`;
}
}
}
const settings = Object.assign({}, default_settings, document.currentScript.dataset);
sib.oidc._connect(settings);
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment