Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre auth improvement for central deployment related authentication #7291

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tasty-crews-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wso2is/console": patch
---

Add pre auth improvement for central deployment related authentication
104 changes: 91 additions & 13 deletions apps/console/src/auth.html
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
var serverOriginGlobal = startupConfig.serverUrlGlobal;
var superTenantGlobal = startupConfig.superTenant;
var tenantPrefixGlobal = startupConfig.tenantPrefix;
var isCentralDeploymentEnabled = startupConfig.enableCentralDeployment;
var isAdaptiveAuthenticationAvailable = true;
var isOrganizationManagementEnabled = true;

Expand All @@ -178,13 +179,14 @@
sessionStorage.setItem("auth_callback_url_console", authCallbackUrl);
}

var serverOrigin = startupConfig.serverUrl;
var serverOrigin = isCentralDeploymentEnabled ? startupConfig.centralServerUrl : startupConfig.serverUrl;
var aduServerOrigin = startupConfig.serverUrl;
var authorizationCode = urlParams.get("code");
var authSessionState = urlParams.get("session_state");
var authIdPs = urlParams.get("AuthenticatedIdPs");

function authenticateWithSDK() {
if(!authorizationCode) {
if (!authorizationCode || isCentralDeploymentEnabled) {
function getTenantName() {
var path = window.location.pathname;
var pathChunks = path.split("/");
Expand Down Expand Up @@ -223,6 +225,20 @@
return serverOrigin + getTenantPath(tenantDomain);
}

function getADUApiPath(path, tenantDomain) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a doc comment explaining the function, especially what ADU refers to :)

if (!tenantDomain) {
if (startupConfig.superTenantProxy) {
tenantDomain = startupConfig.superTenantProxy;
} else {
tenantDomain = startupConfig.superTenant;
}
}
if (path) {
return aduServerOrigin + getTenantPath(tenantDomain) + path;
}
return aduServerOrigin + getTenantPath(tenantDomain);
}

/**
* Get the organization name.
*
Expand Down Expand Up @@ -258,6 +274,9 @@
* @returns {string} Contructed URL.
*/
function signInRedirectURL() {
if (isCentralDeploymentEnabled) {
return applicationDomain
}
if (getTenantName() === startupConfig.superTenant) {
return applicationDomain.replace(/\/+$/, '')
+ "<%= htmlWebpackPlugin.options.basename ? '/' + htmlWebpackPlugin.options.basename : ''%>";
Expand All @@ -267,6 +286,16 @@
+ "<%= htmlWebpackPlugin.options.basename ? '/' + htmlWebpackPlugin.options.basename : ''%>";
}

/**
* Construct the sign-in redirect URL for ADU.
*
* @returns {string} Constructed URL
*/
function signInRedirectURLADU(tenantDomain) {
return `${applicationDomain}/${tenantPrefixGlobal}/${tenantDomain}`
+ "<%= htmlWebpackPlugin.options.basename ? '/' + htmlWebpackPlugin.options.basename : ''%>";
}

/**
* Construct the sign-out redirect URL.
*
Expand Down Expand Up @@ -329,7 +358,22 @@
}

var auth = AsgardeoAuth.AsgardeoSPAClient.getInstance();
if (isCentralDeploymentEnabled) {
auth = AsgardeoAuth.AsgardeoSPAClient.getInstance("primary");
}

var endpoints = {
authorizationEndpoint: getApiPath("/oauth2/authorize"),
clockTolerance: 300,
jwksEndpointURL: isCentralDeploymentEnabled ? ("/oauth2/jwks") : undefined,
logoutEndpointURL: getApiPath("/oidc/logout"),
oidcSessionIFrameEndpointURL: getApiPath("/oidc/checksession"),
tokenEndpointURL: undefined,
tokenRevocationEndpointURL: isCentralDeploymentEnabled ? ("/oauth2/revoke") : undefined
};
if (isCentralDeploymentEnabled) {
endpoints.issuer = `${serverOrigin}/oauth2/token`;
}
var authConfig = {
signInRedirectURL: signInRedirectURL(),
signOutRedirectURL: getSignOutRedirectURL(),
Expand All @@ -340,15 +384,7 @@
storage: "webWorker",
disableTrySignInSilently: true,
enableOIDCSessionManagement: false,
endpoints: {
authorizationEndpoint: getApiPath("/oauth2/authorize"),
clockTolerance: 300,
jwksEndpointURL: undefined,
logoutEndpointURL: getApiPath("/oidc/logout"),
oidcSessionIFrameEndpointURL: getApiPath("/oidc/checksession"),
tokenEndpointURL: undefined,
tokenRevocationEndpointURL: undefined
},
endpoints: endpoints,
enablePKCE: true
}

Expand Down Expand Up @@ -381,13 +417,55 @@
sessionStorage.setItem("auth_callback_url_console", authCallbackUrl);
}

auth.signIn(getAuthParams({}));
if (isCentralDeploymentEnabled) {
if (authorizationCode) {
auth.signIn({callOnlyOnRedirect: true})
.then((response) => {
auth.getDecodedIDToken().then((token) => {
// console.log("id token", token)
var defaultTenant = token["default_tenant"]
var authConfigRegion = {
signInRedirectURL: signInRedirectURLADU(defaultTenant),
signOutRedirectURL: getSignOutRedirectURL(),
clientID: "<%= htmlWebpackPlugin.options.clientID %>",
baseUrl: getADUApiPath(defaultTenant),
responseMode: "query",
scope: ["openid SYSTEM profile"],
storage: "webWorker",
disableTrySignInSilently: false,
enableOIDCSessionManagement: false,
endpoints: {
authorizationEndpoint: getADUApiPath("/oauth2/authorize", defaultTenant),
clockTolerance: 300,
jwksEndpointURL: getADUApiPath("/oauth2/jwks", defaultTenant),
logoutEndpointURL: getADUApiPath("/oidc/logout", defaultTenant),
oidcSessionIFrameEndpointURL: getADUApiPath("/oidc/checksession", defaultTenant),
tokenEndpointURL: getADUApiPath("/oauth2/token", defaultTenant),
tokenRevocationEndpointURL: getADUApiPath("/oauth2/revoke", defaultTenant),
issuer: getADUApiPath("/oauth2/token", defaultTenant)
},
enablePKCE: true
}
var authSecondary = AsgardeoAuth.AsgardeoSPAClient.getInstance("secondary");

authSecondary.initialize(authConfigRegion);

authSecondary.signIn({prompt: "login",fidp: "PlatformIDP"})

})
})
} else {
auth.signIn(getAuthParams({}))
}
} else {
auth.signIn(getAuthParams({}));
}
}
}
}
</script>
<script>
if(!authorizationCode) {
if(!authorizationCode || isCentralDeploymentEnabled) {
var authSPAJS = document.createElement("script");
var authScriptSrc = "<%= htmlWebpackPlugin.options.basename ? '/' + htmlWebpackPlugin.options.basename + '/auth-spa-3.1.2.min.js' : '/auth-spa-3.1.2.min.js'%>";

Expand Down
Loading