/
UaaUrlUtils.java
198 lines (174 loc) · 7.64 KB
/
UaaUrlUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* ****************************************************************************
* Cloud Foundry
* Copyright (c) [2009-2015] Pivotal Software, Inc. All Rights Reserved.
* <p/>
* This product is licensed to you under the Apache License, Version 2.0 (the "License").
* You may not use this product except in compliance with the License.
* <p/>
* This product includes a number of subcomponents with
* separate copyright notices and license terms. Your use of these
* subcomponents is subject to the terms and conditions of the
* subcomponent's license, as noted in the LICENSE file.
* *****************************************************************************
*/
package org.cloudfoundry.identity.uaa.util;
import org.cloudfoundry.identity.uaa.zone.IdentityZone;
import org.cloudfoundry.identity.uaa.zone.IdentityZoneHolder;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static java.util.Collections.emptyList;
import static java.util.Optional.ofNullable;
public abstract class UaaUrlUtils {
public static String getUaaUrl() {
return getUaaUrl("");
}
public static String getUaaUrl(String path) {
return getUaaUrl(path, false);
}
public static String getUaaUrl(String path, boolean zoneSwitchPossible) {
return getURIBuilder(path, zoneSwitchPossible).build().toUriString();
}
public static String getUaaHost() {
return getURIBuilder("").build().getHost();
}
public static UriComponentsBuilder getURIBuilder(String path) {
return getURIBuilder(path, false);
}
public static UriComponentsBuilder getURIBuilder(String path, boolean zoneSwitchPossible) {
UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath().path(path);
if (zoneSwitchPossible) {
String host = builder.build().getHost();
IdentityZone current = IdentityZoneHolder.get();
if (host != null && !IdentityZoneHolder.isUaa()) {
if (!host.startsWith(current.getSubdomain() + ".")) {
host = current.getSubdomain() + "." + host;
builder.host(host);
}
}
}
return builder;
}
/**
* Finds and returns a matching redirect URL according to the following logic:
* <ul>
* <li>If the requstedRedirectUri matches the whitelist the requestedRedirectUri is returned</li>
* <li>If the whitelist is null or empty AND the fallbackRedirectUri is null, the requestedRedirectUri is returned - OPEN REDIRECT</li>
* <li>If the whitelist is null or empty AND the fallbackRedirectUri is not null, the fallbackRedirectUri is returned</li>
* </ul>
* @param redirectUris - a whitelist collection of ant path patterns
* @param requestedRedirectUri - the requested redirect URI, returned if whitelist matches or the fallbackRedirectUri is null
* @param fallbackRedirectUri - returned if non null and the requestedRedirectUri doesn't match the whitelist redirectUris
* @return a redirect URI, either the requested or fallback as described above
*/
public static String findMatchingRedirectUri(Collection<String> redirectUris, String requestedRedirectUri, String fallbackRedirectUri) {
AntPathMatcher matcher = new AntPathMatcher();
for (String pattern : ofNullable(redirectUris).orElse(emptyList())) {
if (matcher.match(pattern, requestedRedirectUri)) {
return requestedRedirectUri;
}
}
return ofNullable(fallbackRedirectUri).orElse(requestedRedirectUri);
}
public static String getHostForURI(String uri) {
UriComponentsBuilder b = UriComponentsBuilder.fromHttpUrl(uri);
return b.build().getHost();
}
public static String getBaseURL(HttpServletRequest request) {
//returns scheme, host and context path
//for example http://localhost:8080/uaa or http://login.identity.cf-app.com
String requestURL = request.getRequestURL().toString();
return StringUtils.hasText(request.getServletPath()) ?
requestURL.substring(0, requestURL.indexOf(request.getServletPath())) :
requestURL;
}
public static Map<String, String[]> getParameterMap(String uri) {
UriComponentsBuilder b = UriComponentsBuilder.fromUriString(uri);
MultiValueMap<String, String> map = b.build().getQueryParams();
Map<String, String[]> result= new HashMap<>();
map
.entrySet()
.stream()
.forEach(
e -> result.put(e.getKey(), decodeValue(e.getValue()))
);
return result;
}
public static String[] decodeValue(List<String> value) {
if (value==null) {
return null;
}
String[] result = new String[value.size()];
int pos = 0;
for (String s : value) {
try {
result[pos++] = UriUtils.decode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException(s, e);
}
}
return result;
}
public static boolean isUrl(String url) {
try {
new URL(url);
return true;
} catch (MalformedURLException e) {
return false;
}
}
public static String addQueryParameter(String url, String name, String value) {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url);
builder.queryParam(name,value);
return builder.build().toUriString();
}
public static String addFragmentComponent(String urlString, String component) {
URI uri = URI.create(urlString);
UriComponentsBuilder builder = UriComponentsBuilder.fromUri(uri);
builder.fragment(StringUtils.hasText(uri.getFragment()) ? uri.getFragment() + "&" + component : component);
return builder.build().toUriString();
}
public static String addSubdomainToUrl(String url) {
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
builder.host(getSubdomain() + builder.build().getHost());
return builder.build().toUriString();
}
public static String getSubdomain() {
String subdomain = IdentityZoneHolder.get().getSubdomain();
if (StringUtils.hasText(subdomain)) {
subdomain += ".";
}
return subdomain.trim();
}
public static String extractPathVariableFromUrl(int pathParameterIndex, String path) {
if (path.startsWith("/")) {
path = path.substring(1);
}
String[] paths = StringUtils.delimitedListToStringArray(path, "/");
if (paths.length!=0 && pathParameterIndex<paths.length) {
return paths[pathParameterIndex];
}
return null;
}
public static String getRequestPath(HttpServletRequest request) {
String servletPath = request.getServletPath();
String pathInfo = request.getPathInfo();
if(servletPath == null) { servletPath = ""; }
if(pathInfo == null) { pathInfo = ""; }
String path = String.format("%s%s", servletPath, pathInfo);
return path;
}
}