-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19658 from sberyozkin/keycloak-test-admin
Add KeycloakTestAdmin
- Loading branch information
Showing
3 changed files
with
115 additions
and
6 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
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
79 changes: 79 additions & 0 deletions
79
...work/keycloak-server/src/main/java/io/quarkus/test/keycloak/server/KeycloakTestAdmin.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,79 @@ | ||
package io.quarkus.test.keycloak.server; | ||
|
||
import org.eclipse.microprofile.config.ConfigProvider; | ||
import org.keycloak.representations.AccessTokenResponse; | ||
|
||
import io.quarkus.runtime.configuration.ConfigurationException; | ||
import io.quarkus.test.junit.QuarkusIntegrationTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class KeycloakTestAdmin { | ||
|
||
private final static String AUTH_SERVER_URL_PROP = "quarkus.oidc.auth-server-url"; | ||
private final static String CLIENT_ID_PROP = "quarkus.oidc.client-id"; | ||
private final static String CLIENT_SECRET_PROP = "quarkus.oidc.credentials.secret"; | ||
|
||
static { | ||
RestAssured.useRelaxedHTTPSValidation(); | ||
} | ||
|
||
private QuarkusIntegrationTest.Context testContext; | ||
|
||
public KeycloakTestAdmin() { | ||
|
||
} | ||
|
||
public KeycloakTestAdmin(QuarkusIntegrationTest.Context testContext) { | ||
this.testContext = testContext; | ||
} | ||
|
||
public String getAccessToken(String userName) { | ||
return getAccessToken(userName, getClientId()); | ||
} | ||
|
||
public String getAccessToken(String userName, String clientId) { | ||
return getAccessToken(userName, userName, clientId); | ||
} | ||
|
||
public String getAccessToken(String userName, String userSecret, String clientId) { | ||
return getAccessToken(userName, userSecret, clientId, getClientSecret()); | ||
} | ||
|
||
public String getAccessToken(String userName, String userSecret, String clientId, String clientSecret) { | ||
return RestAssured.given().param("grant_type", "password") | ||
.param("username", userName) | ||
.param("password", userSecret) | ||
.param("client_id", clientId) | ||
.param("client_secret", clientSecret) | ||
.when() | ||
.post(getAuthServerUrl() + "/protocol/openid-connect/token") | ||
.as(AccessTokenResponse.class).getToken(); | ||
} | ||
|
||
private String getClientId() { | ||
return getPropertyValue(CLIENT_ID_PROP, "quarkus-app"); | ||
} | ||
|
||
private String getClientSecret() { | ||
return getPropertyValue(CLIENT_SECRET_PROP, "secret"); | ||
} | ||
|
||
private String getAuthServerUrl() { | ||
String authServerUrl = getPropertyValue(AUTH_SERVER_URL_PROP, null); | ||
if (authServerUrl == null) { | ||
throw new ConfigurationException(AUTH_SERVER_URL_PROP + " is not configured"); | ||
} | ||
return authServerUrl; | ||
} | ||
|
||
private String getPropertyValue(String prop, String defaultValue) { | ||
return ConfigProvider.getConfig().getOptionalValue(prop, String.class) | ||
.orElseGet(() -> getDevProperty(prop, defaultValue)); | ||
} | ||
|
||
private String getDevProperty(String prop, String defaultValue) { | ||
String value = testContext == null ? null : testContext.devServicesProperties().get(prop); | ||
return value == null ? defaultValue : value; | ||
} | ||
|
||
} |