function start() { if (!window['Msal']) { console.log('Disabling B2C authentication - The microsoft authentication library is not available.'); generateBootstrapScript(); return; } var authentication; var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function(){ //used instead of onload due to browser compatibility if (this.readyState === 4 && this.status === 200){ var result = xhttp.responseText; try { var settings = JSON.parse(result); } catch (e) { var message = "Disabling B2C authentication - Unable to read the application settings json. \nMake sure that it is in valid json format (comments are not allowed).\n" console.log(message + e); generateBootstrapScript(); return; } if (settings.authentication){ authentication = settings.authentication; } if (authentication) { var msalConfig = { auth: { clientId: authentication.clientId, authority: authentication.authority, validateAuthority: false }, cache: { cacheLocation: authentication.cacheLocation || "localStorage", storeAuthStateInCookie: true } } var instance = new Msal.UserAgentApplication(msalConfig); var tokenRequest = { scopes: authentication.b2cScopes, redirectUri: getRedirectUri(instance) } instance.handleRedirectCallback(redirectCallback); getToken(instance, tokenRequest); } else { console.log('Disabling B2C authentication - Could not find authentication object in application settings json. \nMake sure one exists to enable B2C authentication for the client.'); generateBootstrapScript(); } } else if (this.readyState === 4) { var message = "The request to " + xhttp.responseURL + " failed, responded with " + xhttp.status + ": " + xhttp.statusText + "."; console.log("Disabling B2C authentication - " + message); generateBootstrapScript(); } } var domain = window.location.origin; var path = '/resources/application.settings.json'; var url = domain + path; xhttp.open("GET", url, true); xhttp.send(); } function redirectCallback(error, response) { var token = response.accessToken; if (!token && response.idToken) { getToken(); return; } if (token) { generateBootstrapScript(); } } function signIn(instance, tokenRequest) { instance.loginRedirect(tokenRequest); } function getToken(instance, tokenRequest) { if (!instance.getAccount()) { signIn(instance, tokenRequest); return; } return instance.acquireTokenSilent(tokenRequest) .then(handleTokenResponse) .catch(function (error) { handleTokenFailure(error, instance, tokenRequest) }); } function handleTokenResponse(response) { generateBootstrapScript(); } function handleTokenFailure(error, instance, tokenRequest) { if (error.message && error.message.startsWith('AADB2C90205')) { console.log('Disabling B2C authentication - Failed token acquisition', error); generateBootstrapScript(); } else { if (requiresInteraction(error.errorCode)) { return signIn(instance, tokenRequest); } else { instance.acquireTokenRedirect(tokenRequest); } } } function requiresInteraction(errorCode) { if (!errorCode || !errorCode.length) { return false; } return errorCode === "consent_required" || errorCode === "interaction_required" || errorCode === "login_required"; } function getRedirectUri(instance) { var redirectUri = instance.getRedirectUri(); if (redirectUri.indexOf(('index.html')) > -1) { redirectUri = redirectUri.split('index.html')[0]; } if (redirectUri.slice(-1) !== '/') { redirectUri += '/'; } redirectUri = redirectUri + 'auth.html'; return redirectUri; } function generateBootstrapScript() { var bootstrapScript = document.createElement('script'); bootstrapScript.id = 'microloader'; bootstrapScript.setAttribute('type', 'text/javascript'); bootstrapScript.setAttribute('src', 'bootstrap.js'); document.head.appendChild(bootstrapScript); } start();