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

io.quarkus.oidc.runtime.DefaultTenantConfigResolver Maven / Gradle / Ivy

Go to download

Secure your applications with OpenID Connect Adapter and IDP such as Keycloak

There is a newer version: 3.17.5
Show newest version
package io.quarkus.oidc.runtime;

import java.util.concurrent.Executor;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Event;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;

import io.quarkus.oidc.OIDCException;
import io.quarkus.oidc.OidcTenantConfig;
import io.quarkus.oidc.SecurityEvent;
import io.quarkus.oidc.TenantConfigResolver;
import io.quarkus.oidc.TenantResolver;
import io.quarkus.oidc.TokenStateManager;
import io.smallrye.mutiny.Uni;
import io.vertx.ext.web.RoutingContext;

@ApplicationScoped
public class DefaultTenantConfigResolver {

    private static final Logger LOG = Logger.getLogger(DefaultTenantConfigResolver.class);
    private static final String CURRENT_STATIC_TENANT_ID = "static.tenant.id";
    private static final String CURRENT_STATIC_TENANT_ID_NULL = "static.tenant.id.null";
    private static final String CURRENT_DYNAMIC_TENANT_CONFIG = "dynamic.tenant.config";
    private static final String CURRENT_DYNAMIC_TENANT_CONFIG_NULL = "dynamic.tenant.config.null";

    @Inject
    Instance tenantResolver;

    @Inject
    Instance tenantConfigResolver;

    @Inject
    TenantConfigBean tenantConfigBean;

    @Inject
    Instance tokenStateManager;

    @Inject
    Event securityEvent;

    @Inject
    @ConfigProperty(name = "quarkus.http.proxy.enable-forwarded-prefix")
    boolean enableHttpForwardedPrefix;

    private volatile boolean securityEventObserved;

    @PostConstruct
    public void verifyResolvers() {
        if (tenantConfigResolver.isResolvable() && tenantConfigResolver.isAmbiguous()) {
            throw new IllegalStateException("Multiple " + TenantConfigResolver.class + " beans registered");
        }
        if (tenantResolver.isResolvable() && tenantResolver.isAmbiguous()) {
            throw new IllegalStateException("Multiple " + TenantResolver.class + " beans registered");
        }
        if (tokenStateManager.isAmbiguous()) {
            throw new IllegalStateException("Multiple " + TokenStateManager.class + " beans registered");
        }
    }

    OidcTenantConfig resolveConfig(RoutingContext context) {
        OidcTenantConfig tenantConfig = getDynamicTenantConfig(context);
        if (tenantConfig == null) {
            TenantConfigContext tenant = getStaticTenantContext(context);
            if (tenant != null) {
                tenantConfig = tenant.oidcConfig;
            }
        }
        return tenantConfig;
    }

    Uni resolveContext(RoutingContext context) {
        Uni tenantContext = getDynamicTenantContext(context);

        if (tenantContext == null) {
            tenantContext = Uni.createFrom().item(getStaticTenantContext(context));
        }
        return tenantContext;
    }

    private TenantConfigContext getStaticTenantContext(RoutingContext context) {

        String tenantId = null;

        if (tenantResolver.isResolvable()) {
            tenantId = context.get(CURRENT_STATIC_TENANT_ID);
            if (tenantId == null && context.get(CURRENT_STATIC_TENANT_ID_NULL) == null) {
                tenantId = tenantResolver.get().resolve(context);
                if (tenantId != null) {
                    context.put(CURRENT_STATIC_TENANT_ID, tenantId);
                } else {
                    context.put(CURRENT_STATIC_TENANT_ID_NULL, true);
                }
            }
        }

        TenantConfigContext configContext = tenantId != null ? tenantConfigBean.getStaticTenantsConfig().get(tenantId) : null;
        if (configContext == null) {
            if (tenantId != null && !tenantId.isEmpty()) {
                LOG.debugf("No configuration with a tenant id '%s' has been found, using the default configuration");
            }
            configContext = tenantConfigBean.getDefaultTenant();
        }
        return configContext;
    }

    boolean isSecurityEventObserved() {
        return securityEventObserved;
    }

    void setSecurityEventObserved(boolean securityEventObserved) {
        this.securityEventObserved = securityEventObserved;
    }

    Event getSecurityEvent() {
        return securityEvent;
    }

    TokenStateManager getTokenStateManager() {
        return tokenStateManager.get();
    }

    private OidcTenantConfig getDynamicTenantConfig(RoutingContext context) {
        OidcTenantConfig oidcConfig = null;
        if (tenantConfigResolver.isResolvable()) {
            oidcConfig = context.get(CURRENT_DYNAMIC_TENANT_CONFIG);
            if (oidcConfig == null && context.get(CURRENT_DYNAMIC_TENANT_CONFIG_NULL) == null) {
                oidcConfig = tenantConfigResolver.get().resolve(context);
                if (oidcConfig != null) {
                    context.put(CURRENT_DYNAMIC_TENANT_CONFIG, oidcConfig);
                } else {
                    context.put(CURRENT_DYNAMIC_TENANT_CONFIG_NULL, true);
                }
            }
        }
        return oidcConfig;
    }

    private Uni getDynamicTenantContext(RoutingContext context) {

        OidcTenantConfig tenantConfig = getDynamicTenantConfig(context);
        if (tenantConfig != null) {
            String tenantId = tenantConfig.getTenantId()
                    .orElseThrow(() -> new OIDCException("Tenant configuration must have tenant id"));
            TenantConfigContext tenantContext = tenantConfigBean.getDynamicTenantsConfig().get(tenantId);

            if (tenantContext == null) {
                return tenantConfigBean.getTenantConfigContextFactory().apply(tenantConfig);
            } else {
                return Uni.createFrom().item(tenantContext);
            }
        }

        return null;
    }

    boolean isEnableHttpForwardedPrefix() {
        return enableHttpForwardedPrefix;
    }

    public Executor getBlockingExecutor() {
        return tenantConfigBean.getBlockingExecutor();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy