Search in sources :

Example 71 with OAuth

use of org.wso2.carbon.apimgt.rest.integration.tests.store.auth.OAuth in project carbon-apimgt by wso2.

the class OrganizationPurgeDAO method deleteApplicationList.

/**
 * Deletes Applications along with subscriptions, keys and registration data
 *
 * @param organization Organization
 * @throws APIManagementException if failed to delete applications for organization
 */
public void deleteApplicationList(String organization) throws APIManagementException {
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        connection.setAutoCommit(false);
        if (multiGroupAppSharingEnabled) {
            updateGroupIDMappingsBulk(connection, organization);
        }
        try (PreparedStatement prepStmtGetConsumerKey = connection.prepareStatement(OrganizationPurgeConstants.GET_CONSUMER_KEYS_OF_APPLICATION_LIST_SQL);
            PreparedStatement deleteDomainApp = connection.prepareStatement(SQLConstants.REMOVE_APPLICATION_FROM_DOMAIN_MAPPINGS_SQL)) {
            prepStmtGetConsumerKey.setString(1, organization);
            try (ResultSet rs = prepStmtGetConsumerKey.executeQuery()) {
                while (rs.next()) {
                    String consumerKey = rs.getString(APIConstants.FIELD_CONSUMER_KEY);
                    String keyManagerName = rs.getString("NAME");
                    String keyManagerOrganization = rs.getString("ORGANIZATION");
                    // This is true when OAuth App has been created by pasting consumer key/secret in the screen.
                    String mode = rs.getString("CREATE_MODE");
                    if (consumerKey != null) {
                        deleteDomainApp.setString(1, consumerKey);
                        deleteDomainApp.addBatch();
                        KeyManager keyManager = KeyManagerHolder.getKeyManagerInstance(keyManagerOrganization, keyManagerName);
                        if (keyManager != null) {
                            try {
                                keyManager.deleteMappedApplication(consumerKey);
                                log.info("Mapped application deleted for consumer key: " + consumerKey + " and organization: " + organization);
                            } catch (APIManagementException e) {
                                handleException("Error while Deleting Client Application for consumer key: " + consumerKey + " and organization: " + organization, e);
                            }
                        }
                        // call delete.
                        if (!APIConstants.OAuthAppMode.MAPPED.name().equals(mode)) {
                            // delete on oAuthorization server.
                            if (log.isDebugEnabled()) {
                                log.debug("Deleting Oauth application with consumer key " + consumerKey + " from the " + "Oauth server for organization: " + organization);
                            }
                            if (keyManager != null) {
                                try {
                                    keyManager.deleteApplication(consumerKey);
                                    log.info("Client application deleted for consumer key: " + consumerKey + " and organization: " + organization);
                                } catch (APIManagementException e) {
                                    handleException("Error while Deleting Client Application for organization: " + organization, e);
                                }
                            }
                        }
                    }
                }
            }
            deleteDomainApp.executeBatch();
        } catch (SQLException domainAppsException) {
            connection.rollback();
            log.error("Failed to rollback removing domain applications for organization: " + organization, domainAppsException);
        }
        if (log.isDebugEnabled()) {
            log.debug("Subscription Key mapping details are deleted successfully for Applications for " + "organization: " + organization);
        }
        try (PreparedStatement deleteApp = connection.prepareStatement(OrganizationPurgeConstants.REMOVE_APPLICATION_LIST_FROM_APPLICATIONS_SQL)) {
            deleteApp.setString(1, organization);
            deleteApp.execute();
        } catch (SQLException appDeletionException) {
            connection.rollback();
            log.error("Failed to rollback removing applications for organization: " + organization, appDeletionException);
        }
        if (log.isDebugEnabled()) {
            log.debug("Applications are deleted successfully for organization: " + organization);
        }
        connection.commit();
    } catch (SQLException e) {
        handleException("Error while removing application details from the database for organization: " + organization, e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager)

Example 72 with OAuth

use of org.wso2.carbon.apimgt.rest.integration.tests.store.auth.OAuth in project carbon-apimgt by wso2.

the class ApiMgtDAO method deleteApplication.

/**
 * Deletes an Application along with subscriptions, keys and registration data
 *
 * @param application Application object to be deleted from the database which has the application Id
 * @throws APIManagementException
 */
public void deleteApplication(Application application) throws APIManagementException {
    Connection connection = null;
    PreparedStatement deleteMappingQuery = null;
    PreparedStatement prepStmt = null;
    PreparedStatement prepStmtGetConsumerKey = null;
    PreparedStatement deleteRegistrationQuery = null;
    PreparedStatement deleteSubscription = null;
    PreparedStatement deleteDomainApp = null;
    PreparedStatement deleteAppKey = null;
    PreparedStatement deleteApp = null;
    ResultSet rs = null;
    String getSubscriptionsQuery = SQLConstants.GET_SUBSCRIPTION_ID_OF_APPLICATION_SQL;
    String getConsumerKeyQuery = SQLConstants.GET_CONSUMER_KEY_OF_APPLICATION_SQL;
    String deleteSubscriptionsQuery = SQLConstants.REMOVE_APPLICATION_FROM_SUBSCRIPTIONS_SQL;
    String deleteApplicationKeyQuery = SQLConstants.REMOVE_APPLICATION_FROM_APPLICATION_KEY_MAPPINGS_SQL;
    String deleteDomainAppQuery = SQLConstants.REMOVE_APPLICATION_FROM_DOMAIN_MAPPINGS_SQL;
    String deleteApplicationQuery = SQLConstants.REMOVE_APPLICATION_FROM_APPLICATIONS_SQL;
    String deleteRegistrationEntry = SQLConstants.REMOVE_APPLICATION_FROM_APPLICATION_REGISTRATIONS_SQL;
    boolean transactionCompleted = true;
    try {
        connection = APIMgtDBUtil.getConnection();
        connection.setAutoCommit(false);
        prepStmt = connection.prepareStatement(getSubscriptionsQuery);
        prepStmt.setInt(1, application.getId());
        rs = prepStmt.executeQuery();
        if (multiGroupAppSharingEnabled) {
            transactionCompleted = updateGroupIDMappings(connection, application.getId(), null, null);
        }
        List<Integer> subscriptions = new ArrayList<Integer>();
        while (rs.next()) {
            subscriptions.add(rs.getInt("SUBSCRIPTION_ID"));
        }
        prepStmtGetConsumerKey = connection.prepareStatement(getConsumerKeyQuery);
        prepStmtGetConsumerKey.setInt(1, application.getId());
        rs = prepStmtGetConsumerKey.executeQuery();
        deleteDomainApp = connection.prepareStatement(deleteDomainAppQuery);
        while (rs.next()) {
            String consumerKey = rs.getString(APIConstants.FIELD_CONSUMER_KEY);
            String keyManagerName = rs.getString("NAME");
            String keyManagerOrganization = rs.getString("ORGANIZATION");
            // This is true when OAuth App has been created by pasting consumer key/secret in the screen.
            String mode = rs.getString("CREATE_MODE");
            if (consumerKey != null) {
                deleteDomainApp.setString(1, consumerKey);
                deleteDomainApp.addBatch();
                KeyManager keyManager = KeyManagerHolder.getKeyManagerInstance(keyManagerOrganization, keyManagerName);
                if (keyManager != null) {
                    try {
                        keyManager.deleteMappedApplication(consumerKey);
                    } catch (APIManagementException e) {
                        log.error("Error while Deleting Client Application", e);
                    }
                }
                // call delete.
                if (!APIConstants.OAuthAppMode.MAPPED.name().equals(mode)) {
                    // delete on oAuthorization server.
                    if (log.isDebugEnabled()) {
                        log.debug("Deleting Oauth application with consumer key " + consumerKey + " from the " + "Oauth server");
                    }
                    if (keyManager != null) {
                        try {
                            keyManager.deleteApplication(consumerKey);
                        } catch (APIManagementException e) {
                            log.error("Error while Deleting Client Application", e);
                        }
                    }
                }
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Subscription Key mapping details are deleted successfully for Application - " + application.getName());
        }
        deleteRegistrationQuery = connection.prepareStatement(deleteRegistrationEntry);
        deleteRegistrationQuery.setInt(1, application.getId());
        deleteRegistrationQuery.execute();
        if (log.isDebugEnabled()) {
            log.debug("Application Registration details are deleted successfully for Application - " + application.getName());
        }
        deleteSubscription = connection.prepareStatement(deleteSubscriptionsQuery);
        deleteSubscription.setInt(1, application.getId());
        deleteSubscription.execute();
        if (log.isDebugEnabled()) {
            log.debug("Subscription details are deleted successfully for Application - " + application.getName());
        }
        deleteDomainApp.executeBatch();
        deleteAppKey = connection.prepareStatement(deleteApplicationKeyQuery);
        deleteAppKey.setInt(1, application.getId());
        deleteAppKey.execute();
        if (log.isDebugEnabled()) {
            log.debug("Application Key Mapping details are deleted successfully for Application - " + application.getName());
        }
        deleteApp = connection.prepareStatement(deleteApplicationQuery);
        deleteApp.setInt(1, application.getId());
        deleteApp.execute();
        if (log.isDebugEnabled()) {
            log.debug("Application " + application.getName() + " is deleted successfully.");
        }
        if (transactionCompleted) {
            connection.commit();
        }
    } catch (SQLException e) {
        handleException("Error while removing application details from the database", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(prepStmtGetConsumerKey, connection, rs);
        APIMgtDBUtil.closeAllConnections(prepStmt, null, rs);
        APIMgtDBUtil.closeAllConnections(deleteApp, null, null);
        APIMgtDBUtil.closeAllConnections(deleteAppKey, null, null);
        APIMgtDBUtil.closeAllConnections(deleteMappingQuery, null, null);
        APIMgtDBUtil.closeAllConnections(deleteRegistrationQuery, null, null);
        APIMgtDBUtil.closeAllConnections(deleteSubscription, null, null);
        APIMgtDBUtil.closeAllConnections(deleteDomainApp, null, null);
        APIMgtDBUtil.closeAllConnections(deleteAppKey, null, null);
        APIMgtDBUtil.closeAllConnections(deleteApp, null, null);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager)

Example 73 with OAuth

use of org.wso2.carbon.apimgt.rest.integration.tests.store.auth.OAuth in project carbon-apimgt by wso2.

the class APIMappingUtil method getScopes.

/**
 * This method returns the oauth scopes according to the given list of scopes.
 *
 * @param apiDTO list of APIScopes
 * @return scope set
 */
public static Set<Scope> getScopes(APIDTO apiDTO) {
    Set<Scope> scopeSet = new LinkedHashSet<>();
    for (APIScopeDTO apiScopeDTO : apiDTO.getScopes()) {
        Scope scope = new Scope();
        ScopeDTO scopeDTO = apiScopeDTO.getScope();
        scope.setKey(scopeDTO.getName());
        scope.setName(scopeDTO.getDisplayName());
        scope.setDescription(scopeDTO.getDescription());
        scope.setRoles(String.join(",", scopeDTO.getBindings()));
        scopeSet.add(scope);
    }
    return scopeSet;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) APIScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIScopeDTO) Scope(org.wso2.carbon.apimgt.api.model.Scope) ScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO) APIScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIScopeDTO)

Example 74 with OAuth

use of org.wso2.carbon.apimgt.rest.integration.tests.store.auth.OAuth in project carbon-apimgt by wso2.

the class APIMappingUtil method getScopes.

/**
 * This method returns the oauth scopes according to the given list of scopes.
 *
 * @param apiProductDTO list of scopes
 * @return scope set
 */
private static Set<Scope> getScopes(APIProductDTO apiProductDTO) {
    Set<Scope> scopeSet = new LinkedHashSet<>();
    for (APIScopeDTO apiScopeDTO : apiProductDTO.getScopes()) {
        Scope scope = new Scope();
        ScopeDTO scopeDTO = apiScopeDTO.getScope();
        scope.setKey(scopeDTO.getName());
        scope.setName(scopeDTO.getDisplayName());
        scope.setDescription(scopeDTO.getDescription());
        scope.setRoles(String.join(",", scopeDTO.getBindings()));
        scopeSet.add(scope);
    }
    return scopeSet;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) APIScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIScopeDTO) Scope(org.wso2.carbon.apimgt.api.model.Scope) ScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO) APIScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIScopeDTO)

