use of org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin in project carbon-identity-framework by wso2.
the class CacheBackedCORSOriginDAO method getCORSOriginsFromCache.
/**
* Get CORS origins from the cache.
*
* @param tenantId The tenant id specific to the cache entry.
* @return Returns an array of {@code Origin}(s) if the cached origins are found for the tenant.
* Else return {@code null}.
*/
private CORSOrigin[] getCORSOriginsFromCache(int tenantId) {
CORSOriginCacheKey cacheKey = new CORSOriginCacheKey(tenantId);
CORSOriginCache cache = CORSOriginCache.getInstance();
CORSOriginCacheEntry cacheEntry = cache.getValueFromCache(cacheKey, tenantId);
if (cacheEntry != null && cacheEntry.getValidatedOrigins() != null) {
return cacheEntry.getValidatedOrigins();
} else {
if (log.isDebugEnabled()) {
log.debug("Cache entry not found for cache key:" + tenantId);
}
return null;
}
}
use of org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin in project identity-api-server by wso2.
the class ServerApplicationManagementService method deleteApplication.
public void deleteApplication(String applicationId) {
String username = ContextLoader.getUsernameFromContext();
String tenantDomain = ContextLoader.getTenantDomainFromContext();
CORSManagementService corsManagementService = ApplicationManagementServiceHolder.getCorsManagementService();
try {
// Delete CORS origins for OIDC Apps.
List<CORSOrigin> existingCORSOrigins = corsManagementService.getApplicationCORSOrigins(applicationId, tenantDomain);
if (!CollectionUtils.isEmpty(existingCORSOrigins)) {
ApplicationManagementServiceHolder.getCorsManagementService().deleteCORSOrigins(applicationId, existingCORSOrigins.stream().map(CORSOrigin::getId).collect(Collectors.toList()), tenantDomain);
}
// Delete Application.
getApplicationManagementService().deleteApplicationByResourceId(applicationId, tenantDomain, username);
} catch (IdentityApplicationManagementException e) {
String msg = "Error deleting application with id: " + applicationId;
throw handleIdentityApplicationManagementException(e, msg);
} catch (CORSManagementServiceClientException e) {
/*
For not existing application scenarios the following error code will be returned. To preserve the behaviour
we need to return 204.
*/
if (ERROR_CODE_INVALID_APP_ID.getCode().equals(e.getErrorCode())) {
if (log.isDebugEnabled()) {
log.debug("Invalid application id: " + applicationId, e);
}
return;
}
String msg = "Error while trying to remove CORS origins associated with the application: " + applicationId;
throw Utils.buildClientError(e.getErrorCode(), msg, e.getMessage());
} catch (CORSManagementServiceException e) {
String msg = "Error while trying to remove CORS origins associated with the application: " + applicationId;
throw Utils.buildServerError(msg, e);
}
}
use of org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin in project identity-api-server by wso2.
the class OAuthInboundFunctions method getOAuthConfiguration.
public static OpenIDConnectConfiguration getOAuthConfiguration(InboundAuthenticationRequestConfig inboundAuth) {
String clientId = inboundAuth.getInboundAuthKey();
try {
OAuthConsumerAppDTO oauthApp = ApplicationManagementServiceHolder.getOAuthAdminService().getOAuthApplicationData(clientId);
OpenIDConnectConfiguration openIDConnectConfiguration = new OAuthConsumerAppToApiModel().apply(oauthApp);
// Set CORS origins as allowed domains.
String tenantDomain = ContextLoader.getTenantDomainFromContext();
String applicationResourceId = ApplicationManagementServiceHolder.getApplicationManagementService().getServiceProviderByClientId(clientId, OAUTH2, tenantDomain).getApplicationResourceId();
List<CORSOrigin> corsOriginList = ApplicationManagementServiceHolder.getCorsManagementService().getApplicationCORSOrigins(applicationResourceId, tenantDomain);
openIDConnectConfiguration.setAllowedOrigins(corsOriginList.stream().map(CORSOrigin::getOrigin).collect(Collectors.toList()));
return openIDConnectConfiguration;
} catch (IdentityOAuthAdminException | IdentityApplicationManagementException | CORSManagementServiceException e) {
throw buildServerError("Error while retrieving oauth application for clientId: " + clientId, e);
}
}
use of org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin in project identity-api-server by wso2.
the class OAuthInboundFunctions method putOAuthInbound.
public static InboundAuthenticationRequestConfig putOAuthInbound(ServiceProvider application, OpenIDConnectConfiguration oidcConfigModel) {
String tenantDomain = ContextLoader.getTenantDomainFromContext();
List<String> existingCORSOrigins = null;
// First we identify whether this is a insert or update.
try {
String currentClientId = InboundFunctions.getInboundAuthKey(application, StandardInboundProtocols.OAUTH2);
// Retrieve the existing CORS origins for the application.
existingCORSOrigins = ApplicationManagementServiceHolder.getCorsManagementService().getApplicationCORSOrigins(application.getApplicationResourceId(), tenantDomain).stream().map(CORSOrigin::getOrigin).collect(Collectors.toList());
// Update the CORS origins.
List<String> corsOrigins = oidcConfigModel.getAllowedOrigins();
ApplicationManagementServiceHolder.getCorsManagementService().setCORSOrigins(application.getApplicationResourceId(), corsOrigins, tenantDomain);
if (currentClientId != null) {
// Update an existing application.
OAuthConsumerAppDTO oauthApp = ApplicationManagementServiceHolder.getOAuthAdminService().getOAuthApplicationData(currentClientId);
if (!StringUtils.equals(oauthApp.getOauthConsumerKey(), oidcConfigModel.getClientId())) {
throw buildBadRequestError("Invalid ClientID provided for update.");
}
if (!StringUtils.equals(oauthApp.getOauthConsumerSecret(), oidcConfigModel.getClientSecret())) {
throw buildBadRequestError("Invalid ClientSecret provided for update.");
}
OAuthConsumerAppDTO appToUpdate = new ApiModelToOAuthConsumerApp().apply(application.getApplicationName(), oidcConfigModel);
ApplicationManagementServiceHolder.getOAuthAdminService().updateConsumerApplication(appToUpdate);
String updatedClientId = appToUpdate.getOauthConsumerKey();
return createInboundAuthRequestConfig(updatedClientId);
} else {
// Create a new application.
return createOAuthInbound(application.getApplicationName(), oidcConfigModel);
}
} catch (IdentityOAuthAdminException e) {
/*
If an IdentityOAuthAdminException exception is thrown after the CORS update, then the application
update has failed. Therefore rollback the update on CORS origins.
*/
try {
ApplicationManagementServiceHolder.getCorsManagementService().setCORSOrigins(application.getApplicationResourceId(), existingCORSOrigins, tenantDomain);
} catch (CORSManagementServiceException corsManagementServiceException) {
throw handleException(e);
}
throw handleException(e);
} catch (CORSManagementServiceException e) {
throw handleException(e);
}
}
use of org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin in project identity-api-server by wso2.
the class CORSOriginToCORSOriginObject method apply.
@Override
public CORSOriginObject apply(CORSOrigin corsOrigin) {
CORSOriginObject corsOriginGetObject = new CORSOriginObject();
corsOriginGetObject.setId(corsOrigin.getId());
corsOriginGetObject.setUrl(corsOrigin.getOrigin());
return corsOriginGetObject;
}
Aggregations