|
| 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 | + |
| 17 | +package org.springframework.security.saml2.provider.service.web; |
| 18 | + |
| 19 | +import org.apache.commons.logging.Log; |
| 20 | +import org.apache.commons.logging.LogFactory; |
| 21 | +import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationRequestContext; |
| 22 | +import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration; |
| 23 | +import org.springframework.util.Assert; |
| 24 | +import org.springframework.util.StringUtils; |
| 25 | +import org.springframework.web.util.UriComponents; |
| 26 | +import org.springframework.web.util.UriComponentsBuilder; |
| 27 | + |
| 28 | +import javax.servlet.http.HttpServletRequest; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.function.Function; |
| 32 | + |
| 33 | +import static org.springframework.security.web.util.UrlUtils.buildFullRequestUrl; |
| 34 | +import static org.springframework.web.util.UriComponentsBuilder.fromHttpUrl; |
| 35 | + |
| 36 | +/** |
| 37 | + * The default implementation for {@link Saml2AuthenticationRequestContextResolver} |
| 38 | + * which uses the current request and given relying party to formulate a {@link Saml2AuthenticationRequestContext} |
| 39 | + * |
| 40 | + * @author Shazin Sadakath |
| 41 | + * @since 5.4 |
| 42 | + */ |
| 43 | +public final class DefaultSaml2AuthenticationRequestContextResolver implements Saml2AuthenticationRequestContextResolver { |
| 44 | + |
| 45 | + private final Log logger = LogFactory.getLog(getClass()); |
| 46 | + |
| 47 | + private static final char PATH_DELIMITER = '/'; |
| 48 | + |
| 49 | + /** |
| 50 | + * {@inheritDoc} |
| 51 | + */ |
| 52 | + @Override |
| 53 | + public Saml2AuthenticationRequestContext resolve(HttpServletRequest request, |
| 54 | + RelyingPartyRegistration relyingParty) { |
| 55 | + Assert.notNull(request, "request cannot be null"); |
| 56 | + Assert.notNull(relyingParty, "relyingParty cannot be null"); |
| 57 | + if (this.logger.isDebugEnabled()) { |
| 58 | + this.logger.debug("Creating SAML 2.0 Authentication Request for Asserting Party [" + |
| 59 | + relyingParty.getRegistrationId() + "]"); |
| 60 | + } |
| 61 | + return createRedirectAuthenticationRequestContext(request, relyingParty); |
| 62 | + } |
| 63 | + |
| 64 | + private Saml2AuthenticationRequestContext createRedirectAuthenticationRequestContext( |
| 65 | + HttpServletRequest request, RelyingPartyRegistration relyingParty) { |
| 66 | + |
| 67 | + String applicationUri = getApplicationUri(request); |
| 68 | + Function<String, String> resolver = templateResolver(applicationUri, relyingParty); |
| 69 | + String localSpEntityId = resolver.apply(relyingParty.getLocalEntityIdTemplate()); |
| 70 | + String assertionConsumerServiceUrl = resolver.apply(relyingParty.getAssertionConsumerServiceUrlTemplate()); |
| 71 | + return Saml2AuthenticationRequestContext.builder() |
| 72 | + .issuer(localSpEntityId) |
| 73 | + .relyingPartyRegistration(relyingParty) |
| 74 | + .assertionConsumerServiceUrl(assertionConsumerServiceUrl) |
| 75 | + .relayState(request.getParameter("RelayState")) |
| 76 | + .build(); |
| 77 | + } |
| 78 | + |
| 79 | + private Function<String, String> templateResolver(String applicationUri, RelyingPartyRegistration relyingParty) { |
| 80 | + return template -> resolveUrlTemplate(template, applicationUri, relyingParty); |
| 81 | + } |
| 82 | + |
| 83 | + private static String resolveUrlTemplate(String template, String baseUrl, RelyingPartyRegistration relyingParty) { |
| 84 | + if (!StringUtils.hasText(template)) { |
| 85 | + return baseUrl; |
| 86 | + } |
| 87 | + |
| 88 | + String entityId = relyingParty.getProviderDetails().getEntityId(); |
| 89 | + String registrationId = relyingParty.getRegistrationId(); |
| 90 | + Map<String, String> uriVariables = new HashMap<>(); |
| 91 | + UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(baseUrl) |
| 92 | + .replaceQuery(null) |
| 93 | + .fragment(null) |
| 94 | + .build(); |
| 95 | + String scheme = uriComponents.getScheme(); |
| 96 | + uriVariables.put("baseScheme", scheme == null ? "" : scheme); |
| 97 | + String host = uriComponents.getHost(); |
| 98 | + uriVariables.put("baseHost", host == null ? "" : host); |
| 99 | + // following logic is based on HierarchicalUriComponents#toUriString() |
| 100 | + int port = uriComponents.getPort(); |
| 101 | + uriVariables.put("basePort", port == -1 ? "" : ":" + port); |
| 102 | + String path = uriComponents.getPath(); |
| 103 | + if (StringUtils.hasLength(path)) { |
| 104 | + if (path.charAt(0) != PATH_DELIMITER) { |
| 105 | + path = PATH_DELIMITER + path; |
| 106 | + } |
| 107 | + } |
| 108 | + uriVariables.put("basePath", path == null ? "" : path); |
| 109 | + uriVariables.put("baseUrl", uriComponents.toUriString()); |
| 110 | + uriVariables.put("entityId", StringUtils.hasText(entityId) ? entityId : ""); |
| 111 | + uriVariables.put("registrationId", StringUtils.hasText(registrationId) ? registrationId : ""); |
| 112 | + |
| 113 | + return UriComponentsBuilder.fromUriString(template) |
| 114 | + .buildAndExpand(uriVariables) |
| 115 | + .toUriString(); |
| 116 | + } |
| 117 | + |
| 118 | + private static String getApplicationUri(HttpServletRequest request) { |
| 119 | + UriComponents uriComponents = fromHttpUrl(buildFullRequestUrl(request)) |
| 120 | + .replacePath(request.getContextPath()) |
| 121 | + .replaceQuery(null) |
| 122 | + .fragment(null) |
| 123 | + .build(); |
| 124 | + return uriComponents.toUriString(); |
| 125 | + } |
| 126 | +} |
0 commit comments