Browse Source

Add tests to OAuth2AuthorizationServerJackson2ModuleTests

pull/18025/head
Joe Grandja 2 months ago
parent
commit
3656e7ad8c
  1. 88
      oauth2/oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/jackson2/OAuth2AuthorizationServerJackson2ModuleTests.java

88
oauth2/oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/jackson2/OAuth2AuthorizationServerJackson2ModuleTests.java

@ -16,73 +16,101 @@
package org.springframework.security.oauth2.server.authorization.jackson2; package org.springframework.security.oauth2.server.authorization.jackson2;
import java.util.Arrays; import java.security.Principal;
import java.util.Collections; import java.util.List;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.jackson2.SecurityJackson2Modules;
import org.springframework.security.oauth2.jose.jws.MacAlgorithm;
import org.springframework.security.oauth2.jwt.JwtClaimNames;
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
import org.springframework.security.oauth2.server.authorization.TestOAuth2Authorizations;
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2TokenExchangeActor;
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2TokenExchangeCompositeAuthenticationToken;
import org.springframework.security.oauth2.server.authorization.settings.ClientSettings;
import org.springframework.security.oauth2.server.authorization.settings.TokenSettings;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Tests for {@link OAuth2AuthorizationServerJackson2Module}. * Tests for {@link OAuth2AuthorizationServerJackson2Module}.
* *
* @author Steve Riesenberg * @author Steve Riesenberg
* @author Joe Grandja
*/ */
public class OAuth2AuthorizationServerJackson2ModuleTests { public class OAuth2AuthorizationServerJackson2ModuleTests {
private static final TypeReference<Map<String, Object>> STRING_OBJECT_MAP = new TypeReference<>() { private static final TypeReference<Map<String, Object>> STRING_OBJECT_MAP = new TypeReference<>() {
}; };
private static final TypeReference<Set<String>> STRING_SET = new TypeReference<>() {
};
private static final TypeReference<String[]> STRING_ARRAY = new TypeReference<>() {
};
private ObjectMapper objectMapper; private ObjectMapper objectMapper;
@BeforeEach @BeforeEach
public void setup() { public void setup() {
this.objectMapper = new ObjectMapper(); this.objectMapper = new ObjectMapper();
ClassLoader classLoader = OAuth2AuthorizationServerJackson2Module.class.getClassLoader();
List<Module> securityModules = SecurityJackson2Modules.getModules(classLoader);
this.objectMapper.registerModules(securityModules);
this.objectMapper.registerModule(new OAuth2AuthorizationServerJackson2Module()); this.objectMapper.registerModule(new OAuth2AuthorizationServerJackson2Module());
} }
@Test @Test
public void readValueWhenUnmodifiableMapThenSuccess() throws Exception { public void readValueWhenOAuth2AuthorizationAttributesThenSuccess() throws Exception {
Map<String, Object> map = Collections.unmodifiableMap(new HashMap<>(Collections.singletonMap("key", "value"))); Authentication principal = new UsernamePasswordAuthenticationToken("principal", "credentials");
String json = this.objectMapper.writeValueAsString(map); OAuth2Authorization authorization = TestOAuth2Authorizations.authorization()
assertThat(this.objectMapper.readValue(json, STRING_OBJECT_MAP)).isEqualTo(map); .attributes(attrs -> attrs.put(Principal.class.getName(), principal))
.build();
Map<String, Object> attributes = authorization.getAttributes();
String json = this.objectMapper.writeValueAsString(attributes);
assertThat(this.objectMapper.readValue(json, STRING_OBJECT_MAP)).isEqualTo(attributes);
}
@Test
public void readValueWhenOAuth2AccessTokenMetadataThenSuccess() throws Exception {
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization().build();
Map<String, Object> metadata = authorization.getAccessToken().getMetadata();
String json = this.objectMapper.writeValueAsString(metadata);
assertThat(this.objectMapper.readValue(json, STRING_OBJECT_MAP)).isEqualTo(metadata);
} }
@Test @Test
public void readValueWhenHashSetThenSuccess() throws Exception { public void readValueWhenClientSettingsThenSuccess() throws Exception {
Set<String> set = new HashSet<>(Arrays.asList("one", "two")); ClientSettings clientSettings = ClientSettings.builder()
String json = this.objectMapper.writeValueAsString(set); .tokenEndpointAuthenticationSigningAlgorithm(MacAlgorithm.HS256)
assertThat(this.objectMapper.readValue(json, STRING_SET)).isEqualTo(set); .build();
Map<String, Object> clientSettingsMap = clientSettings.getSettings();
String json = this.objectMapper.writeValueAsString(clientSettingsMap);
assertThat(this.objectMapper.readValue(json, STRING_OBJECT_MAP)).isEqualTo(clientSettingsMap);
} }
// gh-457
@Test @Test
public void readValueWhenLinkedHashSetThenSuccess() throws Exception { public void readValueWhenTokenSettingsThenSuccess() throws Exception {
Set<String> set = new LinkedHashSet<>(Arrays.asList("one", "two")); TokenSettings tokenSettings = TokenSettings.builder().build();
String json = this.objectMapper.writeValueAsString(set); Map<String, Object> tokenSettingsMap = tokenSettings.getSettings();
assertThat(this.objectMapper.readValue(json, STRING_SET)).isEqualTo(set); String json = this.objectMapper.writeValueAsString(tokenSettingsMap);
assertThat(this.objectMapper.readValue(json, STRING_OBJECT_MAP)).isEqualTo(tokenSettingsMap);
} }
// gh-1666
@Test @Test
public void readValueWhenStringArrayThenSuccess() throws Exception { public void readValueWhenOAuth2TokenExchangeCompositeAuthenticationTokenThenSuccess() throws Exception {
String[] array = new String[] { "one", "two" }; Authentication subject = new UsernamePasswordAuthenticationToken("principal", "credentials");
String json = this.objectMapper.writeValueAsString(array); OAuth2TokenExchangeActor actor1 = new OAuth2TokenExchangeActor(
assertThat(this.objectMapper.readValue(json, STRING_ARRAY)).isEqualTo(array); Map.of(JwtClaimNames.ISS, "issuer-1", JwtClaimNames.SUB, "actor1"));
OAuth2TokenExchangeActor actor2 = new OAuth2TokenExchangeActor(
Map.of(JwtClaimNames.ISS, "issuer-2", JwtClaimNames.SUB, "actor2"));
OAuth2TokenExchangeCompositeAuthenticationToken authentication = new OAuth2TokenExchangeCompositeAuthenticationToken(
subject, List.of(actor1, actor2));
String json = this.objectMapper.writeValueAsString(authentication);
assertThat(this.objectMapper.readValue(json, OAuth2TokenExchangeCompositeAuthenticationToken.class))
.isEqualTo(authentication);
} }
} }

Loading…
Cancel
Save