/** * Utility method to rollback a database connection * * @param dbConnection Database <code>Connection</code> object */ public static void rollBack(Connection dbConnection) { IdentityDatabaseUtil.rollBack(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 void revokeTokensBatch(String[] tokens) throws IdentityOAuth2Exception { String accessTokenStoreTable = OAuthConstants.ACCESS_TOKEN_STORE_TABLE; Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement ps = null; try { String sqlQuery = SQLQueries.REVOKE_ACCESS_TOKEN.replace(IDN_OAUTH2_ACCESS_TOKEN, accessTokenStoreTable); ps = connection.prepareStatement(sqlQuery); for (String token : tokens) { ps.setString(1, OAuthConstants.TokenStates.TOKEN_STATE_REVOKED); ps.setString(2, UUID.randomUUID().toString()); ps.setString(3, persistenceProcessor.getProcessedAccessTokenIdentifier(token)); ps.addBatch(); } ps.executeBatch(); connection.commit(); } catch (SQLException e) { IdentityDatabaseUtil.rollBack(connection); throw new IdentityOAuth2Exception("Error occurred while revoking Access Tokens : " + tokens.toString(), e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, null, ps); } }
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; }
public Set<String> getActiveTokensForConsumerKey(String consumerKey) throws IdentityOAuth2Exception { Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement ps = null; ResultSet rs = null; Set<String> accessTokens = new HashSet<>(); try { String sqlQuery = SQLQueries.GET_ACCESS_TOKENS_FOR_CONSUMER_KEY; ps = connection.prepareStatement(sqlQuery); ps.setString(1, consumerKey); ps.setString(2, OAuthConstants.TokenStates.TOKEN_STATE_ACTIVE); rs = ps.executeQuery(); while (rs.next()) { accessTokens.add(rs.getString(1)); } connection.commit(); } catch (SQLException e) { IdentityDatabaseUtil.rollBack(connection); throw new IdentityOAuth2Exception("Error occurred while getting access tokens from acces token table for " + "the application with consumer key : " + consumerKey, e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, null, ps); } return accessTokens; }
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); } }
public Set<String> getAuthorizationCodesForConsumerKey(String consumerKey) throws IdentityOAuth2Exception { if (log.isDebugEnabled()) { log.debug("Retrieving authorization codes for client: " + consumerKey); } 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()) { if (isHashDisabled) { authorizationCodes.add(persistenceProcessor.getPreprocessedAuthzCode(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; }
public void renameUserStoreDomainInAccessTokenTable(int tenantId, String currentUserStoreDomain, String newUserStoreDomain) throws IdentityOAuth2Exception { //we do not support access token partitioning here Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement ps = null; currentUserStoreDomain = getSanitizedUserStoreDomain(currentUserStoreDomain); newUserStoreDomain = getSanitizedUserStoreDomain(newUserStoreDomain); try { String sqlQuery = SQLQueries.RENAME_USER_STORE_IN_ACCESS_TOKENS_TABLE; ps = connection.prepareStatement(sqlQuery); ps.setString(1, newUserStoreDomain); ps.setInt(2, tenantId); ps.setString(3, currentUserStoreDomain); int count = ps.executeUpdate(); if (log.isDebugEnabled()) { log.debug("Number of rows being updated : " + count); } connection.commit(); } catch (SQLException e) { IdentityDatabaseUtil.rollBack(connection); throw new IdentityOAuth2Exception("Error occurred while renaming user store : " + currentUserStoreDomain + " in tenant :" + tenantId, e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, null, ps); } }
public void renameUserStoreDomainInAuthorizationCodeTable(int tenantId, String currentUserStoreDomain, String newUserStoreDomain) throws IdentityOAuth2Exception { //we do not support access token partitioning here Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement ps = null; currentUserStoreDomain = getSanitizedUserStoreDomain(currentUserStoreDomain); newUserStoreDomain = getSanitizedUserStoreDomain(newUserStoreDomain); try { String sqlQuery = SQLQueries.RENAME_USER_STORE_IN_AUTHORIZATION_CODES_TABLE; ps = connection.prepareStatement(sqlQuery); ps.setString(1, newUserStoreDomain); ps.setInt(2, tenantId); ps.setString(3, currentUserStoreDomain); int count = ps.executeUpdate(); if (log.isDebugEnabled()) { log.debug("Number of rows being updated : " + count); } connection.commit(); } catch (SQLException e) { IdentityDatabaseUtil.rollBack(connection); throw new IdentityOAuth2Exception("Error occurred while renaming user store : " + currentUserStoreDomain + "in tenant :" + tenantId, e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, null, ps); } }
public void revokeTokensIndividual(String[] tokens) throws IdentityOAuth2Exception { String accessTokenStoreTable = OAuthConstants.ACCESS_TOKEN_STORE_TABLE; Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement ps = null; try { for (String token: tokens){ if (OAuth2Util.checkAccessTokenPartitioningEnabled() && OAuth2Util.checkUserNameAssertionEnabled()) { accessTokenStoreTable = OAuth2Util.getAccessTokenStoreTableFromAccessToken(token); } String sqlQuery = SQLQueries.REVOKE_ACCESS_TOKEN.replace( IDN_OAUTH2_ACCESS_TOKEN, accessTokenStoreTable); ps = connection.prepareStatement(sqlQuery); ps.setString(1, OAuthConstants.TokenStates.TOKEN_STATE_REVOKED); ps.setString(2, UUID.randomUUID().toString()); ps.setString(3, persistenceProcessor.getProcessedAccessTokenIdentifier(token)); int count = ps.executeUpdate(); if (log.isDebugEnabled()) { log.debug("Number of rows being updated : " + count); } } connection.commit(); } catch (SQLException e) { IdentityDatabaseUtil.rollBack(connection); throw new IdentityOAuth2Exception("Error occurred while revoking Access Token : " + tokens.toString(), e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, null, ps); } }
public void doChangeAuthzCodeState(String authzCode, String newState) throws IdentityOAuth2Exception { if (log.isDebugEnabled()) { if (IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.AUTHORIZATION_CODE)) { log.debug("Changing state of authorization code(hashed): " + DigestUtils.sha256Hex(authzCode) + " to: " + newState); } else { log.debug("Changing state of authorization code to: " + newState); } } String authCodeStoreTable = OAuthConstants.AUTHORIZATION_CODE_STORE_TABLE; Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement prepStmt = null; try { String sqlQuery = SQLQueries.UPDATE_AUTHORIZATION_CODE_STATE.replace(IDN_OAUTH2_AUTHORIZATION_CODE, authCodeStoreTable); prepStmt = connection.prepareStatement(sqlQuery); prepStmt.setString(1, newState); prepStmt.setString(2, hashingPersistenceProcessor.getProcessedAuthzCode(authzCode)); prepStmt.execute(); connection.commit(); } catch (SQLException e) { IdentityDatabaseUtil.rollBack(connection); throw new IdentityOAuth2Exception("Error occurred while updating the state of Authorization Code : " + authzCode.toString(), e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, null, prepStmt); } }
/** * This is for updating the token in DB * * @param token Token */ public void updateToken(Token token) throws TrustException { Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement prepStmt = null; ResultSet rs = null; String query = DBQueries.UPDATE_TOKEN; try { prepStmt = connection.prepareStatement(query); byte[] tokenByteContainer = getTokenContent(token); InputStream tokenInputStream = new ByteArrayInputStream(tokenByteContainer); prepStmt.setBinaryStream(1, tokenInputStream, tokenByteContainer.length); prepStmt.setTimestamp(2, new Timestamp(token.getCreated().getTime())); prepStmt.setTimestamp(3, new Timestamp(token.getExpires().getTime())); prepStmt.setInt(4, token.getState()); prepStmt.setString(5, token.getId()); prepStmt.executeUpdate(); connection.commit(); } catch (Exception e) { IdentityDatabaseUtil.rollBack(connection); String msg = "Failed to update token "; throw new TrustException(msg, e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, rs, prepStmt); } }
/** * 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); } }
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); } }
public void updateLocalClaim(ExternalClaim externalClaim, int tenantId) throws ClaimMetadataException { Connection connection = IdentityDatabaseUtil.getDBConnection(); String externalClaimURI = externalClaim.getClaimURI(); String externalClaimDialectURI = externalClaim.getClaimDialectURI(); String mappedLocalClaimURI = externalClaim.getMappedLocalClaim(); try { // Start transaction connection.setAutoCommit(false); int externalClaimId = getClaimId(connection, externalClaimDialectURI, externalClaimURI, tenantId); // TODO : Handle invalid external claim URI int localClaimId = getClaimId(connection, ClaimConstants.LOCAL_CLAIM_DIALECT_URI, mappedLocalClaimURI, tenantId); // TODO : Handle invalid local claim URI updateClaimMapping(connection, externalClaimId, localClaimId, tenantId); // End transaction connection.commit(); } catch (SQLException e) { IdentityDatabaseUtil.rollBack(connection); throw new ClaimMetadataException("Error while updating external claim " + externalClaimURI + " in " + "dialect " + externalClaimDialectURI, e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, null, null); } }
public void updateLocalClaim(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 = getClaimId(connection, ClaimConstants.LOCAL_CLAIM_DIALECT_URI, localClaimURI, tenantId); // TODO : Handle invalid local claim URI deleteClaimAttributeMappings(connection, localClaimId, tenantId); addClaimAttributeMappings(connection, localClaimId, localClaim.getMappedAttributes(), tenantId); deleteClaimProperties(connection, localClaimId, tenantId); addClaimProperties(connection, localClaimId, localClaim.getClaimProperties(), tenantId); // End transaction connection.commit(); } catch (SQLException e) { IdentityDatabaseUtil.rollBack(connection); throw new ClaimMetadataException("Error while updating local claim " + localClaimURI, e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, null, null); } }
public void updateLocalClaim(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 = getClaimId(connection, ClaimConstants.LOCAL_CLAIM_DIALECT_URI, localClaimURI, tenantId); // TODO : Handle invalid local claim URI deleteClaimAttributeMappings(connection, localClaimId, tenantId); addClaimAttributeMappings(connection, localClaimId, localClaim.getMappedAttributes(), tenantId); deleteClaimProperties(connection, localClaimId, tenantId); addClaimProperties(connection, localClaimId, localClaim.getClaimProperties(), tenantId); // End transaction connection.commit(); } catch (SQLException e) { IdentityDatabaseUtil.rollBack(connection); throw new ClaimMetadataException("Error while updating local claim " + localClaimURI, e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, null, null); } }
public void updateLocalClaim(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 = getClaimId(connection, ClaimConstants.LOCAL_CLAIM_DIALECT_URI, localClaimURI, tenantId); // TODO : Handle invalid local claim URI deleteClaimAttributeMappings(connection, localClaimId, tenantId); addClaimAttributeMappings(connection, localClaimId, localClaim.getMappedAttributes(), tenantId); deleteClaimProperties(connection, localClaimId, tenantId); addClaimProperties(connection, localClaimId, localClaim.getClaimProperties(), tenantId); // End transaction connection.commit(); } catch (SQLException e) { IdentityDatabaseUtil.rollBack(connection); throw new ClaimMetadataException("Error while updating local claim " + localClaimURI, e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, null, null); } }
public void updateExternalClaim(ExternalClaim externalClaim, int tenantId) throws ClaimMetadataException { Connection connection = IdentityDatabaseUtil.getDBConnection(); String externalClaimURI = externalClaim.getClaimURI(); String externalClaimDialectURI = externalClaim.getClaimDialectURI(); String mappedLocalClaimURI = externalClaim.getMappedLocalClaim(); try { // Start transaction connection.setAutoCommit(false); int externalClaimId = getClaimId(connection, externalClaimDialectURI, externalClaimURI, tenantId); // TODO : Handle invalid external claim URI int localClaimId = getClaimId(connection, ClaimConstants.LOCAL_CLAIM_DIALECT_URI, mappedLocalClaimURI, tenantId); // TODO : Handle invalid local claim URI updateClaimMapping(connection, externalClaimId, localClaimId, tenantId); deleteClaimProperties(connection, externalClaimId, tenantId); addClaimProperties(connection, externalClaimId, externalClaim.getClaimProperties(), tenantId); // End transaction connection.commit(); } catch (SQLException e) { IdentityDatabaseUtil.rollBack(connection); throw new ClaimMetadataException("Error while updating external claim " + externalClaimURI + " in " + "dialect " + externalClaimDialectURI, e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, null, null); } }
public void updateExternalClaim(ExternalClaim externalClaim, int tenantId) throws ClaimMetadataException { Connection connection = IdentityDatabaseUtil.getDBConnection(); String externalClaimURI = externalClaim.getClaimURI(); String externalClaimDialectURI = externalClaim.getClaimDialectURI(); String mappedLocalClaimURI = externalClaim.getMappedLocalClaim(); try { // Start transaction connection.setAutoCommit(false); int externalClaimId = getClaimId(connection, externalClaimDialectURI, externalClaimURI, tenantId); // TODO : Handle invalid external claim URI int localClaimId = getClaimId(connection, ClaimConstants.LOCAL_CLAIM_DIALECT_URI, mappedLocalClaimURI, tenantId); // TODO : Handle invalid local claim URI updateClaimMapping(connection, externalClaimId, localClaimId, tenantId); deleteClaimProperties(connection, externalClaimId, tenantId); addClaimProperties(connection, externalClaimId, externalClaim.getClaimProperties(), tenantId); // End transaction connection.commit(); } catch (SQLException e) { IdentityDatabaseUtil.rollBack(connection); throw new ClaimMetadataException("Error while updating external claim " + externalClaimURI + " in " + "dialect " + externalClaimDialectURI, e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, null, null); } }