|
| 1 | +/* |
| 2 | + * Copyright 2002-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.config.http; |
| 17 | + |
| 18 | +import org.springframework.beans.BeanMetadataElement; |
| 19 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 20 | +import org.springframework.beans.factory.config.BeanReference; |
| 21 | +import org.springframework.beans.factory.config.RuntimeBeanReference; |
| 22 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 23 | +import org.springframework.beans.factory.xml.BeanDefinitionParser; |
| 24 | +import org.springframework.beans.factory.xml.ParserContext; |
| 25 | +import org.springframework.security.oauth2.client.authentication.OAuth2AuthorizationCodeAuthenticationProvider; |
| 26 | +import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; |
| 27 | +import org.springframework.security.oauth2.client.web.OAuth2AuthorizationCodeGrantFilter; |
| 28 | +import org.springframework.security.oauth2.client.web.OAuth2AuthorizationRequestRedirectFilter; |
| 29 | +import org.springframework.util.StringUtils; |
| 30 | +import org.springframework.util.xml.DomUtils; |
| 31 | +import org.w3c.dom.Element; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Joe Grandja |
| 35 | + * @since 5.3 |
| 36 | + */ |
| 37 | +final class OAuth2ClientBeanDefinitionParser implements BeanDefinitionParser { |
| 38 | + private static final String ELT_AUTHORIZATION_CODE_GRANT = "authorization-code-grant"; |
| 39 | + private static final String ATT_CLIENT_REGISTRATION_REPOSITORY_REF = "client-registration-repository-ref"; |
| 40 | + private static final String ATT_AUTHORIZED_CLIENT_REPOSITORY_REF = "authorized-client-repository-ref"; |
| 41 | + private static final String ATT_AUTHORIZED_CLIENT_SERVICE_REF = "authorized-client-service-ref"; |
| 42 | + private static final String ATT_AUTHORIZATION_REQUEST_REPOSITORY_REF = "authorization-request-repository-ref"; |
| 43 | + private static final String ATT_AUTHORIZATION_REQUEST_RESOLVER_REF = "authorization-request-resolver-ref"; |
| 44 | + private static final String ATT_ACCESS_TOKEN_RESPONSE_CLIENT_REF = "access-token-response-client-ref"; |
| 45 | + private final BeanReference requestCache; |
| 46 | + private final BeanReference authenticationManager; |
| 47 | + private BeanDefinition authorizationRequestRedirectFilter; |
| 48 | + private BeanDefinition authorizationCodeGrantFilter; |
| 49 | + private BeanDefinition authorizationCodeAuthenticationProvider; |
| 50 | + |
| 51 | + OAuth2ClientBeanDefinitionParser(BeanReference requestCache, BeanReference authenticationManager) { |
| 52 | + this.requestCache = requestCache; |
| 53 | + this.authenticationManager = authenticationManager; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public BeanDefinition parse(Element element, ParserContext parserContext) { |
| 58 | + Element authorizationCodeGrantElt = DomUtils.getChildElementByTagName(element, ELT_AUTHORIZATION_CODE_GRANT); |
| 59 | + |
| 60 | + BeanMetadataElement clientRegistrationRepository = getClientRegistrationRepository(element); |
| 61 | + BeanMetadataElement authorizedClientRepository = getAuthorizedClientRepository( |
| 62 | + element, clientRegistrationRepository); |
| 63 | + BeanMetadataElement authorizationRequestRepository = getAuthorizationRequestRepository( |
| 64 | + authorizationCodeGrantElt); |
| 65 | + |
| 66 | + BeanDefinitionBuilder authorizationRequestRedirectFilterBuilder = BeanDefinitionBuilder |
| 67 | + .rootBeanDefinition(OAuth2AuthorizationRequestRedirectFilter.class); |
| 68 | + String authorizationRequestResolverRef = authorizationCodeGrantElt != null ? |
| 69 | + authorizationCodeGrantElt.getAttribute(ATT_AUTHORIZATION_REQUEST_RESOLVER_REF) : null; |
| 70 | + if (!StringUtils.isEmpty(authorizationRequestResolverRef)) { |
| 71 | + authorizationRequestRedirectFilterBuilder.addConstructorArgReference(authorizationRequestResolverRef); |
| 72 | + } else { |
| 73 | + authorizationRequestRedirectFilterBuilder.addConstructorArgValue(clientRegistrationRepository); |
| 74 | + } |
| 75 | + this.authorizationRequestRedirectFilter = authorizationRequestRedirectFilterBuilder |
| 76 | + .addPropertyValue("authorizationRequestRepository", authorizationRequestRepository) |
| 77 | + .addPropertyValue("requestCache", this.requestCache) |
| 78 | + .getBeanDefinition(); |
| 79 | + |
| 80 | + this.authorizationCodeGrantFilter = BeanDefinitionBuilder |
| 81 | + .rootBeanDefinition(OAuth2AuthorizationCodeGrantFilter.class) |
| 82 | + .addConstructorArgValue(clientRegistrationRepository) |
| 83 | + .addConstructorArgValue(authorizedClientRepository) |
| 84 | + .addConstructorArgValue(this.authenticationManager) |
| 85 | + .addPropertyValue("authorizationRequestRepository", authorizationRequestRepository) |
| 86 | + .getBeanDefinition(); |
| 87 | + |
| 88 | + BeanMetadataElement accessTokenResponseClient = getAccessTokenResponseClient(authorizationCodeGrantElt); |
| 89 | + |
| 90 | + this.authorizationCodeAuthenticationProvider = BeanDefinitionBuilder |
| 91 | + .rootBeanDefinition(OAuth2AuthorizationCodeAuthenticationProvider.class) |
| 92 | + .addConstructorArgValue(accessTokenResponseClient) |
| 93 | + .getBeanDefinition(); |
| 94 | + |
| 95 | + return null; |
| 96 | + } |
| 97 | + |
| 98 | + private BeanMetadataElement getClientRegistrationRepository(Element element) { |
| 99 | + BeanMetadataElement clientRegistrationRepository; |
| 100 | + String clientRegistrationRepositoryRef = element.getAttribute(ATT_CLIENT_REGISTRATION_REPOSITORY_REF); |
| 101 | + if (!StringUtils.isEmpty(clientRegistrationRepositoryRef)) { |
| 102 | + clientRegistrationRepository = new RuntimeBeanReference(clientRegistrationRepositoryRef); |
| 103 | + } else { |
| 104 | + clientRegistrationRepository = new RuntimeBeanReference(ClientRegistrationRepository.class); |
| 105 | + } |
| 106 | + return clientRegistrationRepository; |
| 107 | + } |
| 108 | + |
| 109 | + private BeanMetadataElement getAuthorizedClientRepository(Element element, |
| 110 | + BeanMetadataElement clientRegistrationRepository) { |
| 111 | + BeanMetadataElement authorizedClientRepository; |
| 112 | + String authorizedClientRepositoryRef = element.getAttribute(ATT_AUTHORIZED_CLIENT_REPOSITORY_REF); |
| 113 | + if (!StringUtils.isEmpty(authorizedClientRepositoryRef)) { |
| 114 | + authorizedClientRepository = new RuntimeBeanReference(authorizedClientRepositoryRef); |
| 115 | + } else { |
| 116 | + BeanMetadataElement authorizedClientService; |
| 117 | + String authorizedClientServiceRef = element.getAttribute(ATT_AUTHORIZED_CLIENT_SERVICE_REF); |
| 118 | + if (!StringUtils.isEmpty(authorizedClientServiceRef)) { |
| 119 | + authorizedClientService = new RuntimeBeanReference(authorizedClientServiceRef); |
| 120 | + } else { |
| 121 | + authorizedClientService = BeanDefinitionBuilder |
| 122 | + .rootBeanDefinition( |
| 123 | + "org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService") |
| 124 | + .addConstructorArgValue(clientRegistrationRepository).getBeanDefinition(); |
| 125 | + } |
| 126 | + authorizedClientRepository = BeanDefinitionBuilder.rootBeanDefinition( |
| 127 | + "org.springframework.security.oauth2.client.web.AuthenticatedPrincipalOAuth2AuthorizedClientRepository") |
| 128 | + .addConstructorArgValue(authorizedClientService).getBeanDefinition(); |
| 129 | + } |
| 130 | + return authorizedClientRepository; |
| 131 | + } |
| 132 | + |
| 133 | + private BeanMetadataElement getAuthorizationRequestRepository(Element element) { |
| 134 | + BeanMetadataElement authorizationRequestRepository; |
| 135 | + String authorizationRequestRepositoryRef = element != null ? |
| 136 | + element.getAttribute(ATT_AUTHORIZATION_REQUEST_REPOSITORY_REF) : null; |
| 137 | + if (!StringUtils.isEmpty(authorizationRequestRepositoryRef)) { |
| 138 | + authorizationRequestRepository = new RuntimeBeanReference(authorizationRequestRepositoryRef); |
| 139 | + } else { |
| 140 | + authorizationRequestRepository = BeanDefinitionBuilder.rootBeanDefinition( |
| 141 | + "org.springframework.security.oauth2.client.web.HttpSessionOAuth2AuthorizationRequestRepository") |
| 142 | + .getBeanDefinition(); |
| 143 | + } |
| 144 | + return authorizationRequestRepository; |
| 145 | + } |
| 146 | + |
| 147 | + private BeanMetadataElement getAccessTokenResponseClient(Element element) { |
| 148 | + BeanMetadataElement accessTokenResponseClient; |
| 149 | + String accessTokenResponseClientRef = element != null ? |
| 150 | + element.getAttribute(ATT_ACCESS_TOKEN_RESPONSE_CLIENT_REF) : null; |
| 151 | + if (!StringUtils.isEmpty(accessTokenResponseClientRef)) { |
| 152 | + accessTokenResponseClient = new RuntimeBeanReference(accessTokenResponseClientRef); |
| 153 | + } else { |
| 154 | + accessTokenResponseClient = BeanDefinitionBuilder.rootBeanDefinition( |
| 155 | + "org.springframework.security.oauth2.client.endpoint.DefaultAuthorizationCodeTokenResponseClient") |
| 156 | + .getBeanDefinition(); |
| 157 | + } |
| 158 | + return accessTokenResponseClient; |
| 159 | + } |
| 160 | + |
| 161 | + BeanDefinition getAuthorizationRequestRedirectFilter() { |
| 162 | + return this.authorizationRequestRedirectFilter; |
| 163 | + } |
| 164 | + |
| 165 | + BeanDefinition getAuthorizationCodeGrantFilter() { |
| 166 | + return this.authorizationCodeGrantFilter; |
| 167 | + } |
| 168 | + |
| 169 | + BeanDefinition getAuthorizationCodeAuthenticationProvider() { |
| 170 | + return this.authorizationCodeAuthenticationProvider; |
| 171 | + } |
| 172 | +} |
0 commit comments