Skip to content

Commit 42fc2e7

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

File tree

2 files changed

+126
-1
lines changed

2 files changed

+126
-1
lines changed

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,113 @@
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+
System.out.println(this.mapper.writeValueAsString(expectedClientRegistration));
51+
ClientRegistration clientRegistration = this.mapper.readValue(json, ClientRegistration.class);
52+
assertThat(clientRegistration.getClientAuthenticationMethod()).isEqualTo(expectedClientRegistration.getClientAuthenticationMethod());
53+
}
54+
55+
private String asJson(ClientRegistration expectedClientRegistration) {
56+
// @formatter:off
57+
return "{" +
58+
" \"@class\":\"org.springframework.security.oauth2.client.registration.ClientRegistration\"," +
59+
" \"registrationId\":\"registration-id\"," +
60+
" \"clientId\":\"client-id\"," +
61+
" \"clientSecret\":\"client-secret\"," +
62+
" \"clientAuthenticationMethod\":{" +
63+
" \"value\":\"" + expectedClientRegistration.getClientAuthenticationMethod().getValue() + "\"" +
64+
" }," +
65+
" \"authorizationGrantType\":{" +
66+
" \"value\":\"" + expectedClientRegistration.getAuthorizationGrantType().getValue() + "\"" +
67+
" }," +
68+
" \"redirectUri\":\"{baseUrl}/{action}/oauth2/code/{registrationId}\"," +
69+
" \"scopes\":[" +
70+
" \"java.util.Collections$UnmodifiableSet\",[\"read:user\"]" +
71+
" ]," +
72+
" \"providerDetails\":{" +
73+
" \"@class\":\"org.springframework.security.oauth2.client.registration.ClientRegistration$ProviderDetails\"," +
74+
" \"authorizationUri\":\"https://example.com/login/oauth/authorize\"," +
75+
" \"tokenUri\": \"https://example.com/login/oauth/access_token\"," +
76+
" \"userInfoEndpoint\":{" +
77+
" \"@class\":\"org.springframework.security.oauth2.client.registration.ClientRegistration$ProviderDetails$UserInfoEndpoint\"," +
78+
" \"uri\":\"https://api.example.com/user\"," +
79+
" \"authenticationMethod\":{" +
80+
" \"value\":\"header\"" +
81+
" }," +
82+
" \"userNameAttributeName\":\"id\"" +
83+
" }," +
84+
" \"jwkSetUri\":\"https://example.com/oauth2/jwk\"," +
85+
" \"issuerUri\":\"https://example.com\"," +
86+
" \"configurationMetadata\":{" +
87+
" \"@class\":\"java.util.Collections$UnmodifiableMap\"" +
88+
" }" +
89+
" }," +
90+
" \"clientName\":\"Client Name\"}";
91+
// @formatter:on
92+
}
93+
94+
static Stream<Arguments> deserializeWhenMixinRegisteredThenDeserializes() {
95+
return Stream.of(
96+
Arguments.of(
97+
TestClientRegistrations.clientRegistration()
98+
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_POST)
99+
.build()
100+
),
101+
Arguments.of(
102+
TestClientRegistrations.clientRegistration()
103+
.clientAuthenticationMethod(ClientAuthenticationMethod.PRIVATE_KEY_JWT)
104+
.build()
105+
),
106+
Arguments.of(
107+
TestClientRegistrations.clientRegistration()
108+
.clientAuthenticationMethod(ClientAuthenticationMethod.NONE)
109+
.build()
110+
)
111+
);
112+
}
113+
}

0 commit comments

Comments
 (0)