Search in sources :

Example 76 with DataAccessException

use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project carbon-identity-framework by wso2.

the class SecretDAOImpl method deleteSecretById.

@Override
public void deleteSecretById(String secretId, int tenantId) throws SecretManagementException {
    NamedJdbcTemplate jdbcTemplate = getNewTemplate();
    try {
        jdbcTemplate.executeUpdate(SQLConstants.DELETE_SECRET_BY_ID, preparedStatement -> {
            preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ID, secretId);
            preparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, tenantId);
        });
    } catch (DataAccessException e) {
        throw handleServerException(ERROR_CODE_DELETE_SECRET, secretId, e);
    }
}
Also used : NamedJdbcTemplate(org.wso2.carbon.database.utils.jdbc.NamedJdbcTemplate) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 77 with DataAccessException

use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project carbon-identity-framework by wso2.

the class SecretDAOImpl method deleteSecretByName.

@Override
public void deleteSecretByName(String name, String secretTypeId, int tenantId) throws SecretManagementException {
    NamedJdbcTemplate jdbcTemplate = getNewTemplate();
    try {
        jdbcTemplate.executeUpdate(SQLConstants.DELETE_SECRET, preparedStatement -> {
            preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_SECRET_NAME, name);
            preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_TYPE, secretTypeId);
            preparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, tenantId);
        });
    } catch (DataAccessException e) {
        throw handleServerException(ERROR_CODE_DELETE_SECRET, e);
    }
}
Also used : NamedJdbcTemplate(org.wso2.carbon.database.utils.jdbc.NamedJdbcTemplate) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 78 with DataAccessException

use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project identity-inbound-auth-oauth by wso2-extensions.

the class AccessTokenDAOImpl method getAccessTokensByBindingRef.

