|
| 1 | +/* |
| 2 | + * Licensed to Apereo under one or more contributor license |
| 3 | + * agreements. See the NOTICE file distributed with this work |
| 4 | + * for additional information regarding copyright ownership. |
| 5 | + * Apereo licenses this file to you under the Apache License, |
| 6 | + * Version 2.0 (the "License"); you may not use this file |
| 7 | + * except in compliance with the License. You may obtain a |
| 8 | + * copy of the License at the following location: |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.springframework.security.cas.web; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.net.URLEncoder; |
| 24 | +import java.nio.charset.StandardCharsets; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.HashSet; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Set; |
| 29 | + |
| 30 | +import jakarta.servlet.http.HttpServletRequest; |
| 31 | +import jakarta.servlet.http.HttpServletResponse; |
| 32 | + |
| 33 | +import org.jasig.cas.client.Protocol; |
| 34 | +import org.jasig.cas.client.proxy.ProxyGrantingTicketStorage; |
| 35 | +import org.jasig.cas.client.util.URIBuilder; |
| 36 | + |
| 37 | +import org.springframework.util.StringUtils; |
| 38 | + |
| 39 | +final class CommonUtils { |
| 40 | + |
| 41 | + private static final String PARAM_PROXY_GRANTING_TICKET_IOU = "pgtIou"; |
| 42 | + |
| 43 | + /** |
| 44 | + * Constant representing the ProxyGrantingTicket Request Parameter. |
| 45 | + */ |
| 46 | + private static final String PARAM_PROXY_GRANTING_TICKET = "pgtId"; |
| 47 | + |
| 48 | + private static final String SERVICE_PARAMETER_NAMES; |
| 49 | + |
| 50 | + private CommonUtils() { |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + static { |
| 55 | + final Set<String> serviceParameterSet = new HashSet<String>(4); |
| 56 | + for (final Protocol protocol : Protocol.values()) { |
| 57 | + serviceParameterSet.add(protocol.getServiceParameterName()); |
| 58 | + } |
| 59 | + SERVICE_PARAMETER_NAMES = serviceParameterSet.toString().replaceAll("\\[|\\]", "").replaceAll("\\s", ""); |
| 60 | + } |
| 61 | + |
| 62 | + static String constructServiceUrl(final HttpServletRequest request, final HttpServletResponse response, |
| 63 | + final String service, final String serverNames, final String artifactParameterName, final boolean encode) { |
| 64 | + if (StringUtils.hasText(service)) { |
| 65 | + return encode ? response.encodeURL(service) : service; |
| 66 | + } |
| 67 | + |
| 68 | + final String serverName = findMatchingServerName(request, serverNames); |
| 69 | + final URIBuilder originalRequestUrl = new URIBuilder(request.getRequestURL().toString(), encode); |
| 70 | + originalRequestUrl.setParameters(request.getQueryString()); |
| 71 | + |
| 72 | + final URIBuilder builder; |
| 73 | + if (!serverName.startsWith("https://") && !serverName.startsWith("http://")) { |
| 74 | + final String scheme = request.isSecure() ? "https://" : "http://"; |
| 75 | + builder = new URIBuilder(scheme + serverName, encode); |
| 76 | + } |
| 77 | + else { |
| 78 | + builder = new URIBuilder(serverName, encode); |
| 79 | + } |
| 80 | + |
| 81 | + if (builder.getPort() == -1 && !requestIsOnStandardPort(request)) { |
| 82 | + builder.setPort(request.getServerPort()); |
| 83 | + } |
| 84 | + |
| 85 | + builder.setEncodedPath(builder.getEncodedPath() + request.getRequestURI()); |
| 86 | + |
| 87 | + final List<String> serviceParameterNames = Arrays.asList(SERVICE_PARAMETER_NAMES.split(",")); |
| 88 | + if (!serviceParameterNames.isEmpty() && !originalRequestUrl.getQueryParams().isEmpty()) { |
| 89 | + for (final URIBuilder.BasicNameValuePair pair : originalRequestUrl.getQueryParams()) { |
| 90 | + final String name = pair.getName(); |
| 91 | + if (!name.equals(artifactParameterName) && !serviceParameterNames.contains(name)) { |
| 92 | + if (name.contains("&") || name.contains("=")) { |
| 93 | + final URIBuilder encodedParamBuilder = new URIBuilder(); |
| 94 | + encodedParamBuilder.setParameters(name); |
| 95 | + for (final URIBuilder.BasicNameValuePair pair2 : encodedParamBuilder.getQueryParams()) { |
| 96 | + final String name2 = pair2.getName(); |
| 97 | + if (!name2.equals(artifactParameterName) && !serviceParameterNames.contains(name2)) { |
| 98 | + builder.addParameter(name2, pair2.getValue()); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + else { |
| 103 | + builder.addParameter(name, pair.getValue()); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + final String result = builder.toString(); |
| 110 | + final String returnValue = encode ? response.encodeURL(result) : result; |
| 111 | + return returnValue; |
| 112 | + } |
| 113 | + |
| 114 | + static String constructRedirectUrl(final String casServerLoginUrl, final String serviceParameterName, |
| 115 | + final String serviceUrl, final boolean renew, final boolean gateway, final String method) { |
| 116 | + return casServerLoginUrl + (casServerLoginUrl.contains("?") ? "&" : "?") + serviceParameterName + "=" |
| 117 | + + urlEncode(serviceUrl) + (renew ? "&renew=true" : "") + (gateway ? "&gateway=true" : "") |
| 118 | + + ((method != null) ? "&method=" + method : ""); |
| 119 | + } |
| 120 | + |
| 121 | + static String urlEncode(final String value) { |
| 122 | + return URLEncoder.encode(value, StandardCharsets.UTF_8); |
| 123 | + } |
| 124 | + |
| 125 | + static void readAndRespondToProxyReceptorRequest(final HttpServletRequest request, |
| 126 | + final HttpServletResponse response, final ProxyGrantingTicketStorage proxyGrantingTicketStorage) |
| 127 | + throws IOException { |
| 128 | + final String proxyGrantingTicketIou = request.getParameter(PARAM_PROXY_GRANTING_TICKET_IOU); |
| 129 | + |
| 130 | + final String proxyGrantingTicket = request.getParameter(PARAM_PROXY_GRANTING_TICKET); |
| 131 | + |
| 132 | + if (org.jasig.cas.client.util.CommonUtils.isBlank(proxyGrantingTicket) |
| 133 | + || org.jasig.cas.client.util.CommonUtils.isBlank(proxyGrantingTicketIou)) { |
| 134 | + response.getWriter().write(""); |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + proxyGrantingTicketStorage.save(proxyGrantingTicketIou, proxyGrantingTicket); |
| 139 | + |
| 140 | + response.getWriter().write("<?xml version=\"1.0\"?>"); |
| 141 | + response.getWriter().write("<casClient:proxySuccess xmlns:casClient=\"https://www.yale.edu/tp/casClient\" />"); |
| 142 | + } |
| 143 | + |
| 144 | + private static String findMatchingServerName(final HttpServletRequest request, final String serverName) { |
| 145 | + final String[] serverNames = serverName.split(" "); |
| 146 | + |
| 147 | + if (serverNames.length == 0 || serverNames.length == 1) { |
| 148 | + return serverName; |
| 149 | + } |
| 150 | + |
| 151 | + final String host = request.getHeader("Host"); |
| 152 | + final String xHost = request.getHeader("X-Forwarded-Host"); |
| 153 | + |
| 154 | + final String comparisonHost; |
| 155 | + comparisonHost = (xHost != null) ? xHost : host; |
| 156 | + |
| 157 | + if (comparisonHost == null) { |
| 158 | + return serverName; |
| 159 | + } |
| 160 | + |
| 161 | + for (final String server : serverNames) { |
| 162 | + final String lowerCaseServer = server.toLowerCase(); |
| 163 | + |
| 164 | + if (lowerCaseServer.contains(comparisonHost)) { |
| 165 | + return server; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + return serverNames[0]; |
| 170 | + } |
| 171 | + |
| 172 | + private static boolean requestIsOnStandardPort(final HttpServletRequest request) { |
| 173 | + final int serverPort = request.getServerPort(); |
| 174 | + return serverPort == 80 || serverPort == 443; |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments