64
64
import org .wso2 .carbon .identity .oauth2 .OAuth2Constants ;
65
65
import org .wso2 .carbon .identity .oauth2 .util .JWTSignatureValidationUtils ;
66
66
import org .wso2 .carbon .identity .oauth2 .util .OAuth2Util ;
67
+ import org .wso2 .carbon .identity .organization .management .service .exception .OrganizationManagementException ;
67
68
import org .wso2 .carbon .user .api .UserStoreException ;
68
69
69
70
import java .lang .reflect .InvocationTargetException ;
@@ -105,12 +106,12 @@ public class DCRMService {
105
106
*/
106
107
public Application getApplication (String clientId ) throws DCRMException {
107
108
108
- validateRequestTenantDomain (clientId );
109
+ String tenantDomain = getTenantDomain ();
110
+ validateRequestTenantDomain (clientId , tenantDomain );
109
111
OAuthConsumerAppDTO consumerAppDTO = getApplicationById (
110
- clientId , DCRMUtils .isApplicationRolePermissionRequired ());
112
+ clientId , DCRMUtils .isApplicationRolePermissionRequired (), tenantDomain );
111
113
// Get the jwksURI from the service provider.
112
114
String applicationName = consumerAppDTO .getApplicationName ();
113
- String tenantDomain = PrivilegedCarbonContext .getThreadLocalCarbonContext ().getTenantDomain ();
114
115
ServiceProvider serviceProvider = getServiceProvider (applicationName , tenantDomain );
115
116
String jwksURI = serviceProvider .getJwksUri ();
116
117
if (StringUtils .isNotEmpty (jwksURI )) {
@@ -156,7 +157,7 @@ public Application getApplicationByName(String clientName) throws DCRMException
156
157
DCRMConstants .ErrorMessages .BAD_REQUEST_INSUFFICIENT_DATA , null );
157
158
}
158
159
159
- String tenantDomain = PrivilegedCarbonContext . getThreadLocalCarbonContext (). getTenantDomain ();
160
+ String tenantDomain = getTenantDomain ();
160
161
if (!isServiceProviderExist (clientName , tenantDomain )) {
161
162
throw DCRMUtils .generateClientException (
162
163
DCRMConstants .ErrorMessages .NOT_FOUND_APPLICATION_WITH_NAME , clientName );
@@ -203,10 +204,10 @@ public Application registerApplication(ApplicationRegistrationRequest registrati
203
204
*/
204
205
public void deleteApplication (String clientId ) throws DCRMException {
205
206
206
- validateRequestTenantDomain (clientId );
207
- OAuthConsumerAppDTO appDTO = getApplicationById (clientId );
207
+ String tenantDomain = getTenantDomain ();
208
+ validateRequestTenantDomain (clientId , tenantDomain );
209
+ OAuthConsumerAppDTO appDTO = getApplicationById (clientId , tenantDomain );
208
210
String applicationOwner = PrivilegedCarbonContext .getThreadLocalCarbonContext ().getUsername ();
209
- String tenantDomain = PrivilegedCarbonContext .getThreadLocalCarbonContext ().getTenantDomain ();
210
211
String spName ;
211
212
try {
212
213
spName = DCRDataHolder .getInstance ().getApplicationManagementService ()
@@ -241,9 +242,9 @@ public void deleteApplication(String clientId) throws DCRMException {
241
242
*/
242
243
public Application updateApplication (ApplicationUpdateRequest updateRequest , String clientId ) throws DCRMException {
243
244
244
- validateRequestTenantDomain ( clientId );
245
- OAuthConsumerAppDTO appDTO = getApplicationById (clientId );
246
- String tenantDomain = PrivilegedCarbonContext . getThreadLocalCarbonContext (). getTenantDomain ( );
245
+ String tenantDomain = getTenantDomain ( );
246
+ validateRequestTenantDomain (clientId , tenantDomain );
247
+ OAuthConsumerAppDTO appDTO = getApplicationById ( clientId , tenantDomain );
247
248
String applicationOwner = StringUtils .isNotBlank (updateRequest .getExtApplicationOwner ()) ?
248
249
updateRequest .getExtApplicationOwner () :
249
250
PrivilegedCarbonContext .getThreadLocalCarbonContext ().getUsername ();
@@ -426,7 +427,7 @@ public Application updateApplication(ApplicationUpdateRequest updateRequest, Str
426
427
throw DCRMUtils .generateServerException (
427
428
DCRMConstants .ErrorMessages .FAILED_TO_UPDATE_APPLICATION , clientId , e );
428
429
}
429
- OAuthConsumerAppDTO oAuthConsumerAppDTO = getApplicationById (clientId );
430
+ OAuthConsumerAppDTO oAuthConsumerAppDTO = getApplicationById (clientId , tenantDomain );
430
431
// Setting the jwksURI to be sent in the response.
431
432
oAuthConsumerAppDTO .setJwksURI (updateRequest .getJwksURI ());
432
433
Application application = buildResponse (oAuthConsumerAppDTO , tenantDomain );
@@ -489,12 +490,13 @@ private String getDisplayNameProperty(ServiceProvider serviceProvider) {
489
490
return displayNameProperty .map (ServiceProviderProperty ::getValue ).orElse (null );
490
491
}
491
492
492
- private OAuthConsumerAppDTO getApplicationById (String clientId ) throws DCRMException {
493
+ private OAuthConsumerAppDTO getApplicationById (String clientId , String tenantDomain ) throws DCRMException {
493
494
494
- return getApplicationById (clientId , true );
495
+ return getApplicationById (clientId , true , tenantDomain );
495
496
}
496
497
497
- private OAuthConsumerAppDTO getApplicationById (String clientId , boolean isApplicationRolePermissionRequired )
498
+ private OAuthConsumerAppDTO getApplicationById (String clientId , boolean isApplicationRolePermissionRequired ,
499
+ String tenantDomain )
498
500
throws DCRMException {
499
501
500
502
if (StringUtils .isEmpty (clientId )) {
@@ -504,7 +506,7 @@ private OAuthConsumerAppDTO getApplicationById(String clientId, boolean isApplic
504
506
}
505
507
506
508
try {
507
- OAuthConsumerAppDTO dto = oAuthAdminService .getOAuthApplicationData (clientId );
509
+ OAuthConsumerAppDTO dto = oAuthAdminService .getOAuthApplicationData (clientId , tenantDomain );
508
510
if (dto == null || StringUtils .isEmpty (dto .getApplicationName ())) {
509
511
throw DCRMUtils .generateClientException (
510
512
DCRMConstants .ErrorMessages .NOT_FOUND_APPLICATION_WITH_ID , clientId );
@@ -529,8 +531,7 @@ private Application createOAuthApplication(ApplicationRegistrationRequest regist
529
531
String applicationOwner = StringUtils .isNotBlank (registrationRequest .getExtApplicationOwner ()) ?
530
532
registrationRequest .getExtApplicationOwner () :
531
533
PrivilegedCarbonContext .getThreadLocalCarbonContext ().getUsername ();
532
-
533
- String tenantDomain = PrivilegedCarbonContext .getThreadLocalCarbonContext ().getTenantDomain ();
534
+ String tenantDomain = getTenantDomain ();
534
535
535
536
/*
536
537
* ApplicationOwner will be null and a server error is thrown when creating an app, if the api authentication/
@@ -566,7 +567,7 @@ private Application createOAuthApplication(ApplicationRegistrationRequest regist
566
567
}
567
568
568
569
if (StringUtils .isNotEmpty (registrationRequest .getConsumerKey ()) && isClientIdExist (
569
- registrationRequest .getConsumerKey ())) {
570
+ registrationRequest .getConsumerKey (), tenantDomain )) {
570
571
throw DCRMUtils .generateClientException (DCRMConstants .ErrorMessages .CONFLICT_EXISTING_CLIENT_ID ,
571
572
registrationRequest .getConsumerKey ());
572
573
}
@@ -941,10 +942,10 @@ private boolean isServiceProviderExist(String serviceProviderName, String tenant
941
942
* @return true if application exists with the client id.
942
943
* @throws DCRMException in case of failure.
943
944
*/
944
- private boolean isClientIdExist (String clientId ) throws DCRMException {
945
+ private boolean isClientIdExist (String clientId , String tenantDomain ) throws DCRMException {
945
946
946
947
try {
947
- OAuthConsumerAppDTO dto = oAuthAdminService .getOAuthApplicationData (clientId );
948
+ OAuthConsumerAppDTO dto = oAuthAdminService .getOAuthApplicationData (clientId , tenantDomain );
948
949
return dto != null && StringUtils .isNotBlank (dto .getApplicationName ());
949
950
} catch (IdentityOAuthAdminException e ) {
950
951
if (e .getCause () instanceof InvalidOAuthClientException ) {
@@ -1144,7 +1145,7 @@ private String escapeQueryParamsIfPresent(String redirectURI) {
1144
1145
private boolean isUserAuthorized (String clientId ) throws DCRMServerException {
1145
1146
1146
1147
try {
1147
- String tenantDomain = PrivilegedCarbonContext . getThreadLocalCarbonContext (). getTenantDomain ();
1148
+ String tenantDomain = getTenantDomain ();
1148
1149
String spName = DCRDataHolder .getInstance ().getApplicationManagementService ()
1149
1150
.getServiceProviderNameByClientId (clientId , DCRMConstants .OAUTH2 , tenantDomain );
1150
1151
String threadLocalUserName = CarbonContext .getThreadLocalCarbonContext ().getUsername ();
@@ -1176,10 +1177,10 @@ private static boolean clientIdMatchesRegex(String clientId, String clientIdVali
1176
1177
* @param clientId Consumer key of application.
1177
1178
* @throws DCRMException DCRMException
1178
1179
*/
1179
- private void validateRequestTenantDomain (String clientId ) throws DCRMException {
1180
+ private void validateRequestTenantDomain (String clientId , String tenantDomain ) throws DCRMException {
1180
1181
1181
1182
try {
1182
- String tenantDomainOfApp = OAuth2Util .getTenantDomainOfOauthApp (clientId );
1183
+ String tenantDomainOfApp = OAuth2Util .getTenantDomainOfOauthApp (clientId , tenantDomain );
1183
1184
OAuth2Util .validateRequestTenantDomain (tenantDomainOfApp );
1184
1185
} catch (InvalidOAuthClientException e ) {
1185
1186
throw new DCRMClientException (DCRMConstants .ErrorMessages .TENANT_DOMAIN_MISMATCH .getErrorCode (),
@@ -1275,4 +1276,21 @@ private void addSPProperties(Map<String, Object> spProperties, ServiceProvider s
1275
1276
}
1276
1277
serviceProvider .setSpProperties (serviceProviderProperties );
1277
1278
}
1279
+
1280
+ private static String getTenantDomain () throws DCRMServerException {
1281
+
1282
+ String tenantDomain = PrivilegedCarbonContext .getThreadLocalCarbonContext ().getTenantDomain ();
1283
+ String applicationResidentOrgId = PrivilegedCarbonContext .getThreadLocalCarbonContext ()
1284
+ .getApplicationResidentOrganizationId ();
1285
+ if (StringUtils .isNotEmpty (applicationResidentOrgId )) {
1286
+ try {
1287
+ tenantDomain = DCRDataHolder .getInstance ().getOrganizationManager ()
1288
+ .resolveTenantDomain (applicationResidentOrgId );
1289
+ } catch (OrganizationManagementException e ) {
1290
+ throw DCRMUtils .generateServerException (
1291
+ DCRMConstants .ErrorMessages .FAILED_TO_RESOLVE_TENANT_DOMAIN , applicationResidentOrgId , e );
1292
+ }
1293
+ }
1294
+ return tenantDomain ;
1295
+ }
1278
1296
}
0 commit comments