Skip to content

Jackson deserialization of ClientAuthenticationMethods should recognize all values #16826

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -56,9 +56,21 @@ public ClientAuthenticationMethod convert(JsonNode jsonNode) {
if (ClientAuthenticationMethod.CLIENT_SECRET_POST.getValue().equalsIgnoreCase(value)) {
return ClientAuthenticationMethod.CLIENT_SECRET_POST;
}
if (ClientAuthenticationMethod.CLIENT_SECRET_JWT.getValue().equalsIgnoreCase(value)) {
return ClientAuthenticationMethod.CLIENT_SECRET_JWT;
}
if (ClientAuthenticationMethod.PRIVATE_KEY_JWT.getValue().equalsIgnoreCase(value)) {
return ClientAuthenticationMethod.PRIVATE_KEY_JWT;
}
if (ClientAuthenticationMethod.NONE.getValue().equalsIgnoreCase(value)) {
return ClientAuthenticationMethod.NONE;
}
if (ClientAuthenticationMethod.TLS_CLIENT_AUTH.getValue().equalsIgnoreCase(value)) {
return ClientAuthenticationMethod.TLS_CLIENT_AUTH;
}
if (ClientAuthenticationMethod.SELF_SIGNED_TLS_CLIENT_AUTH.getValue().equalsIgnoreCase(value)) {
return ClientAuthenticationMethod.SELF_SIGNED_TLS_CLIENT_AUTH;
}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.security.oauth2.client.jackson2;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.security.jackson2.SecurityJackson2Modules;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;

import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;

public class ClientRegistrationMixinTests {

private ObjectMapper mapper;

@BeforeEach
void setUp() {
ClassLoader loader = getClass().getClassLoader();
this.mapper = new ObjectMapper();
this.mapper.registerModules(SecurityJackson2Modules.getModules(loader));
}

@ParameterizedTest
@MethodSource("deserializeWhenMixinRegisteredThenDeserializes")
void deserializeWhenMixinRegisteredThenDeserializes(
ClientRegistration expectedClientRegistration
) throws Exception {
String json = asJson(expectedClientRegistration);
ClientRegistration clientRegistration = this.mapper.readValue(json, ClientRegistration.class);
assertThat(clientRegistration.getClientAuthenticationMethod()).isEqualTo(expectedClientRegistration.getClientAuthenticationMethod());
}

private String asJson(ClientRegistration expectedClientRegistration) {
// @formatter:off
return "{" +
" \"@class\":\"org.springframework.security.oauth2.client.registration.ClientRegistration\"," +
" \"registrationId\":\"registration-id\"," +
" \"clientId\":\"client-id\"," +
" \"clientSecret\":\"client-secret\"," +
" \"clientAuthenticationMethod\":{" +
" \"value\":\"" + expectedClientRegistration.getClientAuthenticationMethod().getValue() + "\"" +
" }," +
" \"authorizationGrantType\":{" +
" \"value\":\"" + expectedClientRegistration.getAuthorizationGrantType().getValue() + "\"" +
" }," +
" \"redirectUri\":\"{baseUrl}/{action}/oauth2/code/{registrationId}\"," +
" \"scopes\":[" +
" \"java.util.Collections$UnmodifiableSet\",[\"read:user\"]" +
" ]," +
" \"providerDetails\":{" +
" \"@class\":\"org.springframework.security.oauth2.client.registration.ClientRegistration$ProviderDetails\"," +
" \"authorizationUri\":\"https://example.com/login/oauth/authorize\"," +
" \"tokenUri\": \"https://example.com/login/oauth/access_token\"," +
" \"userInfoEndpoint\":{" +
" \"@class\":\"org.springframework.security.oauth2.client.registration.ClientRegistration$ProviderDetails$UserInfoEndpoint\"," +
" \"uri\":\"https://api.example.com/user\"," +
" \"authenticationMethod\":{" +
" \"value\":\"header\"" +
" }," +
" \"userNameAttributeName\":\"id\"" +
" }," +
" \"jwkSetUri\":\"https://example.com/oauth2/jwk\"," +
" \"issuerUri\":\"https://example.com\"," +
" \"configurationMetadata\":{" +
" \"@class\":\"java.util.Collections$UnmodifiableMap\"" +
" }" +
" }," +
" \"clientName\":\"Client Name\"}";
// @formatter:on
}

static Stream<Arguments> deserializeWhenMixinRegisteredThenDeserializes() {
return Stream.of(
Arguments.of(
TestClientRegistrations.clientRegistration()
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_POST)
.build()
),
Arguments.of(
TestClientRegistrations.clientRegistration()
.clientAuthenticationMethod(ClientAuthenticationMethod.PRIVATE_KEY_JWT)
.build()
),
Arguments.of(
TestClientRegistrations.clientRegistration()
.clientAuthenticationMethod(ClientAuthenticationMethod.NONE)
.build()
)
);
}
}
Loading