|
| 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.saml2.provider.service.web; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import jakarta.servlet.http.HttpServletRequest; |
| 23 | + |
| 24 | +import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration; |
| 25 | +import org.springframework.security.web.util.UrlUtils; |
| 26 | +import org.springframework.util.StringUtils; |
| 27 | +import org.springframework.web.util.UriComponents; |
| 28 | +import org.springframework.web.util.UriComponentsBuilder; |
| 29 | + |
| 30 | +/** |
| 31 | + * A factory for creating placeholder resolvers for {@link RelyingPartyRegistration} |
| 32 | + * templates. Supports {@code baseUrl}, {@code baseScheme}, {@code baseHost}, |
| 33 | + * {@code basePort}, {@code basePath}, {@code registrationId}, |
| 34 | + * {@code relyingPartyEntityId}, and {@code assertingPartyEntityId} |
| 35 | + * |
| 36 | + * @author Josh Cummings |
| 37 | + * @since 6.1 |
| 38 | + */ |
| 39 | +public final class RelyingPartyRegistrationPlaceholderResolvers { |
| 40 | + |
| 41 | + private static final char PATH_DELIMITER = '/'; |
| 42 | + |
| 43 | + private RelyingPartyRegistrationPlaceholderResolvers() { |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Create a resolver based on the given {@link HttpServletRequest}. Given the request, |
| 49 | + * placeholders {@code baseUrl}, {@code baseScheme}, {@code baseHost}, |
| 50 | + * {@code basePort}, and {@code basePath} are resolved. |
| 51 | + * @param request the HTTP request |
| 52 | + * @return a resolver that can resolve {@code baseUrl}, {@code baseScheme}, |
| 53 | + * {@code baseHost}, {@code basePort}, and {@code basePath} placeholders |
| 54 | + */ |
| 55 | + public static UriResolver uriResolver(HttpServletRequest request) { |
| 56 | + return new UriResolver(uriVariables(request)); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Create a resolver based on the given {@link HttpServletRequest}. Given the request, |
| 61 | + * placeholders {@code baseUrl}, {@code baseScheme}, {@code baseHost}, |
| 62 | + * {@code basePort}, {@code basePath}, {@code registrationId}, |
| 63 | + * {@code assertingPartyEntityId}, and {@code relyingPartyEntityId} are resolved. |
| 64 | + * @param request the HTTP request |
| 65 | + * @return a resolver that can resolve {@code baseUrl}, {@code baseScheme}, |
| 66 | + * {@code baseHost}, {@code basePort}, {@code basePath}, {@code registrationId}, |
| 67 | + * {@code relyingPartyEntityId}, and {@code assertingPartyEntityId} placeholders |
| 68 | + */ |
| 69 | + public static UriResolver uriResolver(HttpServletRequest request, RelyingPartyRegistration registration) { |
| 70 | + String relyingPartyEntityId = registration.getEntityId(); |
| 71 | + String assertingPartyEntityId = registration.getAssertingPartyDetails().getEntityId(); |
| 72 | + String registrationId = registration.getRegistrationId(); |
| 73 | + Map<String, String> uriVariables = uriVariables(request); |
| 74 | + uriVariables.put("relyingPartyEntityId", StringUtils.hasText(relyingPartyEntityId) ? relyingPartyEntityId : ""); |
| 75 | + uriVariables.put("assertingPartyEntityId", |
| 76 | + StringUtils.hasText(assertingPartyEntityId) ? assertingPartyEntityId : ""); |
| 77 | + uriVariables.put("entityId", StringUtils.hasText(assertingPartyEntityId) ? assertingPartyEntityId : ""); |
| 78 | + uriVariables.put("registrationId", StringUtils.hasText(registrationId) ? registrationId : ""); |
| 79 | + return new UriResolver(uriVariables); |
| 80 | + } |
| 81 | + |
| 82 | + private static Map<String, String> uriVariables(HttpServletRequest request) { |
| 83 | + String baseUrl = getApplicationUri(request); |
| 84 | + Map<String, String> uriVariables = new HashMap<>(); |
| 85 | + UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(baseUrl).replaceQuery(null).fragment(null) |
| 86 | + .build(); |
| 87 | + String scheme = uriComponents.getScheme(); |
| 88 | + uriVariables.put("baseScheme", (scheme != null) ? scheme : ""); |
| 89 | + String host = uriComponents.getHost(); |
| 90 | + uriVariables.put("baseHost", (host != null) ? host : ""); |
| 91 | + // following logic is based on HierarchicalUriComponents#toUriString() |
| 92 | + int port = uriComponents.getPort(); |
| 93 | + uriVariables.put("basePort", (port == -1) ? "" : ":" + port); |
| 94 | + String path = uriComponents.getPath(); |
| 95 | + if (StringUtils.hasLength(path) && path.charAt(0) != PATH_DELIMITER) { |
| 96 | + path = PATH_DELIMITER + path; |
| 97 | + } |
| 98 | + uriVariables.put("basePath", (path != null) ? path : ""); |
| 99 | + uriVariables.put("baseUrl", uriComponents.toUriString()); |
| 100 | + return uriVariables; |
| 101 | + } |
| 102 | + |
| 103 | + private static String getApplicationUri(HttpServletRequest request) { |
| 104 | + UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(UrlUtils.buildFullRequestUrl(request)) |
| 105 | + .replacePath(request.getContextPath()).replaceQuery(null).fragment(null).build(); |
| 106 | + return uriComponents.toUriString(); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * A class for resolving {@link RelyingPartyRegistration} URIs |
| 111 | + */ |
| 112 | + public static final class UriResolver { |
| 113 | + |
| 114 | + private final Map<String, String> uriVariables; |
| 115 | + |
| 116 | + private UriResolver(Map<String, String> uriVariables) { |
| 117 | + this.uriVariables = uriVariables; |
| 118 | + } |
| 119 | + |
| 120 | + public String resolve(String uri) { |
| 121 | + if (uri == null) { |
| 122 | + return null; |
| 123 | + } |
| 124 | + return UriComponentsBuilder.fromUriString(uri).buildAndExpand(this.uriVariables).toUriString(); |
| 125 | + } |
| 126 | + |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments