Search in sources :

Example 6 with InvalidClientException

use of org.springframework.security.oauth2.common.exceptions.InvalidClientException in project spring-security-oauth by spring-projects.

the class DefaultOAuth2RequestFactory method createTokenRequest.

public TokenRequest createTokenRequest(Map<String, String> requestParameters, ClientDetails authenticatedClient) {
    String clientId = requestParameters.get(OAuth2Utils.CLIENT_ID);
    if (clientId == null) {
        // if the clientId wasn't passed in in the map, we add pull it from the authenticated client object
        clientId = authenticatedClient.getClientId();
    } else {
        // otherwise, make sure that they match
        if (!clientId.equals(authenticatedClient.getClientId())) {
            throw new InvalidClientException("Given client ID does not match authenticated client");
        }
    }
    String grantType = requestParameters.get(OAuth2Utils.GRANT_TYPE);
    Set<String> scopes = extractScopes(requestParameters, clientId);
    TokenRequest tokenRequest = new TokenRequest(requestParameters, clientId, scopes, grantType);
    return tokenRequest;
}
Also used : InvalidClientException(org.springframework.security.oauth2.common.exceptions.InvalidClientException) TokenRequest(org.springframework.security.oauth2.provider.TokenRequest)

Example 7 with InvalidClientException

use of org.springframework.security.oauth2.common.exceptions.InvalidClientException in project spring-security-oauth by spring-projects.

the class AuthorizationCodeAccessTokenProviderWithConversionTests method testGetErrorFromJson.

@Test
public void testGetErrorFromJson() throws Exception {
    final InvalidClientException exception = new InvalidClientException("FOO");
    requestFactory = new ClientHttpRequestFactory() {

        public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
            return new StubClientHttpRequest(HttpStatus.BAD_REQUEST, new ObjectMapper().writeValueAsString(exception));
        }
    };
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    request.setAuthorizationCode("foo");
    request.setPreservedState(new Object());
    resource.setAccessTokenUri("http://localhost/oauth/token");
    expected.expect(OAuth2AccessDeniedException.class);
    expected.expect(hasCause(instanceOf(InvalidClientException.class)));
    setUpRestTemplate();
    provider.obtainAccessToken(resource, request);
}
Also used : ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) InvalidClientException(org.springframework.security.oauth2.common.exceptions.InvalidClientException) IOException(java.io.IOException) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) AccessTokenRequest(org.springframework.security.oauth2.client.token.AccessTokenRequest) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) HttpMethod(org.springframework.http.HttpMethod) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) Test(org.junit.Test)

Example 8 with InvalidClientException

use of org.springframework.security.oauth2.common.exceptions.InvalidClientException in project spring-security-oauth by spring-projects.

the class DefaultOAuth2SerializationServiceTests method testExceptionDeserialization.

@Test
public void testExceptionDeserialization() throws Exception {
    Map<String, String> exception = MapBuilder.create("error", "invalid_client").add("error_description", "FOO").build();
    OAuth2Exception result = OAuth2Exception.valueOf(exception);
    // System.err.println(result);
    assertEquals("FOO", result.getMessage());
    assertEquals("invalid_client", result.getOAuth2ErrorCode());
    assertTrue(result instanceof InvalidClientException);
}
Also used : InvalidClientException(org.springframework.security.oauth2.common.exceptions.InvalidClientException) OAuth2Exception(org.springframework.security.oauth2.common.exceptions.OAuth2Exception) Test(org.junit.Test)

Example 9 with InvalidClientException

use of org.springframework.security.oauth2.common.exceptions.InvalidClientException in project spring-security-oauth by spring-projects.

the class JsonSerializationTests method testExceptionDeserialization.

@Test
public void testExceptionDeserialization() throws Exception {
    String exception = "{\"error\": \"invalid_client\", \"error_description\": \"FOO\", \"foo\": \"bar\"}";
    OAuth2Exception result = new ObjectMapper().readValue(exception, OAuth2Exception.class);
    // System.err.println(result);
    assertEquals("FOO", result.getMessage());
    assertEquals("invalid_client", result.getOAuth2ErrorCode());
    assertEquals("{foo=bar}", result.getAdditionalInformation().toString());
    assertTrue(result instanceof InvalidClientException);
}
Also used : InvalidClientException(org.springframework.security.oauth2.common.exceptions.InvalidClientException) OAuth2Exception(org.springframework.security.oauth2.common.exceptions.OAuth2Exception) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.junit.Test)

Example 10 with InvalidClientException

use of org.springframework.security.oauth2.common.exceptions.InvalidClientException in project spring-security-oauth by spring-projects.

the class WhitelabelErrorEndpointTests method testErrorPage.

@Test
public void testErrorPage() throws Exception {
    request.setContextPath("/foo");
    request.setAttribute("error", new InvalidClientException("FOO"));
    ModelAndView result = endpoint.handleError(request);
    result.getView().render(result.getModel(), request, response);
    String content = response.getContentAsString();
    assertTrue("Wrong content: " + content, content.contains("OAuth Error"));
    assertTrue("Wrong content: " + content, content.contains("invalid_client"));
}
Also used : InvalidClientException(org.springframework.security.oauth2.common.exceptions.InvalidClientException) ModelAndView(org.springframework.web.servlet.ModelAndView) Test(org.junit.Test)

Aggregations

InvalidClientException (org.springframework.security.oauth2.common.exceptions.InvalidClientException)11 Test (org.junit.Test)6 Authentication (org.springframework.security.core.Authentication)4 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)4 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)3 InvalidGrantException (org.springframework.security.oauth2.common.exceptions.InvalidGrantException)3 OAuth2Exception (org.springframework.security.oauth2.common.exceptions.OAuth2Exception)3 RedirectMismatchException (org.springframework.security.oauth2.common.exceptions.RedirectMismatchException)3 InsufficientAuthenticationException (org.springframework.security.authentication.InsufficientAuthenticationException)2 InvalidRequestException (org.springframework.security.oauth2.common.exceptions.InvalidRequestException)2 ClientDetails (org.springframework.security.oauth2.provider.ClientDetails)2 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)2 TokenRequest (org.springframework.security.oauth2.provider.TokenRequest)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 IOException (java.io.IOException)1 URI (java.net.URI)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1