Skip to content

Commit 497ef5e

Browse files
benbajgrandja
authored andcommitted
OAuth2AccessTokenResponse.Builder.expiresIn works after withResponse
Closes gh-8702
1 parent edf06a3 commit 497ef5e

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -97,20 +97,19 @@ public static Builder withResponse(OAuth2AccessTokenResponse response) {
9797
public static class Builder {
9898
private String tokenValue;
9999
private OAuth2AccessToken.TokenType tokenType;
100+
private Instant issuedAt;
101+
private Instant expiresAt;
100102
private long expiresIn;
101103
private Set<String> scopes;
102104
private String refreshToken;
103105
private Map<String, Object> additionalParameters;
104106

105-
private Instant issuedAt;
106-
private Instant expiresAt;
107-
108107
private Builder(OAuth2AccessTokenResponse response) {
109108
OAuth2AccessToken accessToken = response.getAccessToken();
110109
this.tokenValue = accessToken.getTokenValue();
111110
this.tokenType = accessToken.getTokenType();
112-
this.expiresAt = accessToken.getExpiresAt();
113111
this.issuedAt = accessToken.getIssuedAt();
112+
this.expiresAt = accessToken.getExpiresAt();
114113
this.scopes = accessToken.getScopes();
115114
this.refreshToken = response.getRefreshToken() == null ?
116115
null : response.getRefreshToken().getTokenValue();
@@ -140,6 +139,7 @@ public Builder tokenType(OAuth2AccessToken.TokenType tokenType) {
140139
*/
141140
public Builder expiresIn(long expiresIn) {
142141
this.expiresIn = expiresIn;
142+
this.expiresAt = null;
143143
return this;
144144
}
145145

@@ -183,7 +183,6 @@ public Builder additionalParameters(Map<String, Object> additionalParameters) {
183183
*/
184184
public OAuth2AccessTokenResponse build() {
185185
Instant issuedAt = getIssuedAt();
186-
187186
Instant expiresAt = getExpiresAt();
188187

189188
OAuth2AccessTokenResponse accessTokenResponse = new OAuth2AccessTokenResponse();

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -153,4 +153,20 @@ public void buildWhenResponseAndRefreshNullThenRefreshNull() {
153153

154154
assertThat(withResponse.getRefreshToken()).isNull();
155155
}
156+
157+
@Test
158+
public void buildWhenResponseAndExpiresInThenExpiresAtEqualToIssuedAtPlusExpiresIn() {
159+
OAuth2AccessTokenResponse tokenResponse = OAuth2AccessTokenResponse
160+
.withToken(TOKEN_VALUE)
161+
.tokenType(OAuth2AccessToken.TokenType.BEARER)
162+
.build();
163+
164+
long expiresIn = 30;
165+
OAuth2AccessTokenResponse withResponse = OAuth2AccessTokenResponse.withResponse(tokenResponse)
166+
.expiresIn(expiresIn)
167+
.build();
168+
169+
assertThat(withResponse.getAccessToken().getExpiresAt()).isEqualTo(
170+
withResponse.getAccessToken().getIssuedAt().plusSeconds(expiresIn));
171+
}
156172
}

0 commit comments

Comments
 (0)