Search in sources :

Example 6 with IdentityOAuth2ScopeServerException

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

the class OAuth2ScopeService method updateScope.

/**
 * Update the scope of the given scope ID
 *
 * @param updatedScope details of updated scope
 * @return updated scope
 * @throws IdentityOAuth2ScopeException
 */
public Scope updateScope(Scope updatedScope) throws IdentityOAuth2ScopeException {
    updateScopePreValidation(updatedScope);
    // Check whether a scope exists with the provided scope name which to be deleted.
    validateScopeExistence(updatedScope.getName());
    int tenantID = Oauth2ScopeUtils.getTenantID();
    try {
        OAuthTokenPersistenceFactory.getInstance().getOAuthScopeDAO().updateScopeByName(updatedScope, tenantID);
    } catch (IdentityOAuth2ScopeServerException e) {
        throw Oauth2ScopeUtils.generateServerException(Oauth2ScopeConstants.ErrorMessages.ERROR_CODE_FAILED_TO_UPDATE_SCOPE_BY_NAME, updatedScope.getName(), e);
    }
    OAuthScopeCache.getInstance().addToCache(new OAuthScopeCacheKey(updatedScope.getName()), updatedScope, tenantID);
    OIDCScopeClaimCache.getInstance().clearScopeClaimMap(tenantID);
    return updatedScope;
}
Also used : OAuthScopeCacheKey(org.wso2.carbon.identity.oauth.cache.OAuthScopeCacheKey)

Example 7 with IdentityOAuth2ScopeServerException

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

the class OAuth2ScopeService method addUserConsentForApplication.

/**
 * Add an OAuth scope consent given for an application by an user.
 *
 * @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 addUserConsentForApplication(String userId, String appId, int userTenantId, List<String> approvedScopes, List<String> deniedScopes) throws IdentityOAuth2ScopeException {
    validateUserId(userId);
    validateAppId(appId);
    try {
        UserApplicationScopeConsentDO userApplicationScopeConsents = new UserApplicationScopeConsentDO(appId, approvedScopes, deniedScopes);
        OAuthTokenPersistenceFactory.getInstance().getOAuthUserConsentedScopesDAO().addUserConsentForApplication(userId, userTenantId, userApplicationScopeConsents);
        if (log.isDebugEnabled()) {
            log.debug("Successfully added the user consent for OAuth scopes for user : " + userId + " and application name : " + appId + " in tenant with id : " + userTenantId);
        }
    } catch (IdentityOAuth2ScopeConsentException e) {
        Oauth2ScopeConstants.ErrorMessages error = Oauth2ScopeConstants.ErrorMessages.ERROR_CODE_FAILED_TO_ADD_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)

Example 8 with IdentityOAuth2ScopeServerException

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

the class OAuth2ScopeService method getUserConsentForApp.

/**
 * Get OAuth scope consent given for an application by the user.
 *
 * @param userId        User Id.
 * @param appId         Application Id.
 * @param userTenantId  Tenant Id.
 * @return  {@link OAuth2ScopeConsentResponse}.
 * @throws IdentityOAuth2ScopeException
 */
public OAuth2ScopeConsentResponse getUserConsentForApp(String userId, String appId, int userTenantId) throws IdentityOAuth2ScopeException {
    validateUserId(userId);
    validateAppId(appId);
    try {
        UserApplicationScopeConsentDO userConsent = OAuthTokenPersistenceFactory.getInstance().getOAuthUserConsentedScopesDAO().getUserConsentForApplication(userId, appId, userTenantId);
        OAuth2ScopeConsentResponse consentResponse = new OAuth2ScopeConsentResponse(userId, appId, userTenantId, userConsent.getApprovedScopes(), userConsent.getDeniedScopes());
        if (log.isDebugEnabled()) {
            log.debug("Successfully retrieved the user consent for userId : " + userId + " and appId: " + appId + " as approved scopes : " + userConsent.getApprovedScopes().stream().collect(Collectors.joining(" ")) + " and denied scopes : " + userConsent.getDeniedScopes().stream().collect(Collectors.joining(" ")));
        }
        return consentResponse;
    } catch (IdentityOAuth2ScopeConsentException e) {
        Oauth2ScopeConstants.ErrorMessages error = Oauth2ScopeConstants.ErrorMessages.ERROR_CODE_FAILED_TO_RETRIEVE_USER_CONSENTS_FOR_APP;
        String msg = String.format(error.getMessage(), userId, appId, userTenantId);
        throw new IdentityOAuth2ScopeServerException(error.getCode(), msg, e);
    }
}
Also used : OAuth2ScopeConsentResponse(org.wso2.carbon.identity.oauth2.model.OAuth2ScopeConsentResponse) UserApplicationScopeConsentDO(org.wso2.carbon.identity.oauth2.model.UserApplicationScopeConsentDO)

Example 9 with IdentityOAuth2ScopeServerException

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

the class EndpointUtil method getRegisteredScopes.

private static Set<String> getRegisteredScopes(Set<String> requestedScopes) throws OAuthSystemException {
    try {
        String requestedScopesStr = StringUtils.join(requestedScopes, " ");
        Set<String> registeredScopes = new HashSet<>();
        Set<Scope> registeredScopeSet = oAuth2ScopeService.getScopes(null, null, true, requestedScopesStr);
        registeredScopeSet.forEach(scope -> registeredScopes.add(scope.getName()));
        return registeredScopes;
    } catch (IdentityOAuth2ScopeServerException e) {
        throw new OAuthSystemException("Error occurred while retrieving registered scopes.", e);
    }
}
Also used : IdentityOAuth2ScopeServerException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeServerException) Scope(org.wso2.carbon.identity.oauth2.bean.Scope) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) HashSet(java.util.HashSet)

Example 10 with IdentityOAuth2ScopeServerException

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

the class OAuthScopeDAOImpl method updateScopeByName.

/**
 * Update a scope of the provided scope name
 *
 * @param updatedScope details of the updated scope
 * @param tenantID     tenant ID
 * @throws IdentityOAuth2ScopeServerException IdentityOAuth2ScopeServerException
 */
@Override
public void updateScopeByName(Scope updatedScope, int tenantID) throws IdentityOAuth2ScopeServerException {
    if (log.isDebugEnabled()) {
        log.debug("Update scope by name for scope name:" + updatedScope.getName());
    }
    try (Connection conn = IdentityDatabaseUtil.getDBConnection()) {
        try {
            int scopeId = getScopeId(updatedScope.getName(), tenantID, conn);
            if (scopeId != Oauth2ScopeConstants.INVALID_SCOPE_ID) {
                updateScopeDetails(updatedScope, conn, scopeId);
                deleteBindings(scopeId, conn);
                addScopeBinding(updatedScope, conn, scopeId);
                IdentityDatabaseUtil.commitTransaction(conn);
            }
        } catch (SQLException e1) {
            IdentityDatabaseUtil.rollbackTransaction(conn);
            String msg = "Error occurred while updating scope by ID ";
            throw new IdentityOAuth2ScopeServerException(msg, e1);
        }
    } catch (SQLException e) {
        String msg = "Error occurred while updating scope by ID ";
        throw new IdentityOAuth2ScopeServerException(msg, e);
    }
}
Also used : IdentityOAuth2ScopeServerException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeServerException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

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