Search in sources :

Example 1 with RowMapper

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

the class ScopeClaimMappingDAOImpl method getScopes.

@Override
public List<ScopeDTO> getScopes(int tenantId) throws IdentityOAuth2Exception {
    String sql = SQLQueries.GET_IDN_OIDC_SCOPES_CLAIMS;
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    List<ScopeDTO> oidcScopeClaimList;
    try {
        Map<String, ScopeDTO> scopeClaimMap = new HashMap<>();
        jdbcTemplate.executeQuery(sql, (RowMapper<ScopeDTO>) (resultSet, i) -> {
            String scope = resultSet.getString(1);
            if (!scopeClaimMap.containsKey(scope)) {
                ScopeDTO tempScopeDTO = new ScopeDTO(scope, resultSet.getString(2), resultSet.getString(3), new String[] {});
                if (resultSet.getString(4) != null) {
                    tempScopeDTO.setClaim(new String[] { resultSet.getString(4) });
                }
                scopeClaimMap.put(scope, tempScopeDTO);
            } else {
                if (resultSet.getString(4) != null) {
                    ScopeDTO tempScope = scopeClaimMap.get(scope);
                    tempScope.addNewClaimToExistingClaims(resultSet.getString(4));
                    scopeClaimMap.replace(scope, tempScope);
                }
            }
            return null;
        }, preparedStatement -> {
            preparedStatement.setInt(1, tenantId);
            preparedStatement.setString(2, Oauth2ScopeConstants.SCOPE_TYPE_OIDC);
            preparedStatement.setInt(3, tenantId);
            preparedStatement.setInt(4, tenantId);
            preparedStatement.setString(5, OIDC_DIALECT_URI);
        });
        oidcScopeClaimList = new ArrayList<ScopeDTO>(scopeClaimMap.values());
    } catch (DataAccessException e) {
        String errorMessage = "Error occured while loading scopes claims mapping.";
        throw new IdentityOAuth2Exception(errorMessage, e);
    }
    return oidcScopeClaimList;
}
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)

Example 2 with RowMapper

use of org.wso2.carbon.database.utils.jdbc.RowMapper 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

SQLIntegrityConstraintViolationException (java.sql.SQLIntegrityConstraintViolationException)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 CollectionUtils (org.apache.commons.collections.CollectionUtils)2 ArrayUtils (org.apache.commons.lang.ArrayUtils)2 Log (org.apache.commons.logging.Log)2 LogFactory (org.apache.commons.logging.LogFactory)2 JdbcTemplate (org.wso2.carbon.database.utils.jdbc.JdbcTemplate)2 RowMapper (org.wso2.carbon.database.utils.jdbc.RowMapper)2 DataAccessException (org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)2 TransactionException (org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException)2 LambdaExceptionUtils.rethrowConsumer (org.wso2.carbon.identity.core.util.LambdaExceptionUtils.rethrowConsumer)2 ScopeDTO (org.wso2.carbon.identity.oauth.dto.ScopeDTO)2 IdentityOAuth2ClientException (org.wso2.carbon.identity.oauth2.IdentityOAuth2ClientException)2 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)2