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

com.tngtech.keycloakmock.api.ServerConfig Maven / Gradle / Ivy

The newest version!
package com.tngtech.keycloakmock.api;

import com.tngtech.keycloakmock.impl.Protocol;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nonnull;

/** Server configuration to use. */
public final class ServerConfig {

  private static final String DEFAULT_HOSTNAME = "localhost";
  private static final String DEFAULT_CONTEXT_PATH = "/auth";
  private static final int DEFAULT_PORT = 8000;
  private static final String DEFAULT_REALM = "master";
  private static final String DEFAULT_SCOPE = "openid";
  private static final Duration DEFAULT_TOKEN_LIFESPAN = Duration.ofHours(10);

  private final int port;
  @Nonnull private final Protocol protocol;
  @Nonnull private final String defaultHostname;
  @Nonnull private final String contextPath;
  @Nonnull private final String defaultRealm;
  @Nonnull private final List resourcesToMapRolesTo;
  @Nonnull private final List defaultScopes;
  @Nonnull private final Duration defaultTokenLifespan;

  private ServerConfig(@Nonnull final Builder builder) {
    this.port = builder.port;
    this.protocol = builder.protocol;
    this.defaultHostname = builder.defaultHostname;
    this.contextPath = builder.contextPath;
    this.defaultRealm = builder.defaultRealm;
    this.resourcesToMapRolesTo = builder.resourcesToMapRolesTo;
    this.defaultScopes = builder.defaultScopes;
    this.defaultTokenLifespan = builder.defaultTokenLifespan;
  }

  /**
   * Get a new builder.
   *
   * @return a server configuration builder
   */
  @Nonnull
  public static Builder aServerConfig() {
    return new Builder();
  }

  /**
   * The port that the server is started on.
   *
   * @return the used port
   */
  public int getPort() {
    return port;
  }

  /**
   * The protocol that the server uses.
   *
   * @return the used protocol
   */
  @Nonnull
  public Protocol getProtocol() {
    return protocol;
  }

