I'm building an outlook add-in that shares contents of a mail to a messaging system.
It requires IDP authentication to login to the messaging system. A pop up appears for the authentication purpose. When the user enters the credentials in the pop up window, the user gets redirected to another page and URL gets changed.
This URL has a token id which I want to extract so I can use it in my code for further API calls. When i tried, I received the following error:
Uncaught DOMException: An attempt was made to break through the security policy of the user agent
Is there a way to extract the new URL without getting the above error ?
function determineAuthenticationMethodAndRedirect(data, tenantId) {
if (data.IdentityProvider.Enabled) {
console.log("Identity Provider authentication is enabled");
openPopup(Office.context.roamingSettings.get("userInput") + "/Apps/Erp/Home/", tenantId);
} else if (data.TokenAuthentication.Enabled) {
console.log("Token-based authentication is enabled");
window.location.href = "ep-collab-login.js";
} else if (data.BasicAuthentication.Enabled) {
console.log("Basic authentication is enabled");
} else {
console.log("No supported authentication method is enabled");
}
}
function openPopup(url, tenantId) {
var popupWidth = 600;
var popupHeight = 400;
var popupSettings = `width=${popupWidth},height=${popupHeight},top=${(window.innerHeight - popupHeight) / 2},left=${(window.innerWidth - popupWidth) / 2},scrollbars=yes,resizable=yes`;
var popup = window.open(url, "_blank", popupSettings);
// Listen for changes in the URL
var checkUrlInterval = setInterval(function () {
if (popup && popup.location.href !== url) {
// The URL has changed, perform actions after the redirect
handleUrlChanged(popup.location.href);
popup.close();
clearInterval(checkUrlInterval);
}
}, 500);
}
function handleUrlChanged(newUrl) {
// Extract the ID token from the new URL
var idToken = extractIdTokenFromUrl(newUrl);
if (idToken) {
console.log('ID Token:', idToken);
} else {
console.log('ID Token not available');
}
}
function extractIdTokenFromUrl(url) {
var match = url.match(/[?&]id-token=([^&]*)/);
return match ? match[1] : null;
}