Search in sources :

Example 11 with IdentityOAuth2ScopeServerException

use of org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeServerException in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuthScopeDAOImpl method deleteScopeByName.

/**
 * Delete a scope of the provided scope ID
 *
 * @param name     name of the scope
 * @param tenantID tenant ID
 * @throws IdentityOAuth2ScopeServerException IdentityOAuth2ScopeServerException
 */
@Override
public void deleteScopeByName(String name, int tenantID) throws IdentityOAuth2ScopeServerException {
    if (log.isDebugEnabled()) {
        log.debug("Delete scope by name for scope name:" + name);
    }
    try (Connection conn = IdentityDatabaseUtil.getDBConnection()) {
        try {
            deleteScope(name, tenantID, conn);
            IdentityDatabaseUtil.commitTransaction(conn);
        } catch (SQLException e1) {
            IdentityDatabaseUtil.rollbackTransaction(conn);
            String msg = "Error occurred while deleting scopes ";
            throw new IdentityOAuth2ScopeServerException(msg, e1);
        }
    } catch (SQLException e) {
        String msg = "Error occurred while deleting scopes ";
        throw new IdentityOAuth2ScopeServerException(msg, e);
    }
}
Also used : IdentityOAuth2ScopeServerException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeServerException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 12 with IdentityOAuth2ScopeServerException

use of org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeServerException in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuthScopeDAOImpl method getScopeByName.

/**
 * Get a scope by name
 *
 * @param name     name of the scope
 * @param tenantID tenant ID
 * @return Scope for the provided ID
 * @throws IdentityOAuth2ScopeServerException IdentityOAuth2ScopeServerException
 */
@Override
public Scope getScopeByName(String name, int tenantID) throws IdentityOAuth2ScopeServerException {
    if (log.isDebugEnabled()) {
        log.debug("Get scope by name called for scope name:" + name);
    }
    Scope scope = null;
    String sql;
    try (Connection conn = IdentityDatabaseUtil.getDBConnection(false)) {
        if (conn.getMetaData().getDriverName().contains(Oauth2ScopeConstants.DataBaseType.ORACLE)) {
            sql = SQLQueries.RETRIEVE_SCOPE_BY_NAME_ORACLE;
        } else {
            sql = SQLQueries.RETRIEVE_SCOPE_BY_NAME;
        }
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, name);
            ps.setInt(2, tenantID);
            ps.setString(3, Oauth2ScopeConstants.SCOPE_TYPE_OAUTH2);
            try (ResultSet rs = ps.executeQuery()) {
                String description = null;
                String displayName = null;
                while (rs.next()) {
                    if (StringUtils.isBlank(description)) {
                        description = rs.getString(3);
                    }
                    if (StringUtils.isBlank(displayName)) {
                        displayName = rs.getString(2);
                    }
                    String bindingType = rs.getString(5);
                    if (bindingType == null) {
                        bindingType = DEFAULT_SCOPE_BINDING;
                    }
                    if (scope == null) {
                        scope = new Scope(name, displayName, new ArrayList<>(), description);
                    }
                    scope.addScopeBinding(bindingType, rs.getString(4));
                }
            }
        }
        return scope;
    } catch (SQLException e) {
        String msg = "Error occurred while getting scope by ID ";
        throw new IdentityOAuth2ScopeServerException(msg, e);
    }
}
Also used : IdentityOAuth2ScopeServerException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeServerException) Scope(org.wso2.carbon.identity.oauth2.bean.Scope) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) NamedPreparedStatement(org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement) PreparedStatement(java.sql.PreparedStatement)

Example 13 with IdentityOAuth2ScopeServerException

use of org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeServerException in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuthScopeDAOImpl method getRequestedScopesOnly.

