Skip to content

Commit b1bbf27

Browse files
Enable Authorization Code grant support for sub organization OAuth2 applications
1 parent 1a416de commit b1bbf27

File tree

15 files changed

+273
-32
lines changed

15 files changed

+273
-32
lines changed

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

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.json.JSONException;
4444
import org.json.JSONObject;
4545
import org.owasp.encoder.Encode;
46+
import org.wso2.carbon.context.PrivilegedCarbonContext;
4647
import org.wso2.carbon.identity.application.authentication.framework.AuthenticationService;
4748
import org.wso2.carbon.identity.application.authentication.framework.AuthenticatorFlowStatus;
4849
import org.wso2.carbon.identity.application.authentication.framework.CommonAuthenticationHandler;
@@ -51,6 +52,7 @@
5152
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsLogger;
5253
import org.wso2.carbon.identity.application.authentication.framework.context.AuthHistory;
5354
import org.wso2.carbon.identity.application.authentication.framework.context.SessionContext;
55+
import org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException;
5456
import org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException;
5557
import org.wso2.carbon.identity.application.authentication.framework.exception.auth.service.AuthServiceClientException;
5658
import org.wso2.carbon.identity.application.authentication.framework.exception.auth.service.AuthServiceException;
@@ -151,6 +153,8 @@
151153
import org.wso2.carbon.identity.openidconnect.OIDCRequestObjectUtil;
152154
import org.wso2.carbon.identity.openidconnect.model.RequestObject;
153155
import 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;
154158
import org.wso2.carbon.utils.CarbonUtils;
155159
import 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<>();

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.extractTenantDomain();
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)