151151import org .wso2 .carbon .identity .openidconnect .OIDCRequestObjectUtil ;
152152import org .wso2 .carbon .identity .openidconnect .model .RequestObject ;
153153import org .wso2 .carbon .identity .openidconnect .model .RequestedClaim ;
154+ import org .wso2 .carbon .identity .organization .management .service .exception .OrganizationManagementException ;
155+ import org .wso2 .carbon .identity .organization .management .service .util .OrganizationManagementUtil ;
154156import org .wso2 .carbon .utils .CarbonUtils ;
155157import org .wso2 .carbon .utils .DiagnosticLog ;
156158
@@ -1523,7 +1525,7 @@ private static void addToAuthenticationResultDetailsToOAuthMessage(OAuthMessage
15231525 addMappedRemoteClaimsToSessionCache (oAuthMessage , authnResult );
15241526 }
15251527
1526- private static void updateAuthTimeInSessionDataCacheEntry (OAuthMessage oAuthMessage ) {
1528+ private static void updateAuthTimeInSessionDataCacheEntry (OAuthMessage oAuthMessage ) throws OAuthSystemException {
15271529
15281530 String commonAuthIdCookieValue = getCommonAuthCookieString (oAuthMessage .getRequest ());
15291531 long authTime = getAuthenticatedTimeFromCommonAuthCookieValue (commonAuthIdCookieValue ,
@@ -2429,7 +2431,7 @@ public static void populateValidationResponseWithAppDetail(OAuthMessage oAuthMes
24292431
24302432 String clientId = oAuthMessage .getRequest ().getParameter (CLIENT_ID );
24312433 try {
2432- OAuthAppDO appDO = OAuth2Util .getAppInformationByClientId (clientId );
2434+ OAuthAppDO appDO = OAuth2Util .getAppInformationByClientId (clientId , OAuth2Util . getLoginTenant () );
24332435 if (Boolean .TRUE .equals (oAuthMessage .getRequest ().getAttribute (OAuthConstants .PKCE_UNSUPPORTED_FLOW ))) {
24342436 validationResponse .setPkceMandatory (false );
24352437 } else {
@@ -2834,7 +2836,8 @@ private static String getSpTenantDomain(String clientId) throws InvalidRequestEx
28342836 try {
28352837 // At this point we have verified that a valid app exists for the client_id. So we directly get the SP
28362838 // tenantDomain.
2837- return OAuth2Util .getTenantDomainOfOauthApp (clientId );
2839+ String tenantDomain = IdentityTenantUtil .getTenantDomain (IdentityTenantUtil .getLoginTenantId ());
2840+ return OAuth2Util .getTenantDomainOfOauthApp (clientId , tenantDomain );
28382841 } catch (InvalidOAuthClientException | IdentityOAuth2Exception e ) {
28392842 throw new InvalidRequestException ("Error retrieving Service Provider tenantDomain for client_id: "
28402843 + clientId , OAuth2ErrorCodes .INVALID_REQUEST , OAuth2ErrorCodes .OAuth2SubErrorCodes
@@ -2852,7 +2855,12 @@ private static String getLoginTenantDomain(OAuthMessage oAuthMessage, String cli
28522855 String loginTenantDomain =
28532856 oAuthMessage .getRequest ().getParameter (FrameworkConstants .RequestParams .LOGIN_TENANT_DOMAIN );
28542857 if (StringUtils .isBlank (loginTenantDomain )) {
2855- return EndpointUtil .getSPTenantDomainFromClientId (oAuthMessage .getClientId ());
2858+ try {
2859+ return EndpointUtil .verifyAndRetrieveTenantDomain (clientId );
2860+ } catch (OAuthSystemException e ) {
2861+ throw new InvalidRequestException ("Error resolving tenant domain for client id: " + clientId ,
2862+ OAuth2ErrorCodes .INVALID_REQUEST , OAuth2ErrorCodes .OAuth2SubErrorCodes .INVALID_REQUEST );
2863+ }
28562864 }
28572865 return loginTenantDomain ;
28582866 }
@@ -4351,10 +4359,29 @@ private static String appendAuthenticatedIDPs(SessionDataCacheEntry sessionDataC
43514359 * @param resultFromLogin The session context.
43524360 * @param cookieValue The cookie string which contains the commonAuthId value.
43534361 */
4354- private static void associateAuthenticationHistory (SessionDataCacheEntry resultFromLogin , String cookieValue ) {
4362+ private static void associateAuthenticationHistory (SessionDataCacheEntry resultFromLogin , String cookieValue )
4363+ throws OAuthSystemException {
43554364
4356- SessionContext sessionContext = getSessionContext (cookieValue ,
4357- resultFromLogin .getoAuth2Parameters ().getLoginTenantDomain ());
4365+ String tenantDomain = resultFromLogin .getoAuth2Parameters ().getLoginTenantDomain ();
4366+ /*
4367+ If the app is created in an organization, get the tenant domain of the primary organization since the
4368+ session is stored against the primary organization.
4369+ */
4370+ try {
4371+ String appTenantDomain = OAuth2Util .getTenantDomainOfOauthApp (
4372+ resultFromLogin .getoAuth2Parameters ().getClientId (), tenantDomain );
4373+ if (OrganizationManagementUtil .isOrganization (appTenantDomain )) {
4374+ String appOrgId = OAuth2ServiceComponentHolder .getInstance ().getOrganizationManager ()
4375+ .resolveOrganizationId (appTenantDomain );
4376+ String primaryOrgId = OAuth2ServiceComponentHolder .getInstance ().getOrganizationManager ()
4377+ .getPrimaryOrganizationId (appOrgId );
4378+ tenantDomain = OAuth2ServiceComponentHolder .getInstance ().getOrganizationManager ()
4379+ .resolveTenantDomain (primaryOrgId );
4380+ }
4381+ } catch (OrganizationManagementException | IdentityOAuth2Exception | InvalidOAuthClientException e ) {
4382+ throw new OAuthSystemException (e );
4383+ }
4384+ SessionContext sessionContext = getSessionContext (cookieValue , tenantDomain );
43584385 if (sessionContext != null && sessionContext .getSessionAuthHistory () != null
43594386 && sessionContext .getSessionAuthHistory ().getHistory () != null ) {
43604387 List <String > authMethods = new ArrayList <>();
0 commit comments