-
Notifications
You must be signed in to change notification settings - Fork 370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize cleanupTokenByTokenValue logic #2508
Open
mpmadhavig
wants to merge
2
commits into
wso2-extensions:master
Choose a base branch
from
mpmadhavig:optimize-token-revokation-logic
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,48 +64,57 @@ public void cleanupTokenByTokenId(String tokenId, Connection connection) throws | |
} | ||
} | ||
|
||
public void cleanupTokenByTokenValue(String token, Connection connection) throws SQLException { | ||
OldAccessTokenDO oldAccessTokenObject = new OldAccessTokenDO(); | ||
|
||
String sql; | ||
if (OAuth2ServiceComponentHolder.isIDPIdColumnEnabled()) { | ||
sql = SQLQueries.RETRIEVE_OLD_TOKEN_BY_TOKEN_HASH_WITH_IDP_NAME; | ||
} else { | ||
sql = SQLQueries.RETRIEVE_OLD_TOKEN_BY_TOKEN_HASH; | ||
} | ||
/** | ||
* This method is used to clean-up old tokens by token hash value. | ||
* | ||
* @param tokenHash Token hash value. | ||
* @param connection Database connection. | ||
* @throws SQLException If an error occurs while cleaning up the token. | ||
*/ | ||
public void cleanupTokenByTokenValue(String tokenHash, Connection connection) throws SQLException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. name should be |
||
|
||
PreparedStatement prepStmt = connection.prepareStatement(sql); | ||
prepStmt.setString(1, token); | ||
ResultSet resultSet = prepStmt.executeQuery(); | ||
//iterate result set and insert to AccessTokenDO object. | ||
if (resultSet.next()) { | ||
oldAccessTokenObject.setTokenId(resultSet.getString(1)); | ||
oldAccessTokenObject.setAccessToken(resultSet.getString(2)); | ||
oldAccessTokenObject.setRefreshToken(resultSet.getString(3)); | ||
oldAccessTokenObject.setConsumerKeyId(resultSet.getInt(4)); | ||
oldAccessTokenObject.setAuthzUser(resultSet.getString(5)); | ||
oldAccessTokenObject.setTenantId(resultSet.getInt(6)); | ||
oldAccessTokenObject.setUserDomain(resultSet.getString(7)); | ||
oldAccessTokenObject.setUserType(resultSet.getString(8)); | ||
oldAccessTokenObject.setGrantType(resultSet.getString(9)); | ||
oldAccessTokenObject.setTimeCreated(resultSet.getTimestamp(10)); | ||
oldAccessTokenObject.setRefreshTokenTimeCreated(resultSet.getTimestamp(11)); | ||
oldAccessTokenObject.setValdityPeriod(resultSet.getLong(12)); | ||
oldAccessTokenObject.setRefreshTokenValidityPeriod(resultSet.getLong(13)); | ||
oldAccessTokenObject.setTokenScopeHash(resultSet.getString(14)); | ||
oldAccessTokenObject.setTokenState(resultSet.getString(15)); | ||
oldAccessTokenObject.setTokenStateId(resultSet.getString(16)); | ||
oldAccessTokenObject.setSubjectIdentifier(resultSet.getString(17)); | ||
oldAccessTokenObject.setAccessTokenHash(resultSet.getString(18)); | ||
oldAccessTokenObject.setRefreshTokenHash(resultSet.getString(19)); | ||
OldAccessTokenDO oldAccessTokenObject = new OldAccessTokenDO(); | ||
if (OAuthServerConfiguration.getInstance().useRetainOldAccessTokens()) { | ||
String sql; | ||
if (OAuth2ServiceComponentHolder.isIDPIdColumnEnabled()) { | ||
oldAccessTokenObject.setIdpId(resultSet.getInt(20)); | ||
sql = SQLQueries.RETRIEVE_OLD_TOKEN_BY_TOKEN_HASH_WITH_IDP_NAME; | ||
} else { | ||
sql = SQLQueries.RETRIEVE_OLD_TOKEN_BY_TOKEN_HASH; | ||
} | ||
|
||
PreparedStatement prepStmt = connection.prepareStatement(sql); | ||
prepStmt.setString(1, tokenHash); | ||
ResultSet resultSet = prepStmt.executeQuery(); | ||
//iterate result set and insert to AccessTokenDO object. | ||
if (resultSet.next()) { | ||
oldAccessTokenObject.setTokenId(resultSet.getString(1)); | ||
oldAccessTokenObject.setAccessToken(resultSet.getString(2)); | ||
oldAccessTokenObject.setRefreshToken(resultSet.getString(3)); | ||
oldAccessTokenObject.setConsumerKeyId(resultSet.getInt(4)); | ||
oldAccessTokenObject.setAuthzUser(resultSet.getString(5)); | ||
oldAccessTokenObject.setTenantId(resultSet.getInt(6)); | ||
oldAccessTokenObject.setUserDomain(resultSet.getString(7)); | ||
oldAccessTokenObject.setUserType(resultSet.getString(8)); | ||
oldAccessTokenObject.setGrantType(resultSet.getString(9)); | ||
oldAccessTokenObject.setTimeCreated(resultSet.getTimestamp(10)); | ||
oldAccessTokenObject.setRefreshTokenTimeCreated(resultSet.getTimestamp(11)); | ||
oldAccessTokenObject.setValdityPeriod(resultSet.getLong(12)); | ||
oldAccessTokenObject.setRefreshTokenValidityPeriod(resultSet.getLong(13)); | ||
oldAccessTokenObject.setTokenScopeHash(resultSet.getString(14)); | ||
oldAccessTokenObject.setTokenState(resultSet.getString(15)); | ||
oldAccessTokenObject.setTokenStateId(resultSet.getString(16)); | ||
oldAccessTokenObject.setSubjectIdentifier(resultSet.getString(17)); | ||
oldAccessTokenObject.setAccessTokenHash(resultSet.getString(18)); | ||
oldAccessTokenObject.setRefreshTokenHash(resultSet.getString(19)); | ||
if (OAuth2ServiceComponentHolder.isIDPIdColumnEnabled()) { | ||
oldAccessTokenObject.setIdpId(resultSet.getInt(20)); | ||
} | ||
} | ||
} | ||
if (OAuthServerConfiguration.getInstance().useRetainOldAccessTokens()) { | ||
saveTokenInAuditTable(oldAccessTokenObject, connection); | ||
removeTokenFromMainTable(oldAccessTokenObject.getTokenId(), connection); | ||
} else { | ||
removeTokenFromMainTableByTokenHash(tokenHash, connection); | ||
} | ||
removeTokenFromMainTable(oldAccessTokenObject.getTokenId(), connection); | ||
} | ||
|
||
private void saveTokenInAuditTable(OldAccessTokenDO oldAccessTokenDAO, Connection connection) throws SQLException { | ||
|
@@ -153,10 +162,10 @@ private void removeTokenFromMainTable(String oldAccessTokenID, Connection connec | |
|
||
connection.setAutoCommit(false); | ||
try { | ||
PreparedStatement deletefromaccesstokentable = | ||
PreparedStatement deleteFromAccessTokenTable = | ||
connection.prepareStatement(SQLQueries.DELETE_OLD_TOKEN_BY_ID); | ||
deletefromaccesstokentable.setString(1, oldAccessTokenID); | ||
deletefromaccesstokentable.executeUpdate(); | ||
deleteFromAccessTokenTable.setString(1, oldAccessTokenID); | ||
deleteFromAccessTokenTable.executeUpdate(); | ||
if (log.isDebugEnabled()) { | ||
log.debug( | ||
"Successfully old access token deleted from access token table. Token ID: " + oldAccessTokenID); | ||
|
@@ -168,6 +177,27 @@ private void removeTokenFromMainTable(String oldAccessTokenID, Connection connec | |
} | ||
} | ||
|
||
private void removeTokenFromMainTableByTokenHash(String accessTokenHash, Connection connection) | ||
throws SQLException { | ||
|
||
connection.setAutoCommit(false); | ||
try { | ||
PreparedStatement deleteFromAccessTokenTable = | ||
connection.prepareStatement(SQLQueries.DELETE_OLD_TOKEN_BY_TOKEN_HASH); | ||
deleteFromAccessTokenTable.setString(1, accessTokenHash); | ||
deleteFromAccessTokenTable.executeUpdate(); | ||
if (log.isDebugEnabled()) { | ||
log.debug( | ||
"Successfully old access token deleted from access token table. Token Hash: " + | ||
accessTokenHash); | ||
} | ||
connection.commit(); | ||
} catch (SQLException e) { | ||
connection.rollback(); | ||
log.error("SQL error occurred while remove token from main table", e); | ||
} | ||
} | ||
|
||
public void cleanupTokensInBatch(List<String> oldTokens, Connection connection) throws SQLException { | ||
for (String token : oldTokens) { | ||
cleanupTokenByTokenValue(token, connection); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it ok to remove public method here?