Search in sources :

Example 6 with UserApplicationScopeConsentDO

use of org.wso2.carbon.identity.oauth2.model.UserApplicationScopeConsentDO 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 7 with UserApplicationScopeConsentDO

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

the class OAuthUserConsentedScopesDAOImpl method addUserConsentForApplication.

@Override
public void addUserConsentForApplication(String userId, int tenantId, UserApplicationScopeConsentDO userConsent) throws IdentityOAuth2ScopeConsentException {
    if (log.isDebugEnabled()) {
        log.debug("Adding scope consents for userId : " + userId + " and appId : " + userConsent.getAppId() + " and tenantId : " + tenantId + " for approved scopes : " + userConsent.getApprovedScopes().stream().collect(Collectors.joining(", ")) + " and " + "disapproved scopes : " + userConsent.getDeniedScopes().stream().collect(Collectors.joining(", ")) + ".");
    }
    try (Connection conn = IdentityDatabaseUtil.getDBConnection(true)) {
        String consentId = generateConsentId();
        deleteUserConsent(conn, userId, userConsent.getAppId(), tenantId);
        addUserConsentInformation(conn, userId, userConsent.getAppId(), tenantId, consentId);
        addUserConsentedScopes(conn, consentId, tenantId, userConsent);
        IdentityDatabaseUtil.commitTransaction(conn);
    } catch (SQLException e) {
        String msg = "Error occurred while adding scope consents for userId : " + userId + " and appId : " + userConsent.getAppId() + " and tenantId : " + tenantId;
        throw new IdentityOAuth2ScopeConsentException(msg, e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) IdentityOAuth2ScopeConsentException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeConsentException)

Example 8 with UserApplicationScopeConsentDO

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

the class CacheBackedOAuthUserConsentedScopesDAOImpl method getUserConsentForApplication.

@Override
public UserApplicationScopeConsentDO getUserConsentForApplication(String userId, String appId, int tenantId) throws IdentityOAuth2ScopeConsentException {
    OAuthUserConsentedScopeCacheEntry entry = cache.getValueFromCache(userId, tenantId);
    if (entry != null && entry.getAppID().equals(appId)) {
        return entry.getUserApplicationScopeConsentDO();
    }
    UserApplicationScopeConsentDO userConsent = dao.getUserConsentForApplication(userId, appId, tenantId);
    cache.addToCache(userId, new OAuthUserConsentedScopeCacheEntry(appId, userConsent), tenantId);
    return userConsent;
}
Also used : UserApplicationScopeConsentDO(org.wso2.carbon.identity.oauth2.model.UserApplicationScopeConsentDO) OAuthUserConsentedScopeCacheEntry(org.wso2.carbon.identity.oauth2.internal.cache.OAuthUserConsentedScopeCacheEntry)

Example 9 with UserApplicationScopeConsentDO

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

the class OAuthUserConsentedScopesDAOImpl method updateExistingConsentForApplication.

@Override
public void updateExistingConsentForApplication(String userId, String appId, int tenantId, UserApplicationScopeConsentDO consentsToBeAdded, UserApplicationScopeConsentDO consentsToBeUpdated) throws IdentityOAuth2ScopeConsentException {
    if (log.isDebugEnabled()) {
        log.debug("Update scope consents for userId : " + userId + " and appId: " + appId + " and tenantId : " + tenantId);
    }
    try (Connection conn = IdentityDatabaseUtil.getDBConnection(true)) {
        String consentId = getConsentId(conn, userId, appId, tenantId);
        if (StringUtils.isBlank(consentId)) {
            if (log.isDebugEnabled()) {
                log.debug("Unable to find an existing consent for user : " + userId + ", app : " + appId + " and tenant with id : " + tenantId);
            }
            throw new IdentityOAuth2ScopeConsentException("Unable to find an existing consent for user : " + userId + ", app : " + appId + " and tenant with id : " + tenantId);
        }
        if (CollectionUtils.isNotEmpty(consentsToBeAdded.getApprovedScopes()) || CollectionUtils.isNotEmpty(consentsToBeAdded.getDeniedScopes())) {
            addUserConsentedScopes(conn, consentId, tenantId, consentsToBeAdded);
        }
        if (CollectionUtils.isNotEmpty(consentsToBeUpdated.getApprovedScopes()) || CollectionUtils.isNotEmpty(consentsToBeUpdated.getDeniedScopes())) {
            updateUserConsentedScopes(conn, userId, tenantId, consentsToBeUpdated);
        }
        IdentityDatabaseUtil.commitTransaction(conn);
    } catch (SQLException e) {
        String msg = "Error occurred while updating scope consents for  userId : " + userId + " and appId : " + appId + " and tenantId : " + tenantId;
        throw new IdentityOAuth2ScopeConsentException(msg, e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) IdentityOAuth2ScopeConsentException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeConsentException)

Example 10 with UserApplicationScopeConsentDO

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

the class OAuthUserConsentedScopesDAOImpl method getUserConsents.

@Override
public List<UserApplicationScopeConsentDO> getUserConsents(String userId, int tenantId) throws IdentityOAuth2ScopeConsentException {
    if (log.isDebugEnabled()) {
        log.debug("Get user consented scopes for user with userId : " + userId + " in tenantId : " + tenantId);
    }
    Map<String, UserApplicationScopeConsentDO> userScopeConsentsMap = new HashMap<>();
    try (Connection conn = IdentityDatabaseUtil.getDBConnection(false)) {
        try (PreparedStatement ps = conn.prepareStatement(SQLQueries.GET_OAUTH2_USER_CONSENTS)) {
            ps.setString(1, userId);
            ps.setInt(2, tenantId);
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    String appId = rs.getString(1);
                    String scope = rs.getString(2);
                    boolean consent = rs.getBoolean(3);
                    userScopeConsentsMap.putIfAbsent(appId, new UserApplicationScopeConsentDO(appId));
                    if (consent) {
                        userScopeConsentsMap.get(appId).getApprovedScopes().add(scope);
                    } else {
                        userScopeConsentsMap.get(appId).getDeniedScopes().add(scope);
                    }
                }
            }
        }
        return new ArrayList<>(userScopeConsentsMap.values());
    } catch (SQLException e) {
        String msg = "Error occurred while retrieving scope consents for userId :" + userId + " in tenantId : " + tenantId;
        throw new IdentityOAuth2ScopeConsentException(msg, e);
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) UserApplicationScopeConsentDO(org.wso2.carbon.identity.oauth2.model.UserApplicationScopeConsentDO) IdentityOAuth2ScopeConsentException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeConsentException)

Aggregations

UserApplicationScopeConsentDO (org.wso2.carbon.identity.oauth2.model.UserApplicationScopeConsentDO)10 Connection (java.sql.Connection)4 SQLException (java.sql.SQLException)4 ArrayList (java.util.ArrayList)4 IdentityOAuth2ScopeConsentException (org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeConsentException)4 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 HashMap (java.util.HashMap)1 OAuthUserConsentedScopeCacheEntry (org.wso2.carbon.identity.oauth2.internal.cache.OAuthUserConsentedScopeCacheEntry)1 OAuth2ScopeConsentResponse (org.wso2.carbon.identity.oauth2.model.OAuth2ScopeConsentResponse)1