Search in sources :

Example 1 with SecurityException

use of com.canoo.platform.server.security.SecurityException in project dolphin-platform by canoo.

the class DolphinKeycloakConfigResolver method resolve.

public KeycloakDeployment resolve(final HttpFacade.Request request) {
    Assert.requireNonNull(request, "request");
    final String realmName = Optional.ofNullable(request.getHeader(REALM_NAME_HEADER)).orElse(configuration.getRealmName());
    final String applicationName = Optional.ofNullable(request.getHeader(APPLICATION_NAME_HEADER)).orElse(configuration.getApplicationName());
    final String authEndPoint = configuration.getAuthEndpoint();
    final boolean cors = configuration.isCors();
    Optional.ofNullable(realmName).orElseThrow(() -> new SecurityException("Realm name for security check is not configured!"));
    Optional.ofNullable(applicationName).orElseThrow(() -> new SecurityException("Application name for security check is not configured!"));
    Optional.ofNullable(authEndPoint).orElseThrow(() -> new SecurityException("Auth endpoint for security check is not configured!"));
    LOG.debug("Defined Keycloak AdapterConfig for request against realm '" + realmName + "' and app '" + applicationName + "'");
    final AdapterConfig adapterConfig = new AdapterConfig();
    LOG.debug("Checking if realm '" + realmName + "' is allowed");
    if (isRealmAllowed(realmName)) {
        adapterConfig.setRealm(realmName);
    } else {
        if (LOG.isDebugEnabled()) {
            final String allowedRealms = configuration.getRealmNames().stream().reduce("", (a, b) -> a + "," + b);
            LOG.debug("Realm '" + realmName + "' is not allowed! Allowed realms are {}", allowedRealms);
        }
        throw new SecurityException("Access Denied! The given realm is not in the allowed realms.");
    }
    adapterConfig.setResource(applicationName);
    adapterConfig.setAuthServerUrl(authEndPoint);
    adapterConfig.setCors(cors);
    Optional.ofNullable(request.getHeader(BEARER_ONLY_HEADER)).ifPresent(v -> adapterConfig.setBearerOnly(true));
    return KeycloakDeploymentBuilder.build(adapterConfig);
}
Also used : AdapterConfig(org.keycloak.representations.adapters.config.AdapterConfig) SecurityException(com.canoo.platform.server.security.SecurityException)

Aggregations

SecurityException (com.canoo.platform.server.security.SecurityException)1 AdapterConfig (org.keycloak.representations.adapters.config.AdapterConfig)1