-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initializer demo to set fixed issuer
- Loading branch information
Showing
4 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
initializer/src/main/java/dasniko/keycloak/initializer/issuer/IssuerInitializerProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package dasniko.keycloak.initializer.issuer; | ||
|
||
import com.google.auto.service.AutoService; | ||
import dasniko.keycloak.initializer.InitializerProviderFactory; | ||
import lombok.extern.slf4j.Slf4j; | ||
import net.bytebuddy.ByteBuddy; | ||
import net.bytebuddy.agent.ByteBuddyAgent; | ||
import net.bytebuddy.dynamic.loading.ClassReloadingStrategy; | ||
import net.bytebuddy.implementation.MethodDelegation; | ||
import org.keycloak.Config; | ||
import org.keycloak.models.KeycloakSessionFactory; | ||
import org.keycloak.provider.ProviderConfigProperty; | ||
import org.keycloak.provider.ProviderConfigurationBuilder; | ||
import org.keycloak.services.Urls; | ||
import org.keycloak.services.validation.Validation; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.List; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.isDeclaredBy; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.returns; | ||
|
||
@Slf4j | ||
@AutoService(InitializerProviderFactory.class) | ||
public class IssuerInitializerProvider implements InitializerProviderFactory { | ||
|
||
public static final String PROVIDER_ID = "issuer"; | ||
|
||
private static final String CONFIG_ATTR_BASE_URI = "base-uri"; | ||
|
||
private static String issuerBaseUri; | ||
|
||
@Override | ||
public String getId() { | ||
return PROVIDER_ID; | ||
} | ||
|
||
@Override | ||
public void init(Config.Scope config) { | ||
issuerBaseUri = config.get(CONFIG_ATTR_BASE_URI); | ||
if (!Validation.isBlank(issuerBaseUri)) { | ||
log.info("Issuer BaseURI fixed value: {}", issuerBaseUri); | ||
} | ||
} | ||
|
||
@Override | ||
public void postInit(KeycloakSessionFactory factory) { | ||
ByteBuddyAgent.install(); | ||
new ByteBuddy() | ||
.redefine(Urls.class) | ||
.method(named("realmIssuer").and(isDeclaredBy(Urls.class).and(returns(String.class)))) | ||
.intercept(MethodDelegation.to(this.getClass())) | ||
.make() | ||
.load(Urls.class.getClassLoader(), ClassReloadingStrategy.fromInstalledAgent()); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static String realmIssuer(URI baseUri, String realmName) { | ||
try { | ||
baseUri = new URI(issuerBaseUri); | ||
} catch (URISyntaxException | NullPointerException ignored) { | ||
} | ||
return Urls.realmBase(baseUri).path("{realm}").build(realmName).toString(); | ||
} | ||
|
||
@Override | ||
public List<ProviderConfigProperty> getConfigMetadata() { | ||
return ProviderConfigurationBuilder.create() | ||
.property() | ||
.name(CONFIG_ATTR_BASE_URI) | ||
.type(ProviderConfigProperty.STRING_TYPE) | ||
.helpText("The baseUri to use for the issuer of this server. Keep empty, if the regular hostname settings should be used.") | ||
.add() | ||
.build(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
initializer/src/test/java/dasniko/keycloak/initializer/InitiailizerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package dasniko.keycloak.initializer; | ||
|
||
import dasniko.testcontainers.keycloak.KeycloakContainer; | ||
import de.keycloak.test.TestBase; | ||
import org.jboss.shrinkwrap.resolver.api.maven.Maven; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.startsWith; | ||
|
||
@Testcontainers | ||
public class InitiailizerTest extends TestBase { | ||
|
||
public static final String ISSUER = "https://auth.keycloak.de"; | ||
|
||
private static final List<File> dependencies = Maven.resolver() | ||
.loadPomFromFile("./pom.xml") | ||
.resolve("net.bytebuddy:byte-buddy-agent") | ||
.withoutTransitivity().asList(File.class); | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = { ISSUER, "" }) | ||
public void testIssuer(String issuerValue) { | ||
final KeycloakContainer keycloak = new KeycloakContainer() | ||
.withProviderClassesFrom("target/classes") | ||
.withProviderLibsFrom(dependencies) | ||
.withEnv("KC_SPI_INITIALIZER_ISSUER_BASE_URI", issuerValue) | ||
// .withDebugFixedPort(8787, true) | ||
; | ||
keycloak.start(); | ||
|
||
String issuer = getOpenIDConfiguration(keycloak, "master").extract().path("issuer"); | ||
if (issuerValue.isEmpty()) { | ||
assertThat(issuer, startsWith(keycloak.getAuthServerUrl())); | ||
} else { | ||
assertThat(issuer, startsWith(issuerValue)); | ||
} | ||
|
||
keycloak.stop(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters