private void addUserDataValue(String userName, int tenantId, String key, String value) throws SQLException { Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement prepStmt = null; try { prepStmt = connection.prepareStatement(SQLQuery.STORE_USER_DATA); prepStmt.setInt(1, tenantId); prepStmt.setString(2, userName); prepStmt.setString(3, key); prepStmt.setString(4, value); prepStmt.execute(); connection.commit(); } finally { IdentityDatabaseUtil.closeStatement(prepStmt); IdentityDatabaseUtil.closeConnection(connection); } }
public void removeConsumerApplication(String consumerKey) throws IdentityOAuthAdminException { Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement prepStmt = null; try { prepStmt = connection.prepareStatement(SQLQueries.OAuthAppDAOSQLQueries.REMOVE_APPLICATION); prepStmt.setString(1, consumerKey); prepStmt.execute(); connection.commit(); } catch (SQLException e) {; throw new IdentityOAuthAdminException("Error when executing the SQL : " + SQLQueries.OAuthAppDAOSQLQueries.REMOVE_APPLICATION, e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, null, prepStmt); } }
public static void closeAllConnections(Connection dbConnection, ResultSet rs, PreparedStatement prepStmt) { closeResultSet(rs); closeStatement(prepStmt); closeConnection(dbConnection); }
/** * Remove Oauth consumer app related properties. * * @param tenantDomain application tenant domain * @param consumerKey client ID * @throws IdentityOAuthAdminException */ public void removeOIDCProperties(String tenantDomain, String consumerKey) throws IdentityOAuthAdminException { Connection connection = IdentityDatabaseUtil.getDBConnection(); try { removeOauthOIDCPropertyTable(connection, tenantDomain, consumerKey); connection.commit(); } catch (SQLException e) { String errorMsg = "Error removing OIDC properties for client ID: " + consumerKey + " and tenant domain: " + tenantDomain; IdentityDatabaseUtil.rollBack(connection); throw new IdentityOAuthAdminException(errorMsg, e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, null, null); } }
private Map<String, String> getClaimProperties(Connection connection, int localClaimId, int tenantId) throws ClaimMetadataException { Map<String, String> claimProperties = new HashMap<>(); PreparedStatement prepStmt = null; ResultSet rs = null; String query = SQLConstants.GET_CLAIM_PROPERTIES; try { prepStmt = connection.prepareStatement(query); prepStmt.setInt(1, localClaimId); prepStmt.setInt(2, tenantId); rs = prepStmt.executeQuery(); while (rs.next()) { String claimPropertyName = rs.getString(SQLConstants.PROPERTY_NAME_COLUMN); String claimPropertyValue = rs.getString(SQLConstants.PROPERTY_VALUE_COLUMN); claimProperties.put(claimPropertyName, claimPropertyValue); } } catch (SQLException e) { throw new ClaimMetadataException("Error while retrieving claim properties", e); } finally { IdentityDatabaseUtil.closeResultSet(rs); IdentityDatabaseUtil.closeStatement(prepStmt); } return claimProperties; }
/** * Get a database connection instance from the Identity Persistence Manager * @return Database Connection */ public static Connection getDBConnection(){ return IdentityDatabaseUtil.getDBConnection(); }
public void addLocalClaim(LocalClaim localClaim, int tenantId) throws ClaimMetadataException { Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement prepStmt = null; String localClaimURI = localClaim.getClaimURI(); try { // Start transaction connection.setAutoCommit(false); int localClaimId = addClaim(connection, ClaimConstants.LOCAL_CLAIM_DIALECT_URI, localClaimURI, tenantId); // Some JDBC Drivers returns this in the result, some don't if (localClaimId == 0) { if (log.isDebugEnabled()) { log.debug("JDBC Driver did not return the claimId, executing Select operation"); } localClaimId = getClaimId(connection, ClaimConstants.LOCAL_CLAIM_DIALECT_URI, localClaimURI, tenantId); // TODO : Handle invalid local claim URI } addClaimAttributeMappings(connection, localClaimId, localClaim.getMappedAttributes(), tenantId); addClaimProperties(connection, localClaimId, localClaim.getClaimProperties(), tenantId); // End transaction connection.commit(); } catch (SQLException e) { IdentityDatabaseUtil.rollBack(connection); throw new ClaimMetadataException("Error while adding local claim " + localClaimURI, e); } finally { IdentityDatabaseUtil.closeConnection(connection); } }
/** * Get permission id for a given permission path * * @param permission Permission path * @return Permission id * @throws SQLException */ private int getPermissionId(String permission) throws SQLException { PreparedStatement loadPermissionsPrepStmt = null; ResultSet resultSet = null; Connection connection = null; int id = -1; try { connection = IdentityDatabaseUtil.getUserDBConnection(); loadPermissionsPrepStmt = connection.prepareStatement(ApplicationMgtDBQueries.LOAD_UM_PERMISSIONS_W); loadPermissionsPrepStmt.setString(1, permission.toLowerCase()); resultSet = loadPermissionsPrepStmt.executeQuery(); if (resultSet.next()) { id = resultSet.getInt(1); } } finally { IdentityDatabaseUtil.closeResultSet(resultSet); IdentityDatabaseUtil.closeStatement(loadPermissionsPrepStmt); IdentityDatabaseUtil.closeConnection(connection); } return id; }
public void deactivateAuthorizationCode(AuthzCodeDO authzCodeDO) throws IdentityOAuth2Exception { Connection connection = IdentityDatabaseUtil.getDBConnection(); try { deactivateAuthorizationCode(authzCodeDO, connection); connection.commit(); } catch (SQLException e) { throw new IdentityOAuth2Exception("Error when deactivating authorization code", e); } finally { IdentityDatabaseUtil.closeConnection(connection); } }
public static void closeStatement(PreparedStatement preparedStatement) { IdentityDatabaseUtil.closeStatement(preparedStatement); }
private void updateAccessTokenState(Connection connection, String tokenId, String tokenState, String tokenStateId, String userStoreDomain) throws IdentityOAuth2Exception { PreparedStatement prepStmt = null; try { if (log.isDebugEnabled()) { log.debug("Changing status of access token with id: " + tokenId + " to: " + tokenState + " userStoreDomain: " + userStoreDomain); } String sql = SQLQueries.UPDATE_TOKE_STATE; sql = OAuth2Util.getTokenPartitionedSqlByUserStore(sql, userStoreDomain); prepStmt = connection.prepareStatement(sql); prepStmt.setString(1, tokenState); prepStmt.setString(2, tokenStateId); prepStmt.setString(3, tokenId); prepStmt.executeUpdate(); OAuth2TokenUtil.postUpdateAccessToken(tokenId, tokenState); } catch (SQLException e) { IdentityDatabaseUtil.rollBack(connection); throw new IdentityOAuth2Exception("Error while updating Access Token with ID : " + tokenId + " to Token State : " + tokenState, e); } finally { IdentityDatabaseUtil.closeStatement(prepStmt); } }
/** * Delete permission entry for a given id * * @param entry_id Entry id * @throws SQLException */ private void deletePermission(int entry_id) throws SQLException { PreparedStatement deletePermissionPrepStmt = null; Connection connection = null; try { connection = IdentityDatabaseUtil.getUserDBConnection(); deletePermissionPrepStmt = connection.prepareStatement(ApplicationMgtDBQueries.REMOVE_UM_PERMISSIONS); deletePermissionPrepStmt.setInt(1, entry_id); deletePermissionPrepStmt.executeUpdate(); } finally { IdentityApplicationManagementUtil.closeStatement(deletePermissionPrepStmt); IdentityDatabaseUtil.closeConnection(connection); } }
private void deactivateAuthorizationCode(AuthzCodeDO authzCodeDO, Connection connection) throws IdentityOAuth2Exception { PreparedStatement prepStmt = null; try { prepStmt = connection.prepareStatement(SQLQueries.DEACTIVATE_AUTHZ_CODE_AND_INSERT_CURRENT_TOKEN); prepStmt.setString(1, authzCodeDO.getOauthTokenId()); prepStmt.setString(2, persistenceProcessor.getPreprocessedAuthzCode(authzCodeDO.getAuthorizationCode())); prepStmt.executeUpdate(); } catch (SQLException e) { throw new IdentityOAuth2Exception("Error when deactivating authorization code", e); } finally { IdentityDatabaseUtil.closeAllConnections(null, null, prepStmt); } }
/** * @param rs */ public static void closeResultSet(ResultSet rs) { IdentityDatabaseUtil.closeResultSet(rs); }
/** * Utility method to close a database connection * * @param dbConnection Database <code>Connection</code> object */ public static void closeConnection(Connection dbConnection) { IdentityDatabaseUtil.closeConnection(dbConnection); }
/** * Utility method to rollback a database connection * * @param dbConnection Database <code>Connection</code> object */ public static void rollBack(Connection dbConnection) { IdentityDatabaseUtil.rollBack(dbConnection); }
public Set<String> getAuthorizationCodesForConsumerKey(String consumerKey) throws IdentityOAuth2Exception { Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement ps = null; ResultSet rs = null; Set<String> authorizationCodes = new HashSet<>(); try { String sqlQuery = SQLQueries.GET_AUTHORIZATION_CODES_FOR_CONSUMER_KEY; ps = connection.prepareStatement(sqlQuery); ps.setString(1, consumerKey); rs = ps.executeQuery(); while (rs.next()) { authorizationCodes.add(rs.getString(1)); } connection.commit(); } catch (SQLException e) { IdentityDatabaseUtil.rollBack(connection); throw new IdentityOAuth2Exception("Error occurred while getting authorization codes from authorization code table for the application with consumer key : " + consumerKey, e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, null, ps); } return authorizationCodes; }
private List<AttributeMapping> getClaimAttributeMappings(Connection connection, int localClaimId, int tenantId) throws ClaimMetadataException { List<AttributeMapping> attributeMappings = new ArrayList<>(); PreparedStatement prepStmt = null; ResultSet rs = null; String query = SQLConstants.GET_MAPPED_ATTRIBUTES; try { prepStmt = connection.prepareStatement(query); prepStmt.setInt(1, localClaimId); prepStmt.setInt(2, tenantId); rs = prepStmt.executeQuery(); while (rs.next()) { String userStoreDomainName = rs.getString(SQLConstants.USER_STORE_DOMAIN_NAME_COLUMN); String attributeName = rs.getString(SQLConstants.ATTRIBUTE_NAME_COLUMN); AttributeMapping attributeMapping = new AttributeMapping(userStoreDomainName, attributeName); attributeMappings.add(attributeMapping); } } catch (SQLException e) { throw new ClaimMetadataException("Error while retrieving attribute mappings", e); } finally { IdentityDatabaseUtil.closeResultSet(rs); IdentityDatabaseUtil.closeStatement(prepStmt); } return attributeMappings; }
protected Connection getConnection() { return IdentityDatabaseUtil.getDBConnection(); }
public void addLocalClaim(LocalClaim localClaim, int tenantId) throws ClaimMetadataException { Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement prepStmt = null; String localClaimURI = localClaim.getClaimURI(); try { // Start transaction connection.setAutoCommit(false); int localClaimId = addClaim(connection, ClaimConstants.LOCAL_CLAIM_DIALECT_URI, localClaimURI, tenantId); // Some JDBC Drivers returns this in the result, some don't if (localClaimId == 0) { if (log.isDebugEnabled()) { log.debug("JDBC Driver did not return the claimId, executing Select operation"); } localClaimId = getClaimId(connection, ClaimConstants.LOCAL_CLAIM_DIALECT_URI, localClaimURI, tenantId); // TODO : Handle invalid local claim URI } addClaimAttributeMappings(connection, localClaimId, localClaim.getMappedAttributes(), tenantId); addClaimProperties(connection, localClaimId, localClaim.getClaimProperties(), tenantId); // End transaction connection.commit(); } catch (SQLException e) { IdentityDatabaseUtil.rollBack(connection); throw new ClaimMetadataException("Error while adding local claim " + localClaimURI, e); } finally { IdentityDatabaseUtil.closeConnection(connection); } }