|
16 | 16 |
|
17 | 17 | package org.springframework.security.oauth2.server.authorization.jackson2; |
18 | 18 |
|
19 | | -import java.util.Arrays; |
20 | | -import java.util.Collections; |
21 | | -import java.util.HashMap; |
22 | | -import java.util.HashSet; |
23 | | -import java.util.LinkedHashSet; |
| 19 | +import java.security.Principal; |
| 20 | +import java.util.List; |
24 | 21 | import java.util.Map; |
25 | | -import java.util.Set; |
26 | 22 |
|
27 | 23 | import com.fasterxml.jackson.core.type.TypeReference; |
| 24 | +import com.fasterxml.jackson.databind.Module; |
28 | 25 | import com.fasterxml.jackson.databind.ObjectMapper; |
29 | 26 | import org.junit.jupiter.api.BeforeEach; |
30 | 27 | import org.junit.jupiter.api.Test; |
31 | 28 |
|
| 29 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 30 | +import org.springframework.security.core.Authentication; |
| 31 | +import org.springframework.security.jackson2.SecurityJackson2Modules; |
| 32 | +import org.springframework.security.oauth2.jose.jws.MacAlgorithm; |
| 33 | +import org.springframework.security.oauth2.jwt.JwtClaimNames; |
| 34 | +import org.springframework.security.oauth2.server.authorization.OAuth2Authorization; |
| 35 | +import org.springframework.security.oauth2.server.authorization.TestOAuth2Authorizations; |
| 36 | +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2TokenExchangeActor; |
| 37 | +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2TokenExchangeCompositeAuthenticationToken; |
| 38 | +import org.springframework.security.oauth2.server.authorization.settings.ClientSettings; |
| 39 | +import org.springframework.security.oauth2.server.authorization.settings.TokenSettings; |
| 40 | + |
32 | 41 | import static org.assertj.core.api.Assertions.assertThat; |
33 | 42 |
|
34 | 43 | /** |
35 | 44 | * Tests for {@link OAuth2AuthorizationServerJackson2Module}. |
36 | 45 | * |
37 | 46 | * @author Steve Riesenberg |
| 47 | + * @author Joe Grandja |
38 | 48 | */ |
39 | 49 | public class OAuth2AuthorizationServerJackson2ModuleTests { |
40 | 50 |
|
41 | 51 | private static final TypeReference<Map<String, Object>> STRING_OBJECT_MAP = new TypeReference<>() { |
42 | 52 | }; |
43 | 53 |
|
44 | | - private static final TypeReference<Set<String>> STRING_SET = new TypeReference<>() { |
45 | | - }; |
46 | | - |
47 | | - private static final TypeReference<String[]> STRING_ARRAY = new TypeReference<>() { |
48 | | - }; |
49 | | - |
50 | 54 | private ObjectMapper objectMapper; |
51 | 55 |
|
52 | 56 | @BeforeEach |
53 | 57 | public void setup() { |
54 | 58 | this.objectMapper = new ObjectMapper(); |
| 59 | + ClassLoader classLoader = OAuth2AuthorizationServerJackson2Module.class.getClassLoader(); |
| 60 | + List<Module> securityModules = SecurityJackson2Modules.getModules(classLoader); |
| 61 | + this.objectMapper.registerModules(securityModules); |
55 | 62 | this.objectMapper.registerModule(new OAuth2AuthorizationServerJackson2Module()); |
56 | 63 | } |
57 | 64 |
|
58 | 65 | @Test |
59 | | - public void readValueWhenUnmodifiableMapThenSuccess() throws Exception { |
60 | | - Map<String, Object> map = Collections.unmodifiableMap(new HashMap<>(Collections.singletonMap("key", "value"))); |
61 | | - String json = this.objectMapper.writeValueAsString(map); |
62 | | - assertThat(this.objectMapper.readValue(json, STRING_OBJECT_MAP)).isEqualTo(map); |
| 66 | + public void readValueWhenOAuth2AuthorizationAttributesThenSuccess() throws Exception { |
| 67 | + Authentication principal = new UsernamePasswordAuthenticationToken("principal", "credentials"); |
| 68 | + OAuth2Authorization authorization = TestOAuth2Authorizations.authorization() |
| 69 | + .attributes(attrs -> attrs.put(Principal.class.getName(), principal)) |
| 70 | + .build(); |
| 71 | + Map<String, Object> attributes = authorization.getAttributes(); |
| 72 | + String json = this.objectMapper.writeValueAsString(attributes); |
| 73 | + assertThat(this.objectMapper.readValue(json, STRING_OBJECT_MAP)).isEqualTo(attributes); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void readValueWhenOAuth2AccessTokenMetadataThenSuccess() throws Exception { |
| 78 | + OAuth2Authorization authorization = TestOAuth2Authorizations.authorization().build(); |
| 79 | + Map<String, Object> metadata = authorization.getAccessToken().getMetadata(); |
| 80 | + String json = this.objectMapper.writeValueAsString(metadata); |
| 81 | + assertThat(this.objectMapper.readValue(json, STRING_OBJECT_MAP)).isEqualTo(metadata); |
63 | 82 | } |
64 | 83 |
|
65 | 84 | @Test |
66 | | - public void readValueWhenHashSetThenSuccess() throws Exception { |
67 | | - Set<String> set = new HashSet<>(Arrays.asList("one", "two")); |
68 | | - String json = this.objectMapper.writeValueAsString(set); |
69 | | - assertThat(this.objectMapper.readValue(json, STRING_SET)).isEqualTo(set); |
| 85 | + public void readValueWhenClientSettingsThenSuccess() throws Exception { |
| 86 | + ClientSettings clientSettings = ClientSettings.builder() |
| 87 | + .tokenEndpointAuthenticationSigningAlgorithm(MacAlgorithm.HS256) |
| 88 | + .build(); |
| 89 | + Map<String, Object> clientSettingsMap = clientSettings.getSettings(); |
| 90 | + String json = this.objectMapper.writeValueAsString(clientSettingsMap); |
| 91 | + assertThat(this.objectMapper.readValue(json, STRING_OBJECT_MAP)).isEqualTo(clientSettingsMap); |
70 | 92 | } |
71 | 93 |
|
72 | | - // gh-457 |
73 | 94 | @Test |
74 | | - public void readValueWhenLinkedHashSetThenSuccess() throws Exception { |
75 | | - Set<String> set = new LinkedHashSet<>(Arrays.asList("one", "two")); |
76 | | - String json = this.objectMapper.writeValueAsString(set); |
77 | | - assertThat(this.objectMapper.readValue(json, STRING_SET)).isEqualTo(set); |
| 95 | + public void readValueWhenTokenSettingsThenSuccess() throws Exception { |
| 96 | + TokenSettings tokenSettings = TokenSettings.builder().build(); |
| 97 | + Map<String, Object> tokenSettingsMap = tokenSettings.getSettings(); |
| 98 | + String json = this.objectMapper.writeValueAsString(tokenSettingsMap); |
| 99 | + assertThat(this.objectMapper.readValue(json, STRING_OBJECT_MAP)).isEqualTo(tokenSettingsMap); |
78 | 100 | } |
79 | 101 |
|
80 | | - // gh-1666 |
81 | 102 | @Test |
82 | | - public void readValueWhenStringArrayThenSuccess() throws Exception { |
83 | | - String[] array = new String[] { "one", "two" }; |
84 | | - String json = this.objectMapper.writeValueAsString(array); |
85 | | - assertThat(this.objectMapper.readValue(json, STRING_ARRAY)).isEqualTo(array); |
| 103 | + public void readValueWhenOAuth2TokenExchangeCompositeAuthenticationTokenThenSuccess() throws Exception { |
| 104 | + Authentication subject = new UsernamePasswordAuthenticationToken("principal", "credentials"); |
| 105 | + OAuth2TokenExchangeActor actor1 = new OAuth2TokenExchangeActor( |
| 106 | + Map.of(JwtClaimNames.ISS, "issuer-1", JwtClaimNames.SUB, "actor1")); |
| 107 | + OAuth2TokenExchangeActor actor2 = new OAuth2TokenExchangeActor( |
| 108 | + Map.of(JwtClaimNames.ISS, "issuer-2", JwtClaimNames.SUB, "actor2")); |
| 109 | + OAuth2TokenExchangeCompositeAuthenticationToken authentication = new OAuth2TokenExchangeCompositeAuthenticationToken( |
| 110 | + subject, List.of(actor1, actor2)); |
| 111 | + String json = this.objectMapper.writeValueAsString(authentication); |
| 112 | + assertThat(this.objectMapper.readValue(json, OAuth2TokenExchangeCompositeAuthenticationToken.class)) |
| 113 | + .isEqualTo(authentication); |
86 | 114 | } |
87 | 115 |
|
88 | 116 | } |
0 commit comments