All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.keycloak.enums.SslRequired Maven / Gradle / Ivy

There is a newer version: 26.0.7
Show newest version
package org.keycloak.enums;

import org.keycloak.ClientConnection;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author Stian Thorgersen
 */
public enum SslRequired {

    ALL,
    EXTERNAL,
    NONE;

    private static final long serialVersionUID = 1L;

    public boolean isRequired(ClientConnection connection) {
        return isRequired(connection.getRemoteAddr());
    }

    public boolean isRequired(String address) {
        switch (this) {
            case ALL:
                return true;
            case NONE:
                return false;
            case EXTERNAL:
                return !isLocal(address);
            default:
                return true;
        }
    }

    private boolean isLocal(String remoteAddress) {
        try {
            InetAddress inetAddress = InetAddress.getByName(remoteAddress);
            return inetAddress.isAnyLocalAddress() || inetAddress.isLoopbackAddress() || inetAddress.isSiteLocalAddress();
        } catch (UnknownHostException e) {
            return false;
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy