use of org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin 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.model.CORSOrigin in project carbon-identity-framework by wso2.
the class CacheBackedCORSOriginDAO method addCORSOriginsToCache.
/**
* Add CORS origins of a particular application to the CORSOriginByAppId cache.
*
* @param corsOrigins The origins that should be added to the cache.
* @param applicationId The id of the application.
* @param tenantId The tenant domain specific to the cache entry.
*/
private void addCORSOriginsToCache(CORSOrigin[] corsOrigins, int applicationId, int tenantId) {
CORSOriginByAppIdCacheKey cacheKey = new CORSOriginByAppIdCacheKey(applicationId);
CORSOriginByAppIdCacheEntry cacheEntry = new CORSOriginByAppIdCacheEntry(corsOrigins);
if (log.isDebugEnabled()) {
log.debug("Adding CORS origins to Cache for application id: " + applicationId + ", tenant id: " + tenantId);
}
CORSOriginByAppIdCache.getInstance().addToCache(cacheKey, cacheEntry, tenantId);
}
use of org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin 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.model.CORSOrigin 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);
}
}
}
use of org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin in project carbon-identity-framework by wso2.
the class CORSManagementServiceTests method testGetCORSOriginsWithApplication.
@Test
public void testGetCORSOriginsWithApplication() throws CORSManagementServiceException {
try (Connection connection = IdentityDatabaseUtil.getDBConnection(true)) {
try {
for (String origin : SAMPLE_ORIGIN_LIST_1) {
try (NamedPreparedStatement namedPreparedStatement = new NamedPreparedStatement(connection, INSERT_CORS_ORIGIN)) {
// Origin is not present. Therefore add an origin.
namedPreparedStatement.setInt(1, SampleTenant.ID);
namedPreparedStatement.setString(2, origin);
namedPreparedStatement.setString(3, UUID.randomUUID().toString());
namedPreparedStatement.executeUpdate();
// Get origin id.
int corsOriginId;
try (ResultSet resultSet = namedPreparedStatement.getGeneratedKeys()) {
if (resultSet.next()) {
corsOriginId = resultSet.getInt(1);
} else {
IdentityDatabaseUtil.rollbackTransaction(connection);
throw handleServerException(ERROR_CODE_CORS_ADD, SampleTenant.DOMAIN_NAME);
}
}
try (PreparedStatement preparedStatement3 = connection.prepareStatement(INSERT_CORS_ASSOCIATION)) {
// Add application associations.
preparedStatement3.setInt(1, corsOriginId);
preparedStatement3.setInt(2, SampleApp1.ID);
preparedStatement3.executeUpdate();
}
}
}
} catch (SQLException e) {
IdentityDatabaseUtil.rollbackTransaction(connection);
throw handleServerException(ERROR_CODE_CORS_ADD, e, IdentityTenantUtil.getTenantDomain(SampleTenant.ID));
}
IdentityDatabaseUtil.commitTransaction(connection);
} catch (SQLException e) {
throw handleServerException(ERROR_CODE_CORS_ADD, e, SampleTenant.DOMAIN_NAME);
}
List<String> retrievedOrigins = corsManagementService.getApplicationCORSOrigins(SampleApp1.UUID, SampleTenant.DOMAIN_NAME).stream().map(CORSOrigin::getOrigin).collect(Collectors.toList());
assertEquals(retrievedOrigins, SAMPLE_ORIGIN_LIST_1);
}
Aggregations