Commit 478e0d3e authored by Lauri Himanen's avatar Lauri Himanen
Browse files

Simplified KeyCloak authentication, enabled the error reporting tab in GUI.

parent a8382fd0
Pipeline #79143 skipped with stage
This diff is collapsed.
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
<script defer src="conf.js"></script> <script defer src="conf.js"></script>
<script defer src="keycloak.min.js"></script> <script defer src="keycloak.min.js"></script>
<script defer src="loadkeycloak.js"></script>
<script defer src="lib/3d-viewers/three.min.js"></script> <script defer src="lib/3d-viewers/three.min.js"></script>
<script defer src="lib/3d-viewers/orthographiccontrols.js"></script> <script defer src="lib/3d-viewers/orthographiccontrols.js"></script>
<script defer src="lib/3d-viewers/matviewer.min.js"></script> <script defer src="lib/3d-viewers/matviewer.min.js"></script>
......
// This separate script is used to handle the login procedure before loading
// the application. As of 0.8.3 nomad-FAIR is using KeyCloak 7.0.0, which does
// not support the "silentCheckSsoRedirectUri" option. This option enables a
// silent login check that does not enforce reloads. In order to do such silent
// login, the Javascript adapter for KeyCloak 8.0.0 is used instead. This is
// against the best practice of directly downloading the Javascript adapter
// from the authentication server (window.nomadEnv.keycloakBase +
// "js/keycloak.min.js"), but is in this case acceptable as it result is a much
// smoother user experience.
var keycloak = new Keycloak({
url: window.nomadEnv.keycloakBase,
realm: window.nomadEnv.keycloakRealm,
clientId: window.nomadEnv.keycloakClientId
});
let loginButton = document.querySelector('#login-button');
let logoutButton = document.querySelector('#logout-button');
let userName = document.querySelector('#user-name');
keycloak.init({
onLoad: "check-sso",
silentCheckSsoRedirectUri: `${window.nomadEnv.guiRoot}silent-check-sso.html`,
promiseType: "native",
}).then((authenticated) => {
if (authenticated) {
keycloak.loadUserProfile()
.then(function(profile) {
userName.textContent = `${profile.firstName} ${profile.lastName}`;
loginButton.style.display = 'none';
logoutButton.style.display = 'inline';
}).catch(function() {
console.log('Failed to load user profile.');
});
} else {
loginButton.style.display = 'inline';
logoutButton.style.display = 'none';
userName.textContent = "Guest";
}
});
loginButton.onclick = () => {
keycloak.login({redirectUri: `${window.nomadEnv.guiRoot}#/search`})
.catch(() => {console.log("Authentication error.")})
};
logoutButton.onclick = () => {
keycloak.logout()
};
...@@ -43,12 +43,6 @@ function route() { ...@@ -43,12 +43,6 @@ function route() {
if (hashPath.lastIndexOf('/') === (hashPath.length-1)) if (hashPath.lastIndexOf('/') === (hashPath.length-1))
hashPath = hashPath.substring(0,hashPath.length-1); hashPath = hashPath.substring(0,hashPath.length-1);
// Remove state parameters from authentication
let stateIndex = hashPath.indexOf('&state');
if (stateIndex != -1) {
hashPath = hashPath.substring(0, stateIndex);
}
if (hashPath.indexOf('/') > 0){ if (hashPath.indexOf('/') > 0){
let a= hashPath.split('/'); let a= hashPath.split('/');
command= a[0]; command= a[0];
......
...@@ -86,12 +86,10 @@ function getCalcMapByFunctional(summaryCalcSet) { ...@@ -86,12 +86,10 @@ function getCalcMapByFunctional(summaryCalcSet) {
if (functCalcMap.has(calc.functional_type)) { if (functCalcMap.has(calc.functional_type)) {
functCalcMap.get(calc.functional_type).add(calc.calc_id); functCalcMap.get(calc.functional_type).add(calc.calc_id);
} else { // New functional, not build if value is unavailable } else { // New functional
if (calc.functional_type !== "unavailable") { let newFunctionalArray = new Set();
let newFunctionalArray = new Set(); newFunctionalArray.add(calc.calc_id);
newFunctionalArray.add(calc.calc_id); functCalcMap.set(calc.functional_type, newFunctionalArray);
functCalcMap.set(calc.functional_type, newFunctionalArray);
}
} }
}); });
return functCalcMap; return functCalcMap;
......
...@@ -41,16 +41,70 @@ let DataStore = require('./material-mod/DataStore.js'); ...@@ -41,16 +41,70 @@ let DataStore = require('./material-mod/DataStore.js');
let contentElement = document.getElementById('content'); let contentElement = document.getElementById('content');
let titleElement = document.querySelector('title'); let titleElement = document.querySelector('title');
// As of 0.8.3 nomad-FAIR is using KeyCloak 7.0.0, which does
// not support the "silentCheckSsoRedirectUri" option. This option enables a
// silent login check that does not enforce reloads. In order to do such silent
// login, the Javascript adapter for KeyCloak 8.0.0 is used instead. This is
// against the best practice of directly downloading the Javascript adapter
// from the authentication server (window.nomadEnv.keycloakBase +
// "js/keycloak.min.js"), but is in this case acceptable as it result is a much
// smoother user experience.
PubSub.subscribe('authenticated', data => {
let hashPath = document.location.hash.substring(2);
if (hashPath.lastIndexOf('/') === (hashPath.length-1))
hashPath = hashPath.substring(0,hashPath.length-1);
if (hashPath.indexOf('/') > 0){
let command = hashPath.split('/')[0];
if (command === "material") {
flaggingTab.style.visibility = 'visible';
}
}
});
var keycloak = new Keycloak({
url: window.nomadEnv.keycloakBase,
realm: window.nomadEnv.keycloakRealm,
clientId: window.nomadEnv.keycloakClientId
});
let loginButton = document.querySelector('#login-button');
let logoutButton = document.querySelector('#logout-button');
let userName = document.querySelector('#user-name');
keycloak.init({
onLoad: "check-sso",
silentCheckSsoRedirectUri: `${window.nomadEnv.guiRoot}silent-check-sso.html`,
promiseType: "native",
}).then((authenticated) => {
if (authenticated) {
keycloak.loadUserProfile()
.then(function(profile) {
userName.textContent = `${profile.firstName} ${profile.lastName}`;
loginButton.style.display = 'none';
logoutButton.style.display = 'inline';
PubSub.publish('authenticated');
}).catch(function() {
console.log('Failed to load user profile.');
});
//util.setAuthRequestHeader(data.user, data.token.data);
} else {
loginButton.style.display = 'inline';
logoutButton.style.display = 'none';
userName.textContent = "Guest";
}
});
loginButton.onclick = () => {
keycloak.login({redirectUri: `${window.nomadEnv.guiRoot}#/search`})
.catch(() => {console.log("Authentication error.");});
};
logoutButton.onclick = () => {
keycloak.logout();
};
/********* User flagging side tab ****************/ /********* User flagging side tab ****************/
/* This side vertical tab is hidden initially /* This side vertical tab is hidden initially
but it has to be set up when the app starts */ but it has to be set up when the app starts */
let flaggingTab = document.getElementById('calc-flagging-tab'); let flaggingTab = document.getElementById('calc-flagging-tab');
flaggingTab.style.top = (window.innerHeight/2)+'px'; flaggingTab.style.top = (window.innerHeight/2)+'px';
flaggingTab.addEventListener('click',e => { flaggingTab.addEventListener('click',e => {
FlaggingFormPopup.show(MaterialModule.getCurrentPageStatus()); FlaggingFormPopup.show(MaterialModule.getCurrentPageStatus());
}); });
...@@ -188,14 +242,18 @@ PubSub.subscribe('show-material', data => { ...@@ -188,14 +242,18 @@ PubSub.subscribe('show-material', data => {
// In case the app comes from the search module through the url (back button) // In case the app comes from the search module through the url (back button)
UserGuidance.show(false); UserGuidance.show(false);
//console.log('User data:',util.getUserData()); // When a logged user comes to a material page, the error reporting tab is shown.
if (util.getUserData() !== null) flaggingTab.style.visibility = 'visible'; if (keycloak.authenticated) {
flaggingTab.style.visibility = 'visible';
}
}); });
PubSub.subscribe('show-search', search => { PubSub.subscribe('show-search', search => {
console.log('Handling event show-search: '+search); console.log('Handling event show-search: '+search);
// When a logged user comes to a material page, the error reporting tab is shown.
flaggingTab.style.visibility = 'hidden';
titleElement.innerHTML = 'NOMAD Encyclopedia - Search'; titleElement.innerHTML = 'NOMAD Encyclopedia - Search';
breadcrumb.setState('search',search); breadcrumb.setState('search',search);
...@@ -203,13 +261,10 @@ PubSub.subscribe('show-search', search => { ...@@ -203,13 +261,10 @@ PubSub.subscribe('show-search', search => {
searchMod.showSearchPage(); searchMod.showSearchPage();
LoadingPopup.hide(); // In case it comes from the result page LoadingPopup.hide(); // In case it comes from the result page
}else if (search === 'results') } else if (search === 'results')
searchMod.showResultsPage(); searchMod.showResultsPage();
showModuleDOM(searchMod.element); showModuleDOM(searchMod.element);
if (flaggingTab.style.visibility !== 'hidden')
flaggingTab.style.visibility = 'hidden';
}); });
...@@ -226,38 +281,16 @@ Router.route(); ...@@ -226,38 +281,16 @@ Router.route();
let userNameElement = document.querySelector('#user-name'); let userNameElement = document.querySelector('#user-name');
//function setAppAuthenticated(data){
//if (data.status === 'Authenticated'){
//userNameElement.innerHTML = data.user.username;
//document.querySelector('#guest-user').style.display = 'none';
//document.querySelector('#auth-user').style.display = 'inline';
//util.setAuthRequestHeader(data.user, data.token.data);
//if (currentModule === materialModDOM) flaggingTab.style.visibility = 'visible';
//}
//}
//function logout(){
//userNameElement.innerHTML = '';
//util.setAuthRequestHeader();
//if (flaggingTab.style.visibility !== 'hidden')
//flaggingTab.style.visibility = 'hidden';
//}
function getCookie(name) { function getCookie(name) {
let value = "; " + document.cookie; let value = "; " + document.cookie;
let parts = value.split("; " + name + "="); let parts = value.split("; " + name + "=");
if (parts.length === 2) return parts.pop().split(";").shift(); if (parts.length === 2) return parts.pop().split(";").shift();
} }
function parseCookie(userData) { function parseCookie(userData) {
return userData.substring(1, userData.length-1).replace(/\\054/g,',').replace(/\\/g,''); return userData.substring(1, userData.length-1).replace(/\\054/g,',').replace(/\\/g,'');
} }
let userInfoCookie = getCookie('user_info'); let userInfoCookie = getCookie('user_info');
if (userInfoCookie !== undefined){ if (userInfoCookie !== undefined){
......
Supports Markdown
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