Skip to content

Commit 0bb8a89

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

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-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,97 @@
1+
package org.springframework.security.oauth2.client.jackson2;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.Arguments;
7+
import org.junit.jupiter.params.provider.MethodSource;
8+
import org.springframework.security.jackson2.SecurityJackson2Modules;
9+
import org.springframework.security.oauth2.client.registration.ClientRegistration;
10+
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
11+
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
12+
13+
import java.util.stream.Stream;
14+
15+
import static org.assertj.core.api.Assertions.assertThat;
16+
17+
public class ClientRegistrationMixinTests {
18+
19+
private ObjectMapper mapper;
20+
21+
@BeforeEach
22+
void setUp() {
23+
ClassLoader loader = getClass().getClassLoader();
24+
this.mapper = new ObjectMapper();
25+
this.mapper.registerModules(SecurityJackson2Modules.getModules(loader));
26+
}
27+
28+
@ParameterizedTest
29+
@MethodSource("deserializeWhenMixinRegisteredThenDeserializes")
30+
void deserializeWhenMixinRegisteredThenDeserializes(
31+
ClientRegistration expectedClientRegistration
32+
) throws Exception {
33+
String json = asJson(expectedClientRegistration);
34+
System.out.println(this.mapper.writeValueAsString(expectedClientRegistration));
35+
ClientRegistration clientRegistration = this.mapper.readValue(json, ClientRegistration.class);
36+
assertThat(clientRegistration.getClientAuthenticationMethod()).isEqualTo(expectedClientRegistration.getClientAuthenticationMethod());
37+
}
38+
39+
private String asJson(ClientRegistration expectedClientRegistration) {
40+
// @formatter:off
41+
return "{" +
42+
" \"@class\":\"org.springframework.security.oauth2.client.registration.ClientRegistration\"," +
43+
" \"registrationId\":\"registration-id\"," +
44+
" \"clientId\":\"client-id\"," +
45+
" \"clientSecret\":\"client-secret\"," +
46+
" \"clientAuthenticationMethod\":{" +
47+
" \"value\":\"" + expectedClientRegistration.getClientAuthenticationMethod().getValue() + "\"" +
48+
" }," +
49+
" \"authorizationGrantType\":{" +
50+
" \"value\":\"" + expectedClientRegistration.getAuthorizationGrantType().getValue() + "\"" +
51+
" }," +
52+
" \"redirectUri\":\"{baseUrl}/{action}/oauth2/code/{registrationId}\"," +
53+
" \"scopes\":[" +
54+
" \"java.util.Collections$UnmodifiableSet\",[\"read:user\"]" +
55+
" ]," +
56+
" \"providerDetails\":{" +
57+
" \"@class\":\"org.springframework.security.oauth2.client.registration.ClientRegistration$ProviderDetails\"," +
58+
" \"authorizationUri\":\"https://example.com/login/oauth/authorize\"," +
59+
" \"tokenUri\": \"https://example.com/login/oauth/access_token\"," +
60+
" \"userInfoEndpoint\":{" +
61+
" \"@class\":\"org.springframework.security.oauth2.client.registration.ClientRegistration$ProviderDetails$UserInfoEndpoint\"," +
62+
" \"uri\":\"https://api.example.com/user\"," +
63+
" \"authenticationMethod\":{" +
64+
" \"value\":\"header\"" +
65+
" }," +
66+
" \"userNameAttributeName\":\"id\"" +
67+
" }," +
68+
" \"jwkSetUri\":\"https://example.com/oauth2/jwk\"," +
69+
" \"issuerUri\":\"https://example.com\"," +
70+
" \"configurationMetadata\":{" +
71+
" \"@class\":\"java.util.Collections$UnmodifiableMap\"" +
72+
" }" +
73+
" }," +
74+
" \"clientName\":\"Client Name\"}";
75+
// @formatter:on
76+
}
77+
78+
static Stream<Arguments> deserializeWhenMixinRegisteredThenDeserializes() {
79+
return Stream.of(
80+
Arguments.of(
81+
TestClientRegistrations.clientRegistration()
82+
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_POST)
83+
.build()
84+
),
85+
Arguments.of(
86+
TestClientRegistrations.clientRegistration()
87+
.clientAuthenticationMethod(ClientAuthenticationMethod.PRIVATE_KEY_JWT)
88+
.build()
89+
),
90+
Arguments.of(
91+
TestClientRegistrations.clientRegistration()
92+
.clientAuthenticationMethod(ClientAuthenticationMethod.NONE)
93+
.build()
94+
)
95+
);
96+
}
97+
}

0 commit comments

Comments
 (0)