From 25d029b0920ff12eb22ae34fa730a7818bcbbcc5 Mon Sep 17 00:00:00 2001 From: Joe Grandja Date: Tue, 4 Feb 2020 11:58:23 -0500 Subject: [PATCH] Fix test gh-7873 --- .../jackson2/OAuth2AccessTokenMixin.java | 6 ++--- .../jackson2/OAuth2RefreshTokenMixin.java | 4 +--- .../client/jackson2/OidcIdTokenMixin.java | 6 ++--- .../OAuth2AuthenticationTokenMixinTests.java | 23 +++++++++++-------- .../OAuth2AuthorizedClientMixinTests.java | 21 ++++++++++------- 5 files changed, 31 insertions(+), 29 deletions(-) diff --git a/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/jackson2/OAuth2AccessTokenMixin.java b/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/jackson2/OAuth2AccessTokenMixin.java index 705ecf376d..f94eb87178 100644 --- a/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/jackson2/OAuth2AccessTokenMixin.java +++ b/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/jackson2/OAuth2AccessTokenMixin.java @@ -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 scopes) { } } diff --git a/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/jackson2/OAuth2RefreshTokenMixin.java b/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/jackson2/OAuth2RefreshTokenMixin.java index 7393d6e8b9..191fb3e2d1 100644 --- a/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/jackson2/OAuth2RefreshTokenMixin.java +++ b/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/jackson2/OAuth2RefreshTokenMixin.java @@ -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) { } } diff --git a/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/jackson2/OidcIdTokenMixin.java b/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/jackson2/OidcIdTokenMixin.java index 32c1d51891..d795e157b7 100644 --- a/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/jackson2/OidcIdTokenMixin.java +++ b/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/jackson2/OidcIdTokenMixin.java @@ -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 claims) { } } diff --git a/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/jackson2/OAuth2AuthenticationTokenMixinTests.java b/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/jackson2/OAuth2AuthenticationTokenMixinTests.java index f0b9793719..8ac3721ecc 100644 --- a/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/jackson2/OAuth2AuthenticationTokenMixinTests.java +++ b/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/jackson2/OAuth2AuthenticationTokenMixinTests.java @@ -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(); + } } diff --git a/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/jackson2/OAuth2AuthorizedClientMixinTests.java b/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/jackson2/OAuth2AuthorizedClientMixinTests.java index 835760f00d..93dfd2dd08 100644 --- a/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/jackson2/OAuth2AuthorizedClientMixinTests.java +++ b/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/jackson2/OAuth2AuthorizedClientMixinTests.java @@ -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 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(); + } }