|
| 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