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

io.phasetwo.keycloak.config.ConfigurationAware Maven / Gradle / Ivy

package io.phasetwo.keycloak.config;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;

public interface ConfigurationAware {

  String getId();

  default List> getConfigurations(KeycloakSession session) {
    return RealmAttributesConfigLoader.loadConfigurations(
            session, getRealm(session).getName(), getId())
        .stream()
        .map(config -> RealmAttributesConfigLoader.safeConvertToMap(config))
        .collect(Collectors.toList());
  }

  /** Potential workaround for keycloak/keycloak#20105 */
  static RealmModel getRealm(KeycloakSession session) {
    if (session.getContext() == null) {
      return null;
    }
    if (session.getContext().getRealm() != null) {
      return session.getContext().getRealm();
    }
    if (session.getContext().getAuthenticationSession() != null
        && session.getContext().getAuthenticationSession().getRealm() != null) {
      return session.getContext().getAuthenticationSession().getRealm();
    }
    if (session.getContext().getClient() != null
        && session.getContext().getClient().getRealm() != null) {
      return session.getContext().getClient().getRealm();
    }
    return null;
  }

  default Map getConfiguration(KeycloakSession session) {
    List> configs = getConfigurations(session);
    if (configs == null || configs.size() == 0) return ImmutableMap.of();
    else return configs.get(0);
  }

  default String configToString(Map config) {
    if (config == null) return "[empty]";
    else return "[" + Joiner.on(",").withKeyValueSeparator("=").join(config) + "]";
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy