Skip to content

Commit fda8ddf

Browse files
Merge pull request #2971 from ShanChathusanda93/sub-org-app-auth-code-feature-brach
Enable Authorization Code grant support for sub organization OAuth2 applications
2 parents 3330bb4 + 41e01ac commit fda8ddf

File tree

15 files changed

+312
-36
lines changed

15 files changed

+312
-36
lines changed

components/org.wso2.carbon.identity.oauth.endpoint/src/main/java/org/wso2/carbon/identity/oauth/endpoint/util/AuthzUtil.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@
151151
import org.wso2.carbon.identity.openidconnect.OIDCRequestObjectUtil;
152152
import org.wso2.carbon.identity.openidconnect.model.RequestObject;
153153
import 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;
154156
import org.wso2.carbon.utils.CarbonUtils;
155157
import 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<>();

components/org.wso2.carbon.identity.oauth.endpoint/src/main/java/org/wso2/carbon/identity/oauth/endpoint/util/EndpointUtil.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,29 @@ public static String getSPTenantDomainFromClientId(String clientId) {
15441544
}
15451545
}
15461546

1547+
/**
1548+
* This method verifies the service provider tenant domain using the client ID.
1549+
*
1550+
* @param clientId Client id of the application.
1551+
* @return tenantDomain domain of the service provider.
1552+
*/
1553+
public static String verifyAndRetrieveTenantDomain(String clientId)
1554+
throws OAuthSystemException {
1555+
1556+
try {
1557+
String extractedTenantDomain = OAuth2Util.getLoginTenant();
1558+
OAuthAppDO oAuthAppDO = OAuth2Util.getAppInformationByClientId(clientId, extractedTenantDomain);
1559+
String appTenantDomain = OAuth2Util.getTenantDomainOfOauthApp(oAuthAppDO);
1560+
if (StringUtils.equals(extractedTenantDomain, appTenantDomain)) {
1561+
return appTenantDomain;
1562+
}
1563+
throw new OAuthSystemException("Provided tenant domain: " + extractedTenantDomain + " does not " +
1564+
"match with the application's tenant domain: " + appTenantDomain);
1565+
} catch (IdentityOAuth2Exception | InvalidOAuthClientException e) {
1566+
throw new OAuthSystemException("Error while getting oauth app for client Id: " + clientId, e);
1567+
}
1568+
}
1569+
15471570
/**
15481571
* Extract information related to the token request and exception and publish the event to listeners.
15491572
*

0 commit comments

Comments
 (0)