Skip to content

Commit c069692

Browse files
committed
Extract OAuth2Token from AbstractOAuth2Token
Closes gh-5502
1 parent 8b7751f commit c069692

File tree

8 files changed

+80
-20
lines changed

8 files changed

+80
-20
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/ClientCredentialsOAuth2AuthorizedClientProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
import org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient;
2626
import org.springframework.security.oauth2.client.endpoint.OAuth2ClientCredentialsGrantRequest;
2727
import org.springframework.security.oauth2.client.registration.ClientRegistration;
28-
import org.springframework.security.oauth2.core.AbstractOAuth2Token;
2928
import org.springframework.security.oauth2.core.AuthorizationGrantType;
3029
import org.springframework.security.oauth2.core.OAuth2AuthorizationException;
30+
import org.springframework.security.oauth2.core.OAuth2Token;
3131
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
3232
import org.springframework.util.Assert;
3333

@@ -97,7 +97,7 @@ private OAuth2AccessTokenResponse getTokenResponse(ClientRegistration clientRegi
9797
}
9898
}
9999

100-
private boolean hasTokenExpired(AbstractOAuth2Token token) {
100+
private boolean hasTokenExpired(OAuth2Token token) {
101101
return this.clock.instant().isAfter(token.getExpiresAt().minus(this.clockSkew));
102102
}
103103

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/ClientCredentialsReactiveOAuth2AuthorizedClientProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
import org.springframework.security.oauth2.client.endpoint.ReactiveOAuth2AccessTokenResponseClient;
2727
import org.springframework.security.oauth2.client.endpoint.WebClientReactiveClientCredentialsTokenResponseClient;
2828
import org.springframework.security.oauth2.client.registration.ClientRegistration;
29-
import org.springframework.security.oauth2.core.AbstractOAuth2Token;
3029
import org.springframework.security.oauth2.core.AuthorizationGrantType;
3130
import org.springframework.security.oauth2.core.OAuth2AuthorizationException;
31+
import org.springframework.security.oauth2.core.OAuth2Token;
3232
import org.springframework.util.Assert;
3333

3434
/**
@@ -89,7 +89,7 @@ public Mono<OAuth2AuthorizedClient> authorize(OAuth2AuthorizationContext context
8989
tokenResponse.getAccessToken()));
9090
}
9191

92-
private boolean hasTokenExpired(AbstractOAuth2Token token) {
92+
private boolean hasTokenExpired(OAuth2Token token) {
9393
return this.clock.instant().isAfter(token.getExpiresAt().minus(this.clockSkew));
9494
}
9595

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/PasswordOAuth2AuthorizedClientProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
import org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient;
2626
import org.springframework.security.oauth2.client.endpoint.OAuth2PasswordGrantRequest;
2727
import org.springframework.security.oauth2.client.registration.ClientRegistration;
28-
import org.springframework.security.oauth2.core.AbstractOAuth2Token;
2928
import org.springframework.security.oauth2.core.AuthorizationGrantType;
3029
import org.springframework.security.oauth2.core.OAuth2AuthorizationException;
30+
import org.springframework.security.oauth2.core.OAuth2Token;
3131
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
3232
import org.springframework.util.Assert;
3333
import org.springframework.util.StringUtils;
@@ -116,7 +116,7 @@ private OAuth2AccessTokenResponse getTokenResponse(ClientRegistration clientRegi
116116
}
117117
}
118118

119-
private boolean hasTokenExpired(AbstractOAuth2Token token) {
119+
private boolean hasTokenExpired(OAuth2Token token) {
120120
return this.clock.instant().isAfter(token.getExpiresAt().minus(this.clockSkew));
121121
}
122122

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/PasswordReactiveOAuth2AuthorizedClientProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
import org.springframework.security.oauth2.client.endpoint.ReactiveOAuth2AccessTokenResponseClient;
2727
import org.springframework.security.oauth2.client.endpoint.WebClientReactivePasswordTokenResponseClient;
2828
import org.springframework.security.oauth2.client.registration.ClientRegistration;
29-
import org.springframework.security.oauth2.core.AbstractOAuth2Token;
3029
import org.springframework.security.oauth2.core.AuthorizationGrantType;
3130
import org.springframework.security.oauth2.core.OAuth2AuthorizationException;
31+
import org.springframework.security.oauth2.core.OAuth2Token;
3232
import org.springframework.util.Assert;
3333
import org.springframework.util.StringUtils;
3434

@@ -110,7 +110,7 @@ public Mono<OAuth2AuthorizedClient> authorize(OAuth2AuthorizationContext context
110110
tokenResponse.getAccessToken(), tokenResponse.getRefreshToken()));
111111
}
112112

113-
private boolean hasTokenExpired(AbstractOAuth2Token token) {
113+
private boolean hasTokenExpired(OAuth2Token token) {
114114
return this.clock.instant().isAfter(token.getExpiresAt().minus(this.clockSkew));
115115
}
116116

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/RefreshTokenOAuth2AuthorizedClientProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
import org.springframework.security.oauth2.client.endpoint.DefaultRefreshTokenTokenResponseClient;
2929
import org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient;
3030
import org.springframework.security.oauth2.client.endpoint.OAuth2RefreshTokenGrantRequest;
31-
import org.springframework.security.oauth2.core.AbstractOAuth2Token;
3231
import org.springframework.security.oauth2.core.AuthorizationGrantType;
3332
import org.springframework.security.oauth2.core.OAuth2AuthorizationException;
33+
import org.springframework.security.oauth2.core.OAuth2Token;
3434
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
3535
import org.springframework.util.Assert;
3636

@@ -106,7 +106,7 @@ private OAuth2AccessTokenResponse getTokenResponse(OAuth2AuthorizedClient author
106106
}
107107
}
108108

109-
private boolean hasTokenExpired(AbstractOAuth2Token token) {
109+
private boolean hasTokenExpired(OAuth2Token token) {
110110
return this.clock.instant().isAfter(token.getExpiresAt().minus(this.clockSkew));
111111
}
112112

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/RefreshTokenReactiveOAuth2AuthorizedClientProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
import org.springframework.security.oauth2.client.endpoint.ReactiveOAuth2AccessTokenResponseClient;
3131
import org.springframework.security.oauth2.client.endpoint.WebClientReactiveRefreshTokenTokenResponseClient;
3232
import org.springframework.security.oauth2.client.registration.ClientRegistration;
33-
import org.springframework.security.oauth2.core.AbstractOAuth2Token;
3433
import org.springframework.security.oauth2.core.AuthorizationGrantType;
3534
import org.springframework.security.oauth2.core.OAuth2AuthorizationException;
35+
import org.springframework.security.oauth2.core.OAuth2Token;
3636
import org.springframework.util.Assert;
3737

3838
/**
@@ -100,7 +100,7 @@ public Mono<OAuth2AuthorizedClient> authorize(OAuth2AuthorizationContext context
100100
tokenResponse.getAccessToken(), tokenResponse.getRefreshToken()));
101101
}
102102

103-
private boolean hasTokenExpired(AbstractOAuth2Token token) {
103+
private boolean hasTokenExpired(OAuth2Token token) {
104104
return this.clock.instant().isAfter(token.getExpiresAt().minus(this.clockSkew));
105105
}
106106

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/AbstractOAuth2Token.java

Lines changed: 12 additions & 8 deletions
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.
@@ -28,9 +28,11 @@
2828
*
2929
* @author Joe Grandja
3030
* @since 5.0
31+
* @see OAuth2Token
3132
* @see OAuth2AccessToken
33+
* @see OAuth2RefreshToken
3234
*/
33-
public abstract class AbstractOAuth2Token implements Serializable {
35+
public abstract class AbstractOAuth2Token implements OAuth2Token, Serializable {
3436

3537
private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
3638

@@ -51,9 +53,9 @@ protected AbstractOAuth2Token(String tokenValue) {
5153
/**
5254
* Sub-class constructor.
5355
* @param tokenValue the token value
54-
* @param issuedAt the time at which the token was issued, may be null
56+
* @param issuedAt the time at which the token was issued, may be {@code null}
5557
* @param expiresAt the expiration time on or after which the token MUST NOT be
56-
* accepted, may be null
58+
* accepted, may be {@code null}
5759
*/
5860
protected AbstractOAuth2Token(String tokenValue, @Nullable Instant issuedAt, @Nullable Instant expiresAt) {
5961
Assert.hasText(tokenValue, "tokenValue cannot be empty");
@@ -75,17 +77,19 @@ public String getTokenValue() {
7577

7678
/**
7779
* Returns the time at which the token was issued.
78-
* @return the time the token was issued or null
80+
* @return the time the token was issued or {@code null}
7981
*/
80-
public @Nullable Instant getIssuedAt() {
82+
@Nullable
83+
public Instant getIssuedAt() {
8184
return this.issuedAt;
8285
}
8386

8487
/**
8588
* Returns the expiration time on or after which the token MUST NOT be accepted.
86-
* @return the expiration time of the token or null
89+
* @return the token expiration time or {@code null}
8790
*/
88-
public @Nullable Instant getExpiresAt() {
91+
@Nullable
92+
public Instant getExpiresAt() {
8993
return this.expiresAt;
9094
}
9195

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2002-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+
17+
package org.springframework.security.oauth2.core;
18+
19+
import java.time.Instant;
20+
21+
import org.springframework.lang.Nullable;
22+
23+
/**
24+
* Core interface representing an OAuth 2.0 Token.
25+
*
26+
* @author Joe Grandja
27+
* @since 5.5
28+
* @see AbstractOAuth2Token
29+
*/
30+
public interface OAuth2Token {
31+
32+
/**
33+
* Returns the token value.
34+
* @return the token value
35+
*/
36+
String getTokenValue();
37+
38+
/**
39+
* Returns the time at which the token was issued.
40+
* @return the time the token was issued or {@code null}
41+
*/
42+
@Nullable
43+
default Instant getIssuedAt() {
44+
return null;
45+
}
46+
47+
/**
48+
* Returns the expiration time on or after which the token MUST NOT be accepted.
49+
* @return the token expiration time or {@code null}
50+
*/
51+
@Nullable
52+
default Instant getExpiresAt() {
53+
return null;
54+
}
55+
56+
}

0 commit comments

Comments
 (0)