Skip to content

Commit 3a46ba8

Browse files
committed
OAuth2ErrorHttpMessageConverter handles JSON object parameters
Fixes gh-8157
1 parent a485109 commit 3a46ba8

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.
@@ -35,6 +35,7 @@
3535
import java.nio.charset.StandardCharsets;
3636
import java.util.HashMap;
3737
import java.util.Map;
38+
import java.util.stream.Collectors;
3839

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

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

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

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

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