46
46
import org .springframework .security .oauth2 .common .exceptions .InvalidClientException ;
47
47
import org .springframework .security .oauth2 .common .exceptions .InvalidScopeException ;
48
48
import org .springframework .security .oauth2 .common .exceptions .InvalidTokenException ;
49
+ import org .springframework .security .oauth2 .provider .ClientAlreadyExistsException ;
49
50
import org .springframework .security .oauth2 .provider .OAuth2Authentication ;
50
51
import org .springframework .security .oauth2 .provider .OAuth2Request ;
51
52
import org .springframework .security .oauth2 .provider .TokenRequest ;
@@ -84,14 +85,18 @@ public class DefaultOAuth2ProviderTokenService implements OAuth2TokenEntityServi
84
85
@ Autowired
85
86
private SystemScopeService scopeService ;
86
87
88
+ @ Autowired
89
+ private ApprovedSiteService approvedSiteService ;
90
+
91
+
87
92
@ Override
88
93
public Set <OAuth2AccessTokenEntity > getAllAccessTokensForUser (String id ) {
89
94
90
95
Set <OAuth2AccessTokenEntity > all = tokenRepository .getAllAccessTokens ();
91
96
Set <OAuth2AccessTokenEntity > results = Sets .newLinkedHashSet ();
92
97
93
98
for (OAuth2AccessTokenEntity token : all ) {
94
- if (token .getAuthenticationHolder ().getAuthentication ().getName ().equals (id )) {
99
+ if (clearExpiredAccessToken ( token ) != null && token .getAuthenticationHolder ().getAuthentication ().getName ().equals (id )) {
95
100
results .add (token );
96
101
}
97
102
}
@@ -106,7 +111,7 @@ public Set<OAuth2RefreshTokenEntity> getAllRefreshTokensForUser(String id) {
106
111
Set <OAuth2RefreshTokenEntity > results = Sets .newLinkedHashSet ();
107
112
108
113
for (OAuth2RefreshTokenEntity token : all ) {
109
- if (token .getAuthenticationHolder ().getAuthentication ().getName ().equals (id )) {
114
+ if (clearExpiredRefreshToken ( token ) != null && token .getAuthenticationHolder ().getAuthentication ().getName ().equals (id )) {
110
115
results .add (token );
111
116
}
112
117
}
@@ -116,18 +121,50 @@ public Set<OAuth2RefreshTokenEntity> getAllRefreshTokensForUser(String id) {
116
121
117
122
@ Override
118
123
public OAuth2AccessTokenEntity getAccessTokenById (Long id ) {
119
- return tokenRepository .getAccessTokenById (id );
124
+ return clearExpiredAccessToken ( tokenRepository .getAccessTokenById (id ) );
120
125
}
121
126
122
127
@ Override
123
128
public OAuth2RefreshTokenEntity getRefreshTokenById (Long id ) {
124
- return tokenRepository .getRefreshTokenById (id );
129
+ return clearExpiredRefreshToken ( tokenRepository .getRefreshTokenById (id ) );
125
130
}
126
131
127
- @ Autowired
128
- private ApprovedSiteService approvedSiteService ;
129
-
130
-
132
+ /**
133
+ * Utility function to delete an access token that's expired before returning it.
134
+ * @param token the token to check
135
+ * @return null if the token is null or expired, the input token (unchanged) if it hasn't
136
+ */
137
+ private OAuth2AccessTokenEntity clearExpiredAccessToken (OAuth2AccessTokenEntity token ) {
138
+ if (token == null ) {
139
+ return null ;
140
+ } else if (token .isExpired ()) {
141
+ // immediately revoke expired token
142
+ logger .debug ("Clearing expired access token: " + token .getValue ());
143
+ revokeAccessToken (token );
144
+ return null ;
145
+ } else {
146
+ return token ;
147
+ }
148
+ }
149
+
150
+ /**
151
+ * Utility function to delete a refresh token that's expired before returning it.
152
+ * @param token the token to check
153
+ * @return null if the token is null or expired, the input token (unchanged) if it hasn't
154
+ */
155
+ private OAuth2RefreshTokenEntity clearExpiredRefreshToken (OAuth2RefreshTokenEntity token ) {
156
+ if (token == null ) {
157
+ return null ;
158
+ } else if (token .isExpired ()) {
159
+ // immediately revoke expired token
160
+ logger .debug ("Clearing expired refresh token: " + token .getValue ());
161
+ revokeRefreshToken (token );
162
+ return null ;
163
+ } else {
164
+ return token ;
165
+ }
166
+ }
167
+
131
168
@ Override
132
169
public OAuth2AccessTokenEntity createAccessToken (OAuth2Authentication authentication ) throws AuthenticationException , InvalidClientException {
133
170
if (authentication != null && authentication .getOAuth2Request () != null ) {
@@ -238,7 +275,7 @@ private OAuth2RefreshTokenEntity createRefreshToken(ClientDetailsEntity client,
238
275
@ Override
239
276
public OAuth2AccessTokenEntity refreshAccessToken (String refreshTokenValue , TokenRequest authRequest ) throws AuthenticationException {
240
277
241
- OAuth2RefreshTokenEntity refreshToken = tokenRepository .getRefreshTokenByValue (refreshTokenValue );
278
+ OAuth2RefreshTokenEntity refreshToken = clearExpiredRefreshToken ( tokenRepository .getRefreshTokenByValue (refreshTokenValue ) );
242
279
243
280
if (refreshToken == null ) {
244
281
throw new InvalidTokenException ("Invalid refresh token: " + refreshTokenValue );
@@ -331,14 +368,10 @@ public OAuth2AccessTokenEntity refreshAccessToken(String refreshTokenValue, Toke
331
368
@ Override
332
369
public OAuth2Authentication loadAuthentication (String accessTokenValue ) throws AuthenticationException {
333
370
334
- OAuth2AccessTokenEntity accessToken = tokenRepository .getAccessTokenByValue (accessTokenValue );
371
+ OAuth2AccessTokenEntity accessToken = clearExpiredAccessToken ( tokenRepository .getAccessTokenByValue (accessTokenValue ) );
335
372
336
373
if (accessToken == null ) {
337
374
throw new InvalidTokenException ("Invalid access token: " + accessTokenValue );
338
- } else if (accessToken .isExpired ()) {
339
- //tokenRepository.removeAccessToken(accessToken);
340
- revokeAccessToken (accessToken );
341
- throw new InvalidTokenException ("Expired access token: " + accessTokenValue );
342
375
} else {
343
376
return accessToken .getAuthenticationHolder ().getAuthentication ();
344
377
}
@@ -350,13 +383,9 @@ public OAuth2Authentication loadAuthentication(String accessTokenValue) throws A
350
383
*/
351
384
@ Override
352
385
public OAuth2AccessTokenEntity readAccessToken (String accessTokenValue ) throws AuthenticationException {
353
- OAuth2AccessTokenEntity accessToken = tokenRepository .getAccessTokenByValue (accessTokenValue );
386
+ OAuth2AccessTokenEntity accessToken = clearExpiredAccessToken ( tokenRepository .getAccessTokenByValue (accessTokenValue ) );
354
387
if (accessToken == null ) {
355
388
throw new InvalidTokenException ("Access token for value " + accessTokenValue + " was not found" );
356
- } else if (accessToken .isExpired ()) {
357
- // immediately revoke the expired token
358
- revokeAccessToken (accessToken );
359
- throw new InvalidTokenException ("Access token for value " + accessTokenValue + " is expired" );
360
389
} else {
361
390
return accessToken ;
362
391
}
0 commit comments