Search in sources :

Example 21 with IdentityOAuth2ScopeServerException

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

the class OAuthScopeDAOImpl method getScopes.

@Override
public Set<Scope> getScopes(int tenantID, String bindingType) throws IdentityOAuth2ScopeServerException {
    if (log.isDebugEnabled()) {
        log.debug("Get scopes for tenantId  :" + tenantID + " and bindingType: " + bindingType);
    }
    Set<Scope> scopes = new HashSet<>();
    Map<Integer, Scope> scopeMap = new HashMap<>();
    try (Connection conn = IdentityDatabaseUtil.getDBConnection(false)) {
        try (PreparedStatement ps = conn.prepareStatement(SQLQueries.RETRIEVE_SCOPES_BY_BINDING_TYPE)) {
            ps.setInt(1, tenantID);
            ps.setString(2, bindingType);
            ps.setString(3, 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);
                    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 scopes ";
        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 22 with IdentityOAuth2ScopeServerException

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

the class OAuthScopeDAOImpl method getAllScopesIncludingOIDCScopes.

/**
 * Get all scopes including OAuth2 scopes and OIDC scopes as well.
 *
 * @param tenantID Tenant ID.
 * @return List of scopes.
 * @throws IdentityOAuth2ScopeServerException
 */
private Set<Scope> getAllScopesIncludingOIDCScopes(int tenantID) throws IdentityOAuth2ScopeServerException {
    if (log.isDebugEnabled()) {
        log.debug("Get all scopes including OAUTH2 and OIDC 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_SCOPES_ORACLE;
        } else {
            sql = SQLQueries.RETRIEVE_ALL_SCOPES;
        }
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, tenantID);
            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 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 23 with IdentityOAuth2ScopeServerException

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

the class OAuth2ScopeService method deleteScope.

/**
 * Delete the scope for the given scope ID
 *
 * @param name Scope ID of the scope which need to get deleted
 * @throws IdentityOAuth2ScopeException
 */
public void deleteScope(String name) throws IdentityOAuth2ScopeException {
    validateScopeName(name);
    // Check whether a scope exists with the provided scope name which to be deleted.
    validateScopeExistence(name);
    int tenantID = Oauth2ScopeUtils.getTenantID();
    OAuthScopeCache.getInstance().clearCacheEntry(new OAuthScopeCacheKey(name), tenantID);
    try {
        OAuthTokenPersistenceFactory.getInstance().getOAuthScopeDAO().deleteScopeByName(name, tenantID);
        if (log.isDebugEnabled()) {
            log.debug("Scope: " + name + " is deleted from the database.");
        }
    } catch (IdentityOAuth2ScopeServerException e) {
        throw Oauth2ScopeUtils.generateServerException(Oauth2ScopeConstants.ErrorMessages.ERROR_CODE_FAILED_TO_DELETE_SCOPE_BY_NAME, name, e);
    }
}
Also used : OAuthScopeCacheKey(org.wso2.carbon.identity.oauth.cache.OAuthScopeCacheKey)

Example 24 with IdentityOAuth2ScopeServerException

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

the class OAuth2ScopeService method isScopeExists.

/**
 * Check the existence of a scope
 *
 * @param name Name of the scope
 * @return true if scope with the given scope name exists
 * @throws IdentityOAuth2ScopeException
 */
public boolean isScopeExists(String name) throws IdentityOAuth2ScopeException {
    boolean isScopeExists;
    int tenantID = Oauth2ScopeUtils.getTenantID();
    if (name == null) {
        throw Oauth2ScopeUtils.generateClientException(Oauth2ScopeConstants.ErrorMessages.ERROR_CODE_BAD_REQUEST_SCOPE_NAME_NOT_SPECIFIED, null);
    }
    Scope scopeFromCache = OAuthScopeCache.getInstance().getValueFromCache(new OAuthScopeCacheKey(name), tenantID);
    if (scopeFromCache != null) {
        isScopeExists = true;
    } else {
        try {
            isScopeExists = OAuthTokenPersistenceFactory.getInstance().getOAuthScopeDAO().isScopeExists(name, tenantID);
        } catch (IdentityOAuth2ScopeServerException e) {
            throw Oauth2ScopeUtils.generateServerException(Oauth2ScopeConstants.ErrorMessages.ERROR_CODE_FAILED_TO_GET_SCOPE_BY_NAME, name, e);
        }
    }
    return isScopeExists;
}
Also used : Scope(org.wso2.carbon.identity.oauth2.bean.Scope) OAuthScopeCacheKey(org.wso2.carbon.identity.oauth.cache.OAuthScopeCacheKey)

Example 25 with IdentityOAuth2ScopeServerException

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

the class OAuth2ScopeService method updateUserConsentForApplication.

/**
 * Update consent given for OAuth scopes by a user for a given application.
 *
 * @param userId            User Id.
 * @param appId             Application Id.
 * @param userTenantId      Tenant Id.
 * @param approvedScopes    List of approved scopes.
 * @param deniedScopes      List of denied scopes.
 * @throws IdentityOAuth2ScopeException
 */
public void updateUserConsentForApplication(String userId, String appId, int userTenantId, List<String> approvedScopes, List<String> deniedScopes) throws IdentityOAuth2ScopeException {
    validateUserId(userId);
    validateAppId(appId);
    try {
        UserApplicationScopeConsentDO updatedUserApplicationScopeConsents = new UserApplicationScopeConsentDO(appId, approvedScopes, deniedScopes);
        UserApplicationScopeConsentDO existingConsent = OAuthTokenPersistenceFactory.getInstance().getOAuthUserConsentedScopesDAO().getUserConsentForApplication(userId, updatedUserApplicationScopeConsents.getAppId(), userTenantId);
        UserApplicationScopeConsentDO consentsToBeUpdated = getConsentsToBeUpdated(existingConsent, updatedUserApplicationScopeConsents);
        UserApplicationScopeConsentDO consentsToBeAdded = getConsentsToBeAdded(consentsToBeUpdated, updatedUserApplicationScopeConsents);
        OAuthTokenPersistenceFactory.getInstance().getOAuthUserConsentedScopesDAO().updateExistingConsentForApplication(userId, appId, userTenantId, consentsToBeAdded, consentsToBeUpdated);
        if (log.isDebugEnabled()) {
            log.debug("Successfully updated the user consent for OAuth scopes for user : " + userId + " and application : " + appId + " in tenant with Id : " + userTenantId);
        }
    } catch (IdentityOAuth2ScopeConsentException e) {
        Oauth2ScopeConstants.ErrorMessages error = Oauth2ScopeConstants.ErrorMessages.ERROR_CODE_FAILED_TO_UPDATE_USER_CONSENT_FOR_APP;
        String msg = String.format(error.getMessage(), userId, appId, userTenantId);
        throw new IdentityOAuth2ScopeServerException(error.getCode(), msg, e);
    }
}
Also used : UserApplicationScopeConsentDO(org.wso2.carbon.identity.oauth2.model.UserApplicationScopeConsentDO)

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