Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception;
import org.wso2.carbon.identity.oauth2.dao.OAuthTokenPersistenceFactory;
import org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenReqDTO;
import org.wso2.carbon.identity.oauth2.internal.OAuth2ServiceComponentHolder;
import org.wso2.carbon.identity.oauth2.model.AccessTokenDO;
import org.wso2.carbon.identity.oauth2.model.RefreshTokenValidationDataDO;
import org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext;
Expand Down Expand Up @@ -98,25 +97,23 @@ public AccessTokenDO createAccessTokenBean(OAuthTokenReqMessageContext tokReqMsg
accessTokenDO.setIssuedTime(timestamp);
accessTokenDO.setTokenBinding(tokReqMsgCtx.getTokenBinding());

if (OAuth2ServiceComponentHolder.isConsentedTokenColumnEnabled()) {
String previousGrantType = validationBean.getGrantType();
// Check if the previous grant type is consent refresh token type or not.
if (!OAuthConstants.GrantTypes.REFRESH_TOKEN.equals(previousGrantType)) {
// If the previous grant type is not a refresh token, then check if it's a consent token or not.
if (OIDCClaimUtil.isConsentBasedClaimFilteringApplicable(previousGrantType)) {
accessTokenDO.setIsConsentedToken(true);
}
} else {
/* When previousGrantType == refresh_token, we need to check whether the original grant type
is consented or not. */
AccessTokenDO accessTokenDOFromTokenIdentifier = OAuth2Util.getAccessTokenDOFromTokenIdentifier(
validationBean.getAccessToken(), false);
accessTokenDO.setIsConsentedToken(accessTokenDOFromTokenIdentifier.isConsentedToken());
String previousGrantType = validationBean.getGrantType();
// Check if the previous grant type is consent refresh token type or not.
if (!OAuthConstants.GrantTypes.REFRESH_TOKEN.equals(previousGrantType)) {
// If the previous grant type is not a refresh token, then check if it's a consent token or not.
if (OIDCClaimUtil.isConsentBasedClaimFilteringApplicable(previousGrantType)) {
accessTokenDO.setIsConsentedToken(true);
Comment on lines +103 to +105

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log Improvement Suggestion No: 2

Suggested change
// If the previous grant type is not a refresh token, then check if it's a consent token or not.
if (OIDCClaimUtil.isConsentBasedClaimFilteringApplicable(previousGrantType)) {
accessTokenDO.setIsConsentedToken(true);
// If the previous grant type is not a refresh token, then check if it's a consent token or not.
if (OIDCClaimUtil.isConsentBasedClaimFilteringApplicable(previousGrantType)) {
if (log.isDebugEnabled()) {
log.debug("Previous grant type " + previousGrantType + " requires consent-based claim filtering");
}
accessTokenDO.setIsConsentedToken(true);

}
} else {
/* When previousGrantType == refresh_token, we need to check whether the original grant type
is consented or not. */
AccessTokenDO accessTokenDOFromTokenIdentifier = OAuth2Util.getAccessTokenDOFromTokenIdentifier(
validationBean.getAccessToken(), false);
accessTokenDO.setIsConsentedToken(accessTokenDOFromTokenIdentifier.isConsentedToken());
}

if (accessTokenDO.isConsentedToken()) {
tokReqMsgCtx.setConsentedToken(true);
}
if (accessTokenDO.isConsentedToken()) {
tokReqMsgCtx.setConsentedToken(true);
}
return accessTokenDO;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,16 +665,15 @@ private static AccessTokenDO createNewTokenBean(OAuthAuthzReqMessageContext oaut
newTokenBean.setGrantType(grantType);
/* If the existing token is available, the consented token flag will be extracted from that. Otherwise,
from the current grant. */
if (OAuth2ServiceComponentHolder.isConsentedTokenColumnEnabled()) {
if (existingTokenBean != null) {
newTokenBean.setIsConsentedToken(existingTokenBean.isConsentedToken());
} else {
if (OIDCClaimUtil.isConsentBasedClaimFilteringApplicable(grantType)) {
newTokenBean.setIsConsentedToken(true);
}
if (existingTokenBean != null) {
newTokenBean.setIsConsentedToken(existingTokenBean.isConsentedToken());
} else {
if (OIDCClaimUtil.isConsentBasedClaimFilteringApplicable(grantType)) {
newTokenBean.setIsConsentedToken(true);
}
oauthAuthzMsgCtx.setConsentedToken(newTokenBean.isConsentedToken());
}
oauthAuthzMsgCtx.setConsentedToken(newTokenBean.isConsentedToken());

newTokenBean.setAccessToken(getNewAccessToken(oauthAuthzMsgCtx, oauthIssuerImpl));
setRefreshTokenDetails(oauthAuthzMsgCtx, oAuthAppBean, existingTokenBean, newTokenBean, oauthIssuerImpl,
timestamp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,7 @@ private void insertAccessToken(String accessToken, String consumerKey, AccessTok

String sql;

if (OAuth2ServiceComponentHolder.isConsentedTokenColumnEnabled()) {
sql = SQLQueries.INSERT_OAUTH2_ACCESS_TOKEN_WITH_IDP_NAME_WITH_CONSENTED_TOKEN;
} else {
sql = SQLQueries.INSERT_OAUTH2_ACCESS_TOKEN_WITH_IDP_NAME;
}

sql = SQLQueries.INSERT_OAUTH2_ACCESS_TOKEN_WITH_IDP_NAME_WITH_CONSENTED_TOKEN;
sql = OAuth2Util.getTokenPartitionedSqlByUserStore(sql, userDomain);
String sqlAddScopes = OAuth2Util.getTokenPartitionedSqlByUserStore(SQLQueries.INSERT_OAUTH2_TOKEN_SCOPE,
userDomain);
Expand Down Expand Up @@ -266,20 +261,12 @@ private void insertAccessToken(String accessToken, String consumerKey, AccessTok
}
}

if (OAuth2ServiceComponentHolder.isConsentedTokenColumnEnabled()) {
insertTokenPrepStmt.setString(20, Boolean.toString(accessTokenDO.isConsentedToken()));
insertTokenPrepStmt.setString(21, authenticatedIDP);
// Set tenant ID of the IDP by considering it is same as appTenantID.
insertTokenPrepStmt.setInt(22, appTenantId);
insertTokenPrepStmt.setString(23, getPersistenceProcessor().getProcessedClientId(consumerKey));
insertTokenPrepStmt.setInt(24, appTenantId);
} else {
insertTokenPrepStmt.setString(20, authenticatedIDP);
// Set tenant ID of the IDP by considering it is same as appTenantID.
insertTokenPrepStmt.setInt(21, appTenantId);
insertTokenPrepStmt.setString(22, getPersistenceProcessor().getProcessedClientId(consumerKey));
insertTokenPrepStmt.setInt(23, appTenantId);
}
insertTokenPrepStmt.setString(20, Boolean.toString(accessTokenDO.isConsentedToken()));
insertTokenPrepStmt.setString(21, authenticatedIDP);
// Set tenant ID of the IDP by considering it is same as appTenantID.
insertTokenPrepStmt.setInt(22, appTenantId);
insertTokenPrepStmt.setString(23, getPersistenceProcessor().getProcessedClientId(consumerKey));
insertTokenPrepStmt.setInt(24, appTenantId);

insertTokenPrepStmt.executeUpdate();

Expand Down Expand Up @@ -560,9 +547,7 @@ public AccessTokenDO getLatestAccessToken(String consumerKey, AuthenticatedUser
String subjectIdentifier = resultSet.getString(10);
String grantType = resultSet.getString(11);
String isConsentedToken = StringUtils.EMPTY;
if (OAuth2ServiceComponentHolder.isConsentedTokenColumnEnabled()) {
isConsentedToken = resultSet.getString(12);
}
isConsentedToken = resultSet.getString(12);
// data loss at dividing the validity period but can be neglected
AuthenticatedUser user = OAuth2Util.createAuthenticatedUser(authzUser, userDomain,
tenantDomain, authenticatedIDP);
Expand Down Expand Up @@ -612,50 +597,30 @@ private String getLatestAccessTokenQuerySQL(Connection connection) throws SQLExc

String sql;

if (OAuth2ServiceComponentHolder.isConsentedTokenColumnEnabled()) {
if (connection.getMetaData().getDriverName().contains("MySQL")
|| connection.getMetaData().getDriverName().contains(FrameworkConstants.H2)
|| connection.getMetaData().getDriverName().contains(FrameworkConstants.MARIA_DB)) {
sql = SQLQueries.
GET_LATEST_ACCESS_TOKEN_WITH_CONSENTED_TOKEN_BY_CLIENT_ID_USER_SCOPE_IDP_NAME_MYSQL;
} else if (connection.getMetaData().getDatabaseProductName().contains("DB2")) {
sql = SQLQueries.
GET_LATEST_ACCESS_TOKEN_WITH_CONSENTED_TOKEN_BY_CLIENT_ID_USER_SCOPE_IDP_NAME_DB2SQL;
} else if (connection.getMetaData().getDriverName().contains("MS SQL")) {
sql = SQLQueries.
GET_LATEST_ACCESS_TOKEN_WITH_CONSENTED_TOKEN_BY_CLIENT_ID_USER_SCOPE_IDP_NAME_MSSQL;
} else if (connection.getMetaData().getDriverName().contains("Microsoft")) {
sql = SQLQueries.
GET_LATEST_ACCESS_TOKEN_WITH_CONSENTED_TOKEN_BY_CLIENT_ID_USER_SCOPE_IDP_NAME_MSSQL;
} else if (connection.getMetaData().getDriverName().contains("PostgreSQL")) {
sql = SQLQueries.
GET_LATEST_ACCESS_TOKEN_WITH_CONSENTED_TOKEN_BY_CLIENT_ID_USER_SCOPE_IDP_NAME_POSTGRESQL;
} else if (connection.getMetaData().getDriverName().contains("Informix")) {
// Driver name = "IBM Informix JDBC Driver for IBM Informix Dynamic Server"
sql = SQLQueries.
GET_LATEST_ACCESS_TOKEN_WITH_CONSENTED_TOKEN_BY_CLIENT_ID_USER_SCOPE_IDP_NAME_INFORMIX;
} else {
sql = SQLQueries.
GET_LATEST_ACCESS_TOKEN_WITH_CONSENTED_TOKEN_BY_CLIENT_ID_USER_SCOPE_IDP_NAME_ORACLE;
}
if (connection.getMetaData().getDriverName().contains("MySQL")
|| connection.getMetaData().getDriverName().contains(FrameworkConstants.H2)
|| connection.getMetaData().getDriverName().contains(FrameworkConstants.MARIA_DB)) {
sql = SQLQueries.
GET_LATEST_ACCESS_TOKEN_WITH_CONSENTED_TOKEN_BY_CLIENT_ID_USER_SCOPE_IDP_NAME_MYSQL;
} else if (connection.getMetaData().getDatabaseProductName().contains("DB2")) {
sql = SQLQueries.
GET_LATEST_ACCESS_TOKEN_WITH_CONSENTED_TOKEN_BY_CLIENT_ID_USER_SCOPE_IDP_NAME_DB2SQL;
} else if (connection.getMetaData().getDriverName().contains("MS SQL")) {
sql = SQLQueries.
GET_LATEST_ACCESS_TOKEN_WITH_CONSENTED_TOKEN_BY_CLIENT_ID_USER_SCOPE_IDP_NAME_MSSQL;
} else if (connection.getMetaData().getDriverName().contains("Microsoft")) {
sql = SQLQueries.
GET_LATEST_ACCESS_TOKEN_WITH_CONSENTED_TOKEN_BY_CLIENT_ID_USER_SCOPE_IDP_NAME_MSSQL;
} else if (connection.getMetaData().getDriverName().contains("PostgreSQL")) {
sql = SQLQueries.
GET_LATEST_ACCESS_TOKEN_WITH_CONSENTED_TOKEN_BY_CLIENT_ID_USER_SCOPE_IDP_NAME_POSTGRESQL;
} else if (connection.getMetaData().getDriverName().contains("Informix")) {
// Driver name = "IBM Informix JDBC Driver for IBM Informix Dynamic Server"
sql = SQLQueries.
GET_LATEST_ACCESS_TOKEN_WITH_CONSENTED_TOKEN_BY_CLIENT_ID_USER_SCOPE_IDP_NAME_INFORMIX;
} else {
if (connection.getMetaData().getDriverName().contains("MySQL")
|| connection.getMetaData().getDriverName().contains("H2")) {
sql = SQLQueries.RETRIEVE_LATEST_ACCESS_TOKEN_BY_CLIENT_ID_USER_SCOPE_IDP_NAME_MYSQL;
} else if (connection.getMetaData().getDatabaseProductName().contains("DB2")) {
sql = SQLQueries.RETRIEVE_LATEST_ACCESS_TOKEN_BY_CLIENT_ID_USER_SCOPE_IDP_NAME_DB2SQL;
} else if (connection.getMetaData().getDriverName().contains("MS SQL")) {
sql = SQLQueries.RETRIEVE_LATEST_ACCESS_TOKEN_BY_CLIENT_ID_USER_SCOPE_IDP_NAME_MSSQL;
} else if (connection.getMetaData().getDriverName().contains("Microsoft")) {
sql = SQLQueries.RETRIEVE_LATEST_ACCESS_TOKEN_BY_CLIENT_ID_USER_SCOPE_IDP_NAME_MSSQL;
} else if (connection.getMetaData().getDriverName().contains("PostgreSQL")) {
sql = SQLQueries.RETRIEVE_LATEST_ACCESS_TOKEN_BY_CLIENT_ID_USER_SCOPE_IDP_NAME_POSTGRESQL;
} else if (connection.getMetaData().getDriverName().contains("Informix")) {
// Driver name = "IBM Informix JDBC Driver for IBM Informix Dynamic Server"
sql = SQLQueries.RETRIEVE_LATEST_ACCESS_TOKEN_BY_CLIENT_ID_USER_SCOPE_IDP_NAME_INFORMIX;
} else {
sql = SQLQueries.RETRIEVE_LATEST_ACCESS_TOKEN_BY_CLIENT_ID_USER_SCOPE_IDP_NAME_ORACLE;
}
sql = SQLQueries.
GET_LATEST_ACCESS_TOKEN_WITH_CONSENTED_TOKEN_BY_CLIENT_ID_USER_SCOPE_IDP_NAME_ORACLE;
}

return sql;
Expand Down Expand Up @@ -964,12 +929,8 @@ public AccessTokenDO getAccessToken(String accessTokenIdentifier, boolean includ
if (includeExpired) {
sql = SQLQueries.RETRIEVE_ACTIVE_EXPIRED_ACCESS_TOKEN_IDP_NAME;
} else {
if (OAuth2ServiceComponentHolder.isConsentedTokenColumnEnabled()) {
sql = SQLQueries.RETRIEVE_ACTIVE_ACCESS_TOKEN_IDP_NAME_WITH_CONSENTED_TOKEN;
isConsentedColumnDataFetched = true;
} else {
sql = SQLQueries.RETRIEVE_ACTIVE_ACCESS_TOKEN_IDP_NAME;
}
sql = SQLQueries.RETRIEVE_ACTIVE_ACCESS_TOKEN_IDP_NAME_WITH_CONSENTED_TOKEN;
isConsentedColumnDataFetched = true;

}

Expand Down Expand Up @@ -2028,11 +1989,9 @@ public void invalidateAndCreateNewAccessToken(String oldAccessTokenId, String to
boolean tokenUpdateSuccessful;
Connection connection = IdentityDatabaseUtil.getDBConnection(true);
try {
if (OAuth2ServiceComponentHolder.isConsentedTokenColumnEnabled() && !accessTokenDO.isConsentedToken()) {
// Check whether the previous token is issued for a consent required grant or not.
boolean isPreviousTokenConsented = isPreviousTokenConsented(connection, oldAccessTokenId);
accessTokenDO.setIsConsentedToken(isPreviousTokenConsented);
}
// Check whether the previous token is issued for a consent required grant or not.
boolean isPreviousTokenConsented = isPreviousTokenConsented(connection, oldAccessTokenId);
accessTokenDO.setIsConsentedToken(isPreviousTokenConsented);
// update existing token as inactive
updateAccessTokenState(connection, oldAccessTokenId, tokenState, tokenStateId, userStoreDomain, grantType);

Expand Down Expand Up @@ -3172,32 +3131,25 @@ public Set<AccessTokenDO> getAccessTokensByBindingRef(String bindingRef) throws
public void updateTokenIsConsented(String tokenId, boolean isConsentedGrant)
throws IdentityOAuth2Exception {

if (OAuth2ServiceComponentHolder.isConsentedTokenColumnEnabled()) {
if (log.isDebugEnabled()) {
log.debug("Updating the token's last issued grant type for token with id: " + tokenId + " to: " +
isConsentedGrant);
}

String sql = SQLQueries.UPDATE_TOKEN_CONSENTED_TOKEN;
try (Connection connection = IdentityDatabaseUtil.getDBConnection(true)) {
try (PreparedStatement prepStmt = connection.prepareStatement(sql)) {
prepStmt.setString(1, Boolean.toString(isConsentedGrant));
prepStmt.setString(2, tokenId);
prepStmt.executeUpdate();
IdentityDatabaseUtil.commitTransaction(connection);
} catch (SQLException e) {
IdentityDatabaseUtil.rollbackTransaction(connection); // ToDo add the exception here
throw new IdentityOAuth2Exception("Error while updating the access token.", e);
}
if (log.isDebugEnabled()) {
log.debug("Updating the token's last issued grant type for token with id: " + tokenId + " to: " +
isConsentedGrant);
}

String sql = SQLQueries.UPDATE_TOKEN_CONSENTED_TOKEN;
try (Connection connection = IdentityDatabaseUtil.getDBConnection(true)) {
try (PreparedStatement prepStmt = connection.prepareStatement(sql)) {
prepStmt.setString(1, Boolean.toString(isConsentedGrant));
prepStmt.setString(2, tokenId);
prepStmt.executeUpdate();
IdentityDatabaseUtil.commitTransaction(connection);
} catch (SQLException e) {
throw new IdentityOAuth2Exception("Error while updating Access Token with ID: " + tokenId +
" to last issued grant type : ", e);
}
} else {
if (log.isDebugEnabled()) {
log.debug("CONSENTED_TOKEN column is not available. Since not updating the token with id: "
+ tokenId + " to: " + isConsentedGrant);
IdentityDatabaseUtil.rollbackTransaction(connection); // ToDo add the exception here
throw new IdentityOAuth2Exception("Error while updating the access token.", e);
}
} catch (SQLException e) {
throw new IdentityOAuth2Exception("Error while updating Access Token with ID: " + tokenId +
" to last issued grant type : ", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -904,15 +904,6 @@ public class SQLQueries {
+ " TOKEN_BINDING_REF, AUTHORIZED_ORGANIZATION, CONSENTED_TOKEN) SELECT ?,?,ID,?,?,?,?,?,?,?,?,?,?,?,?,?," +
"?,?,?,?,? FROM IDN_OAUTH_CONSUMER_APPS WHERE CONSUMER_KEY=? AND TENANT_ID = ?";

public static final String INSERT_OAUTH2_ACCESS_TOKEN_WITH_IDP_NAME = "INSERT INTO IDN_OAUTH2_ACCESS_TOKEN " +
"(ACCESS_TOKEN, REFRESH_TOKEN, CONSUMER_KEY_ID, AUTHZ_USER, TENANT_ID, USER_DOMAIN, TIME_CREATED, " +
"REFRESH_TOKEN_TIME_CREATED, VALIDITY_PERIOD, REFRESH_TOKEN_VALIDITY_PERIOD, TOKEN_SCOPE_HASH, " +
"TOKEN_STATE, USER_TYPE, TOKEN_ID, GRANT_TYPE, SUBJECT_IDENTIFIER, ACCESS_TOKEN_HASH, REFRESH_TOKEN_HASH," +
"IDP_ID, TOKEN_BINDING_REF, AUTHORIZED_ORGANIZATION) SELECT ?,?,IDN_OAUTH_CONSUMER_APPS.ID,?,?,?,?,?,?,?," +
"?,?,?,?,?,?,?,?,IDP.ID,?,? "
+ "FROM IDN_OAUTH_CONSUMER_APPS, IDP WHERE IDP.NAME=? AND IDP.TENANT_ID=? AND CONSUMER_KEY=? AND " +
"IDN_OAUTH_CONSUMER_APPS.TENANT_ID=?";

public static final String INSERT_OAUTH2_ACCESS_TOKEN_WITH_IDP_NAME_WITH_CONSENTED_TOKEN =
"INSERT INTO IDN_OAUTH2_ACCESS_TOKEN (ACCESS_TOKEN, REFRESH_TOKEN, CONSUMER_KEY_ID, AUTHZ_USER, " +
"TENANT_ID, USER_DOMAIN, TIME_CREATED, REFRESH_TOKEN_TIME_CREATED, VALIDITY_PERIOD, " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ protected void activate(ComponentContext context) {
}

boolean isConsentedTokenColumnAvailable = checkConsentedTokenColumnAvailable();
OAuth2ServiceComponentHolder.setConsentedTokenColumnEnabled(isConsentedTokenColumnAvailable);
if (log.isDebugEnabled()) {
if (isConsentedTokenColumnAvailable) {
log.debug("CONSENTED_TOKEN column is available in IDN_OAUTH2_ACCESS_TOKEN table. Hence setting " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,6 @@ public static boolean isConsentedTokenColumnEnabled() {
return consentedTokenColumnEnabled;
}

public static void setConsentedTokenColumnEnabled(boolean consentedTokenColumnEnabled) {

OAuth2ServiceComponentHolder.consentedTokenColumnEnabled = consentedTokenColumnEnabled;
}

public static RegistryService getRegistryService() {

return registryService;
Expand Down
Loading