|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 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.server.authorization.authentication; |
| 18 | + |
| 19 | +import java.util.Set; |
| 20 | + |
| 21 | +import org.apache.commons.logging.Log; |
| 22 | +import org.apache.commons.logging.LogFactory; |
| 23 | + |
| 24 | +import org.springframework.security.authentication.AnonymousAuthenticationToken; |
| 25 | +import org.springframework.security.authentication.AuthenticationProvider; |
| 26 | +import org.springframework.security.core.Authentication; |
| 27 | +import org.springframework.security.core.AuthenticationException; |
| 28 | +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; |
| 29 | +import org.springframework.security.oauth2.core.OAuth2ErrorCodes; |
| 30 | +import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; |
| 31 | +import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
| 32 | +import org.springframework.security.oauth2.core.oidc.OidcScopes; |
| 33 | +import org.springframework.security.oauth2.server.authorization.OAuth2Authorization; |
| 34 | +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsent; |
| 35 | +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsentService; |
| 36 | +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService; |
| 37 | +import org.springframework.security.oauth2.server.authorization.OAuth2UserCode; |
| 38 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; |
| 39 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; |
| 40 | +import org.springframework.util.Assert; |
| 41 | + |
| 42 | +/** |
| 43 | + * @author Steve Riesenberg |
| 44 | + * @since 1.1 |
| 45 | + */ |
| 46 | +public final class OAuth2DeviceVerificationAuthenticationProvider implements AuthenticationProvider { |
| 47 | + |
| 48 | + private final Log logger = LogFactory.getLog(getClass()); |
| 49 | + private final RegisteredClientRepository registeredClientRepository; |
| 50 | + private final OAuth2AuthorizationService authorizationService; |
| 51 | + private final OAuth2AuthorizationConsentService authorizationConsentService; |
| 52 | + |
| 53 | + public OAuth2DeviceVerificationAuthenticationProvider( |
| 54 | + RegisteredClientRepository registeredClientRepository, |
| 55 | + OAuth2AuthorizationService authorizationService, |
| 56 | + OAuth2AuthorizationConsentService authorizationConsentService) { |
| 57 | + Assert.notNull(registeredClientRepository, "registeredClientRepository cannot be null"); |
| 58 | + Assert.notNull(authorizationService, "authorizationService cannot be null"); |
| 59 | + Assert.notNull(authorizationConsentService, "authorizationConsentService cannot be null"); |
| 60 | + this.registeredClientRepository = registeredClientRepository; |
| 61 | + this.authorizationService = authorizationService; |
| 62 | + this.authorizationConsentService = authorizationConsentService; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public Authentication authenticate(Authentication authentication) throws AuthenticationException { |
| 67 | + OAuth2DeviceVerificationAuthenticationToken deviceVerificationAuthentication = |
| 68 | + (OAuth2DeviceVerificationAuthenticationToken) authentication; |
| 69 | + |
| 70 | + String userCode = deviceVerificationAuthentication.getUserCode(); |
| 71 | + |
| 72 | + OAuth2Authorization authorization = this.authorizationService.findByToken( |
| 73 | + userCode, OAuth2UserCode.TOKEN_TYPE); |
| 74 | + if (authorization == null) { |
| 75 | + throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_GRANT); |
| 76 | + } |
| 77 | + |
| 78 | + if (this.logger.isTraceEnabled()) { |
| 79 | + this.logger.trace("Retrieved authorization with user code"); |
| 80 | + } |
| 81 | + |
| 82 | + RegisteredClient registeredClient = this.registeredClientRepository.findById( |
| 83 | + authorization.getRegisteredClientId()); |
| 84 | + Assert.notNull(registeredClient, "registeredClient cannot be null"); |
| 85 | + |
| 86 | + if (this.logger.isTraceEnabled()) { |
| 87 | + this.logger.trace("Retrieved registered client"); |
| 88 | + } |
| 89 | + |
| 90 | + Authentication principal = (Authentication) deviceVerificationAuthentication.getPrincipal(); |
| 91 | + if (!isPrincipalAuthenticated(principal)) { |
| 92 | + if (this.logger.isTraceEnabled()) { |
| 93 | + this.logger.trace("Did not authenticate device authorization request since principal not authenticated"); |
| 94 | + } |
| 95 | + // Return the authorization request as-is where isAuthenticated() is false |
| 96 | + return deviceVerificationAuthentication; |
| 97 | + } |
| 98 | + |
| 99 | + OAuth2AuthorizationRequest authorizationRequest = authorization.getAttribute(OAuth2AuthorizationRequest.class.getName()); |
| 100 | + Assert.notNull(authorizationRequest, "authorizationRequest cannot be null"); |
| 101 | + |
| 102 | + OAuth2AuthorizationConsent currentAuthorizationConsent = this.authorizationConsentService.findById( |
| 103 | + registeredClient.getId(), principal.getName()); |
| 104 | + |
| 105 | + Set<String> currentAuthorizedScopes = currentAuthorizationConsent != null ? |
| 106 | + currentAuthorizationConsent.getScopes() : null; |
| 107 | + |
| 108 | + if (requireAuthorizationConsent(registeredClient, authorizationRequest, currentAuthorizationConsent)) { |
| 109 | + String state = authorization.getAttribute(OAuth2ParameterNames.STATE); |
| 110 | + |
| 111 | + return new OAuth2DeviceAuthorizationConsentAuthenticationToken( |
| 112 | + authorizationRequest.getAuthorizationUri(), registeredClient.getClientId(), principal, userCode, |
| 113 | + state, authorizationRequest.getScopes(), currentAuthorizedScopes, null); |
| 114 | + } |
| 115 | + |
| 116 | + return null; |
| 117 | + } |
| 118 | + |
| 119 | + private static boolean requireAuthorizationConsent(RegisteredClient registeredClient, |
| 120 | + OAuth2AuthorizationRequest authorizationRequest, OAuth2AuthorizationConsent authorizationConsent) { |
| 121 | + |
| 122 | + if (!registeredClient.getClientSettings().isRequireAuthorizationConsent()) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + // 'openid' scope does not require consent |
| 126 | + if (authorizationRequest.getScopes().contains(OidcScopes.OPENID) && |
| 127 | + authorizationRequest.getScopes().size() == 1) { |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + if (authorizationConsent != null && |
| 132 | + authorizationConsent.getScopes().containsAll(authorizationRequest.getScopes())) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + return true; |
| 137 | + } |
| 138 | + |
| 139 | + private static boolean isPrincipalAuthenticated(Authentication principal) { |
| 140 | + return principal != null && |
| 141 | + !AnonymousAuthenticationToken.class.isAssignableFrom(principal.getClass()) && |
| 142 | + principal.isAuthenticated(); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public boolean supports(Class<?> authentication) { |
| 147 | + return OAuth2DeviceVerificationAuthenticationToken.class.isAssignableFrom(authentication); |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments