Fix test gh-7873

This commit is contained in:
Joe Grandja 2020-02-04 11:58:23 -05:00
parent 04f3fe8af9
commit 25d029b092
5 changed files with 31 additions and 29 deletions

View File

@ -17,12 +17,10 @@ package org.springframework.security.oauth2.client.jackson2;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import java.time.Instant;
@ -46,8 +44,8 @@ abstract class OAuth2AccessTokenMixin {
OAuth2AccessTokenMixin(
@JsonProperty("tokenType") @JsonDeserialize(converter = StdConverters.AccessTokenTypeConverter.class) OAuth2AccessToken.TokenType tokenType,
@JsonProperty("tokenValue") String tokenValue,
@JsonProperty("issuedAt") @JsonFormat(pattern = StdDateFormat.DATE_FORMAT_STR_ISO8601, timezone = "UTC") Instant issuedAt,
@JsonProperty("expiresAt") @JsonFormat(pattern = StdDateFormat.DATE_FORMAT_STR_ISO8601, timezone = "UTC") Instant expiresAt,
@JsonProperty("issuedAt") Instant issuedAt,
@JsonProperty("expiresAt") Instant expiresAt,
@JsonProperty("scopes") Set<String> scopes) {
}
}

View File

@ -17,11 +17,9 @@ package org.springframework.security.oauth2.client.jackson2;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
import java.time.Instant;
@ -43,6 +41,6 @@ abstract class OAuth2RefreshTokenMixin {
@JsonCreator
OAuth2RefreshTokenMixin(
@JsonProperty("tokenValue") String tokenValue,
@JsonProperty("issuedAt") @JsonFormat(pattern = StdDateFormat.DATE_FORMAT_STR_ISO8601, timezone = "UTC") Instant issuedAt) {
@JsonProperty("issuedAt") Instant issuedAt) {
}
}

View File

@ -17,11 +17,9 @@ package org.springframework.security.oauth2.client.jackson2;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import org.springframework.security.oauth2.core.oidc.OidcIdToken;
import java.time.Instant;
@ -44,8 +42,8 @@ abstract class OidcIdTokenMixin {
@JsonCreator
OidcIdTokenMixin(
@JsonProperty("tokenValue") String tokenValue,
@JsonProperty("issuedAt") @JsonFormat(pattern = StdDateFormat.DATE_FORMAT_STR_ISO8601, timezone = "UTC") Instant issuedAt,
@JsonProperty("expiresAt") @JsonFormat(pattern = StdDateFormat.DATE_FORMAT_STR_ISO8601, timezone = "UTC") Instant expiresAt,
@JsonProperty("issuedAt") Instant issuedAt,
@JsonProperty("expiresAt") Instant expiresAt,
@JsonProperty("claims") Map<String, Object> claims) {
}
}

View File

