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);
}
}
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);
}
}
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;
}
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);
}
}
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);
}
}
Aggregations