@Override
public Set<Scope> getRequestedScopesOnly(int tenantID, Boolean includeOIDCScopes, String requestedScopes) throws IdentityOAuth2ScopeServerException {
    if (log.isDebugEnabled()) {
        log.debug(String.format("Get requested scopes for scopes: %s for tenantId: %s with includeOIDCScopes: %s", requestedScopes, tenantID, includeOIDCScopes));
    }
    // Validate requestedScopes.
    if (StringUtils.isBlank(requestedScopes)) {
        return new HashSet<>();
    }
    String sql;
    if (includeOIDCScopes) {
        sql = String.format(SQLQueries.RETRIEVE_REQUESTED_ALL_SCOPES_WITHOUT_SCOPE_TYPE);
    } else {
        sql = String.format(SQLQueries.RETRIEVE_REQUESTED_OAUTH2_SCOPES);
    }
    List<String> requestedScopeList = Arrays.asList(requestedScopes.split("\\s+"));
    String placeholder = String.join(", ", Collections.nCopies(requestedScopeList.size(), "?"));
    sql = sql.replace(SCOPE_LIST_PLACEHOLDER, placeholder);
    Set<Scope> scopes = new HashSet<>();
    Map<Integer, Scope> scopeMap = new HashMap<>();
    try (Connection conn = IdentityDatabaseUtil.getDBConnection(false)) {
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, tenantID);
            int scopeIndex = 2;
            if (!includeOIDCScopes) {
                ps.setString(2, Oauth2ScopeConstants.SCOPE_TYPE_OAUTH2);
                scopeIndex++;
            }
            for (String scope : requestedScopeList) {
                ps.setString(scopeIndex, scope);
                scopeIndex++;
            }
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    int scopeID = rs.getInt(1);
                    String name = rs.getString(2);
                    String displayName = rs.getString(3);
                    String description = rs.getString(4);
                    final String binding = rs.getString(5);
                    String bindingType = rs.getString(6);
                    if (scopeMap.containsKey(scopeID) && scopeMap.get(scopeID) != null) {
                        scopeMap.get(scopeID).setName(name);
                        scopeMap.get(scopeID).setDescription(description);
                        scopeMap.get(scopeID).setDisplayName(displayName);
                        if (binding != null) {
                            scopeMap.get(scopeID).addScopeBinding(bindingType, binding);
                        }
                    } else {
                        scopeMap.put(scopeID, new Scope(name, displayName, new ArrayList<>(), description));
                        if (binding != null) {
                            scopeMap.get(scopeID).addScopeBinding(bindingType, binding);
                        }
                    }
                }
            }
        }
        for (Map.Entry<Integer, Scope> entry : scopeMap.entrySet()) {
            scopes.add(entry.getValue());
        }
        return scopes;
    } catch (SQLException e) {
        String msg = "Error occurred while getting requested scopes in tenant :" + tenantID;
        throw new IdentityOAuth2ScopeServerException(msg, e);
    }
}
Also used : IdentityOAuth2ScopeServerException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeServerException) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) NamedPreparedStatement(org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement) PreparedStatement(java.sql.PreparedStatement) Scope(org.wso2.carbon.identity.oauth2.bean.Scope) ResultSet(java.sql.ResultSet) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 14 with IdentityOAuth2ScopeServerException

use of org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeServerException in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuthScopeDAOImpl method getAllScopes.

/**
 * Get all available OAuth2 scopes.
 *
 * @param tenantID tenant ID
 * @return available scope list
 * @throws IdentityOAuth2ScopeServerException IdentityOAuth2ScopeServerException
 */
