|
| 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.server.authorization.authentication; |
| 17 | + |
| 18 | +import org.springframework.security.authentication.AuthenticationProvider; |
| 19 | +import org.springframework.security.core.Authentication; |
| 20 | +import org.springframework.security.core.AuthenticationException; |
| 21 | +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; |
| 22 | +import org.springframework.security.oauth2.core.OAuth2Error; |
| 23 | +import org.springframework.security.oauth2.core.OAuth2ErrorCodes; |
| 24 | +import org.springframework.security.oauth2.server.authorization.OAuth2Authorization; |
| 25 | +import org.springframework.security.oauth2.server.authorization.OAuth2TokenRevocationService; |
| 26 | +import org.springframework.security.oauth2.server.authorization.TokenType; |
| 27 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; |
| 28 | +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService; |
| 29 | +import org.springframework.util.Assert; |
| 30 | + |
| 31 | +/** |
| 32 | + * An {@link AuthenticationProvider} implementation for the OAuth 2.0 Token Revocation. |
| 33 | + * |
| 34 | + * @author Vivek Babu |
| 35 | + * @since 0.0.1 |
| 36 | + * @see OAuth2TokenRevocationAuthenticationToken |
| 37 | + * @see OAuth2AuthorizationService |
| 38 | + * @see OAuth2TokenRevocationService |
| 39 | + * @see <a target="_blank" href="https://tools.ietf.org/html/rfc7009#section-2.1">Section 2.1 Revocation Request</a> |
| 40 | + */ |
| 41 | +public class OAuth2TokenRevocationAuthenticationProvider implements AuthenticationProvider { |
| 42 | + |
| 43 | + private OAuth2AuthorizationService authorizationService; |
| 44 | + private OAuth2TokenRevocationService tokenRevocationService; |
| 45 | + |
| 46 | + /** |
| 47 | + * Constructs an {@code OAuth2TokenRevocationAuthenticationProvider} using the provided parameters. |
| 48 | + * |
| 49 | + * @param authorizationService the authorization service |
| 50 | + * @param tokenRevocationService the token revocation service |
| 51 | + */ |
| 52 | + public OAuth2TokenRevocationAuthenticationProvider(OAuth2AuthorizationService authorizationService, |
| 53 | + OAuth2TokenRevocationService tokenRevocationService) { |
| 54 | + Assert.notNull(authorizationService, "authorizationService cannot be null"); |
| 55 | + Assert.notNull(tokenRevocationService, "tokenRevocationService cannot be null"); |
| 56 | + this.authorizationService = authorizationService; |
| 57 | + this.tokenRevocationService = tokenRevocationService; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public Authentication authenticate(Authentication authentication) throws AuthenticationException { |
| 62 | + OAuth2TokenRevocationAuthenticationToken tokenRevocationAuthenticationToken = |
| 63 | + (OAuth2TokenRevocationAuthenticationToken) authentication; |
| 64 | + |
| 65 | + OAuth2ClientAuthenticationToken clientPrincipal = null; |
| 66 | + if (OAuth2ClientAuthenticationToken.class.isAssignableFrom(tokenRevocationAuthenticationToken.getPrincipal() |
| 67 | + .getClass())) { |
| 68 | + clientPrincipal = (OAuth2ClientAuthenticationToken) tokenRevocationAuthenticationToken.getPrincipal(); |
| 69 | + } |
| 70 | + if (clientPrincipal == null || !clientPrincipal.isAuthenticated()) { |
| 71 | + throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_CLIENT)); |
| 72 | + } |
| 73 | + |
| 74 | + final RegisteredClient registeredClient = clientPrincipal.getRegisteredClient(); |
| 75 | + final String tokenTypeHint = tokenRevocationAuthenticationToken.getTokenTypeHint(); |
| 76 | + final String token = tokenRevocationAuthenticationToken.getToken(); |
| 77 | + final OAuth2Authorization authorization = authorizationService.findByTokenAndTokenType(token, |
| 78 | + TokenType.ACCESS_TOKEN); |
| 79 | + |
| 80 | + OAuth2TokenRevocationAuthenticationToken successfulAuthentication = |
| 81 | + new OAuth2TokenRevocationAuthenticationToken(token, registeredClient, tokenTypeHint); |
| 82 | + |
| 83 | + if (authorization == null) { |
| 84 | + return successfulAuthentication; |
| 85 | + } |
| 86 | + |
| 87 | + if (!registeredClient.getClientId().equals(authorization.getRegisteredClientId())) { |
| 88 | + throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_GRANT)); |
| 89 | + } |
| 90 | + |
| 91 | + tokenRevocationService.revoke(token, TokenType.ACCESS_TOKEN); |
| 92 | + return successfulAuthentication; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public boolean supports(Class<?> authentication) { |
| 97 | + return OAuth2TokenRevocationAuthenticationToken.class.isAssignableFrom(authentication); |
| 98 | + } |
| 99 | +} |
0 commit comments