4343import org .json .JSONException ;
4444import org .json .JSONObject ;
4545import org .owasp .encoder .Encode ;
46+ import org .wso2 .carbon .context .PrivilegedCarbonContext ;
4647import org .wso2 .carbon .identity .application .authentication .framework .AuthenticationService ;
4748import org .wso2 .carbon .identity .application .authentication .framework .AuthenticatorFlowStatus ;
4849import org .wso2 .carbon .identity .application .authentication .framework .CommonAuthenticationHandler ;
5152import org .wso2 .carbon .identity .application .authentication .framework .config .model .graph .js .JsLogger ;
5253import org .wso2 .carbon .identity .application .authentication .framework .context .AuthHistory ;
5354import org .wso2 .carbon .identity .application .authentication .framework .context .SessionContext ;
55+ import org .wso2 .carbon .identity .application .authentication .framework .exception .FrameworkException ;
5456import org .wso2 .carbon .identity .application .authentication .framework .exception .UserIdNotFoundException ;
5557import org .wso2 .carbon .identity .application .authentication .framework .exception .auth .service .AuthServiceClientException ;
5658import org .wso2 .carbon .identity .application .authentication .framework .exception .auth .service .AuthServiceException ;
151153import org .wso2 .carbon .identity .openidconnect .OIDCRequestObjectUtil ;
152154import org .wso2 .carbon .identity .openidconnect .model .RequestObject ;
153155import org .wso2 .carbon .identity .openidconnect .model .RequestedClaim ;
156+ import org .wso2 .carbon .identity .organization .management .service .exception .OrganizationManagementException ;
157+ import org .wso2 .carbon .identity .organization .management .service .util .OrganizationManagementUtil ;
154158import org .wso2 .carbon .utils .CarbonUtils ;
155159import org .wso2 .carbon .utils .DiagnosticLog ;
156160
@@ -1523,7 +1527,7 @@ private static void addToAuthenticationResultDetailsToOAuthMessage(OAuthMessage
15231527 addMappedRemoteClaimsToSessionCache (oAuthMessage , authnResult );
15241528 }
15251529
1526- private static void updateAuthTimeInSessionDataCacheEntry (OAuthMessage oAuthMessage ) {
1530+ private static void updateAuthTimeInSessionDataCacheEntry (OAuthMessage oAuthMessage ) throws OAuthSystemException {
15271531
15281532 String commonAuthIdCookieValue = getCommonAuthCookieString (oAuthMessage .getRequest ());
15291533 long authTime = getAuthenticatedTimeFromCommonAuthCookieValue (commonAuthIdCookieValue ,
@@ -2429,7 +2433,7 @@ public static void populateValidationResponseWithAppDetail(OAuthMessage oAuthMes
24292433
24302434 String clientId = oAuthMessage .getRequest ().getParameter (CLIENT_ID );
24312435 try {
2432- OAuthAppDO appDO = OAuth2Util .getAppInformationByClientId (clientId );
2436+ OAuthAppDO appDO = OAuth2Util .getAppInformationByClientId (clientId , OAuth2Util . extractTenantDomain () );
24332437 if (Boolean .TRUE .equals (oAuthMessage .getRequest ().getAttribute (OAuthConstants .PKCE_UNSUPPORTED_FLOW ))) {
24342438 validationResponse .setPkceMandatory (false );
24352439 } else {
@@ -2834,7 +2838,8 @@ private static String getSpTenantDomain(String clientId) throws InvalidRequestEx
28342838 try {
28352839 // At this point we have verified that a valid app exists for the client_id. So we directly get the SP
28362840 // tenantDomain.
2837- return OAuth2Util .getTenantDomainOfOauthApp (clientId );
2841+ String tenantDomain = IdentityTenantUtil .getTenantDomain (IdentityTenantUtil .getLoginTenantId ());
2842+ return OAuth2Util .getTenantDomainOfOauthApp (clientId , tenantDomain );
28382843 } catch (InvalidOAuthClientException | IdentityOAuth2Exception e ) {
28392844 throw new InvalidRequestException ("Error retrieving Service Provider tenantDomain for client_id: "
28402845 + clientId , OAuth2ErrorCodes .INVALID_REQUEST , OAuth2ErrorCodes .OAuth2SubErrorCodes
@@ -2852,7 +2857,12 @@ private static String getLoginTenantDomain(OAuthMessage oAuthMessage, String cli
28522857 String loginTenantDomain =
28532858 oAuthMessage .getRequest ().getParameter (FrameworkConstants .RequestParams .LOGIN_TENANT_DOMAIN );
28542859 if (StringUtils .isBlank (loginTenantDomain )) {
2855- return EndpointUtil .getSPTenantDomainFromClientId (oAuthMessage .getClientId ());
2860+ try {
2861+ return EndpointUtil .verifyAndRetrieveTenantDomain (clientId );
2862+ } catch (OAuthSystemException e ) {
2863+ throw new InvalidRequestException ("Error resolving tenant domain for client id: " + clientId ,
2864+ OAuth2ErrorCodes .INVALID_REQUEST , OAuth2ErrorCodes .OAuth2SubErrorCodes .INVALID_REQUEST );
2865+ }
28562866 }
28572867 return loginTenantDomain ;
28582868 }
@@ -4351,10 +4361,29 @@ private static String appendAuthenticatedIDPs(SessionDataCacheEntry sessionDataC
43514361 * @param resultFromLogin The session context.
43524362 * @param cookieValue The cookie string which contains the commonAuthId value.
43534363 */
4354- private static void associateAuthenticationHistory (SessionDataCacheEntry resultFromLogin , String cookieValue ) {
4364+ private static void associateAuthenticationHistory (SessionDataCacheEntry resultFromLogin , String cookieValue )
4365+ throws OAuthSystemException {
43554366
4356- SessionContext sessionContext = getSessionContext (cookieValue ,
4357- resultFromLogin .getoAuth2Parameters ().getLoginTenantDomain ());
4367+ String tenantDomain = resultFromLogin .getoAuth2Parameters ().getLoginTenantDomain ();
4368+ /*
4369+ If the app is created in an organization, get the tenant domain of the primary organization since the
4370+ session is stored against the primary organization.
4371+ */
4372+ try {
4373+ String appTenantDomain = OAuth2Util .getTenantDomainOfOauthApp (
4374+ resultFromLogin .getoAuth2Parameters ().getClientId (), tenantDomain );
4375+ if (OrganizationManagementUtil .isOrganization (appTenantDomain )) {
4376+ String appOrgId = OAuth2ServiceComponentHolder .getInstance ().getOrganizationManager ()
4377+ .resolveOrganizationId (appTenantDomain );
4378+ String primaryOrgId = OAuth2ServiceComponentHolder .getInstance ().getOrganizationManager ()
4379+ .getPrimaryOrganizationId (appOrgId );
4380+ tenantDomain = OAuth2ServiceComponentHolder .getInstance ().getOrganizationManager ()
4381+ .resolveTenantDomain (primaryOrgId );
4382+ }
4383+ } catch (OrganizationManagementException | IdentityOAuth2Exception | InvalidOAuthClientException e ) {
4384+ throw new OAuthSystemException (e );
4385+ }
4386+ SessionContext sessionContext = getSessionContext (cookieValue , tenantDomain );
43584387 if (sessionContext != null && sessionContext .getSessionAuthHistory () != null
43594388 && sessionContext .getSessionAuthHistory ().getHistory () != null ) {
43604389 List <String > authMethods = new ArrayList <>();
0 commit comments