Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -16,6 +16,7 @@

package org.springframework.security.jackson;

import java.net.URL;
import java.time.Duration;
import java.time.Instant;

Expand Down Expand Up @@ -73,6 +74,7 @@ protected CoreJacksonModule(String name, Version version) {
public void configurePolymorphicTypeValidator(BasicPolymorphicTypeValidator.Builder builder) {
builder.allowIfSubType(Instant.class)
.allowIfSubType(Duration.class)
.allowIfSubType(URL.class)
.allowIfSubType(SimpleGrantedAuthority.class)
.allowIfSubType(FactorGrantedAuthority.class)
.allowIfSubType(UsernamePasswordAuthenticationToken.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

package org.springframework.security.jackson;

import java.net.URI;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -72,6 +76,18 @@ public void addModulesWithCustomTypeValidator() {
assertThat(deserializedUer).isEqualTo(user);
}

@Test
public void deserializeWhenMapContainsUrlThenDeserializes() throws Exception {
ClassLoader loader = getClass().getClassLoader();
List<JacksonModule> modules = SecurityJacksonModules.getModules(loader);
JsonMapper mapper = JsonMapper.builder().addModules(modules).build();
Map<String, Object> map = new LinkedHashMap<>();
map.put("url", URI.create("https://example.com").toURL());
String json = mapper.writeValueAsString(map);
Map<String, Object> deserialized = mapper.readerFor(Map.class).readValue(json);
assertThat(deserialized.get("url")).isInstanceOf(URL.class).hasToString("https://example.com");
}

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
private static class TestGrantedAuthority implements GrantedAuthority {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@

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

import java.net.URI;
import java.net.URL;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.fasterxml.jackson.datatype.jsr310.DecimalUtils;
Expand Down Expand Up @@ -171,6 +175,27 @@ public void deserializeWhenRequiredAttributesOnlyThenDeserializes() throws Excep
assertThat(principal.getUserInfo()).isNull();
}

@Test
public void deserializeWhenClaimsContainUrlThenDeserializes() throws Exception {
Instant issuedAt = Instant.now();
Instant expiresAt = issuedAt.plusSeconds(3600);
Map<String, Object> claims = new HashMap<>();
claims.put(IdTokenClaimNames.ISS, URI.create("https://example.com/issuer").toURL());
claims.put(IdTokenClaimNames.SUB, "subject");
claims.put(IdTokenClaimNames.IAT, issuedAt);
claims.put(IdTokenClaimNames.EXP, expiresAt);
OidcIdToken idToken = new OidcIdToken("id-token", issuedAt, expiresAt, claims);
Collection<GrantedAuthority> authorities = Collections.singleton(new OidcUserAuthority(idToken));
DefaultOidcUser principal = new DefaultOidcUser(authorities, idToken);
OAuth2AuthenticationToken authentication = new OAuth2AuthenticationToken(principal, authorities,
"registration-id");
String json = this.mapper.writeValueAsString(authentication);
OAuth2AuthenticationToken deserialized = this.mapper.readValue(json, OAuth2AuthenticationToken.class);
DefaultOidcUser deserializedUser = (DefaultOidcUser) deserialized.getPrincipal();
assertThat(deserializedUser.getIdToken().getClaims().get(IdTokenClaimNames.ISS)).isInstanceOf(URL.class)
.hasToString("https://example.com/issuer");
}

private static String asJson(OAuth2AuthenticationToken authentication) {
String principalJson = (authentication.getPrincipal() instanceof DefaultOidcUser)
? asJson((DefaultOidcUser) authentication.getPrincipal())
Expand Down