소스 검색

OAuth2AccessTokenResponse.Builder.expiresIn works after withResponse

Closes gh-8702
Benjamin Bargeton 5 년 전
부모
커밋
497ef5e74e

+ 5 - 6
oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AccessTokenResponse.java

@@ -1,5 +1,5 @@
 /*
- * Copyright 2002-2018 the original author or authors.
+ * Copyright 2002-2020 the original author or authors.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -97,20 +97,19 @@ public final class OAuth2AccessTokenResponse {
 	public static class Builder {
 		private String tokenValue;
 		private OAuth2AccessToken.TokenType tokenType;
+		private Instant issuedAt;
+		private Instant expiresAt;
 		private long expiresIn;
 		private Set<String> scopes;
 		private String refreshToken;
 		private Map<String, Object> additionalParameters;
 
-		private Instant issuedAt;
-		private Instant expiresAt;
-
 		private Builder(OAuth2AccessTokenResponse response) {
 			OAuth2AccessToken accessToken = response.getAccessToken();
 			this.tokenValue = accessToken.getTokenValue();
 			this.tokenType = accessToken.getTokenType();
-			this.expiresAt = accessToken.getExpiresAt();
 			this.issuedAt = accessToken.getIssuedAt();
+			this.expiresAt = accessToken.getExpiresAt();
 			this.scopes = accessToken.getScopes();
 			this.refreshToken = response.getRefreshToken() == null ?
 					null : response.getRefreshToken().getTokenValue();
@@ -140,6 +139,7 @@ public final class OAuth2AccessTokenResponse {
 		 */
 		public Builder expiresIn(long expiresIn) {
 			this.expiresIn = expiresIn;
+			this.expiresAt = null;
 			return this;
 		}
 
@@ -183,7 +183,6 @@ public final class OAuth2AccessTokenResponse {
 		 */
 		public OAuth2AccessTokenResponse build() {
 			Instant issuedAt = getIssuedAt();
-
 			Instant expiresAt = getExpiresAt();
 
 			OAuth2AccessTokenResponse accessTokenResponse = new OAuth2AccessTokenResponse();

+ 17 - 1
oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AccessTokenResponseTests.java

@@ -1,5 +1,5 @@
 /*
- * Copyright 2002-2018 the original author or authors.
+ * Copyright 2002-2020 the original author or authors.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -153,4 +153,20 @@ public class OAuth2AccessTokenResponseTests {
 
 		assertThat(withResponse.getRefreshToken()).isNull();
 	}
+
+	@Test
+	public void buildWhenResponseAndExpiresInThenExpiresAtEqualToIssuedAtPlusExpiresIn() {
+		OAuth2AccessTokenResponse tokenResponse = OAuth2AccessTokenResponse
+				.withToken(TOKEN_VALUE)
+				.tokenType(OAuth2AccessToken.TokenType.BEARER)
+				.build();
+
+		long expiresIn = 30;
+		OAuth2AccessTokenResponse withResponse = OAuth2AccessTokenResponse.withResponse(tokenResponse)
+				.expiresIn(expiresIn)
+				.build();
+
+		assertThat(withResponse.getAccessToken().getExpiresAt()).isEqualTo(
+				withResponse.getAccessToken().getIssuedAt().plusSeconds(expiresIn));
+	}
 }