  /**
   * The resources for which roles will be set.
   *
   * 

If this list is empty, a login via the built-in login page will take the comma-separated * roles from the password field and assign it as realm roles. If it contains at least one * resource, the roles will be mapped only to this resource. * * @return the list of resources to which roles are mapped * @see Realm Roles * @see Client Roles */ @Nonnull public List getResourcesToMapRolesTo() { return Collections.unmodifiableList(resourcesToMapRolesTo); } /** * The default hostname used in issuer claim. * * @return default hostname * @deprecated use {@link #getDefaultHostname()} instead */ @Nonnull @Deprecated public String getHostname() { return getDefaultHostname(); } /** * The default hostname used in issuer claim. * * @return default hostname * @see TokenConfig.Builder#withHostname(String) */ @Nonnull public String getDefaultHostname() { return defaultHostname; } /** * Keycloak context path. * * @return context path * @see Builder#withContextPath(String) * @see Builder#withNoContextPath() */ @Nonnull public String getContextPath() { return contextPath; } /** * The default realm used in issuer claim. * * @return default realm * @deprecated use {@link #getDefaultRealm()} instead */ @Nonnull @Deprecated public String getRealm() { return getDefaultRealm(); } /** * The default realm used in issuer claim. * * @return default realm */ @Nonnull public String getDefaultRealm() { return defaultRealm; } /** * The default scopes used in scope claim. * * @return default scopes */ @Nonnull public List getDefaultScopes() { return Collections.unmodifiableList(defaultScopes); } /** * Get default access token lifespan. * * @return default token lifespan */ @Nonnull public Duration getDefaultTokenLifespan() { return defaultTokenLifespan; } /** * Builder for {@link ServerConfig}. * *

Use this to generate a server configuration to your needs. */ public static final class Builder { private int port = DEFAULT_PORT; @Nonnull private Protocol protocol = Protocol.HTTP; @Nonnull private String defaultHostname = DEFAULT_HOSTNAME; @Nonnull private String contextPath = DEFAULT_CONTEXT_PATH; @Nonnull private String defaultRealm = DEFAULT_REALM; @Nonnull private final List resourcesToMapRolesTo = new ArrayList<>(); @Nonnull private final List defaultScopes = new ArrayList<>(); @Nonnull private Duration defaultTokenLifespan = DEFAULT_TOKEN_LIFESPAN; private Builder() { defaultScopes.add(DEFAULT_SCOPE); } /** * Set TLS flag. * *

If set to true, start the server with TLS. Default value is false. * * @param tls the flag to use * @return builder */ @Nonnull public Builder withTls(final boolean tls) { this.protocol = tls ? Protocol.HTTPS : Protocol.HTTP; return this; } /** * Set port. * *

The port that the server is started on. Default value is 8000. * * @param port the port to use * @return builder */ @Nonnull public Builder withPort(final int port) { this.port = port; return this; } /** * Set default hostname. * *

The hostname that is used as token issuer if no explicit hostname is configured for the * token. Default value is 'localhost'. * * @param defaultHostname the hostname to use * @return builder * @see TokenConfig.Builder#withHostname(String) * @deprecated use {@link #withDefaultHostname(String)} instead */ @Nonnull @Deprecated public Builder withHostname(@Nonnull final String defaultHostname) { return withDefaultHostname(defaultHostname); } /** * Set default hostname. * *

The hostname that is used as token issuer if no explicit hostname is configured for the * token. Default value is 'localhost'. * * @param defaultHostname the hostname to use * @return builder * @see TokenConfig.Builder#withHostname(String) */ @Nonnull public Builder withDefaultHostname(@Nonnull final String defaultHostname) { this.defaultHostname = defaultHostname; return this; } /** * Set default realm. * *

The realm that is used in issued tokens if no explicit realm is configured for the token. * Default value is 'master'. * * @param defaultRealm the realm to use * @return builder * @see TokenConfig.Builder#withRealm(String) * @deprecated use {@link #withDefaultRealm(String)} instead */ @Nonnull @Deprecated public Builder withRealm(@Nonnull final String defaultRealm) { return withDefaultRealm(defaultRealm); } /** * Set default realm. * *

The realm that is used in issued tokens if no explicit realm is configured for the token. * Default value is 'master'. * * @param defaultRealm the realm to use * @return builder * @see TokenConfig.Builder#withRealm(String) */ @Nonnull public Builder withDefaultRealm(@Nonnull final String defaultRealm) { this.defaultRealm = defaultRealm; return this; } /** * Set resources for which roles will be set. * *

If this list is empty, a login via the built-in login page will take the comma-separated * roles from the password field and assign it as realm roles. If it contains at least one * resource, the roles will be mapped only to this resource. * * @param resources the list of resources to which roles will be mapped * @return builder * @see #withResourceToMapRolesTo(String) * @see Realm Roles * @see Client * Roles */ @Nonnull public Builder withResourcesToMapRolesTo(@Nonnull List resources) { resourcesToMapRolesTo.addAll(resources); return this; } /** * Set context path. * *

Before quarkus based Keycloak distribution /auth prefix was obligatory. Now /auth prefix * is removed and can be enabled/overridden in configuration to keep backward compatibility. * Default value is '/auth' To disable context path use {@link #withNoContextPath()} method. * * @see hostname-path * @see Default * context path changed * @param contextPath context path to use * @return builder */ @Nonnull public Builder withContextPath(@Nonnull String contextPath) { this.contextPath = contextPath; return this; } /** * Disabling context path. * * @see #withContextPath(String) * @return builder */ @Nonnull public Builder withNoContextPath() { this.contextPath = ""; return this; } /** * Add a resource for which roles will be set. * *

If no resource is set, a login via the built-in login page will take the comma-separated * roles from the password field and assign it as realm roles. If at least one resource is * configured, the roles will be mapped only to this resource. * * @param resource a resource to which roles will be mapped * @return builder * @see #withResourcesToMapRolesTo(List) * @see Realm Roles * @see Client * Roles */ @Nonnull public Builder withResourceToMapRolesTo(@Nonnull String resource) { resourcesToMapRolesTo.add(Objects.requireNonNull(resource)); return this; } /** * Set default client scopes. * *

Set of client scopes to be configured. Default scope 'openid' is always added. * * @param defaultScopes the scopes to add * @return builder * @see scope * claims */ @Nonnull public Builder withDefaultScopes(@Nonnull final Collection defaultScopes) { this.defaultScopes.addAll(defaultScopes); return this; } /** * Set default client scope. * *

A client scope to be configured. Default scope 'openid' is always added. * * @param defaultScope as string * @return builder * @see scope * claims */ @Nonnull public Builder withDefaultScope(@Nonnull final String defaultScope) { this.defaultScopes.add(defaultScope); return this; } /** * Set default access token lifespan. * *

Token expiry 'exp' will be set as 'issuedAt' + 'tokenLifespan'. The default lifespan is 10 * hours. * * @param tokenLifespan as duration * @return builder */ @Nonnull public Builder withDefaultTokenLifespan(@Nonnull final Duration tokenLifespan) { this.defaultTokenLifespan = tokenLifespan; return this; } /** * Build the server configuration. * * @return the server configuration */ @Nonnull public ServerConfig build() { return new ServerConfig(this); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy