Search in sources :

Example 1 with OAuthError

use of org.orcid.core.oauth.OAuthError in project ORCID-Source by ORCID.

the class OauthGenericCallsController method obtainOauth2TokenPost.

@RequestMapping(value = "/oauth/token", consumes = MediaType.APPLICATION_FORM_URLENCODED, produces = MediaType.APPLICATION_JSON)
public ResponseEntity<?> obtainOauth2TokenPost(HttpServletRequest request) {
    String authorization = request.getHeader("Authorization");
    Enumeration<String> paramNames = request.getParameterNames();
    MultivaluedMap<String, String> formParams = new MultivaluedMapImpl();
    while (paramNames.hasMoreElements()) {
        String paramName = paramNames.nextElement();
        formParams.add(paramName, request.getParameter(paramName));
    }
    try {
        Response response = orcidClientCredentialEndPointDelegator.obtainOauth2Token(authorization, formParams);
        return ResponseEntity.ok(response.getEntity());
    } catch (Exception e) {
        OAuthError error = OAuthErrorUtils.getOAuthError(e);
        HttpStatus status = HttpStatus.valueOf(error.getResponseStatus().getStatusCode());
        return ResponseEntity.status(status).body(error);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) OAuthError(org.orcid.core.oauth.OAuthError) HttpStatus(org.springframework.http.HttpStatus) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) InvalidRequestException(org.springframework.security.oauth2.common.exceptions.InvalidRequestException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with OAuthError

use of org.orcid.core.oauth.OAuthError in project ORCID-Source by ORCID.

the class OauthGenericCallsControllerTest method testObtainOauth2TokenPostLockedClient.

@Test
public void testObtainOauth2TokenPostLockedClient() {
    when(orcidClientCredentialEndPointDelegator.obtainOauth2Token(isNull(), any())).thenThrow(new LockedException("Client is locked"));
    ResponseEntity<?> responseEntity = controller.obtainOauth2TokenPost(new MockHttpServletRequest());
    assertNotNull(responseEntity);
    assertNotNull(responseEntity.getBody());
    assertTrue(responseEntity.getBody() instanceof OAuthError);
    OAuthError error = (OAuthError) responseEntity.getBody();
    assertEquals(OAuthError.UNAUTHORIZED_CLIENT, error.getError());
    assertEquals("Client is locked", error.getErrorDescription());
}
Also used : OAuthError(org.orcid.core.oauth.OAuthError) LockedException(org.orcid.core.security.aop.LockedException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.Test)

Aggregations

OAuthError (org.orcid.core.oauth.OAuthError)2 MultivaluedMapImpl (com.sun.jersey.core.util.MultivaluedMapImpl)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Response (javax.ws.rs.core.Response)1 Test (org.junit.Test)1 LockedException (org.orcid.core.security.aop.LockedException)1 HttpStatus (org.springframework.http.HttpStatus)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 InvalidRequestException (org.springframework.security.oauth2.common.exceptions.InvalidRequestException)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1