Example usage for org.springframework.security.oauth2.common.exceptions OAuth2Exception getOAuth2ErrorCode

List of usage examples for org.springframework.security.oauth2.common.exceptions OAuth2Exception getOAuth2ErrorCode

Introduction

In this page you can find the example usage for org.springframework.security.oauth2.common.exceptions OAuth2Exception getOAuth2ErrorCode.

Prototype

public String getOAuth2ErrorCode() 

Source Link

Document

The OAuth2 error code.

Usage

From source file:org.saiku.web.AuthTest.java

/**
 * tests that an error occurs if you attempt to use username/password creds for a non-password grant type.
 *//*from w w w. j a v  a2 s .co m*/
@Test
public void testInvalidGrantType() throws Exception {
    int port = 9999;
    Client client = Client.create();
    client.setFollowRedirects(false);

    MultivaluedMap<String, String> formData = new MultivaluedMapImpl();
    formData.add("grant_type", "authorization_code");
    formData.add("client_id", "my-trusted-client");
    formData.add("username", "marissa");
    formData.add("password", "koala");
    ClientResponse response = client.resource("http://localhost:" + port + "/saiku/oauth/authorize")
            .type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).post(ClientResponse.class, formData);
    assertEquals(400, response.getClientResponseStatus().getStatusCode());
    List<NewCookie> newCookies = response.getCookies();
    if (!newCookies.isEmpty()) {
        fail("No cookies should be set. Found: " + newCookies.get(0).getName() + ".");
    }
    assertEquals("no-store", response.getHeaders().getFirst("Cache-Control"));

    DefaultOAuth2SerializationService serializationService = new DefaultOAuth2SerializationService();
    try {
        throw serializationService.deserializeJsonError(response.getEntityInputStream());
    } catch (OAuth2Exception e) {
        assertEquals("invalid_request", e.getOAuth2ErrorCode());
    }
}

From source file:org.springframework.security.oauth2.common.exceptions.OAuth2ExceptionJackson2Serializer.java

@Override
public void serialize(OAuth2Exception value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonProcessingException {
    jgen.writeStartObject();//  w  ww  .  j  ava2s .  co  m
    jgen.writeStringField("error", value.getOAuth2ErrorCode());
    jgen.writeStringField("error_description", value.getMessage());
    if (value.getAdditionalInformation() != null) {
        for (Entry<String, String> entry : value.getAdditionalInformation().entrySet()) {
            String key = entry.getKey();
            String add = entry.getValue();
            jgen.writeStringField(key, add);
        }
    }
    jgen.writeEndObject();
}

From source file:org.cloudfoundry.identity.uaa.integration.NativeApplicationIntegrationTests.java

/**
 * tests that an error occurs if you attempt to use bad client credentials.
 *//*ww w .  j a  v  a  2s  .co  m*/
@Test
// Need a custom auth entry point to get the correct JSON response here.
public void testInvalidClient() throws Exception {

    MultiValueMap<String, String> formData = new LinkedMultiValueMap<String, String>();
    formData.add("grant_type", "password");
    formData.add("username", resource.getUsername());
    formData.add("password", resource.getPassword());
    formData.add("scope", "cloud_controller.read");
    HttpHeaders headers = new HttpHeaders();
    headers.set("Authorization", "Basic " + new String(Base64.encode("no-such-client:".getBytes("UTF-8"))));
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> response = serverRunning.postForMap("/oauth/token", formData, headers);
    assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
    List<String> newCookies = response.getHeaders().get("Set-Cookie");
    if (newCookies != null && !newCookies.isEmpty()) {
        fail("No cookies should be set. Found: " + newCookies.get(0) + ".");
    }
    assertEquals("no-store", response.getHeaders().getFirst("Cache-Control"));

    @SuppressWarnings("unchecked")
    OAuth2Exception error = OAuth2Exception.valueOf(response.getBody());
    assertEquals("invalid_client", error.getOAuth2ErrorCode());
}

From source file:org.joyrest.oauth2.endpoint.AuthorizationEndpoint.java

private String getUnsuccessfulRedirect(AuthorizationRequest authorizationRequest, OAuth2Exception failure,
        boolean fragment) {
    if (isNull(authorizationRequest) || isNull(authorizationRequest.getRedirectUri())) {
        // we have no redirect for the user. very sad.
        throw new UnapprovedClientAuthenticationException("Authorization failure, and no redirect URI.",
                failure);//from   w ww .ja va2s  .  c  o m
    }

    Map<String, String> query = new LinkedHashMap<>();

    query.put("error", failure.getOAuth2ErrorCode());
    query.put("error_description", failure.getMessage());

    if (nonNull(authorizationRequest.getState())) {
        query.put("state", authorizationRequest.getState());
    }

    if (nonNull(failure.getAdditionalInformation())) {
        for (Map.Entry<String, String> additionalInfo : failure.getAdditionalInformation().entrySet()) {
            query.put(additionalInfo.getKey(), additionalInfo.getValue());
        }
    }

    return append(authorizationRequest.getRedirectUri(), query, fragment);
}

From source file:org.cloudfoundry.identity.uaa.oauth.UaaAuthorizationEndpoint.java

private String getUnsuccessfulRedirect(AuthorizationRequest authorizationRequest, OAuth2Exception failure,
        boolean fragment) {

    if (authorizationRequest == null || authorizationRequest.getRedirectUri() == null) {
        // we have no redirect for the user. very sad.
        throw new UnapprovedClientAuthenticationException("Authorization failure, and no redirect URI.",
                failure);//from   w ww. ja  v  a 2s  .  c  o m
    }

    UriComponentsBuilder template = UriComponentsBuilder.fromUriString(authorizationRequest.getRedirectUri());
    StringBuilder values = new StringBuilder();

    values.append("error=" + encode(failure.getOAuth2ErrorCode()));
    values.append("&error_description=" + encode(failure.getMessage()));

    if (authorizationRequest.getState() != null) {
        values.append("&state=" + encode(authorizationRequest.getState()));
    }

    if (failure.getAdditionalInformation() != null) {
        for (Map.Entry<String, String> additionalInfo : failure.getAdditionalInformation().entrySet()) {
            values.append("&" + encode(additionalInfo.getKey()) + "=" + encode(additionalInfo.getValue()));
        }
    }

    if (fragment) {
        template.fragment(values.toString());
    } else {
        template.query(values.toString());
    }

    return template.build(true).toUriString();

}

From source file:org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint.java

private String getUnsuccessfulRedirect(AuthorizationRequest authorizationRequest, OAuth2Exception failure,
        boolean fragment) {

    if (authorizationRequest == null || authorizationRequest.getRedirectUri() == null) {
        // we have no redirect for the user. very sad.
        throw new UnapprovedClientAuthenticationException("Authorization failure, and no redirect URI.",
                failure);/*w w  w. jav  a 2  s.  c o  m*/
    }

    Map<String, String> query = new LinkedHashMap<String, String>();

    query.put("error", failure.getOAuth2ErrorCode());
    query.put("error_description", failure.getMessage());

    if (authorizationRequest.getState() != null) {
        query.put("state", authorizationRequest.getState());
    }

    if (failure.getAdditionalInformation() != null) {
        for (Map.Entry<String, String> additionalInfo : failure.getAdditionalInformation().entrySet()) {
            query.put(additionalInfo.getKey(), additionalInfo.getValue());
        }
    }

    return append(authorizationRequest.getRedirectUri(), query, fragment);

}