(function() { const SSO_IFRAME_URL = 'https://sso.erickson.it/account/bc/status.php'; const SSO_ORIGIN = 'https://sso.erickson.it'; const SSO_IS_LOGGED_STORAGE_KEY = "sso_is_logged"; const SSO_CHECK_URL = 'https://sso.erickson.it/account/bc/check.php'; let statusChangeCallback = null; let loginCallback = null; let logoutCallback = null; window.SSOManager = { initialStatusCheck: 1, init: function() { if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', createSsoIframe); } else { createSsoIframe(); } if (this.initialStatusCheck == 1) { checkThirdPartyCookieAccess().then(isAccessible => { if (isAccessible === false) { checkByRedirect(); } }); } else if (this.initialStatusCheck == 2) { checkByRedirect(); } }, onStatusChange: function(callback) { if (typeof callback === 'function') { statusChangeCallback = callback; } }, onLogin: function(callback) { if (typeof callback === 'function') { loginCallback = callback; } }, onLogout: function(callback) { if (typeof callback === 'function') { logoutCallback = callback; } }, }; function createSsoIframe() { const iframe = document.createElement('iframe'); iframe.style.display = 'none'; const parentOrigin = window.location.origin; iframe.src = `${SSO_IFRAME_URL}?parentOrigin=${encodeURIComponent(parentOrigin)}&initialStatusCheck=${SSOManager.initialStatusCheck}`; iframe.title = 'SSO Status Checker'; document.body.appendChild(iframe); } window.addEventListener('message', function(event) { if (event.origin !== SSO_ORIGIN) { return; // Ignora messaggi da origini sconosciute } if (event.data && typeof event.data.idLogged !== 'undefined') { if (typeof statusChangeCallback === 'function') { statusChangeCallback(event.data); } const isLogged = event.data.idLogged; sendLocalEvent(isLogged); } }); function sendLocalEvent(isLogged){ sessionStorage.setItem(SSO_IS_LOGGED_STORAGE_KEY, isLogged); if (isLogged) { if (typeof loginCallback === 'function') { loginCallback(); } } else { if (typeof logoutCallback === 'function') { logoutCallback(); } } } function checkThirdPartyCookieAccess() { if (!document.requestStorageAccess || !document.hasStorageAccess) { return Promise.resolve(null); } return document.hasStorageAccess() .then(hasAccess => { if (hasAccess) { return true; } else { return false; } }) .catch(error => { return null; }); } function checkByRedirect() { if (isInSessionStorage()) { return; } if (handleRedirectResponse()) { return; } const currentUrl = window.location.href; let redirectUrl = `${SSO_CHECK_URL}?redirectTo=${encodeURIComponent(currentUrl)}`; window.location.replace(redirectUrl); } function isInSessionStorage() { const isLogged = sessionStorage.getItem(SSO_IS_LOGGED_STORAGE_KEY); if (typeof isLogged == 'undefined' || isLogged == null) { return false; } sendLocalEvent(isLogged); return true; } function handleRedirectResponse() { const urlParams = new URLSearchParams(window.location.search); const ssoIsLogged = urlParams.get('SsoIsLogged'); if (ssoIsLogged === null) { return false; } const cleanUrlSearchParams = new URLSearchParams(window.location.search); cleanUrlSearchParams.delete('SsoIsLogged'); const cleanSearch = cleanUrlSearchParams.toString(); const cleanUrl = window.location.pathname + (cleanSearch ? '?' + cleanSearch : '') + window.location.hash; try { window.history.replaceState({}, document.title, cleanUrl); console.log('URL pulito dopo la risposta SSO.'); } catch (e) { console.warn('Impossibile pulire l\'URL:', e); // Può accadere in alcuni contesti (es. sandboxed iframe) } sendLocalEvent(ssoIsLogged); return true; } })(); // Fine IIFE