Search in sources :

Example 1 with IdentityOAuth2ScopeClientException

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();
}
Also used : Scope(org.wso2.carbon.identity.oauth2.bean.Scope) IdentityOAuth2ScopeClientException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeClientException) IdentityOAuth2ScopeException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeException)

Example 2 with IdentityOAuth2ScopeClientException

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);
        }
    }
}
Also used : ScopeEndpointException(org.wso2.carbon.identity.oauth.scope.endpoint.exceptions.ScopeEndpointException) Scope(org.wso2.carbon.identity.oauth2.bean.Scope) ScopeDTO(org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeDTO) ErrorDTO(org.wso2.carbon.identity.oauth.scope.endpoint.dto.ErrorDTO) ScopeToUpdateDTO(org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeToUpdateDTO) IdentityOAuth2ScopeClientException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeClientException) IdentityOAuth2ScopeException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeException) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 3 with IdentityOAuth2ScopeClientException

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 } };
}
Also used : IdentityOAuth2ScopeClientException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeClientException) IdentityOAuth2ScopeException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeException) DataProvider(org.testng.annotations.DataProvider)

Example 4 with IdentityOAuth2ScopeClientException

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();
    }
}
Also used : NamedPreparedStatement(org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement) PreparedStatement(java.sql.PreparedStatement) ScopeBinding(org.wso2.carbon.identity.oauth2.bean.ScopeBinding)

Example 5 with IdentityOAuth2ScopeClientException

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);
    }
}
Also used : ResultSet(java.sql.ResultSet) NamedPreparedStatement(org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement) PreparedStatement(java.sql.PreparedStatement)

Aggregations

IdentityOAuth2ScopeException (org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeException)9 IdentityOAuth2ScopeClientException (org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeClientException)8 DataProvider (org.testng.annotations.DataProvider)5 Scope (org.wso2.carbon.identity.oauth2.bean.Scope)4 Test (org.testng.annotations.Test)3 ScopeDTO (org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeDTO)3 PreparedStatement (java.sql.PreparedStatement)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 ErrorDTO (org.wso2.carbon.identity.oauth.scope.endpoint.dto.ErrorDTO)2 ScopeEndpointException (org.wso2.carbon.identity.oauth.scope.endpoint.exceptions.ScopeEndpointException)2 NamedPreparedStatement (org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement)2 PowerMockIdentityBaseTest (org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)2 ResultSet (java.sql.ResultSet)1 ScopeToUpdateDTO (org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeToUpdateDTO)1 ScopeBinding (org.wso2.carbon.identity.oauth2.bean.ScopeBinding)1