|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 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.config.http; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Objects; |
| 22 | +import java.util.function.Predicate; |
| 23 | + |
| 24 | +import javax.servlet.http.HttpServletRequest; |
| 25 | + |
| 26 | +import org.w3c.dom.Element; |
| 27 | + |
| 28 | +import org.springframework.beans.BeanMetadataElement; |
| 29 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 30 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 31 | +import org.springframework.beans.factory.support.ManagedList; |
| 32 | +import org.springframework.beans.factory.xml.BeanDefinitionParser; |
| 33 | +import org.springframework.beans.factory.xml.ParserContext; |
| 34 | +import org.springframework.security.core.Authentication; |
| 35 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 36 | +import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal; |
| 37 | +import org.springframework.security.saml2.provider.service.web.DefaultRelyingPartyRegistrationResolver; |
| 38 | +import org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutRequestFilter; |
| 39 | +import org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutResponseFilter; |
| 40 | +import org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2RelyingPartyInitiatedLogoutSuccessHandler; |
| 41 | +import org.springframework.security.web.authentication.logout.LogoutFilter; |
| 42 | +import org.springframework.security.web.authentication.logout.LogoutSuccessEventPublishingLogoutHandler; |
| 43 | +import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler; |
| 44 | +import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler; |
| 45 | +import org.springframework.security.web.util.matcher.AndRequestMatcher; |
| 46 | +import org.springframework.security.web.util.matcher.AntPathRequestMatcher; |
| 47 | +import org.springframework.security.web.util.matcher.RequestMatcher; |
| 48 | +import org.springframework.util.CollectionUtils; |
| 49 | +import org.springframework.util.StringUtils; |
| 50 | + |
| 51 | +/** |
| 52 | + * SAML 2.0 Single Logout {@link BeanDefinitionParser} |
| 53 | + * |
| 54 | + * @author Marcus da Coregio |
| 55 | + * @since 5.7 |
| 56 | + */ |
| 57 | +final class Saml2LogoutBeanDefinitionParser implements BeanDefinitionParser { |
| 58 | + |
| 59 | + private static final String ATT_LOGOUT_REQUEST_URL = "logout-request-url"; |
| 60 | + |
| 61 | + private static final String ATT_LOGOUT_RESPONSE_URL = "logout-response-url"; |
| 62 | + |
| 63 | + private static final String ATT_LOGOUT_URL = "logout-url"; |
| 64 | + |
| 65 | + private List<BeanMetadataElement> logoutHandlers; |
| 66 | + |
| 67 | + private String logoutUrl = "/logout"; |
| 68 | + |
| 69 | + private String logoutRequestUrl = "/logout/saml2/slo"; |
| 70 | + |
| 71 | + private String logoutResponseUrl = "/logout/saml2/slo"; |
| 72 | + |
| 73 | + private BeanMetadataElement logoutSuccessHandler; |
| 74 | + |
| 75 | + private BeanDefinition logoutRequestFilter; |
| 76 | + |
| 77 | + private BeanDefinition logoutResponseFilter; |
| 78 | + |
| 79 | + private BeanDefinition logoutFilter; |
| 80 | + |
| 81 | + Saml2LogoutBeanDefinitionParser(ManagedList<BeanMetadataElement> logoutHandlers, |
| 82 | + BeanMetadataElement logoutSuccessHandler) { |
| 83 | + this.logoutHandlers = logoutHandlers; |
| 84 | + this.logoutSuccessHandler = logoutSuccessHandler; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public BeanDefinition parse(Element element, ParserContext pc) { |
| 89 | + String logoutUrl = element.getAttribute(ATT_LOGOUT_URL); |
| 90 | + if (StringUtils.hasText(logoutUrl)) { |
| 91 | + this.logoutUrl = logoutUrl; |
| 92 | + } |
| 93 | + String logoutRequestUrl = element.getAttribute(ATT_LOGOUT_REQUEST_URL); |
| 94 | + if (StringUtils.hasText(logoutRequestUrl)) { |
| 95 | + this.logoutRequestUrl = logoutRequestUrl; |
| 96 | + } |
| 97 | + String logoutResponseUrl = element.getAttribute(ATT_LOGOUT_RESPONSE_URL); |
| 98 | + if (StringUtils.hasText(logoutResponseUrl)) { |
| 99 | + this.logoutResponseUrl = logoutResponseUrl; |
| 100 | + } |
| 101 | + WebConfigUtils.validateHttpRedirect(this.logoutUrl, pc, element); |
| 102 | + WebConfigUtils.validateHttpRedirect(this.logoutRequestUrl, pc, element); |
| 103 | + WebConfigUtils.validateHttpRedirect(this.logoutResponseUrl, pc, element); |
| 104 | + if (CollectionUtils.isEmpty(this.logoutHandlers)) { |
| 105 | + this.logoutHandlers = createDefaultLogoutHandlers(); |
| 106 | + } |
| 107 | + if (this.logoutSuccessHandler == null) { |
| 108 | + this.logoutSuccessHandler = createDefaultLogoutSuccessHandler(); |
| 109 | + } |
| 110 | + BeanMetadataElement relyingPartyRegistrationRepository = Saml2LogoutBeanDefinitionParserUtils |
| 111 | + .getRelyingPartyRegistrationRepository(element); |
| 112 | + BeanMetadataElement registrations = BeanDefinitionBuilder |
| 113 | + .rootBeanDefinition(DefaultRelyingPartyRegistrationResolver.class) |
| 114 | + .addConstructorArgValue(relyingPartyRegistrationRepository).getBeanDefinition(); |
| 115 | + BeanMetadataElement logoutResponseResolver = Saml2LogoutBeanDefinitionParserUtils |
| 116 | + .getLogoutResponseResolver(element, registrations); |
| 117 | + BeanMetadataElement logoutRequestValidator = Saml2LogoutBeanDefinitionParserUtils |
| 118 | + .getLogoutRequestValidator(element); |
| 119 | + BeanMetadataElement logoutRequestMatcher = createSaml2LogoutRequestMatcher(); |
| 120 | + this.logoutRequestFilter = BeanDefinitionBuilder.rootBeanDefinition(Saml2LogoutRequestFilter.class) |
| 121 | + .addConstructorArgValue(registrations).addConstructorArgValue(logoutRequestValidator) |
| 122 | + .addConstructorArgValue(logoutResponseResolver).addConstructorArgValue(this.logoutHandlers) |
| 123 | + .addPropertyValue("logoutRequestMatcher", logoutRequestMatcher).getBeanDefinition(); |
| 124 | + BeanMetadataElement logoutResponseValidator = Saml2LogoutBeanDefinitionParserUtils |
| 125 | + .getLogoutResponseValidator(element); |
| 126 | + BeanMetadataElement logoutRequestRepository = Saml2LogoutBeanDefinitionParserUtils |
| 127 | + .getLogoutRequestRepository(element); |
| 128 | + BeanMetadataElement logoutResponseMatcher = createSaml2LogoutResponseMatcher(); |
| 129 | + this.logoutResponseFilter = BeanDefinitionBuilder.rootBeanDefinition(Saml2LogoutResponseFilter.class) |
| 130 | + .addConstructorArgValue(registrations).addConstructorArgValue(logoutResponseValidator) |
| 131 | + .addConstructorArgValue(this.logoutSuccessHandler) |
| 132 | + .addPropertyValue("logoutRequestMatcher", logoutResponseMatcher) |
| 133 | + .addPropertyValue("logoutRequestRepository", logoutRequestRepository).getBeanDefinition(); |
| 134 | + BeanMetadataElement logoutRequestResolver = Saml2LogoutBeanDefinitionParserUtils |
| 135 | + .getLogoutRequestResolver(element, registrations); |
| 136 | + BeanMetadataElement saml2LogoutRequestSuccessHandler = BeanDefinitionBuilder |
| 137 | + .rootBeanDefinition(Saml2RelyingPartyInitiatedLogoutSuccessHandler.class) |
| 138 | + .addConstructorArgValue(logoutRequestResolver).getBeanDefinition(); |
| 139 | + this.logoutFilter = BeanDefinitionBuilder.rootBeanDefinition(LogoutFilter.class) |
| 140 | + .addConstructorArgValue(saml2LogoutRequestSuccessHandler).addConstructorArgValue(this.logoutHandlers) |
| 141 | + .addPropertyValue("logoutRequestMatcher", createLogoutRequestMatcher()).getBeanDefinition(); |
| 142 | + return null; |
| 143 | + } |
| 144 | + |
| 145 | + private static List<BeanMetadataElement> createDefaultLogoutHandlers() { |
| 146 | + List<BeanMetadataElement> handlers = new ManagedList<>(); |
| 147 | + handlers.add(BeanDefinitionBuilder.rootBeanDefinition(SecurityContextLogoutHandler.class).getBeanDefinition()); |
| 148 | + handlers.add(BeanDefinitionBuilder.rootBeanDefinition(LogoutSuccessEventPublishingLogoutHandler.class) |
| 149 | + .getBeanDefinition()); |
| 150 | + return handlers; |
| 151 | + } |
| 152 | + |
| 153 | + private static BeanMetadataElement createDefaultLogoutSuccessHandler() { |
| 154 | + return BeanDefinitionBuilder.rootBeanDefinition(SimpleUrlLogoutSuccessHandler.class) |
| 155 | + .addPropertyValue("defaultTargetUrl", "/login?logout").getBeanDefinition(); |
| 156 | + } |
| 157 | + |
| 158 | + private BeanMetadataElement createLogoutRequestMatcher() { |
| 159 | + BeanMetadataElement logoutMatcher = BeanDefinitionBuilder.rootBeanDefinition(AntPathRequestMatcher.class) |
| 160 | + .addConstructorArgValue(this.logoutUrl).addConstructorArgValue("POST").getBeanDefinition(); |
| 161 | + BeanMetadataElement saml2Matcher = BeanDefinitionBuilder.rootBeanDefinition(Saml2RequestMatcher.class) |
| 162 | + .getBeanDefinition(); |
| 163 | + return BeanDefinitionBuilder.rootBeanDefinition(AndRequestMatcher.class) |
| 164 | + .addConstructorArgValue(toManagedList(logoutMatcher, saml2Matcher)).getBeanDefinition(); |
| 165 | + } |
| 166 | + |
| 167 | + private BeanMetadataElement createSaml2LogoutRequestMatcher() { |
| 168 | + BeanMetadataElement logoutRequestMatcher = BeanDefinitionBuilder.rootBeanDefinition(AntPathRequestMatcher.class) |
| 169 | + .addConstructorArgValue(this.logoutRequestUrl).getBeanDefinition(); |
| 170 | + BeanMetadataElement saml2RequestMatcher = BeanDefinitionBuilder |
| 171 | + .rootBeanDefinition(ParameterRequestMatcher.class).addConstructorArgValue("SAMLRequest") |
| 172 | + .getBeanDefinition(); |
| 173 | + return BeanDefinitionBuilder.rootBeanDefinition(AndRequestMatcher.class) |
| 174 | + .addConstructorArgValue(toManagedList(logoutRequestMatcher, saml2RequestMatcher)).getBeanDefinition(); |
| 175 | + } |
| 176 | + |
| 177 | + private BeanMetadataElement createSaml2LogoutResponseMatcher() { |
| 178 | + BeanMetadataElement logoutResponseMatcher = BeanDefinitionBuilder |
| 179 | + .rootBeanDefinition(AntPathRequestMatcher.class).addConstructorArgValue(this.logoutResponseUrl) |
| 180 | + .getBeanDefinition(); |
| 181 | + BeanMetadataElement saml2ResponseMatcher = BeanDefinitionBuilder |
| 182 | + .rootBeanDefinition(ParameterRequestMatcher.class).addConstructorArgValue("SAMLResponse") |
| 183 | + .getBeanDefinition(); |
| 184 | + return BeanDefinitionBuilder.rootBeanDefinition(AndRequestMatcher.class) |
| 185 | + .addConstructorArgValue(toManagedList(logoutResponseMatcher, saml2ResponseMatcher)).getBeanDefinition(); |
| 186 | + } |
| 187 | + |
| 188 | + private static List<BeanMetadataElement> toManagedList(BeanMetadataElement... elements) { |
| 189 | + List<BeanMetadataElement> managedList = new ManagedList<>(); |
| 190 | + managedList.addAll(Arrays.asList(elements)); |
| 191 | + return managedList; |
| 192 | + } |
| 193 | + |
| 194 | + BeanDefinition getLogoutRequestFilter() { |
| 195 | + return this.logoutRequestFilter; |
| 196 | + } |
| 197 | + |
| 198 | + BeanDefinition getLogoutResponseFilter() { |
| 199 | + return this.logoutResponseFilter; |
| 200 | + } |
| 201 | + |
| 202 | + BeanDefinition getLogoutFilter() { |
| 203 | + return this.logoutFilter; |
| 204 | + } |
| 205 | + |
| 206 | + private static class ParameterRequestMatcher implements RequestMatcher { |
| 207 | + |
| 208 | + Predicate<String> test = Objects::nonNull; |
| 209 | + |
| 210 | + String name; |
| 211 | + |
| 212 | + ParameterRequestMatcher(String name) { |
| 213 | + this.name = name; |
| 214 | + } |
| 215 | + |
| 216 | + @Override |
| 217 | + public boolean matches(HttpServletRequest request) { |
| 218 | + return this.test.test(request.getParameter(this.name)); |
| 219 | + } |
| 220 | + |
| 221 | + } |
| 222 | + |
| 223 | + private static class Saml2RequestMatcher implements RequestMatcher { |
| 224 | + |
| 225 | + @Override |
| 226 | + public boolean matches(HttpServletRequest request) { |
| 227 | + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 228 | + if (authentication == null) { |
| 229 | + return false; |
| 230 | + } |
| 231 | + return authentication.getPrincipal() instanceof Saml2AuthenticatedPrincipal; |
| 232 | + } |
| 233 | + |
| 234 | + } |
| 235 | + |
| 236 | +} |
0 commit comments