| | |
| | | |
| | | package org.keycloak.protocol.oidc.utils; |
| | | |
| | | import org.jboss.logging.Logger; |
| | | import org.keycloak.common.util.UriUtils; |
| | | import org.keycloak.models.ClientModel; |
| | | import org.keycloak.models.Constants; |
| | | import org.keycloak.models.KeycloakSession; |
| | | import org.keycloak.models.KeycloakUriInfo; |
| | | import org.keycloak.models.RealmModel; |
| | | import org.keycloak.protocol.oidc.OIDCLoginProtocol; |
| | | import org.keycloak.services.Urls; |
| | | import org.keycloak.services.ServicesLogger; |
| | | import org.keycloak.services.util.ResolveRelative; |
| | | |
| | | import javax.ws.rs.core.UriInfo; |
| | | import java.net.URI; |
| | | import java.util.Collection; |
| | | import java.util.HashSet; |
| | | import java.util.Set; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a> |
| | | */ |
| | | public class RedirectUtils { |
| | | |
| | | private static final ServicesLogger logger = ServicesLogger.ROOT_LOGGER; |
| | | private static final Logger logger = Logger.getLogger(RedirectUtils.class); |
| | | |
| | | public static String verifyRealmRedirectUri(UriInfo uriInfo, String redirectUri, RealmModel realm) { |
| | | Set<String> validRedirects = getValidateRedirectUris(uriInfo, realm); |
| | | return verifyRedirectUri(uriInfo, null, redirectUri, realm, validRedirects); |
| | | public static String verifyRealmRedirectUri(KeycloakSession session, String redirectUri) { |
| | | Set<String> validRedirects = getValidateRedirectUris(session); |
| | | return verifyRedirectUri(session, null, redirectUri, validRedirects, true); |
| | | } |
| | | |
| | | public static String verifyRedirectUri(UriInfo uriInfo, String redirectUri, RealmModel realm, ClientModel client) { |
| | | Set<String> validRedirects = client.getRedirectUris(); |
| | | return verifyRedirectUri(uriInfo, client.getRootUrl(), redirectUri, realm, validRedirects); |
| | | public static String verifyRedirectUri(KeycloakSession session, String redirectUri, ClientModel client) { |
| | | return verifyRedirectUri(session, redirectUri, client, true); |
| | | } |
| | | |
| | | public static Set<String> resolveValidRedirects(UriInfo uriInfo, String rootUrl, Set<String> validRedirects) { |
| | | public static String verifyRedirectUri(KeycloakSession session, String redirectUri, ClientModel client, boolean requireRedirectUri) { |
| | | if (client != null) |
| | | return verifyRedirectUri(session, client.getRootUrl(), redirectUri, client.getRedirectUris(), requireRedirectUri); |
| | | return null; |
| | | } |
| | | |
| | | public static Set<String> resolveValidRedirects(KeycloakSession session, String rootUrl, Set<String> validRedirects) { |
| | | // If the valid redirect URI is relative (no scheme, host, port) then use the request's scheme, host, and port |
| | | Set<String> resolveValidRedirects = new HashSet<String>(); |
| | | Set<String> resolveValidRedirects = new HashSet<>(); |
| | | for (String validRedirect : validRedirects) { |
| | | resolveValidRedirects.add(validRedirect); // add even relative urls. |
| | | if (validRedirect.startsWith("/")) { |
| | | validRedirect = relativeToAbsoluteURI(uriInfo, rootUrl, validRedirect); |
| | | validRedirect = relativeToAbsoluteURI(session, rootUrl, validRedirect); |
| | | logger.debugv("replacing relative valid redirect with: {0}", validRedirect); |
| | | resolveValidRedirects.add(validRedirect); |
| | | } else { |
| | | resolveValidRedirects.add(validRedirect); |
| | | } |
| | | } |
| | | return resolveValidRedirects; |
| | | } |
| | | |
| | | private static Set<String> getValidateRedirectUris(UriInfo uriInfo, RealmModel realm) { |
| | | Set<String> redirects = new HashSet<>(); |
| | | for (ClientModel client : realm.getClients()) { |
| | | redirects.addAll(resolveValidRedirects(uriInfo, client.getRootUrl(), client.getRedirectUris())); |
| | | } |
| | | return redirects; |
| | | private static Set<String> getValidateRedirectUris(KeycloakSession session) { |
| | | RealmModel realm = session.getContext().getRealm(); |
| | | return session.clientStorageManager().getAllRedirectUrisOfEnabledClients(realm).entrySet().stream() |
| | | .filter(me -> me.getKey().isEnabled() && OIDCLoginProtocol.LOGIN_PROTOCOL.equals(me.getKey().getProtocol()) && !me.getKey().isBearerOnly() && (me.getKey().isStandardFlowEnabled() || me.getKey().isImplicitFlowEnabled())) |
| | | .map(me -> resolveValidRedirects(session, me.getKey().getRootUrl(), me.getValue())) |
| | | .flatMap(Collection::stream) |
| | | .collect(Collectors.toSet()); |
| | | } |
| | | |
| | | private static String verifyRedirectUri(UriInfo uriInfo, String rootUrl, String redirectUri, RealmModel realm, Set<String> validRedirects) { |
| | | public static String verifyRedirectUri(KeycloakSession session, String rootUrl, String redirectUri, Set<String> validRedirects, boolean requireRedirectUri) { |
| | | KeycloakUriInfo uriInfo = session.getContext().getUri(); |
| | | RealmModel realm = session.getContext().getRealm(); |
| | | |
| | | if (redirectUri != null) { |
| | | try { |
| | | URI uri = URI.create(redirectUri); |
| | | redirectUri = uri.normalize().toString(); |
| | | } catch (IllegalArgumentException cause) { |
| | | logger.debug("Invalid redirect uri", cause); |
| | | return null; |
| | | } catch (Exception cause) { |
| | | logger.debug("Unexpected error when parsing redirect uri", cause); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | if (redirectUri == null) { |
| | | logger.debug("No Redirect URI parameter specified"); |
| | | return null; |
| | | if (!requireRedirectUri) { |
| | | redirectUri = getSingleValidRedirectUri(validRedirects); |
| | | } |
| | | |
| | | if (redirectUri == null) { |
| | | logger.debug("No Redirect URI parameter specified"); |
| | | return null; |
| | | } |
| | | } else if (validRedirects.isEmpty()) { |
| | | logger.debug("No Redirect URIs supplied"); |
| | | redirectUri = null; |
| | | } else { |
| | | redirectUri = lowerCaseHostname(redirectUri); |
| | | |
| | | String r = redirectUri.indexOf('?') != -1 ? redirectUri.substring(0, redirectUri.indexOf('?')) : redirectUri; |
| | | Set<String> resolveValidRedirects = resolveValidRedirects(uriInfo, rootUrl, validRedirects); |
| | | String r = redirectUri; |
| | | Set<String> resolveValidRedirects = resolveValidRedirects(session, rootUrl, validRedirects); |
| | | |
| | | boolean valid = matchesRedirects(resolveValidRedirects, r); |
| | | |
| | | if (!valid && r.startsWith(Constants.INSTALLED_APP_URL) && r.indexOf(':', Constants.INSTALLED_APP_URL.length()) >= 0) { |
| | | if (!valid && (r.startsWith(Constants.INSTALLED_APP_URL) || r.startsWith(Constants.INSTALLED_APP_LOOPBACK)) && r.indexOf(':', Constants.INSTALLED_APP_URL.length()) >= 0) { |
| | | int i = r.indexOf(':', Constants.INSTALLED_APP_URL.length()); |
| | | |
| | | StringBuilder sb = new StringBuilder(); |
| | |
| | | valid = matchesRedirects(resolveValidRedirects, r); |
| | | } |
| | | if (valid && redirectUri.startsWith("/")) { |
| | | redirectUri = relativeToAbsoluteURI(uriInfo, rootUrl, redirectUri); |
| | | redirectUri = relativeToAbsoluteURI(session, rootUrl, redirectUri); |
| | | } |
| | | redirectUri = valid ? redirectUri : null; |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | private static String relativeToAbsoluteURI(UriInfo uriInfo, String rootUrl, String relative) { |
| | | if (rootUrl == null || rootUrl.isEmpty()) { |
| | | URI baseUri = uriInfo.getBaseUri(); |
| | | String uri = baseUri.getScheme() + "://" + baseUri.getHost(); |
| | | if (baseUri.getPort() != -1) { |
| | | uri += ":" + baseUri.getPort(); |
| | | } |
| | | rootUrl = uri; |
| | | private static String relativeToAbsoluteURI(KeycloakSession session, String rootUrl, String relative) { |
| | | if (rootUrl != null) { |
| | | rootUrl = ResolveRelative.resolveRootUrl(session, rootUrl); |
| | | } |
| | | relative = rootUrl + relative; |
| | | return relative; |
| | | |
| | | if (rootUrl == null || rootUrl.isEmpty()) { |
| | | rootUrl = UriUtils.getOrigin(session.getContext().getUri().getBaseUri()); |
| | | } |
| | | StringBuilder sb = new StringBuilder(); |
| | | sb.append(rootUrl); |
| | | sb.append(relative); |
| | | return sb.toString(); |
| | | } |
| | | |
| | | private static boolean matchesRedirects(Set<String> validRedirects, String redirect) { |
| | | for (String validRedirect : validRedirects) { |
| | | if (validRedirect.endsWith("*")) { |
| | | if (validRedirect.endsWith("*") && !validRedirect.contains("?")) { |
| | | // 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 * |
| | | int length = validRedirect.length() - 1; |
| | | validRedirect = validRedirect.substring(0, length); |
| | | if (redirect.startsWith(validRedirect)) return true; |
| | | if (r.startsWith(validRedirect)) return true; |
| | | // strip off trailing '/' |
| | | if (length - 1 > 0 && validRedirect.charAt(length - 1) == '/') length--; |
| | | validRedirect = validRedirect.substring(0, length); |
| | | if (validRedirect.equals(redirect)) return true; |
| | | if (validRedirect.equals(r)) return true; |
| | | } else if (validRedirect.equals(redirect)) return true; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | private static String getSingleValidRedirectUri(Collection<String> validRedirects) { |
| | | if (validRedirects.size() != 1) return null; |
| | | String validRedirect = validRedirects.iterator().next(); |
| | | return validateRedirectUriWildcard(validRedirect); |
| | | } |
| | | |
| | | public static String validateRedirectUriWildcard(String redirectUri) { |
| | | if (redirectUri == null) |
| | | return null; |
| | | |
| | | int idx = redirectUri.indexOf("/*"); |
| | | if (idx > -1) { |
| | | redirectUri = redirectUri.substring(0, idx); |
| | | } |
| | | 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)); |
| | | } |
| | | } |