@Override
public Set<AccessTokenDO> getAccessTokensByBindingRef(String bindingRef) throws IdentityOAuth2Exception {
    if (log.isDebugEnabled()) {
        log.debug("Retrieving active access tokens issued with binding reference : " + bindingRef);
    }
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    try {
        String sqlQuery = GET_ACCESS_TOKENS_BY_BINDING_REFERENCE;
        Map<String, AccessTokenDO> tokenMap = new HashMap<>();
        jdbcTemplate.executeQuery(sqlQuery, rethrowRowMapper((resultSet, i) -> {
            String token = getPersistenceProcessor().getPreprocessedAccessTokenIdentifier(resultSet.getString("ACCESS_TOKEN"));
            AccessTokenDO accessTokenDO = new AccessTokenDO();
            if (tokenMap.containsKey(token)) {
                AccessTokenDO tokenObj = tokenMap.get(token);
                String[] previousScope = tokenObj.getScope();
                String[] newScope = new String[tokenObj.getScope().length + 1];
                System.arraycopy(previousScope, 0, newScope, 0, previousScope.length);
                newScope[previousScope.length] = resultSet.getString("TOKEN_SCOPE");
                tokenObj.setScope(newScope);
            } else {
                String consumerKey = resultSet.getString("CONSUMER_KEY");
                String tokenScope = resultSet.getString("TOKEN_SCOPE");
                String refreshToken = resultSet.getString("REFRESH_TOKEN");
                String tokenId = resultSet.getString("TOKEN_ID");
                int tenantId = resultSet.getInt("TENANT_ID");
                String authzUser = resultSet.getString("AUTHZ_USER");
                String userDomain = resultSet.getString("USER_DOMAIN");
                String authenticatedIDPName = resultSet.getString("NAME");
                AuthenticatedUser user = OAuth2Util.createAuthenticatedUser(authzUser, userDomain, OAuth2Util.getTenantDomain(tenantId), authenticatedIDPName);
                Timestamp issuedTime = resultSet.getTimestamp("TIME_CREATED", Calendar.getInstance(TimeZone.getTimeZone(UTC)));
                Timestamp refreshTokenIssuedTime = resultSet.getTimestamp("REFRESH_TOKEN_TIME_CREATED", Calendar.getInstance(TimeZone.getTimeZone(UTC)));
                long validityPeriodInMillis = resultSet.getLong("VALIDITY_PERIOD");
                long refreshTokenValidityPeriodMillis = resultSet.getLong("REFRESH_TOKEN_VALIDITY_PERIOD");
                String tokenType = resultSet.getString("USER_TYPE");
                String[] scope = OAuth2Util.buildScopeArray(tokenScope);
                accessTokenDO.setAccessToken(token);
                accessTokenDO.setConsumerKey(consumerKey);
                accessTokenDO.setScope(scope);
                accessTokenDO.setAuthzUser(user);
                accessTokenDO.setTenantID(tenantId);
                accessTokenDO.setRefreshToken(refreshToken);
                accessTokenDO.setTokenId(tokenId);
                accessTokenDO.setIssuedTime(issuedTime);
                accessTokenDO.setRefreshTokenIssuedTime(refreshTokenIssuedTime);
                accessTokenDO.setValidityPeriod(validityPeriodInMillis);
                accessTokenDO.setRefreshTokenValidityPeriod(refreshTokenValidityPeriodMillis);
                accessTokenDO.setTokenType(tokenType);
                tokenMap.put(token, accessTokenDO);
            }
            return Collections.emptySet();
        }), (PreparedStatement preparedStatement) -> {
            preparedStatement.setString(1, bindingRef);
        });
        return new HashSet<>(tokenMap.values());
    } catch (DataAccessException e) {
        throw new IdentityOAuth2Exception("Error occurred while retrieving access tokens.", e);
    }
}
Also used : AccessTokenDO(org.wso2.carbon.identity.oauth2.model.AccessTokenDO) StringUtils(org.apache.commons.lang.StringUtils) OAuthServerConfiguration(org.wso2.carbon.identity.oauth.config.OAuthServerConfiguration) Arrays(java.util.Arrays) Connection(java.sql.Connection) IdentityDatabaseUtil(org.wso2.carbon.identity.core.util.IdentityDatabaseUtil) Date(java.util.Date) ResultSet(java.sql.ResultSet) AccessTokenDO(org.wso2.carbon.identity.oauth2.model.AccessTokenDO) Map(java.util.Map) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) OAuth2Util(org.wso2.carbon.identity.oauth2.util.OAuth2Util) DataTruncation(java.sql.DataTruncation) TimeZone(java.util.TimeZone) Timestamp(java.sql.Timestamp) Set(java.util.Set) UUID(java.util.UUID) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) PreparedStatement(java.sql.PreparedStatement) LambdaExceptionUtils.rethrowRowMapper(org.wso2.carbon.identity.core.util.LambdaExceptionUtils.rethrowRowMapper) InvalidOAuthClientException(org.wso2.carbon.identity.oauth.common.exception.InvalidOAuthClientException) OAuthTokenReqMessageContext(org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext) List(java.util.List) GET_ACCESS_TOKENS_BY_BINDING_REFERENCE(org.wso2.carbon.identity.oauth2.dao.SQLQueries.GET_ACCESS_TOKENS_BY_BINDING_REFERENCE) IdentityConstants(org.wso2.carbon.identity.base.IdentityConstants) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) LogFactory(org.apache.commons.logging.LogFactory) STORE_TOKEN_BINDING(org.wso2.carbon.identity.oauth2.dao.SQLQueries.STORE_TOKEN_BINDING) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) OAuth2TokenUtil(org.wso2.carbon.identity.oauth2.util.OAuth2TokenUtil) TokenBinding(org.wso2.carbon.identity.oauth2.token.bindings.TokenBinding) JdbcUtils(org.wso2.carbon.identity.application.mgt.util.JdbcUtils) IdentityUtil.getProperty(org.wso2.carbon.identity.core.util.IdentityUtil.getProperty) HashMap(java.util.HashMap) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) OauthTokenIssuer(org.wso2.carbon.identity.oauth2.token.OauthTokenIssuer) SQLException(java.sql.SQLException) Calendar(java.util.Calendar) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) OAuthConstants(org.wso2.carbon.identity.oauth.common.OAuthConstants) RETRIEVE_TOKEN_BINDING_BY_TOKEN_ID(org.wso2.carbon.identity.oauth2.dao.SQLQueries.RETRIEVE_TOKEN_BINDING_BY_TOKEN_ID) StringUtils.isNotBlank(org.apache.commons.lang.StringUtils.isNotBlank) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) IdentityUtil(org.wso2.carbon.identity.core.util.IdentityUtil) OAuth2ServiceComponentHolder(org.wso2.carbon.identity.oauth2.internal.OAuth2ServiceComponentHolder) Log(org.apache.commons.logging.Log) NONE(org.wso2.carbon.identity.oauth.common.OAuthConstants.TokenBindings.NONE) DigestUtils(org.apache.commons.codec.digest.DigestUtils) Collections(java.util.Collections) ArrayUtils(org.apache.commons.lang.ArrayUtils) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) HashMap(java.util.HashMap) PreparedStatement(java.sql.PreparedStatement) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) Timestamp(java.sql.Timestamp) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) HashSet(java.util.HashSet)

Example 79 with DataAccessException

use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project identity-inbound-auth-oauth by wso2-extensions.

the class AccessTokenDAOImpl method getAccessTokensByBindingRef.

