Skip to content

Commit bf24cfb

Browse files
committed
Add temporary OAuth2RefreshToken2
Issue spring-projects/spring-security#9146
1 parent 77a9b2e commit bf24cfb

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.oauth2.core;
17+
18+
import java.time.Instant;
19+
20+
/**
21+
* TODO
22+
* This class is temporary and will be removed after upgrading to Spring Security 5.5.0 GA.
23+
*
24+
* @author Joe Grandja
25+
* @since 0.0.3
26+
* @see <a target="_blank" href="https://github.com/spring-projects/spring-security/pull/9146">Issue gh-9146</a>
27+
*/
28+
public class OAuth2RefreshToken2 extends OAuth2RefreshToken {
29+
private final Instant expiresAt;
30+
31+
public OAuth2RefreshToken2(String tokenValue, Instant issuedAt, Instant expiresAt) {
32+
super(tokenValue, issuedAt);
33+
this.expiresAt = expiresAt;
34+
}
35+
36+
@Override
37+
public Instant getExpiresAt() {
38+
return this.expiresAt;
39+
}
40+
}

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2TokenIssuerUtil.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.springframework.security.crypto.keygen.Base64StringKeyGenerator;
1919
import org.springframework.security.crypto.keygen.StringKeyGenerator;
2020
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
21+
import org.springframework.security.oauth2.core.OAuth2RefreshToken2;
2122
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
2223
import org.springframework.security.oauth2.jose.JoseHeader;
2324
import org.springframework.security.oauth2.jose.jws.SignatureAlgorithm;
@@ -72,6 +73,6 @@ static OAuth2RefreshToken issueRefreshToken(Duration refreshTokenTimeToLive) {
7273
Instant issuedAt = Instant.now();
7374
Instant expiresAt = issuedAt.plus(refreshTokenTimeToLive);
7475

75-
return new OAuth2RefreshToken(TOKEN_GENERATOR.generateKey(), issuedAt, expiresAt);
76+
return new OAuth2RefreshToken2(TOKEN_GENERATOR.generateKey(), issuedAt, expiresAt);
7677
}
7778
}

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/token/OAuth2Tokens.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.security.oauth2.core.AbstractOAuth2Token;
2020
import org.springframework.security.oauth2.core.OAuth2AccessToken;
2121
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
22+
import org.springframework.security.oauth2.core.OAuth2RefreshToken2;
2223
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
2324
import org.springframework.security.oauth2.server.authorization.Version;
2425
import org.springframework.util.Assert;
@@ -64,7 +65,8 @@ public OAuth2AccessToken getAccessToken() {
6465
*/
6566
@Nullable
6667
public OAuth2RefreshToken getRefreshToken() {
67-
return getToken(OAuth2RefreshToken.class);
68+
OAuth2RefreshToken refreshToken = getToken(OAuth2RefreshToken.class);
69+
return refreshToken != null ? refreshToken : getToken(OAuth2RefreshToken2.class);
6870
}
6971

7072
/**

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/TestOAuth2Authorizations.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.springframework.security.oauth2.core.OAuth2AccessToken;
1919
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
20+
import org.springframework.security.oauth2.core.OAuth2RefreshToken2;
2021
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
2122
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
2223
import org.springframework.security.oauth2.server.authorization.client.TestRegisteredClients;
@@ -48,7 +49,7 @@ public static OAuth2Authorization.Builder authorization(RegisteredClient registe
4849
"code", Instant.now(), Instant.now().plusSeconds(120));
4950
OAuth2AccessToken accessToken = new OAuth2AccessToken(
5051
OAuth2AccessToken.TokenType.BEARER, "access-token", Instant.now(), Instant.now().plusSeconds(300));
51-
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken(
52+
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken2(
5253
"refresh-token", Instant.now(), Instant.now().plus(1, ChronoUnit.HOURS));
5354
OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.authorizationCode()
5455
.authorizationUri("https://provider.com/oauth2/authorize")

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2RefreshTokenAuthenticationProviderTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
2424
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
2525
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
26+
import org.springframework.security.oauth2.core.OAuth2RefreshToken2;
2627
import org.springframework.security.oauth2.jose.JoseHeaderNames;
2728
import org.springframework.security.oauth2.jose.jws.SignatureAlgorithm;
2829
import org.springframework.security.oauth2.jwt.Jwt;
@@ -291,7 +292,7 @@ public void authenticateWhenClientNotAuthorizedToRefreshTokenThenThrowOAuth2Auth
291292
public void authenticateWhenExpiredRefreshTokenThenThrowOAuth2AuthenticationException() {
292293
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
293294
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build();
294-
OAuth2RefreshToken expiredRefreshToken = new OAuth2RefreshToken(
295+
OAuth2RefreshToken expiredRefreshToken = new OAuth2RefreshToken2(
295296
"expired-refresh-token", Instant.now().minusSeconds(120), Instant.now().minusSeconds(60));
296297
OAuth2Tokens tokens = OAuth2Tokens.from(authorization.getTokens()).refreshToken(expiredRefreshToken).build();
297298
authorization = OAuth2Authorization.from(authorization).tokens(tokens).build();

0 commit comments

Comments
 (0)