@ -17,7 +17,7 @@ package org.springframework.security.oauth2.client.jackson2;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.DecimalUtils;
import org.junit.Before;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;
@ -37,11 +37,10 @@ import org.springframework.security.oauth2.core.user.OAuth2UserAuthority;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.text.DateFormat;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@ -55,7 +54,6 @@ import static org.springframework.security.oauth2.core.oidc.StandardClaimNames.N
* @author Joe Grandja
*/
public class OAuth2AuthenticationTokenMixinTests {
private static DateFormat dateFormatter;
private ObjectMapper mapper;
@Before
@ -63,8 +61,6 @@ public class OAuth2AuthenticationTokenMixinTests {
ClassLoader loader = getClass().getClassLoader();
this.mapper = new ObjectMapper();
this.mapper.registerModules(SecurityJackson2Modules.getModules(loader));
this.mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
dateFormatter = this.mapper.getDateFormat();
}
@Test
@ -309,17 +305,17 @@ public class OAuth2AuthenticationTokenMixinTests {
return "{\n" +
" \"@class\": \"org.springframework.security.oauth2.core.oidc.OidcIdToken\",\n" +
" \"tokenValue\": \"" + idToken.getTokenValue() + "\",\n" +
" \"issuedAt\": \"" + dateFormatter.format(Date.from(idToken.getIssuedAt())) + "\",\n" +
" \"expiresAt\": \"" + dateFormatter.format(Date.from(idToken.getExpiresAt())) + "\",\n" +
" \"issuedAt\": " + toString(idToken.getIssuedAt()) + ",\n" +
" \"expiresAt\": " + toString(idToken.getExpiresAt()) + ",\n" +
" \"claims\": {\n" +
" \"@class\": \"java.util.Collections$UnmodifiableMap\",\n" +
" \"iat\": [\n" +
" \"java.time.Instant\",\n" +
" \"" + idToken.getIssuedAt().toString() + "\"\n" +
" " + toString(idToken.getIssuedAt()) + "\n" +
" ],\n" +
" \"exp\": [\n" +
" \"java.time.Instant\",\n" +
" \"" + idToken.getExpiresAt().toString() + "\"\n" +
" " + toString(idToken.getExpiresAt()) + "\n" +
" ],\n" +
" \"sub\": \"" + idToken.getSubject() + "\",\n" +
" \"iss\": \"" + idToken.getIssuer() + "\",\n" +
@ -348,4 +344,11 @@ public class OAuth2AuthenticationTokenMixinTests {
" }";
// @formatter:on
}
private static String toString(Instant instant) {
if (instant == null) {
return null;
}
return DecimalUtils.toBigDecimal(instant.getEpochSecond(), instant.getNano()).toString();
}
}

View File

@ -17,6 +17,7 @@ package org.springframework.security.oauth2.client.jackson2;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.DecimalUtils;
import org.junit.Before;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;
@ -31,8 +32,7 @@ import org.springframework.security.oauth2.core.TestOAuth2RefreshTokens;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.text.DateFormat;
import java.util.Date;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;
@ -46,7 +46,6 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
* @author Joe Grandja
*/
public class OAuth2AuthorizedClientMixinTests {
private static DateFormat dateFormatter;
private ObjectMapper mapper;
private ClientRegistration.Builder clientRegistrationBuilder;
private OAuth2AccessToken accessToken;
@ -58,7 +57,6 @@ public class OAuth2AuthorizedClientMixinTests {
ClassLoader loader = getClass().getClassLoader();
this.mapper = new ObjectMapper();
this.mapper.registerModules(SecurityJackson2Modules.getModules(loader));
dateFormatter = this.mapper.getDateFormat();
Map<String, Object> providerConfigurationMetadata = new LinkedHashMap<>();
providerConfigurationMetadata.put("config1", "value1");
providerConfigurationMetadata.put("config2", "value2");
@ -299,8 +297,8 @@ public class OAuth2AuthorizedClientMixinTests {
" \"value\": \"" + accessToken.getTokenType().getValue() + "\"\n" +
" },\n" +
" \"tokenValue\": \"" + accessToken.getTokenValue() + "\",\n" +
" \"issuedAt\": \"" + dateFormatter.format(Date.from(accessToken.getIssuedAt())) + "\",\n" +
" \"expiresAt\": \"" + dateFormatter.format(Date.from(accessToken.getExpiresAt())) + "\",\n" +
" \"issuedAt\": " + toString(accessToken.getIssuedAt()) + ",\n" +
" \"expiresAt\": " + toString(accessToken.getExpiresAt()) + ",\n" +
" \"scopes\": [\n" +
" \"java.util.Collections$UnmodifiableSet\",\n" +
" [" + scopes + "]\n" +
@ -317,9 +315,16 @@ public class OAuth2AuthorizedClientMixinTests {
return "{\n" +
" \"@class\": \"org.springframework.security.oauth2.core.OAuth2RefreshToken\",\n" +
" \"tokenValue\": \"" + refreshToken.getTokenValue() + "\",\n" +
" \"issuedAt\": \"" + dateFormatter.format(Date.from(refreshToken.getIssuedAt())) + "\",\n" +
" \"expiresAt\": " + (refreshToken.getExpiresAt() != null ? "\"" + dateFormatter.format(Date.from(refreshToken.getExpiresAt())) + "\"" : null) + "\n" +
" \"issuedAt\": " + toString(refreshToken.getIssuedAt()) + ",\n" +
" \"expiresAt\": " + toString(refreshToken.getExpiresAt()) + "\n" +
"}";
// @formatter:on
}
private static String toString(Instant instant) {
if (instant == null) {
return null;
}
return DecimalUtils.toBigDecimal(instant.getEpochSecond(), instant.getNano()).toString();
}
}