public Set<AccessTokenDO> getAccessTokensByBindingRef(AuthenticatedUser user, String bindingRef) throws IdentityOAuth2Exception {
    if (log.isDebugEnabled()) {
        log.debug("Retrieving active access tokens issued to user, " + user.getUserName() + " with binding " + "reference " + bindingRef);
    }
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    try {
        String sqlQuery = OAuth2Util.getTokenPartitionedSqlByUserStore(SQLQueries.GET_ACCESS_TOKENS_BY_BINDING_REFERENCE_AND_USER, user.getUserStoreDomain());
        int tenantId = OAuth2Util.getTenantId(user.getTenantDomain());
        Map<String, AccessTokenDO> tokenMap = new HashMap<>();
        jdbcTemplate.executeQuery(sqlQuery, rethrowRowMapper((resultSet, i) -> {
            String token = getPersistenceProcessor().getPreprocessedAccessTokenIdentifier(resultSet.getString("ACCESS_TOKEN"));
            AccessTokenDO accessTokenDO = new AccessTokenDO();
            if (tokenMap.containsKey(token)) {
                AccessTokenDO tokenObj = tokenMap.get(token);
                String[] previousScope = tokenObj.getScope();
                String[] newSope = new String[tokenObj.getScope().length + 1];
                System.arraycopy(previousScope, 0, newSope, 0, previousScope.length);
                newSope[previousScope.length] = resultSet.getString(2);
                tokenObj.setScope(newSope);
            } else {
                String consumerKey = resultSet.getString("CONSUMER_KEY");
                String tokenScope = resultSet.getString("TOKEN_SCOPE");
                String refreshToken = resultSet.getString("REFRESH_TOKEN");
                String tokenId = resultSet.getString("TOKEN_ID");
                Timestamp issuedTime = resultSet.getTimestamp("TIME_CREATED", Calendar.getInstance(TimeZone.getTimeZone(UTC)));
                Timestamp refreshTokenIssuedTime = resultSet.getTimestamp("REFRESH_TOKEN_TIME_CREATED", Calendar.getInstance(TimeZone.getTimeZone(UTC)));
                long validityPeriodInMillis = resultSet.getLong("VALIDITY_PERIOD");
                long refreshTokenValidityPeriodMillis = resultSet.getLong("REFRESH_TOKEN_VALIDITY_PERIOD");
                String tokenType = resultSet.getString("USER_TYPE");
                String[] scope = OAuth2Util.buildScopeArray(tokenScope);
                accessTokenDO.setAccessToken(token);
                accessTokenDO.setConsumerKey(consumerKey);
                accessTokenDO.setScope(scope);
                accessTokenDO.setAuthzUser(user);
                accessTokenDO.setTenantID(tenantId);
                accessTokenDO.setRefreshToken(refreshToken);
                accessTokenDO.setTokenId(tokenId);
                accessTokenDO.setIssuedTime(issuedTime);
                accessTokenDO.setRefreshTokenIssuedTime(refreshTokenIssuedTime);
                accessTokenDO.setValidityPeriod(validityPeriodInMillis);
                accessTokenDO.setRefreshTokenValidityPeriod(refreshTokenValidityPeriodMillis);
                accessTokenDO.setTokenType(tokenType);
                tokenMap.put(token, accessTokenDO);
            }
            return null;
        }), (PreparedStatement preparedStatement) -> {
            preparedStatement.setString(1, user.getUserName());
            preparedStatement.setInt(2, tenantId);
            preparedStatement.setString(3, user.getUserStoreDomain());
            preparedStatement.setString(4, bindingRef);
        });
        return new HashSet<>(tokenMap.values());
    } catch (DataAccessException e) {
        throw new IdentityOAuth2Exception("Error occurred while retrieving access tokens.", e);
    }
}
Also used : AccessTokenDO(org.wso2.carbon.identity.oauth2.model.AccessTokenDO) StringUtils(org.apache.commons.lang.StringUtils) OAuthServerConfiguration(org.wso2.carbon.identity.oauth.config.OAuthServerConfiguration) Arrays(java.util.Arrays) Connection(java.sql.Connection) IdentityDatabaseUtil(org.wso2.carbon.identity.core.util.IdentityDatabaseUtil) Date(java.util.Date) ResultSet(java.sql.ResultSet) AccessTokenDO(org.wso2.carbon.identity.oauth2.model.AccessTokenDO) Map(java.util.Map) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) OAuth2Util(org.wso2.carbon.identity.oauth2.util.OAuth2Util) DataTruncation(java.sql.DataTruncation) TimeZone(java.util.TimeZone) Timestamp(java.sql.Timestamp) Set(java.util.Set) UUID(java.util.UUID) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) PreparedStatement(java.sql.PreparedStatement) LambdaExceptionUtils.rethrowRowMapper(org.wso2.carbon.identity.core.util.LambdaExceptionUtils.rethrowRowMapper) InvalidOAuthClientException(org.wso2.carbon.identity.oauth.common.exception.InvalidOAuthClientException) OAuthTokenReqMessageContext(org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext) List(java.util.List) GET_ACCESS_TOKENS_BY_BINDING_REFERENCE(org.wso2.carbon.identity.oauth2.dao.SQLQueries.GET_ACCESS_TOKENS_BY_BINDING_REFERENCE) IdentityConstants(org.wso2.carbon.identity.base.IdentityConstants) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) LogFactory(org.apache.commons.logging.LogFactory) STORE_TOKEN_BINDING(org.wso2.carbon.identity.oauth2.dao.SQLQueries.STORE_TOKEN_BINDING) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) OAuth2TokenUtil(org.wso2.carbon.identity.oauth2.util.OAuth2TokenUtil) TokenBinding(org.wso2.carbon.identity.oauth2.token.bindings.TokenBinding) JdbcUtils(org.wso2.carbon.identity.application.mgt.util.JdbcUtils) IdentityUtil.getProperty(org.wso2.carbon.identity.core.util.IdentityUtil.getProperty) HashMap(java.util.HashMap) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) OauthTokenIssuer(org.wso2.carbon.identity.oauth2.token.OauthTokenIssuer) SQLException(java.sql.SQLException) Calendar(java.util.Calendar) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) OAuthConstants(org.wso2.carbon.identity.oauth.common.OAuthConstants) RETRIEVE_TOKEN_BINDING_BY_TOKEN_ID(org.wso2.carbon.identity.oauth2.dao.SQLQueries.RETRIEVE_TOKEN_BINDING_BY_TOKEN_ID) StringUtils.isNotBlank(org.apache.commons.lang.StringUtils.isNotBlank) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) IdentityUtil(org.wso2.carbon.identity.core.util.IdentityUtil) OAuth2ServiceComponentHolder(org.wso2.carbon.identity.oauth2.internal.OAuth2ServiceComponentHolder) Log(org.apache.commons.logging.Log) NONE(org.wso2.carbon.identity.oauth.common.OAuthConstants.TokenBindings.NONE) DigestUtils(org.apache.commons.codec.digest.DigestUtils) Collections(java.util.Collections) ArrayUtils(org.apache.commons.lang.ArrayUtils) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) HashMap(java.util.HashMap) PreparedStatement(java.sql.PreparedStatement) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) Timestamp(java.sql.Timestamp) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) HashSet(java.util.HashSet)

