use of org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeClientException in project identity-inbound-auth-oauth by wso2-extensions.
the class ScopesApiServiceImpl method registerScope.
/**
* Register a scope with the bindings.
*
* @param scope details of the scope to be registered
* @return Response with the status of the registration.
*/
@Override
public Response registerScope(ScopeDTO scope) {
Scope registeredScope = null;
try {
validateAddRequest(scope);
registeredScope = ScopeUtils.getOAuth2ScopeService().registerScope(ScopeUtils.getScope(scope));
} catch (IdentityOAuth2ScopeClientException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Client Error while registering scope \n" + scope.toString(), e);
}
if (Oauth2ScopeConstants.ErrorMessages.ERROR_CODE_CONFLICT_REQUEST_EXISTING_SCOPE.getCode().equals(e.getErrorCode())) {
ScopeUtils.handleErrorResponse(Response.Status.CONFLICT, Response.Status.CONFLICT.getReasonPhrase(), e, false, LOG);
} else if (Oauth2ScopeConstants.ErrorMessages.ERROR_CODE_NOT_AUTHORIZED_ADD_INTERNAL_SCOPE.getCode().equals(e.getErrorCode())) {
ScopeUtils.handleErrorResponse(Response.Status.FORBIDDEN, Response.Status.FORBIDDEN.getReasonPhrase(), e, false, LOG);
} else {
ScopeUtils.handleErrorResponse(Response.Status.BAD_REQUEST, Response.Status.BAD_REQUEST.getReasonPhrase(), e, false, LOG);
}
} catch (IdentityOAuth2ScopeException e) {
ScopeUtils.handleErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase(), e, true, LOG);
} catch (Throwable throwable) {
ScopeUtils.handleErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase(), throwable, true, LOG);
}
return Response.status(Response.Status.CREATED).location(buildURIForHeader(scope.getName())).entity(registeredScope).build();
}
use of org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeClientException in project identity-inbound-auth-oauth by wso2-extensions.
the class ScopesApiServiceImplTest method testUpdateScope.
@Test(dataProvider = "BuildUpdateScope")
public void testUpdateScope(Response.Status expectation, Throwable throwable) throws Exception {
ScopeToUpdateDTO scopeToUpdateDTO = new ScopeToUpdateDTO();
scopeToUpdateDTO.setDescription("some description");
scopeToUpdateDTO.setBindings(Collections.<String>emptyList());
if (Response.Status.OK.equals(expectation)) {
when(ScopeUtils.getScopeDTO(any(Scope.class))).thenReturn(any(ScopeDTO.class));
assertEquals(scopesApiService.updateScope(scopeToUpdateDTO, someScopeName).getStatus(), Response.Status.OK.getStatusCode(), "Error occurred while updating scopes");
} else if (Response.Status.BAD_REQUEST.equals(expectation)) {
when(oAuth2ScopeService.updateScope(any(Scope.class))).thenThrow(IdentityOAuth2ScopeClientException.class);
callRealMethod();
try {
scopesApiService.updateScope(scopeToUpdateDTO, someScopeName);
} catch (ScopeEndpointException e) {
assertEquals(e.getResponse().getStatus(), Response.Status.BAD_REQUEST.getStatusCode(), "Cannot find HTTP Response, Bad Request in Case of " + "IdentityOAuth2ScopeClientException");
assertEquals(((ErrorDTO) (e.getResponse().getEntity())).getMessage(), Response.Status.BAD_REQUEST.getReasonPhrase(), "Cannot find appropriate error message " + "for HTTP Response, Bad Request");
} finally {
reset(oAuth2ScopeService);
}
} else if (Response.Status.NOT_FOUND.equals(expectation)) {
((IdentityOAuth2ScopeException) throwable).setErrorCode(Oauth2ScopeConstants.ErrorMessages.ERROR_CODE_NOT_FOUND_SCOPE.getCode());
when(oAuth2ScopeService.updateScope(any(Scope.class))).thenThrow(throwable);
callRealMethod();
try {
scopesApiService.updateScope(scopeToUpdateDTO, someScopeName);
} catch (ScopeEndpointException e) {
assertEquals(e.getResponse().getStatus(), Response.Status.NOT_FOUND.getStatusCode(), "Cannot find HTTP Response, Not Found in Case of " + "IdentityOAuth2ScopeClientException");
assertEquals(((ErrorDTO) (e.getResponse().getEntity())).getMessage(), Response.Status.NOT_FOUND.getReasonPhrase(), "Cannot find appropriate error message " + "for HTTP Response, Not Found");
} finally {
reset(oAuth2ScopeService);
}
} else if (Response.Status.INTERNAL_SERVER_ERROR.equals(expectation)) {
when(oAuth2ScopeService.updateScope(any(Scope.class))).thenThrow(IdentityOAuth2ScopeException.class);
callRealMethod();
try {
scopesApiService.updateScope(scopeToUpdateDTO, someScopeName);
} catch (ScopeEndpointException e) {
assertEquals(e.getResponse().getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Cannot find HTTP Response, Internal Server Error in case of " + "IdentityOAuth2ScopeException");
assertNull(e.getResponse().getEntity(), "Do not include error message in case of " + "Server Exception");
} finally {
reset(oAuth2ScopeService);
}
}
}
use of org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeClientException in project identity-inbound-auth-oauth by wso2-extensions.
the class ScopesApiServiceImplTest method buildUpdateApplication.
@DataProvider(name = "BuildUpdateScope")
public Object[][] buildUpdateApplication() {
IdentityOAuth2ScopeClientException identityOAuth2ScopeClientException = new IdentityOAuth2ScopeClientException("Oauth2 Scope Client Exception");
IdentityOAuth2ScopeException identityOAuth2ScopeException = new IdentityOAuth2ScopeException("Oauth2 Scope " + "Exception");
return new Object[][] { { Response.Status.OK, null }, { Response.Status.BAD_REQUEST, identityOAuth2ScopeClientException }, { Response.Status.NOT_FOUND, identityOAuth2ScopeClientException }, { Response.Status.INTERNAL_SERVER_ERROR, identityOAuth2ScopeException } };
}
use of org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeClientException in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuthScopeDAOImpl method addScopeBinding.
/**
* Add bindings to a scope.
*
* @param scope Scope.
* @param conn Connection.
* @param scopeID Scope ID.
* @throws SQLException
* @throws IdentityOAuth2ScopeClientException
*/
private void addScopeBinding(Scope scope, Connection conn, int scopeID) throws SQLException {
// Adding scope bindings.
try (PreparedStatement ps = conn.prepareStatement(SQLQueries.ADD_SCOPE_BINDING)) {
List<ScopeBinding> scopeBindings = scope.getScopeBindings();
for (ScopeBinding scopeBinding : scopeBindings) {
String bindingType = scopeBinding.getBindingType();
for (String binding : scopeBinding.getBindings()) {
ps.setInt(1, scopeID);
ps.setString(2, binding);
ps.setString(3, bindingType);
ps.addBatch();
}
}
ps.executeBatch();
}
}
use of org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeClientException in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuthScopeDAOImpl method addScope.
/**
* Add an OIDC scope.
*
* @param scope Scope.
* @param conn Databse connection.
* @param tenantID Tenant ID.
* @throws SQLException
* @throws IdentityOAuth2ScopeClientException
*/
private void addScope(Scope scope, Connection conn, int tenantID) throws SQLException {
// Adding the scope
if (scope != null) {
int scopeID = 0;
String dbProductName = conn.getMetaData().getDatabaseProductName();
try (PreparedStatement ps = conn.prepareStatement(SQLQueries.ADD_SCOPE, new String[] { DBUtils.getConvertedAutoGeneratedColumnName(dbProductName, Oauth2ScopeConstants.SCOPE_ID) })) {
ps.setString(1, scope.getName());
ps.setString(2, scope.getDisplayName());
ps.setString(3, scope.getDescription());
ps.setInt(4, tenantID);
ps.setString(5, Oauth2ScopeConstants.SCOPE_TYPE_OAUTH2);
ps.execute();
try (ResultSet rs = ps.getGeneratedKeys()) {
if (rs.next()) {
scopeID = rs.getInt(1);
}
}
}
// some JDBC Drivers returns this in the result, some don't
if (scopeID == 0) {
if (log.isDebugEnabled()) {
log.debug("JDBC Driver did not return the scope id, executing Select operation");
}
try (PreparedStatement ps = conn.prepareStatement(SQLQueries.RETRIEVE_SCOPE_ID_BY_NAME)) {
ps.setString(1, scope.getName());
ps.setInt(2, tenantID);
ps.setString(3, Oauth2ScopeConstants.SCOPE_TYPE_OAUTH2);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
scopeID = rs.getInt(1);
}
}
}
}
addScopeBinding(scope, conn, scopeID);
}
}
Aggregations