|
| 1 | +/* |
| 2 | + * Copyright 2020-2021 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.oidc.authentication; |
| 17 | + |
| 18 | +import java.util.Collection; |
| 19 | + |
| 20 | +import org.springframework.security.authentication.AuthenticationProvider; |
| 21 | +import org.springframework.security.core.Authentication; |
| 22 | +import org.springframework.security.core.AuthenticationException; |
| 23 | +import org.springframework.security.oauth2.core.OAuth2AccessToken; |
| 24 | +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; |
| 25 | +import org.springframework.security.oauth2.core.OAuth2ErrorCodes; |
| 26 | +import org.springframework.security.oauth2.core.OAuth2TokenType; |
| 27 | +import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
| 28 | +import org.springframework.security.oauth2.server.authorization.OAuth2Authorization; |
| 29 | +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService; |
| 30 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; |
| 31 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; |
| 32 | +import org.springframework.security.oauth2.server.resource.authentication.AbstractOAuth2TokenAuthenticationToken; |
| 33 | +import org.springframework.util.Assert; |
| 34 | + |
| 35 | +/** |
| 36 | + * An {@link AuthenticationProvider} implementation for OpenID Connect Client Configuration 1.0. |
| 37 | + * |
| 38 | + * @author Ovidiu Popa |
| 39 | + * @since 0.2.1 |
| 40 | + * @see RegisteredClientRepository |
| 41 | + * @see OAuth2AuthorizationService |
| 42 | + * @see <a href="https://openid.net/specs/openid-connect-registration-1_0.html#ClientConfigurationEndpoint">4. Client Configuration Endpoint</a> |
| 43 | + */ |
| 44 | +public final class OidcClientConfigurationAuthenticationProvider implements AuthenticationProvider { |
| 45 | + |
| 46 | + private static final String DEFAULT_AUTHORIZED_SCOPE = "client.read"; |
| 47 | + |
| 48 | + private final RegisteredClientRepository registeredClientRepository; |
| 49 | + private final OAuth2AuthorizationService authorizationService; |
| 50 | + |
| 51 | + /** |
| 52 | + * Constructs an {@code OidcClientConfigurationAuthenticationProvider} using the provided parameters. |
| 53 | + * |
| 54 | + * @param registeredClientRepository the repository of registered clients |
| 55 | + * @param authorizationService the authorization service |
| 56 | + */ |
| 57 | + public OidcClientConfigurationAuthenticationProvider(RegisteredClientRepository registeredClientRepository, |
| 58 | + OAuth2AuthorizationService authorizationService) { |
| 59 | + Assert.notNull(registeredClientRepository, "registeredClientRepository cannot be null"); |
| 60 | + Assert.notNull(authorizationService, "authorizationService cannot be null"); |
| 61 | + this.registeredClientRepository = registeredClientRepository; |
| 62 | + this.authorizationService = authorizationService; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public Authentication authenticate(Authentication authentication) throws AuthenticationException { |
| 67 | + OidcClientConfigurationAuthenticationToken clientConfigurationAuthentication = |
| 68 | + (OidcClientConfigurationAuthenticationToken) authentication; |
| 69 | + |
| 70 | + // Validate the "initial" access token |
| 71 | + AbstractOAuth2TokenAuthenticationToken<?> accessTokenAuthentication = null; |
| 72 | + if (AbstractOAuth2TokenAuthenticationToken.class.isAssignableFrom(clientConfigurationAuthentication.getPrincipal().getClass())) { |
| 73 | + accessTokenAuthentication = (AbstractOAuth2TokenAuthenticationToken<?>) clientConfigurationAuthentication.getPrincipal(); |
| 74 | + } |
| 75 | + if (accessTokenAuthentication == null || !accessTokenAuthentication.isAuthenticated()) { |
| 76 | + throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_TOKEN); |
| 77 | + } |
| 78 | + |
| 79 | + String accessTokenValue = accessTokenAuthentication.getToken().getTokenValue(); |
| 80 | + |
| 81 | + OAuth2Authorization authorization = this.authorizationService.findByToken( |
| 82 | + accessTokenValue, OAuth2TokenType.ACCESS_TOKEN); |
| 83 | + if (authorization == null) { |
| 84 | + throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_TOKEN); |
| 85 | + } |
| 86 | + |
| 87 | + OAuth2Authorization.Token<OAuth2AccessToken> authorizedAccessToken = authorization.getAccessToken(); |
| 88 | + if (!authorizedAccessToken.isActive()) { |
| 89 | + throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_TOKEN); |
| 90 | + } |
| 91 | + |
| 92 | + if (!isAuthorized(authorizedAccessToken)) { |
| 93 | + throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INSUFFICIENT_SCOPE); |
| 94 | + } |
| 95 | + |
| 96 | + RegisteredClient registeredClient = this.registeredClientRepository |
| 97 | + .findByClientId(clientConfigurationAuthentication.getClientId()); |
| 98 | + |
| 99 | + if (registeredClient == null) { |
| 100 | + throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_CLIENT); |
| 101 | + } |
| 102 | + |
| 103 | + // Invalidate the "initial" access token as it can only be used once |
| 104 | + authorization = OidcAuthenticationProviderUtils.invalidate(authorization, authorizedAccessToken.getToken()); |
| 105 | + if (authorization.getRefreshToken() != null) { |
| 106 | + authorization = OidcAuthenticationProviderUtils.invalidate(authorization, authorization.getRefreshToken().getToken()); |
| 107 | + } |
| 108 | + |
| 109 | + this.authorizationService.save(authorization); |
| 110 | + |
| 111 | + return new OidcClientConfigurationAuthenticationToken( |
| 112 | + accessTokenAuthentication, OidcAuthenticationProviderUtils.convert(registeredClient)); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public boolean supports(Class<?> authentication) { |
| 117 | + return OidcClientConfigurationAuthenticationToken.class.isAssignableFrom(authentication); |
| 118 | + } |
| 119 | + |
| 120 | + @SuppressWarnings("unchecked") |
| 121 | + private static boolean isAuthorized(OAuth2Authorization.Token<OAuth2AccessToken> authorizedAccessToken) { |
| 122 | + Object scope = authorizedAccessToken.getClaims().get(OAuth2ParameterNames.SCOPE); |
| 123 | + return scope != null && ((Collection<String>) scope).contains(DEFAULT_AUTHORIZED_SCOPE); |
| 124 | + } |
| 125 | +} |
0 commit comments