| | |
| | | return redirectUri; |
| | | } |
| | | |
| | | // Decode redirectUri. We don't decode query and fragment as those can be encoded in the original URL. |
| | | // Decode redirectUri. Only path is decoded as other elements can be encoded in the original URL or cannot be encoded at all. |
| | | // URL can be decoded multiple times (in case it was encoded multiple times, or some of it's parts were encoded multiple times) |
| | | private static String decodeRedirectUri(String redirectUri) { |
| | | if (redirectUri == null) return null; |
| | |
| | | |
| | | try { |
| | | KeycloakUriBuilder uriBuilder = KeycloakUriBuilder.fromUri(redirectUri, false).preserveDefaultPort(); |
| | | String origQuery = uriBuilder.getQuery(); |
| | | String origFragment = uriBuilder.getFragment(); |
| | | String origUserInfo = uriBuilder.getUserInfo(); |
| | | String encodedRedirectUri = uriBuilder |
| | | .replaceQuery(null) |
| | | .fragment(null) |
| | | .userInfo(null) |
| | | .buildAsString(); |
| | | String decodedRedirectUri = null; |
| | | if (uriBuilder.getPath() == null) { |
| | | return redirectUri; |
| | | } |
| | | String encodedPath = uriBuilder.getPath(); |
| | | String decodedPath; |
| | | |
| | | for (int i = 0; i < MAX_DECODING_COUNT; i++) { |
| | | decodedRedirectUri = Encode.decode(encodedRedirectUri); |
| | | if (decodedRedirectUri.equals(encodedRedirectUri)) { |
| | | // URL is decoded. We can return it (after attach original query and fragment) |
| | | return KeycloakUriBuilder.fromUri(decodedRedirectUri, false).preserveDefaultPort() |
| | | .replaceQuery(origQuery) |
| | | .fragment(origFragment) |
| | | .userInfo(origUserInfo) |
| | | .buildAsString(); |
| | | decodedPath = Encode.decode(encodedPath); |
| | | if (decodedPath.equals(encodedPath)) { |
| | | // URL path is decoded. We can return it in the original redirect URI |
| | | return uriBuilder.replacePath(decodedPath, false).buildAsString(); |
| | | } else { |
| | | // Next attempt |
| | | encodedRedirectUri = decodedRedirectUri; |
| | | encodedPath = decodedPath; |
| | | } |
| | | } |
| | | } catch (IllegalArgumentException iae) { |
| | |
| | | return sb.toString(); |
| | | } |
| | | |
| | | // removes the queryString, fragment and userInfo from the redirect |
| | | // to avoid comparing this when wildcards are used |
| | | private static String stripOffRedirectForWildcard(String redirect) { |
| | | return KeycloakUriBuilder.fromUri(redirect, false) |
| | | .preserveDefaultPort() |
| | | .userInfo(null) |
| | | .replaceQuery(null) |
| | | .fragment(null) |
| | | .buildAsString(); |
| | | } |
| | | |
| | | // return the String that matched the redirect or null if not matched |
| | | private static String matchesRedirects(Set<String> validRedirects, String redirect, boolean allowWildcards) { |
| | | logger.tracef("matchesRedirects: redirect URL to check: %s, allow wildcards: %b, Configured valid redirect URLs: %s", redirect, allowWildcards, validRedirects); |
| | | for (String validRedirect : validRedirects) { |
| | | if (validRedirect.endsWith("*") && !validRedirect.contains("?") && allowWildcards) { |
| | | // strip off the query component - we don't check them when wildcards are effective |
| | | String r = redirect.contains("?") ? redirect.substring(0, redirect.indexOf("?")) : redirect; |
| | | // strip off the userInfo, query or fragment components - we don't check them when wildcards are effective |
| | | String r = stripOffRedirectForWildcard(redirect); |
| | | // strip off * |
| | | int length = validRedirect.length() - 1; |
| | | validRedirect = validRedirect.substring(0, length); |
| | |
| | | } |
| | | return redirectUri; |
| | | } |
| | | |
| | | private static String getFirstValidRedirectUri(Collection<String> validRedirects) { |
| | | final String redirectUri = validRedirects.stream().findFirst().orElse(null); |
| | | return (redirectUri != null) ? validateRedirectUriWildcard(redirectUri) : null; |
| | | } |
| | | |
| | | public static String getFirstValidRedirectUri(KeycloakSession session, String rootUrl, Set<String> validRedirects) { |
| | | return getFirstValidRedirectUri(resolveValidRedirects(session, rootUrl, validRedirects)); |
| | | } |
| | | } |