Skip to content

Commit

Permalink
allow configuring default scopes and token lifespan in CLI
Browse files Browse the repository at this point in the history
Signed-off-by: Kai Helbig <[email protected]>
  • Loading branch information
ostrya committed Oct 9, 2023
1 parent fc1a03f commit ad759a5
Showing 1 changed file with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import static com.tngtech.keycloakmock.api.ServerConfig.aServerConfig;

import com.tngtech.keycloakmock.api.KeycloakMock;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.Callable;
import javax.annotation.Nonnull;
import org.slf4j.Logger;
Expand Down Expand Up @@ -40,18 +42,34 @@ public class Main implements Callable<Void> {
+ "If present, must be prefixed with '/', eg. --contextPath=/example-path")
private String contextPath = "/auth";

@SuppressWarnings("FieldMayBeFinal")
@Option(
names = {"-ncp", "--noContextPath"},
description = "If present context path will not be used. Good for mocking Keycloak 18.0.0+.")
private boolean noContextPath;

@SuppressWarnings("FieldMayBeFinal")
@Option(
names = {"-r", "--mapRolesToResources"},
description = "If set, roles will be assigned to these resources instead of the realm.",
paramLabel = "RESOURCE",
split = ",")
private final List<String> resourcesToMapRolesTo = Collections.emptyList();
private List<String> resourcesToMapRolesTo = Collections.emptyList();

@SuppressWarnings("FieldMayBeFinal")
@Option(
names = {"-sc", "--scopes"},
description = "Scopes to add to generated token (default: ${DEFAULT-VALUE}).",
paramLabel = "SCOPE",
split = ",")
private List<String> scopes = Collections.singletonList("openid");

@SuppressWarnings("FieldMayBeFinal")
@Option(
names = {"-tl", "--tokenLifespan"},
description =
"Lifespan of generated tokens (default: ${DEFAULT-VALUE}). Valid values are e.g. '10h',"
+ " '15m', '3m45s'.")
private String tokenLifespan = "10h";

public static void main(@Nonnull final String[] args) {
if (System.getProperty("org.slf4j.simpleLogger.logFile") == null) {
Expand All @@ -73,6 +91,8 @@ public Void call() {
.withTls(tls)
.withContextPath(usedContextPath)
.withResourcesToMapRolesTo(resourcesToMapRolesTo)
.withDefaultScopes(scopes)
.withDefaultTokenLifespan(getParsedLifespan())
.build())
.start();

Expand All @@ -84,4 +104,9 @@ public Void call() {

return null;
}

private Duration getParsedLifespan() {
// simple trick: just interpret the given string as the suffix part of a Duration string
return Duration.parse("PT" + tokenLifespan.toUpperCase(Locale.ROOT));
}
}

0 comments on commit ad759a5

Please sign in to comment.