Search in sources :

Example 16 with CORSOrigin

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;
    }
}
Also used : CORSOriginCacheEntry(org.wso2.carbon.identity.cors.mgt.core.internal.cache.CORSOriginCacheEntry) CORSOriginCacheKey(org.wso2.carbon.identity.cors.mgt.core.internal.cache.CORSOriginCacheKey) CORSOriginCache(org.wso2.carbon.identity.cors.mgt.core.internal.cache.CORSOriginCache)

Example 17 with CORSOrigin

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);
    }
}
Also used : CORSOrigin(org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin) CORSManagementServiceClientException(org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceClientException) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) CORSManagementServiceException(org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceException) CORSManagementService(org.wso2.carbon.identity.cors.mgt.core.CORSManagementService)

Example 18 with CORSOrigin

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);
    }
}
Also used : CORSOrigin(org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin) IdentityOAuthAdminException(org.wso2.carbon.identity.oauth.IdentityOAuthAdminException) OpenIDConnectConfiguration(org.wso2.carbon.identity.api.server.application.management.v1.OpenIDConnectConfiguration) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) OAuthConsumerAppDTO(org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO) CORSManagementServiceException(org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceException)

Example 19 with CORSOrigin

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);
    }
}
Also used : CORSOrigin(org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin) IdentityOAuthAdminException(org.wso2.carbon.identity.oauth.IdentityOAuthAdminException) OAuthConsumerAppDTO(org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO) CORSManagementServiceException(org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceException)

Example 20 with CORSOrigin

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;
}
Also used : CORSOriginObject(org.wso2.carbon.identity.api.server.cors.v1.model.CORSOriginObject)

Aggregations

CORSOrigin (org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin)14 Connection (java.sql.Connection)9 SQLException (java.sql.SQLException)9 NamedPreparedStatement (org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement)9 ResultSet (java.sql.ResultSet)8 ArrayList (java.util.ArrayList)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 Test (org.testng.annotations.Test)5 ApplicationBasicInfo (org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo)5 PreparedStatement (java.sql.PreparedStatement)4 CORSManagementServiceException (org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceException)3 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)2 Origin (org.wso2.carbon.identity.cors.mgt.core.model.Origin)2 IdentityOAuthAdminException (org.wso2.carbon.identity.oauth.IdentityOAuthAdminException)2 OAuthConsumerAppDTO (org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1