Search in sources :

Example 46 with NamedPreparedStatement

use of org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement in project carbon-identity-framework by wso2.

the class CORSOriginDAOImpl method getCORSOriginsByTenantId.

/**
 * {@inheritDoc}
 */
@Override
public List<CORSOrigin> getCORSOriginsByTenantId(int tenantId) throws CORSManagementServiceServerException {
    String tenantDomain = IdentityTenantUtil.getTenantDomain(tenantId);
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(false);
        NamedPreparedStatement namedPreparedStatement = new NamedPreparedStatement(connection, GET_CORS_ORIGINS_BY_TENANT_ID)) {
        namedPreparedStatement.setInt(1, tenantId);
        try (ResultSet resultSet = namedPreparedStatement.executeQuery()) {
            List<CORSOrigin> corsOrigins = new ArrayList<>();
            while (resultSet.next()) {
                CORSOrigin corsOrigin = new CORSOrigin();
                corsOrigin.setOrigin(resultSet.getString(ORIGIN));
                corsOrigin.setId(resultSet.getString(UNIQUE_ID));
                corsOrigins.add(corsOrigin);
            }
            return corsOrigins;
        }
    } catch (SQLException e) {
        throw handleServerException(ERROR_CODE_CORS_RETRIEVE, e, tenantDomain);
    }
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) CORSOrigin(org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList)

Example 47 with NamedPreparedStatement

use of org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement in project carbon-identity-framework by wso2.

the class CORSOriginDAOImpl method deleteCORSOrigins.

/**
 * {@inheritDoc}
 */
@Override
public void deleteCORSOrigins(int applicationId, List<String> corsOriginIds, int tenantId) throws CORSManagementServiceServerException {
    String currentId = null;
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(true)) {
        try {
            for (String corsOriginId : corsOriginIds) {
                currentId = corsOriginId;
                try (NamedPreparedStatement namedPreparedStatement1 = new NamedPreparedStatement(connection, GET_CORS_ORIGIN_ID_BY_UUID)) {
                    namedPreparedStatement1.setString(1, corsOriginId);
                    try (ResultSet resultSet = namedPreparedStatement1.executeQuery()) {
                        if (resultSet.next()) {
                            int corsOriginDbId = resultSet.getInt(CORSOriginTableColumns.ID);
                            // Delete application association.
                            try (PreparedStatement preparedStatement2 = connection.prepareStatement(DELETE_CORS_APPLICATION_ASSOCIATION)) {
                                preparedStatement2.setInt(1, corsOriginDbId);
                                preparedStatement2.setInt(2, applicationId);
                                preparedStatement2.executeUpdate();
                            }
                        } else {
                            IdentityDatabaseUtil.rollbackTransaction(connection);
                            throw handleServerException(ERROR_CODE_CORS_DELETE, currentId);
                        }
                    }
                }
            }
        // Cleanup dangling origins (origins without any association to an application) is disabled temporary.
        // Even the CORS Origins are stored for each application separately, the CORS valve filters them
        // based on the tenant level. Because of that there might be other applications which are not configured
        // allowed origins but still working as another application has already set is as an allowed origin.
        // Related issue: https://github.com/wso2/product-is/issues/11241
        // cleanupDanglingOrigins(connection, tenantId);
        } catch (SQLException e) {
            IdentityDatabaseUtil.rollbackTransaction(connection);
            throw handleServerException(ERROR_CODE_CORS_DELETE, e, currentId);
        }
        // Commit the transaction as no errors were thrown.
        IdentityDatabaseUtil.commitTransaction(connection);
    } catch (SQLException e) {
        throw handleServerException(ERROR_CODE_CORS_DELETE, e, currentId);
    }
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) PreparedStatement(java.sql.PreparedStatement)

Example 48 with NamedPreparedStatement

use of org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuthScopeDAOImpl method getPreparedStatementForGetAllScopesWithPagination.

/**
 * Get SQL statement for get all scope with pagination. (including OAuth2 scopes and OIDC scopes).
 *
 * @param offset   Offset.
 * @param limit    Limit.
 * @param tenantID Tenet ID.
 * @param conn     Database connection.
 * @return
 * @throws SQLException
 */