Example 75 with OAuth

use of org.wso2.carbon.apimgt.rest.integration.tests.store.auth.OAuth in project carbon-apimgt by wso2.

the class RegistrationServiceImpl method createOAuthApp.

/**
 * Method to create a OAuth App with client credentials
 *
 * @param appName    application name
 * @param grantTypes grant types
 * @param userName   username of the application
 * @return created Oauth App
 */
private OAuthConsumerAppDTO createOAuthApp(String appName, OAuthApplicationInfo applicationInfo, String grantTypes, String userName) {
    OAuthConsumerAppDTO createdApp = null;
    OAuthAdminService oauthAdminService = new OAuthAdminService();
    OAuthConsumerAppDTO oauthConsumerAppDTO = new OAuthConsumerAppDTO();
    oauthConsumerAppDTO.setApplicationName(appName);
    if (StringUtils.isNotBlank(applicationInfo.getCallBackURL())) {
        oauthConsumerAppDTO.setCallbackUrl(applicationInfo.getCallBackURL());
    }
    oauthConsumerAppDTO.setUsername(userName);
    oauthConsumerAppDTO.setOAuthVersion(OAuthConstants.OAuthVersions.VERSION_2);
    oauthConsumerAppDTO.setGrantTypes(grantTypes.trim());
    try {
        boolean isHashDisabled = OAuth2Util.isHashDisabled();
        if (isHashDisabled) {
            // Creating the Oauth app
            oauthAdminService.registerOAuthApplicationData(oauthConsumerAppDTO);
            // Retrieving the created OAuth application
            createdApp = oauthAdminService.getOAuthApplicationDataByAppName(oauthConsumerAppDTO.getApplicationName());
        } else {
            createdApp = oauthAdminService.registerAndRetrieveOAuthApplicationData(oauthConsumerAppDTO);
        }
    } catch (IdentityOAuthAdminException e) {
        log.error("Error occurred while creating the OAuth app", e);
    }
    if (log.isDebugEnabled()) {
        log.debug("Created OAuth App " + appName);
    }
    return createdApp;
}
Also used : IdentityOAuthAdminException(org.wso2.carbon.identity.oauth.IdentityOAuthAdminException) OAuthAdminService(org.wso2.carbon.identity.oauth.OAuthAdminService) OAuthConsumerAppDTO(org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)26 HashMap (java.util.HashMap)18 ArrayList (java.util.ArrayList)14 Test (org.junit.Test)14 OAuthApplicationInfo (org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo)13 Map (java.util.Map)11 JSONObject (org.json.simple.JSONObject)9 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 OAuthApplicationInfo (org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo)9 JsonObject (com.google.gson.JsonObject)8 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)8 KeyManagementException (org.wso2.carbon.apimgt.core.exception.KeyManagementException)8 TokenResponse (org.wso2.carbon.apimgt.gateway.mediators.oauth.client.TokenResponse)8 LinkedHashMap (java.util.LinkedHashMap)6 Test (org.testng.annotations.Test)6 IOException (java.io.IOException)5 ParseException (org.json.simple.parser.ParseException)5 OAuthAppRequest (org.wso2.carbon.apimgt.api.model.OAuthAppRequest)5 MultiEnvironmentOverview (org.wso2.carbon.apimgt.core.configuration.models.MultiEnvironmentOverview)5 APIMAppConfigurations (org.wso2.carbon.apimgt.rest.api.authenticator.configuration.models.APIMAppConfigurations)5