Search in sources :

Example 81 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 getClaims.

@Override
public ScopeDTO getClaims(String scope, int tenantId) throws IdentityOAuth2Exception {
    String sql = SQLQueries.GET_IDN_OIDC_CLAIMS;
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    ScopeDTO scopeDTO = new ScopeDTO();
    try {
        List<String> claimsList = jdbcTemplate.executeQuery(sql, (resultSet, i) -> resultSet.getString(1), preparedStatement -> {
            preparedStatement.setString(1, scope);
            preparedStatement.setInt(2, tenantId);
        });
        scopeDTO.setName(scope);
        String[] claimsArr = new String[claimsList.size()];
        scopeDTO.setClaim(claimsList.toArray(claimsArr));
    } catch (DataAccessException e) {
        String errorMessage = "Error while loading OIDC claims for the scope: " + scope;
        throw new IdentityOAuth2Exception(errorMessage, e);
    }
    return scopeDTO;
}
Also used : IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) ScopeDTO(org.wso2.carbon.identity.oauth.dto.ScopeDTO) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 82 with DataAccessException

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

the class RequestObjectDAOImpl method getRequestedClaimsbySessionDataKey.

/**
 * Retrieve Requested claims for the sessionDataKey and user info endpoint.
 *
 * @param sessionDataKey      sessionDataKey
 * @param isUserInfo      isUserInfo
 * @throws IdentityOAuth2Exception
 */
@Override
public List<RequestedClaim> getRequestedClaimsbySessionDataKey(String sessionDataKey, boolean isUserInfo) throws IdentityOAuth2Exception {
    Connection connection = null;
    PreparedStatement prepStmt = null;
    ResultSet resultSet = null;
    List<RequestedClaim> essentialClaims = new ArrayList<>();
    try {
        connection = IdentityDatabaseUtil.getDBConnection(false);
        String sql = isH2DB() ? SQLQueries.RETRIEVE_REQUESTED_CLAIMS_BY_SESSION_DATA_KEY_H2 : SQLQueries.RETRIEVE_REQUESTED_CLAIMS_BY_SESSION_DATA_KEY;
        prepStmt = connection.prepareStatement(sql);
        prepStmt.setString(1, sessionDataKey);
        prepStmt.setString(2, isUserInfo ? "1" : "0");
        resultSet = prepStmt.executeQuery();
        while (resultSet.next()) {
            RequestedClaim requestedClaim = new RequestedClaim();
            requestedClaim.setName(resultSet.getString(1));
            requestedClaim.setEssential(!"0".equals(resultSet.getString(2)));
            requestedClaim.setValue(resultSet.getString(3));
            essentialClaims.add(requestedClaim);
        }
    } catch (DataAccessException | SQLException e) {
        String errorMsg = "Error occurred while retrieving request object by session data key: " + sessionDataKey + ", isUserInfo: " + isUserInfo;
        throw new IdentityOAuth2Exception(errorMsg, e);
    } finally {
        IdentityDatabaseUtil.closeAllConnections(connection, resultSet, prepStmt);
    }
    return essentialClaims;
}
Also used : IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) SQLException(java.sql.SQLException) RequestedClaim(org.wso2.carbon.identity.openidconnect.model.RequestedClaim) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 83 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 getScope.

/**
 * Get OIDC scope details by scope name.
 *
 * @param scopeName Scope name.
 * @param tenantId  Tenant ID.
 * @return OIDC scope object.
 * @throws IdentityOAuth2Exception
 */
@Override
public ScopeDTO getScope(String scopeName, int tenantId) throws IdentityOAuth2Exception {
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    String sql = SQLQueries.GET_IDN_OIDC_SCOPE_DETAILS;
    try {
        Map<String, ScopeDTO> tempScopeMap = new HashMap<>();
        jdbcTemplate.executeQuery(sql, (RowMapper<ScopeDTO>) (resultSet, i) -> {
            if (!tempScopeMap.containsKey(resultSet.getString(1))) {
                ScopeDTO scopeDTO = new ScopeDTO(resultSet.getString(1), resultSet.getString(2), resultSet.getString(3), new String[] {});
                if (resultSet.getString(4) != null) {
                    scopeDTO.setClaim(new String[] { resultSet.getString(4) });
                }
                tempScopeMap.put(resultSet.getString(1), scopeDTO);
            } else {
                if (resultSet.getString(4) != null) {
                    ScopeDTO tempScope = tempScopeMap.get(resultSet.getString(1));
                    tempScope.addNewClaimToExistingClaims(resultSet.getString(4));
                    tempScopeMap.replace(resultSet.getString(1), tempScope);
                }
            }
            return null;
        }, preparedStatement -> {
            preparedStatement.setString(1, scopeName);
            preparedStatement.setInt(2, tenantId);
            preparedStatement.setString(3, Oauth2ScopeConstants.SCOPE_TYPE_OIDC);
            preparedStatement.setInt(4, tenantId);
            preparedStatement.setInt(5, tenantId);
            preparedStatement.setString(6, OIDC_DIALECT_URI);
        });
        return tempScopeMap.get(scopeName);
    } catch (DataAccessException e) {
        String errorMessage = "Error while fetching scope details for scope: " + scopeName;
        throw new IdentityOAuth2Exception(errorMessage, e);
    }
}
Also used : IdentityOAuth2ClientException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ClientException) Arrays(java.util.Arrays) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) LambdaExceptionUtils.rethrowConsumer(org.wso2.carbon.identity.core.util.LambdaExceptionUtils.rethrowConsumer) Oauth2ScopeConstants(org.wso2.carbon.identity.oauth2.Oauth2ScopeConstants) Set(java.util.Set) HashMap(java.util.HashMap) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) JdbcUtils(org.wso2.carbon.identity.oauth2.util.JdbcUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) CollectionUtils(org.apache.commons.collections.CollectionUtils) TransactionException(org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) ScopeDTO(org.wso2.carbon.identity.oauth.dto.ScopeDTO) Map(java.util.Map) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) Log(org.apache.commons.logging.Log) RowMapper(org.wso2.carbon.database.utils.jdbc.RowMapper) LogFactory(org.apache.commons.logging.LogFactory) ArrayUtils(org.apache.commons.lang.ArrayUtils) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) HashMap(java.util.HashMap) ScopeDTO(org.wso2.carbon.identity.oauth.dto.ScopeDTO) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

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