private NamedPreparedStatement getPreparedStatementForGetAllScopesWithPagination(Integer offset, Integer limit, int tenantID, Connection conn) throws SQLException {
    String query;
    String driverName = conn.getMetaData().getDriverName();
    if (driverName.contains("MySQL") || driverName.contains("MariaDB") || driverName.contains("H2")) {
        query = SQLQueries.RETRIEVE_ALL_SCOPES_WITH_PAGINATION_MYSQL;
    } else if (conn.getMetaData().getDatabaseProductName().contains("DB2")) {
        query = SQLQueries.RETRIEVE_ALL_SCOPES_WITH_PAGINATION_DB2SQL;
    } else if (driverName.contains("MS SQL")) {
        query = SQLQueries.RETRIEVE_ALL_SCOPES_WITH_PAGINATION_MSSQL;
    } else if (driverName.contains("Microsoft") || driverName.contains("microsoft")) {
        query = SQLQueries.RETRIEVE_ALL_SCOPES_WITH_PAGINATION_MSSQL;
    } else if (driverName.contains("PostgreSQL")) {
        query = SQLQueries.RETRIEVE_ALL_SCOPES_WITH_PAGINATION_POSTGRESQL;
    } else if (driverName.contains("Informix")) {
        // Driver name = "IBM Informix JDBC Driver for IBM Informix Dynamic Server"
        query = SQLQueries.RETRIEVE_ALL_SCOPES_WITH_PAGINATION_INFORMIX;
    } else {
        query = SQLQueries.RETRIEVE_ALL_SCOPES_WITH_PAGINATION_ORACLE;
    }
    NamedPreparedStatement namedPreparedStatement = new NamedPreparedStatement(conn, query);
    namedPreparedStatement.setInt(Oauth2ScopeConstants.SQLPlaceholders.TENANT_ID, tenantID);
    namedPreparedStatement.setInt(Oauth2ScopeConstants.SQLPlaceholders.OFFSET, offset);
    namedPreparedStatement.setInt(Oauth2ScopeConstants.SQLPlaceholders.LIMIT, limit);
    return namedPreparedStatement;
}
Also used : NamedPreparedStatement(org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement)

Example 49 with NamedPreparedStatement

use of org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuthScopeDAOImpl method getScopesWithPagination.

@Override
public Set<Scope> getScopesWithPagination(Integer offset, Integer limit, int tenantID, Boolean includeOIDCScopes) throws IdentityOAuth2ScopeServerException {
    if (log.isDebugEnabled()) {
        log.debug("Get all scopes with pagination for tenantId  :" + tenantID + " including OIDC scope: " + includeOIDCScopes);
    }
    Set<Scope> scopes = new HashSet<>();
    Map<Integer, Scope> scopeMap = new HashMap<>();
    try (Connection conn = IdentityDatabaseUtil.getDBConnection(false)) {
        NamedPreparedStatement namedPreparedStatement;
        if (includeOIDCScopes) {
            namedPreparedStatement = getPreparedStatementForGetAllScopesWithPagination(offset, limit, tenantID, conn);
        } else {
            namedPreparedStatement = getPreparedStatementForGetScopesWithPagination(offset, limit, tenantID, conn);
        }
        try (PreparedStatement preparedStatement = namedPreparedStatement.getPreparedStatement()) {
            try (ResultSet rs = preparedStatement.executeQuery()) {
                while (rs.next()) {
                    int scopeID = rs.getInt(1);
                    String name = rs.getString(2);
                    String displayName = rs.getString(3);
                    String description = rs.getString(4);
                    final String binding = rs.getString(5);
                    if (scopeMap.containsKey(scopeID) && scopeMap.get(scopeID) != null) {
                        scopeMap.get(scopeID).setName(name);
                        scopeMap.get(scopeID).setDescription(description);
                        scopeMap.get(scopeID).setDisplayName(displayName);
                        if (binding != null) {
                            if (scopeMap.get(scopeID).getBindings() != null) {
                                scopeMap.get(scopeID).addBinding(binding);
                            } else {
                                scopeMap.get(scopeID).setBindings(new ArrayList<String>() {

                                    {
                                        add(binding);
                                    }
                                });
                            }
                        }
                    } else {
                        scopeMap.put(scopeID, new Scope(name, displayName, description, new ArrayList<String>()));
                        if (binding != null) {
                            scopeMap.get(scopeID).addBinding(binding);
                        }
                    }
                }
            }
        }
        for (Map.Entry<Integer, Scope> entry : scopeMap.entrySet()) {
            scopes.add(entry.getValue());
        }
        return scopes;
    } catch (SQLException e) {
        String msg = "Error occurred while getting all scopes with pagination ";
        throw new IdentityOAuth2ScopeServerException(msg, e);
    }
}
Also used : IdentityOAuth2ScopeServerException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeServerException) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) NamedPreparedStatement(org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement) PreparedStatement(java.sql.PreparedStatement) NamedPreparedStatement(org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement) Scope(org.wso2.carbon.identity.oauth2.bean.Scope) ResultSet(java.sql.ResultSet) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

NamedPreparedStatement (org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement)45 SQLException (java.sql.SQLException)40 Connection (java.sql.Connection)39 ResultSet (java.sql.ResultSet)33 IdentityRoleManagementServerException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementServerException)17 ArrayList (java.util.ArrayList)12 IdentityRoleManagementClientException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementClientException)12 PreparedStatement (java.sql.PreparedStatement)7 CORSOrigin (org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Test (org.testng.annotations.Test)6 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)6 HashMap (java.util.HashMap)5 IdentityApplicationManagementServerException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementServerException)4 ApplicationBasicInfo (org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo)4 RoleBasicInfo (org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo)4 NamedPreparedStatement (org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement)3 IdentityRoleManagementException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException)3 UserRealm (org.wso2.carbon.user.api.UserRealm)3 UserStoreException (org.wso2.carbon.user.api.UserStoreException)3