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