4646import org .springframework .security .oauth2 .common .exceptions .InvalidClientException ;
4747import org .springframework .security .oauth2 .common .exceptions .InvalidScopeException ;
4848import org .springframework .security .oauth2 .common .exceptions .InvalidTokenException ;
49+ import org .springframework .security .oauth2 .provider .ClientAlreadyExistsException ;
4950import org .springframework .security .oauth2 .provider .OAuth2Authentication ;
5051import org .springframework .security .oauth2 .provider .OAuth2Request ;
5152import org .springframework .security .oauth2 .provider .TokenRequest ;
@@ -84,14 +85,18 @@ public class DefaultOAuth2ProviderTokenService implements OAuth2TokenEntityServi
8485 @ Autowired
8586 private SystemScopeService scopeService ;
8687
88+ @ Autowired
89+ private ApprovedSiteService approvedSiteService ;
90+
91+
8792 @ Override
8893 public Set <OAuth2AccessTokenEntity > getAllAccessTokensForUser (String id ) {
8994
9095 Set <OAuth2AccessTokenEntity > all = tokenRepository .getAllAccessTokens ();
9196 Set <OAuth2AccessTokenEntity > results = Sets .newLinkedHashSet ();
9297
9398 for (OAuth2AccessTokenEntity token : all ) {
94- if (token .getAuthenticationHolder ().getAuthentication ().getName ().equals (id )) {
99+ if (clearExpiredAccessToken ( token ) != null && token .getAuthenticationHolder ().getAuthentication ().getName ().equals (id )) {
95100 results .add (token );
96101 }
97102 }
@@ -106,7 +111,7 @@ public Set<OAuth2RefreshTokenEntity> getAllRefreshTokensForUser(String id) {
106111 Set <OAuth2RefreshTokenEntity > results = Sets .newLinkedHashSet ();
107112
108113 for (OAuth2RefreshTokenEntity token : all ) {
109- if (token .getAuthenticationHolder ().getAuthentication ().getName ().equals (id )) {
114+ if (clearExpiredRefreshToken ( token ) != null && token .getAuthenticationHolder ().getAuthentication ().getName ().equals (id )) {
110115 results .add (token );
111116 }
112117 }
@@ -116,18 +121,50 @@ public Set<OAuth2RefreshTokenEntity> getAllRefreshTokensForUser(String id) {
116121
117122 @ Override
118123 public OAuth2AccessTokenEntity getAccessTokenById (Long id ) {
119- return tokenRepository .getAccessTokenById (id );
124+ return clearExpiredAccessToken ( tokenRepository .getAccessTokenById (id ) );
120125 }
121126
122127 @ Override
123128 public OAuth2RefreshTokenEntity getRefreshTokenById (Long id ) {
124- return tokenRepository .getRefreshTokenById (id );
129+ return clearExpiredRefreshToken ( tokenRepository .getRefreshTokenById (id ) );
125130 }
126131
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+
131168 @ Override
132169 public OAuth2AccessTokenEntity createAccessToken (OAuth2Authentication authentication ) throws AuthenticationException , InvalidClientException {
133170 if (authentication != null && authentication .getOAuth2Request () != null ) {
@@ -238,7 +275,7 @@ private OAuth2RefreshTokenEntity createRefreshToken(ClientDetailsEntity client,
238275 @ Override
239276 public OAuth2AccessTokenEntity refreshAccessToken (String refreshTokenValue , TokenRequest authRequest ) throws AuthenticationException {
240277
241- OAuth2RefreshTokenEntity refreshToken = tokenRepository .getRefreshTokenByValue (refreshTokenValue );
278+ OAuth2RefreshTokenEntity refreshToken = clearExpiredRefreshToken ( tokenRepository .getRefreshTokenByValue (refreshTokenValue ) );
242279
243280 if (refreshToken == null ) {
244281 throw new InvalidTokenException ("Invalid refresh token: " + refreshTokenValue );
@@ -331,14 +368,10 @@ public OAuth2AccessTokenEntity refreshAccessToken(String refreshTokenValue, Toke
331368 @ Override
332369 public OAuth2Authentication loadAuthentication (String accessTokenValue ) throws AuthenticationException {
333370
334- OAuth2AccessTokenEntity accessToken = tokenRepository .getAccessTokenByValue (accessTokenValue );
371+ OAuth2AccessTokenEntity accessToken = clearExpiredAccessToken ( tokenRepository .getAccessTokenByValue (accessTokenValue ) );
335372
336373 if (accessToken == null ) {
337374 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 );
342375 } else {
343376 return accessToken .getAuthenticationHolder ().getAuthentication ();
344377 }
@@ -350,13 +383,9 @@ public OAuth2Authentication loadAuthentication(String accessTokenValue) throws A
350383 */
351384 @ Override
352385 public OAuth2AccessTokenEntity readAccessToken (String accessTokenValue ) throws AuthenticationException {
353- OAuth2AccessTokenEntity accessToken = tokenRepository .getAccessTokenByValue (accessTokenValue );
386+ OAuth2AccessTokenEntity accessToken = clearExpiredAccessToken ( tokenRepository .getAccessTokenByValue (accessTokenValue ) );
354387 if (accessToken == null ) {
355388 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" );
360389 } else {
361390 return accessToken ;
362391 }
0 commit comments