Skip to content

Commit 044c30c

Browse files
committed
OAuth2ErrorHttpMessageConverter handles JSON object parameters
Fixes gh-8157
1 parent a1bcd4e commit 044c30c

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/http/converter/OAuth2ErrorHttpMessageConverter.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@
3434
import java.nio.charset.StandardCharsets;
3535
import java.util.HashMap;
3636
import java.util.Map;
37+
import java.util.stream.Collectors;
3738

3839
/**
3940
* A {@link HttpMessageConverter} for an {@link OAuth2Error OAuth 2.0 Error}.
@@ -46,8 +47,8 @@
4647
public class OAuth2ErrorHttpMessageConverter extends AbstractHttpMessageConverter<OAuth2Error> {
4748
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
4849

49-
private static final ParameterizedTypeReference<Map<String, String>> PARAMETERIZED_RESPONSE_TYPE =
50-
new ParameterizedTypeReference<Map<String, String>>() {};
50+
private static final ParameterizedTypeReference<Map<String, Object>> PARAMETERIZED_RESPONSE_TYPE =
51+
new ParameterizedTypeReference<Map<String, Object>>() {};
5152

5253
private GenericHttpMessageConverter<Object> jsonMessageConverter = HttpMessageConverters.getJsonMessageConverter();
5354

@@ -69,10 +70,16 @@ protected OAuth2Error readInternal(Class<? extends OAuth2Error> clazz, HttpInput
6970
throws HttpMessageNotReadableException {
7071

7172
try {
73+
// gh-8157
74+
// Parse parameter values as Object in order to handle potential JSON Object and then convert values to String
7275
@SuppressWarnings("unchecked")
73-
Map<String, String> errorParameters = (Map<String, String>) this.jsonMessageConverter.read(
76+
Map<String, Object> errorParameters = (Map<String, Object>) this.jsonMessageConverter.read(
7477
PARAMETERIZED_RESPONSE_TYPE.getType(), null, inputMessage);
75-
return this.errorConverter.convert(errorParameters);
78+
return this.errorConverter.convert(
79+
errorParameters.entrySet().stream()
80+
.collect(Collectors.toMap(
81+
Map.Entry::getKey,
82+
entry -> String.valueOf(entry.getValue()))));
7683
} catch (Exception ex) {
7784
throw new HttpMessageNotReadableException("An error occurred reading the OAuth 2.0 Error: " +
7885
ex.getMessage(), ex, inputMessage);

oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/http/converter/OAuth2ErrorHttpMessageConverterTests.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -78,6 +78,25 @@ public void readInternalWhenErrorResponseThenReadOAuth2Error() throws Exception
7878
assertThat(oauth2Error.getUri()).isEqualTo("https://tools.ietf.org/html/rfc6749#section-5.2");
7979
}
8080

81+
// gh-8157
82+
@Test
83+
public void readInternalWhenErrorResponseWithObjectThenReadOAuth2Error() throws Exception {
84+
String errorResponse = "{\n" +
85+
" \"error\": \"unauthorized_client\",\n" +
86+
" \"error_description\": \"The client is not authorized\",\n" +
87+
" \"error_codes\": [65001],\n" +
88+
" \"error_uri\": \"https://tools.ietf.org/html/rfc6749#section-5.2\"\n" +
89+
"}\n";
90+
91+
MockClientHttpResponse response = new MockClientHttpResponse(
92+
errorResponse.getBytes(), HttpStatus.BAD_REQUEST);
93+
94+
OAuth2Error oauth2Error = this.messageConverter.readInternal(OAuth2Error.class, response);
95+
assertThat(oauth2Error.getErrorCode()).isEqualTo("unauthorized_client");
96+
assertThat(oauth2Error.getDescription()).isEqualTo("The client is not authorized");
97+
assertThat(oauth2Error.getUri()).isEqualTo("https://tools.ietf.org/html/rfc6749#section-5.2");
98+
}
99+
81100
@Test
82101
public void readInternalWhenConversionFailsThenThrowHttpMessageNotReadableException() {
83102
Converter errorConverter = mock(Converter.class);

0 commit comments

Comments
 (0)