Skip to content

Commit a75c0c6

Browse files
committed
Add missing ClientAuthenticationMethods to jackson2 converter
Closes gh-16825 Signed-off-by: Risto Virtanen <[email protected]>
1 parent 1f3dd53 commit a75c0c6

File tree

2 files changed

+125
-1
lines changed

2 files changed

+125
-1
lines changed

Diff for: oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/jackson2/StdConverters.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -56,9 +56,21 @@ public ClientAuthenticationMethod convert(JsonNode jsonNode) {
5656
if (ClientAuthenticationMethod.CLIENT_SECRET_POST.getValue().equalsIgnoreCase(value)) {
5757
return ClientAuthenticationMethod.CLIENT_SECRET_POST;
5858
}
59+
if (ClientAuthenticationMethod.CLIENT_SECRET_JWT.getValue().equalsIgnoreCase(value)) {
60+
return ClientAuthenticationMethod.CLIENT_SECRET_JWT;
61+
}
62+
if (ClientAuthenticationMethod.PRIVATE_KEY_JWT.getValue().equalsIgnoreCase(value)) {
63+
return ClientAuthenticationMethod.PRIVATE_KEY_JWT;
64+
}
5965
if (ClientAuthenticationMethod.NONE.getValue().equalsIgnoreCase(value)) {
6066
return ClientAuthenticationMethod.NONE;
6167
}
68+
if (ClientAuthenticationMethod.TLS_CLIENT_AUTH.getValue().equalsIgnoreCase(value)) {
69+
return ClientAuthenticationMethod.TLS_CLIENT_AUTH;
70+
}
71+
if (ClientAuthenticationMethod.SELF_SIGNED_TLS_CLIENT_AUTH.getValue().equalsIgnoreCase(value)) {
72+
return ClientAuthenticationMethod.SELF_SIGNED_TLS_CLIENT_AUTH;
73+
}
6274
return null;
6375
}
6476

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright 2002-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.oauth2.client.jackson2;
18+
19+
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.Arguments;
23+
import org.junit.jupiter.params.provider.MethodSource;
24+
import org.springframework.security.jackson2.SecurityJackson2Modules;
25+
import org.springframework.security.oauth2.client.registration.ClientRegistration;
26+
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
27+
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
28+
29+
import java.util.stream.Stream;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
public class ClientRegistrationMixinTests {
34+
35+
private ObjectMapper mapper;
36+
37+
@BeforeEach
38+
void setUp() {
39+
ClassLoader loader = getClass().getClassLoader();
40+
this.mapper = new ObjectMapper();
41+
this.mapper.registerModules(SecurityJackson2Modules.getModules(loader));
42+
}
43+
44+
@ParameterizedTest
45+
@MethodSource("deserializeWhenMixinRegisteredThenDeserializes")
46+
void deserializeWhenMixinRegisteredThenDeserializes(
47+
ClientRegistration expectedClientRegistration
48+
) throws Exception {
49+
String json = asJson(expectedClientRegistration);
50+
ClientRegistration clientRegistration = this.mapper.readValue(json, ClientRegistration.class);
51+
assertThat(clientRegistration.getClientAuthenticationMethod()).isEqualTo(expectedClientRegistration.getClientAuthenticationMethod());
52+
}
53+
54+
private String asJson(ClientRegistration expectedClientRegistration) {
55+
// @formatter:off
56+
return "{" +
57+
" \"@class\":\"org.springframework.security.oauth2.client.registration.ClientRegistration\"," +
58+
" \"registrationId\":\"registration-id\"," +
59+
" \"clientId\":\"client-id\"," +
60+
" \"clientSecret\":\"client-secret\"," +
61+
" \"clientAuthenticationMethod\":{" +
62+
" \"value\":\"" + expectedClientRegistration.getClientAuthenticationMethod().getValue() + "\"" +
63+
" }," +
64+
" \"authorizationGrantType\":{" +
65+
" \"value\":\"" + expectedClientRegistration.getAuthorizationGrantType().getValue() + "\"" +
66+
" }," +
67+
" \"redirectUri\":\"{baseUrl}/{action}/oauth2/code/{registrationId}\"," +
68+
" \"scopes\":[" +
69+
" \"java.util.Collections$UnmodifiableSet\",[\"read:user\"]" +
70+
" ]," +
71+
" \"providerDetails\":{" +
72+
" \"@class\":\"org.springframework.security.oauth2.client.registration.ClientRegistration$ProviderDetails\"," +
73+
" \"authorizationUri\":\"https://example.com/login/oauth/authorize\"," +
74+
" \"tokenUri\": \"https://example.com/login/oauth/access_token\"," +
75+
" \"userInfoEndpoint\":{" +
76+
" \"@class\":\"org.springframework.security.oauth2.client.registration.ClientRegistration$ProviderDetails$UserInfoEndpoint\"," +
77+
" \"uri\":\"https://api.example.com/user\"," +
78+
" \"authenticationMethod\":{" +
79+
" \"value\":\"header\"" +
80+
" }," +
81+
" \"userNameAttributeName\":\"id\"" +
82+
" }," +
83+
" \"jwkSetUri\":\"https://example.com/oauth2/jwk\"," +
84+
" \"issuerUri\":\"https://example.com\"," +
85+
" \"configurationMetadata\":{" +
86+
" \"@class\":\"java.util.Collections$UnmodifiableMap\"" +
87+
" }" +
88+
" }," +
89+
" \"clientName\":\"Client Name\"}";
90+
// @formatter:on
91+
}
92+
93+
static Stream<Arguments> deserializeWhenMixinRegisteredThenDeserializes() {
94+
return Stream.of(
95+
Arguments.of(
96+
TestClientRegistrations.clientRegistration()
97+
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_POST)
98+
.build()
99+
),
100+
Arguments.of(
101+
TestClientRegistrations.clientRegistration()
102+
.clientAuthenticationMethod(ClientAuthenticationMethod.PRIVATE_KEY_JWT)
103+
.build()
104+
),
105+
Arguments.of(
106+
TestClientRegistrations.clientRegistration()
107+
.clientAuthenticationMethod(ClientAuthenticationMethod.NONE)
108+
.build()
109+
)
110+
);
111+
}
112+
}

0 commit comments

Comments
 (0)