@Override
public Set<Scope> getAllScopes(int tenantID) throws IdentityOAuth2ScopeServerException {
    if (log.isDebugEnabled()) {
        log.debug("Get all scopes for tenantId  :" + tenantID);
    }
    Set<Scope> scopes = new HashSet<>();
    Map<Integer, Scope> scopeMap = new HashMap<>();
    String sql;
    try (Connection conn = IdentityDatabaseUtil.getDBConnection(false)) {
        if (conn.getMetaData().getDriverName().contains(Oauth2ScopeConstants.DataBaseType.ORACLE)) {
            sql = SQLQueries.RETRIEVE_ALL_OAUTH2_SCOPES_ORACLE;
        } else {
            sql = SQLQueries.RETRIEVE_ALL_OAUTH2_SCOPES;
        }
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, tenantID);
            ps.setString(2, Oauth2ScopeConstants.SCOPE_TYPE_OAUTH2);
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    int scopeID = rs.getInt(1);
                    String name = rs.getString(2);
                    String displayName = rs.getString(3);
                    String description = rs.getString(4);
                    final String binding = rs.getString(5);
                    String bindingType = rs.getString(6);
                    if (scopeMap.containsKey(scopeID) && scopeMap.get(scopeID) != null) {
                        scopeMap.get(scopeID).setName(name);
                        scopeMap.get(scopeID).setDescription(description);
                        scopeMap.get(scopeID).setDisplayName(displayName);
                        if (binding != null) {
                            scopeMap.get(scopeID).addScopeBinding(bindingType, binding);
                        }
                    } else {
                        scopeMap.put(scopeID, new Scope(name, displayName, new ArrayList<>(), description));
                        if (binding != null) {
                            scopeMap.get(scopeID).addScopeBinding(bindingType, binding);
                        }
                    }
                }
            }
        }
        for (Map.Entry<Integer, Scope> entry : scopeMap.entrySet()) {
            scopes.add(entry.getValue());
        }
        return scopes;
    } catch (SQLException e) {
        String msg = "Error occurred while getting all OAUTH2 scopes in tenant :" + tenantID;
        throw new IdentityOAuth2ScopeServerException(msg, e);
    }
}
Also used : IdentityOAuth2ScopeServerException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeServerException) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) NamedPreparedStatement(org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement) PreparedStatement(java.sql.PreparedStatement) Scope(org.wso2.carbon.identity.oauth2.bean.Scope) ResultSet(java.sql.ResultSet) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 15 with IdentityOAuth2ScopeServerException

use of org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeServerException in project identity-inbound-auth-oauth by wso2-extensions.

the class JDBCPermissionBasedInternalScopeValidator method getScopesOfPermissionType.

private Set<Scope> getScopesOfPermissionType(int tenantId) throws IdentityOAuth2ScopeServerException {
    if (Oauth2ScopeUtils.isSystemLevelInternalSystemScopeManagementEnabled()) {
        List<Scope> oauthScopeBinding = OAuth2ServiceComponentHolder.getInstance().getOauthScopeBinding();
        return new HashSet<>(oauthScopeBinding);
    }
    Scope[] scopesFromCache = OAuthScopeBindingCache.getInstance().getValueFromCache(new OAuthScopeBindingCacheKey(PERMISSION_BINDING_TYPE), tenantId);
    Set<Scope> allScopes;
    if (scopesFromCache != null) {
        allScopes = Arrays.stream(scopesFromCache).collect(Collectors.toSet());
    } else {
        allScopes = OAuthTokenPersistenceFactory.getInstance().getOAuthScopeDAO().getScopes(tenantId, PERMISSION_BINDING_TYPE);
        if (CollectionUtils.isNotEmpty(allScopes)) {
            OAuthScopeBindingCache.getInstance().addToCache(new OAuthScopeBindingCacheKey(PERMISSION_BINDING_TYPE), allScopes.toArray(new Scope[0]), tenantId);
        }
    }
    return allScopes;
}
Also used : Scope(org.wso2.carbon.identity.oauth2.bean.Scope) OAuthScopeBindingCacheKey(org.wso2.carbon.identity.oauth.cache.OAuthScopeBindingCacheKey) HashSet(java.util.HashSet)

Aggregations

IdentityOAuth2ScopeServerException (org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeServerException)13 Scope (org.wso2.carbon.identity.oauth2.bean.Scope)13 Connection (java.sql.Connection)10 SQLException (java.sql.SQLException)10 ArrayList (java.util.ArrayList)8 PreparedStatement (java.sql.PreparedStatement)7 ResultSet (java.sql.ResultSet)7 HashSet (java.util.HashSet)7 NamedPreparedStatement (org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement)7 OAuthScopeCacheKey (org.wso2.carbon.identity.oauth.cache.OAuthScopeCacheKey)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 UserApplicationScopeConsentDO (org.wso2.carbon.identity.oauth2.model.UserApplicationScopeConsentDO)5 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)2 OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)1 UserIdNotFoundException (org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException)1 OAuthScopeBindingCacheKey (org.wso2.carbon.identity.oauth.cache.OAuthScopeBindingCacheKey)1 InvalidOAuthClientException (org.wso2.carbon.identity.oauth.common.exception.InvalidOAuthClientException)1 OAuthAppDO (org.wso2.carbon.identity.oauth.dao.OAuthAppDO)1 ScopeBinding (org.wso2.carbon.identity.oauth2.bean.ScopeBinding)1