use of org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeServerException in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuthScopeDAOImpl method deleteScopeByName.
/**
* Delete a scope of the provided scope ID
*
* @param name name of the scope
* @param tenantID tenant ID
* @throws IdentityOAuth2ScopeServerException IdentityOAuth2ScopeServerException
*/
@Override
public void deleteScopeByName(String name, int tenantID) throws IdentityOAuth2ScopeServerException {
if (log.isDebugEnabled()) {
log.debug("Delete scope by name for scope name:" + name);
}
try (Connection conn = IdentityDatabaseUtil.getDBConnection()) {
try {
deleteScope(name, tenantID, conn);
IdentityDatabaseUtil.commitTransaction(conn);
} catch (SQLException e1) {
IdentityDatabaseUtil.rollbackTransaction(conn);
String msg = "Error occurred while deleting scopes ";
throw new IdentityOAuth2ScopeServerException(msg, e1);
}
} catch (SQLException e) {
String msg = "Error occurred while deleting 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 getScopeByName.
/**
* Get a scope by name
*
* @param name name of the scope
* @param tenantID tenant ID
* @return Scope for the provided ID
* @throws IdentityOAuth2ScopeServerException IdentityOAuth2ScopeServerException
*/
@Override
public Scope getScopeByName(String name, int tenantID) throws IdentityOAuth2ScopeServerException {
if (log.isDebugEnabled()) {
log.debug("Get scope by name called for scope name:" + name);
}
Scope scope = null;
String sql;
try (Connection conn = IdentityDatabaseUtil.getDBConnection(false)) {
if (conn.getMetaData().getDriverName().contains(Oauth2ScopeConstants.DataBaseType.ORACLE)) {
sql = SQLQueries.RETRIEVE_SCOPE_BY_NAME_ORACLE;
} else {
sql = SQLQueries.RETRIEVE_SCOPE_BY_NAME;
}
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, name);
ps.setInt(2, tenantID);
ps.setString(3, Oauth2ScopeConstants.SCOPE_TYPE_OAUTH2);
try (ResultSet rs = ps.executeQuery()) {
String description = null;
String displayName = null;
while (rs.next()) {
if (StringUtils.isBlank(description)) {
description = rs.getString(3);
}
if (StringUtils.isBlank(displayName)) {
displayName = rs.getString(2);
}
String bindingType = rs.getString(5);
if (bindingType == null) {
bindingType = DEFAULT_SCOPE_BINDING;
}
if (scope == null) {
scope = new Scope(name, displayName, new ArrayList<>(), description);
}
scope.addScopeBinding(bindingType, rs.getString(4));
}
}
}
return scope;
} catch (SQLException e) {
String msg = "Error occurred while getting scope by ID ";
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 getRequestedScopesOnly.
@Override
public Set<Scope> getRequestedScopesOnly(int tenantID, Boolean includeOIDCScopes, String requestedScopes) throws IdentityOAuth2ScopeServerException {
if (log.isDebugEnabled()) {
log.debug(String.format("Get requested scopes for scopes: %s for tenantId: %s with includeOIDCScopes: %s", requestedScopes, tenantID, includeOIDCScopes));
}
// Validate requestedScopes.
if (StringUtils.isBlank(requestedScopes)) {
return new HashSet<>();
}
String sql;
if (includeOIDCScopes) {
sql = String.format(SQLQueries.RETRIEVE_REQUESTED_ALL_SCOPES_WITHOUT_SCOPE_TYPE);
} else {
sql = String.format(SQLQueries.RETRIEVE_REQUESTED_OAUTH2_SCOPES);
}
List<String> requestedScopeList = Arrays.asList(requestedScopes.split("\\s+"));
String placeholder = String.join(", ", Collections.nCopies(requestedScopeList.size(), "?"));
sql = sql.replace(SCOPE_LIST_PLACEHOLDER, placeholder);
Set<Scope> scopes = new HashSet<>();
Map<Integer, Scope> scopeMap = new HashMap<>();
try (Connection conn = IdentityDatabaseUtil.getDBConnection(false)) {
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, tenantID);
int scopeIndex = 2;
if (!includeOIDCScopes) {
ps.setString(2, Oauth2ScopeConstants.SCOPE_TYPE_OAUTH2);
scopeIndex++;
}
for (String scope : requestedScopeList) {
ps.setString(scopeIndex, scope);
scopeIndex++;
}
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 requested 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 OAuthScopeDAOImpl method getAllScopes.
/**
* Get all available OAuth2 scopes.
*
* @param tenantID tenant ID
* @return available scope list
* @throws IdentityOAuth2ScopeServerException IdentityOAuth2ScopeServerException
*/
@Override
public Set<Scope> getAllScopes(int tenantID) throws IdentityOAuth2ScopeServerException {
if (log.isDebugEnabled()) {
log.debug("Get all 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_OAUTH2_SCOPES_ORACLE;
} else {
sql = SQLQueries.RETRIEVE_ALL_OAUTH2_SCOPES;
}
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, tenantID);
ps.setString(2, 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);
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 OAUTH2 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 JDBCPermissionBasedInternalScopeValidator method getScopesOfPermissionType.
private Set<Scope> getScopesOfPermissionType(int tenantId) throws IdentityOAuth2ScopeServerException {
if (Oauth2ScopeUtils.isSystemLevelInternalSystemScopeManagementEnabled()) {
List<Scope> oauthScopeBinding = OAuth2ServiceComponentHolder.getInstance().getOauthScopeBinding();
return new HashSet<>(oauthScopeBinding);
}
Scope[] scopesFromCache = OAuthScopeBindingCache.getInstance().getValueFromCache(new OAuthScopeBindingCacheKey(PERMISSION_BINDING_TYPE), tenantId);
Set<Scope> allScopes;
if (scopesFromCache != null) {
allScopes = Arrays.stream(scopesFromCache).collect(Collectors.toSet());
} else {
allScopes = OAuthTokenPersistenceFactory.getInstance().getOAuthScopeDAO().getScopes(tenantId, PERMISSION_BINDING_TYPE);
if (CollectionUtils.isNotEmpty(allScopes)) {
OAuthScopeBindingCache.getInstance().addToCache(new OAuthScopeBindingCacheKey(PERMISSION_BINDING_TYPE), allScopes.toArray(new Scope[0]), tenantId);
}
}
return allScopes;
}
Aggregations