Example 80 with DataAccessException

use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project identity-inbound-auth-oauth by wso2-extensions.

the class ScopeClaimMappingDAOImpl method updateScope.

/**
 * To add new claims for an existing scope.
 *
 * @param scope    Updated scope name.
 * @param tenantId Tenant Id.
 * @throws IdentityOAuth2Exception If an error occurs when adding a new claim for a scope.
 */
@Override
public void updateScope(ScopeDTO scope, int tenantId) throws IdentityOAuth2Exception {
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    try {
        int scopeId = getScopeId(scope.getName(), tenantId);
        if (scopeId != Oauth2ScopeConstants.INVALID_SCOPE_ID) {
            updateScopeDetails(scope, jdbcTemplate, scopeId);
            deleteClaimMappings(scopeId, jdbcTemplate);
            Set<String> claimsSet = new HashSet<>(Arrays.asList(scope.getClaim()));
            insertClaims(tenantId, scopeId, claimsSet);
        }
    } catch (DataAccessException e) {
        throw new IdentityOAuth2Exception("Error while updating the scope: " + scope.getName() + " and it's related claims.", e);
    }
}
Also used : IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) HashSet(java.util.HashSet)

Aggregations

DataAccessException (org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)79 JdbcTemplate (org.wso2.carbon.database.utils.jdbc.JdbcTemplate)58 PreparedStatement (java.sql.PreparedStatement)33 SQLException (java.sql.SQLException)33 List (java.util.List)31 Log (org.apache.commons.logging.Log)29 LogFactory (org.apache.commons.logging.LogFactory)29 TransactionException (org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException)28 ArrayList (java.util.ArrayList)26 SQLIntegrityConstraintViolationException (java.sql.SQLIntegrityConstraintViolationException)25 Map (java.util.Map)25 HashMap (java.util.HashMap)24 HashSet (java.util.HashSet)22 Set (java.util.Set)22 StringUtils (org.apache.commons.lang.StringUtils)22 Timestamp (java.sql.Timestamp)21 IdentityTenantUtil (org.wso2.carbon.identity.core.util.IdentityTenantUtil)21 Date (java.util.Date)19 Calendar (java.util.Calendar)18 JdbcUtils.isH2DB (org.wso2.carbon.identity.core.util.JdbcUtils.isH2DB)18