mirror of https://github.com/jacekkow/keycloak-protocol-cas

Stian Thorgersen
2022-12-14 226dd13eeed0242b88958ce7ea107831152c68de
src/main/java/org/keycloak/protocol/cas/utils/RedirectUtils.java
@@ -18,6 +18,8 @@
package org.keycloak.protocol.oidc.utils;
import org.jboss.logging.Logger;
import org.keycloak.common.util.Encode;
import org.keycloak.common.util.KeycloakUriBuilder;
import org.keycloak.common.util.UriUtils;
import org.keycloak.models.ClientModel;
import org.keycloak.models.Constants;
@@ -41,6 +43,12 @@
    private static final Logger logger = Logger.getLogger(RedirectUtils.class);
    /**
     * 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);
@@ -71,9 +79,10 @@
        return resolveValidRedirects;
    }
    @Deprecated
    private static Set<String> getValidateRedirectUris(KeycloakSession session) {
        RealmModel realm = session.getContext().getRealm();
        return session.clientStorageManager().getAllRedirectUrisOfEnabledClients(realm).entrySet().stream()
        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)
@@ -84,6 +93,7 @@
        KeycloakUriInfo uriInfo = session.getContext().getUri();
        RealmModel realm = session.getContext().getRealm();
        redirectUri = decodeRedirectUri(redirectUri);
        if (redirectUri != null) {
            try {
                URI uri = URI.create(redirectUri);
@@ -145,6 +155,42 @@
        }
    }
    // Decode redirectUri. We don't decode query and fragment as those can be encoded in the original URL.
    // 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;
        int MAX_DECODING_COUNT = 5; // Max count of attempts for decoding URL (in case it was encoded multiple times)
        try {
            KeycloakUriBuilder uriBuilder = KeycloakUriBuilder.fromUri(redirectUri);
            String origQuery = uriBuilder.getQuery();
            String origFragment = uriBuilder.getFragment();
            String encodedRedirectUri = uriBuilder
                    .replaceQuery(null)
                    .fragment(null)
                    .buildAsString();
            String decodedRedirectUri = null;
            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)
                            .replaceQuery(origQuery)
                            .fragment(origFragment)
                            .buildAsString();
                } else {
                    // Next attempt
                    encodedRedirectUri = decodedRedirectUri;
                }
            }
        } catch (IllegalArgumentException iae) {
            logger.debugf("Illegal redirect URI used: %s, Details: %s", redirectUri, iae.getMessage());
        }
        logger.debugf("Was not able to decode redirect URI: %s", redirectUri);
        return null;
    }
    private static String lowerCaseHostname(String redirectUri) {
        int n = redirectUri.indexOf('/', 7);
        if (n == -1) {