| | |
| | | |
| | | package org.keycloak.protocol.oidc.utils; |
| | | |
| | | import org.jboss.logging.Logger; |
| | | import java.net.URI; |
| | | import java.util.Arrays; |
| | | import java.util.Collection; |
| | | import java.util.HashSet; |
| | | import java.util.Set; |
| | | import java.util.TreeSet; |
| | | import java.util.regex.Pattern; |
| | | |
| | | import org.keycloak.common.util.KeycloakUriBuilder; |
| | | 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.util.ResolveRelative; |
| | | |
| | | import java.net.URI; |
| | | import java.util.Collection; |
| | | import java.util.Set; |
| | | import java.util.TreeSet; |
| | | import java.util.regex.Pattern; |
| | | import java.util.stream.Collectors; |
| | | import org.jboss.logging.Logger; |
| | | |
| | | /** |
| | | * @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a> |
| | | */ |
| | | public class RedirectUtils { |
| | | |
| | | private static final Logger logger = Logger.getLogger(RedirectUtils.class); |
| | | public static final Set<String> LOOPBACK_INTERFACES = new HashSet<>(Arrays.asList("localhost", "127.0.0.1", "[::1]")); |
| | | |
| | | /** |
| | | * This method is deprecated for performance and security reasons and it is available just for the |
| | | * backwards compatibility. It is recommended to use some other methods of this class where the client is given as an argument |
| | | * to the method, so we know the client, which redirect-uri we are trying to resolve. |
| | | */ |
| | | @Deprecated |
| | | public static String verifyRealmRedirectUri(KeycloakSession session, String redirectUri) { |
| | | Set<String> validRedirects = getValidateRedirectUris(session); |
| | | return verifyRedirectUri(session, null, redirectUri, validRedirects, true); |
| | | } |
| | | private static final Logger logger = Logger.getLogger(RedirectUtils.class); |
| | | |
| | | public static String verifyRedirectUri(KeycloakSession session, String redirectUri, ClientModel client) { |
| | | return verifyRedirectUri(session, redirectUri, client, true); |
| | |
| | | return resolveValidRedirects; |
| | | } |
| | | |
| | | @Deprecated |
| | | private static Set<String> getValidateRedirectUris(KeycloakSession session) { |
| | | RealmModel realm = session.getContext().getRealm(); |
| | | return session.clients().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()); |
| | | } |
| | | |
| | | 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(); |
| | |
| | | |
| | | String valid = matchesRedirects(resolveValidRedirects, r, allowWildcards); |
| | | |
| | | if (valid == null && (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(); |
| | | sb.append(r.substring(0, i)); |
| | | |
| | | i = r.indexOf('/', i); |
| | | if (i >= 0) { |
| | | sb.append(r.substring(i)); |
| | | } |
| | | |
| | | r = sb.toString(); |
| | | |
| | | valid = matchesRedirects(resolveValidRedirects, r, allowWildcards); |
| | | if (valid == null && "http".equals(originalRedirect.getScheme()) && LOOPBACK_INTERFACES.contains(originalRedirect.getHost())) { |
| | | String redirectWithDefaultPort = KeycloakUriBuilder.fromUri(originalRedirect).port(80).buildAsString(); |
| | | valid = matchesRedirects(resolveValidRedirects, redirectWithDefaultPort, allowWildcards); |
| | | } |
| | | |
| | | if (valid != null && !originalRedirect.isAbsolute()) { |