use of org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceException in project carbon-identity-framework by wso2.
the class CORSManagementServiceImpl method setCORSOrigins.
/**
* {@inheritDoc}
*/
@Override
public void setCORSOrigins(String applicationId, List<String> origins, String tenantDomain) throws CORSManagementServiceException {
int tenantId = getTenantId(tenantDomain);
ApplicationBasicInfo applicationBasicInfo = getApplicationBasicInfo(applicationId, tenantDomain);
// Check for duplicate entries.
if (CORSConfigurationUtils.hasDuplicates(origins)) {
throw handleClientException(ERROR_CODE_DUPLICATE_ORIGINS);
}
List<Origin> originList = CORSConfigurationUtils.createOriginList(origins);
// Set the CORS origins.
getCORSOriginDAO().setCORSOrigins(applicationBasicInfo.getApplicationId(), originList.stream().map(origin -> {
// Create the CORS origin.
CORSOrigin corsOrigin = new CORSOrigin();
corsOrigin.setOrigin(origin.getValue());
return corsOrigin;
}).collect(Collectors.toList()), tenantId);
}
use of org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceException in project carbon-identity-framework by wso2.
the class CORSManagementServiceImpl method addCORSOrigins.
/**
* {@inheritDoc}
*/
@Override
public void addCORSOrigins(String applicationId, List<String> origins, String tenantDomain) throws CORSManagementServiceException {
int tenantId = getTenantId(tenantDomain);
ApplicationBasicInfo applicationBasicInfo = getApplicationBasicInfo(applicationId, tenantDomain);
List<Origin> originList = CORSConfigurationUtils.createOriginList(origins);
// Check if the CORS origins are already present.
List<CORSOrigin> existingCORSOrigins = getCORSOriginDAO().getCORSOriginsByApplicationId(applicationBasicInfo.getApplicationId(), tenantId);
List<String> corsOriginIdList = existingCORSOrigins.stream().map(CORSOrigin::getId).collect(Collectors.toList());
for (Origin origin : originList) {
if (corsOriginIdList.contains(origin.getValue())) {
// CORS origin is already registered for the application.
if (log.isDebugEnabled()) {
log.debug(String.format("Duplicate addition of existing CORS Origin (%s) for the " + "application id: %s, tenant domain: %s", origin, applicationId, tenantDomain));
}
throw handleClientException(ERROR_CODE_ORIGIN_PRESENT, tenantDomain, origin.getValue());
}
}
// Add the CORS origins.
getCORSOriginDAO().addCORSOrigins(applicationBasicInfo.getApplicationId(), originList.stream().map(origin -> {
// Create the CORS origin.
CORSOrigin corsOrigin = new CORSOrigin();
corsOrigin.setOrigin(origin.getValue());
return corsOrigin;
}).collect(Collectors.toList()), tenantId);
}
use of org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceException in project carbon-identity-framework by wso2.
the class CORSManagementServiceImpl method getApplicationCORSOrigins.
/**
* {@inheritDoc}
*/
@Override
public List<CORSOrigin> getApplicationCORSOrigins(String applicationId, String tenantDomain) throws CORSManagementServiceException {
int tenantId = getTenantId(tenantDomain);
ApplicationBasicInfo applicationBasicInfo = getApplicationBasicInfo(applicationId, tenantDomain);
return Collections.unmodifiableList(getCORSOriginDAO().getCORSOriginsByApplicationId(applicationBasicInfo.getApplicationId(), tenantId));
}
use of org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceException in project carbon-identity-framework by wso2.
the class CORSManagementServiceTests method testAddCORSOrigins.
@Test
public void testAddCORSOrigins() throws CORSManagementServiceException {
corsManagementService.addCORSOrigins(SampleApp1.UUID, SAMPLE_ORIGIN_LIST_1, SUPER_TENANT_DOMAIN_NAME);
List<CORSOrigin> retrievedCORSOrigins = new ArrayList<>();
try (Connection connection = IdentityDatabaseUtil.getDBConnection(false)) {
try (NamedPreparedStatement namedPreparedStatement = new NamedPreparedStatement(connection, GET_CORS_ORIGINS_BY_APPLICATION_ID)) {
namedPreparedStatement.setInt(1, SUPER_TENANT_ID);
namedPreparedStatement.setInt(2, SampleApp1.ID);
try (ResultSet resultSet = namedPreparedStatement.executeQuery()) {
while (resultSet.next()) {
CORSOrigin corsOrigin = new CORSOrigin();
corsOrigin.setId(resultSet.getString(CORSOriginTableColumns.ID));
corsOrigin.setOrigin(resultSet.getString(CORSOriginTableColumns.ORIGIN));
retrievedCORSOrigins.add(corsOrigin);
}
}
}
} catch (SQLException e) {
throw handleServerException(ERROR_CODE_CORS_RETRIEVE, e, SUPER_TENANT_DOMAIN_NAME);
}
assertEquals(retrievedCORSOrigins.stream().map(CORSOrigin::getOrigin).collect(Collectors.toList()), SAMPLE_ORIGIN_LIST_1);
}
use of org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceException in project identity-api-server by wso2.
the class ServerApplicationManagementService method deleteInbound.
private void deleteInbound(String applicationId, String inboundType) {
ServiceProvider appToUpdate;
try {
appToUpdate = cloneApplication(applicationId);
} catch (APIError e) {
if (ErrorMessage.APPLICATION_NOT_FOUND.getCode().equals(e.getCode())) {
// Ignoring the delete operation and return 204 response code, since the resource does not exist.
return;
}
throw e;
}
InboundAuthenticationConfig inboundAuthConfig = appToUpdate.getInboundAuthenticationConfig();
if (ArrayUtils.isNotEmpty(inboundAuthConfig.getInboundAuthenticationRequestConfigs())) {
// Remove the deleted inbound type by filtering it out of the available inbounds and doing an update.
InboundAuthenticationRequestConfig[] filteredInbounds = Arrays.stream(inboundAuthConfig.getInboundAuthenticationRequestConfigs()).filter(inbound -> !StringUtils.equals(inboundType, inbound.getInboundAuthType())).toArray(InboundAuthenticationRequestConfig[]::new);
appToUpdate.getInboundAuthenticationConfig().setInboundAuthenticationRequestConfigs(filteredInbounds);
updateServiceProvider(applicationId, appToUpdate);
}
// Delete the associated CORS origins if the inboundType is oauth2.
if (inboundType.equals(OAUTH2)) {
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
CORSManagementService corsManagementService = ApplicationManagementServiceHolder.getCorsManagementService();
try {
List<CORSOrigin> existingCORSOrigins = corsManagementService.getApplicationCORSOrigins(applicationId, tenantDomain);
corsManagementService.deleteCORSOrigins(applicationId, existingCORSOrigins.stream().map(CORSOrigin::getId).collect(Collectors.toList()), tenantDomain);
} catch (CORSManagementServiceException e) {
log.error("Error while trying to remove CORS origins associated with the application.", e);
}
}
}
Aggregations