-
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 #24278 from sberyozkin/keycloak_admin_client_from_…
…endpoint Update Keycloak AdminClientTest
- Loading branch information
Showing
4 changed files
with
114 additions
and
35 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
112 changes: 99 additions & 13 deletions
112
...ests/keycloak-authorization/src/main/java/io/quarkus/it/keycloak/AdminClientResource.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 |
---|---|---|
@@ -1,33 +1,119 @@ | ||
package io.quarkus.it.keycloak; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.jboss.logging.Logger; | ||
import org.keycloak.admin.client.Keycloak; | ||
import org.keycloak.admin.client.KeycloakBuilder; | ||
import org.keycloak.representations.idm.ClientRepresentation; | ||
import org.keycloak.representations.idm.CredentialRepresentation; | ||
import org.keycloak.representations.idm.RealmRepresentation; | ||
import org.keycloak.representations.idm.RoleRepresentation; | ||
import org.keycloak.representations.idm.RolesRepresentation; | ||
import org.keycloak.representations.idm.UserRepresentation; | ||
|
||
@Path("/admin-client") | ||
public class AdminClientResource { | ||
|
||
private static final Logger LOG = Logger.getLogger(AdminClientResource.class); | ||
|
||
@ConfigProperty(name = "admin-url") | ||
String url; | ||
|
||
@GET | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public String hello() { | ||
LOG.info("Hello invoked"); | ||
|
||
Keycloak keycloak = KeycloakBuilder.builder().serverUrl(url) | ||
.realm("master") | ||
.clientId("admin-cli") | ||
.username("admin") | ||
.password("admin").build(); | ||
return keycloak.realm("quarkus").toRepresentation().getRealm(); | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Path("realm") | ||
public RealmRepresentation getRealm() { | ||
try (Keycloak keycloak = keycloak()) { | ||
return keycloak.realm("quarkus").toRepresentation(); | ||
} | ||
} | ||
|
||
@GET | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Path("newrealm") | ||
public RealmRepresentation createRealm() { | ||
RealmRepresentation newRealm = createRealm("quarkus2"); | ||
|
||
newRealm.getClients().add(createClient("quarkus-app2")); | ||
newRealm.getUsers().add(createUser("alice", "user")); | ||
|
||
try (Keycloak keycloak = keycloak()) { | ||
keycloak.realms().create(newRealm); | ||
} | ||
|
||
try (Keycloak keycloak = keycloak()) { | ||
return keycloak.realm("quarkus2").toRepresentation(); | ||
} | ||
} | ||
|
||
private Keycloak keycloak() { | ||
try { | ||
return KeycloakBuilder.builder() | ||
.serverUrl(url) | ||
.realm("master") | ||
.clientId("admin-cli") | ||
.username("admin") | ||
.password("admin") | ||
.build(); | ||
} catch (Exception ex) { | ||
throw new RuntimeException(ex); | ||
} | ||
} | ||
|
||
private static RealmRepresentation createRealm(String name) { | ||
RealmRepresentation realm = new RealmRepresentation(); | ||
|
||
realm.setRealm(name); | ||
realm.setEnabled(true); | ||
realm.setUsers(new ArrayList<>()); | ||
realm.setClients(new ArrayList<>()); | ||
|
||
RolesRepresentation roles = new RolesRepresentation(); | ||
List<RoleRepresentation> realmRoles = new ArrayList<>(); | ||
|
||
roles.setRealm(realmRoles); | ||
realm.setRoles(roles); | ||
|
||
realm.getRoles().getRealm().add(new RoleRepresentation("user", null, false)); | ||
|
||
return realm; | ||
} | ||
|
||
private static ClientRepresentation createClient(String clientId) { | ||
ClientRepresentation client = new ClientRepresentation(); | ||
|
||
client.setClientId(clientId); | ||
client.setRedirectUris(Arrays.asList("*")); | ||
client.setPublicClient(false); | ||
client.setSecret("secret"); | ||
client.setDirectAccessGrantsEnabled(true); | ||
client.setEnabled(true); | ||
|
||
return client; | ||
} | ||
|
||
private static UserRepresentation createUser(String username, String... realmRoles) { | ||
UserRepresentation user = new UserRepresentation(); | ||
|
||
user.setUsername(username); | ||
user.setEnabled(true); | ||
user.setCredentials(new ArrayList<>()); | ||
user.setRealmRoles(Arrays.asList(realmRoles)); | ||
|
||
CredentialRepresentation credential = new CredentialRepresentation(); | ||
|
||
credential.setType(CredentialRepresentation.PASSWORD); | ||
credential.setValue(username); | ||
credential.setTemporary(false); | ||
|
||
user.getCredentials().add(credential); | ||
|
||
return user; | ||
} | ||
} |
21 changes: 13 additions & 8 deletions
21
...ests/keycloak-authorization/src/test/java/io/quarkus/it/keycloak/AdminClientTestCase.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 |
---|---|---|
@@ -1,22 +1,27 @@ | ||
package io.quarkus.it.keycloak; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.keycloak.representations.idm.RealmRepresentation; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
public class AdminClientTestCase { | ||
|
||
@Test | ||
public void testHelloEndpoint() { | ||
given() | ||
.when().get("/admin-client") | ||
.then() | ||
.statusCode(200) | ||
.body(is("quarkus")); | ||
public void testGetExistingRealm() { | ||
RealmRepresentation realm = given() | ||
.when().get("/admin-client/realm").as(RealmRepresentation.class); | ||
assertEquals("quarkus", realm.getRealm()); | ||
} | ||
|
||
} | ||
@Test | ||
public void testGetNewRealm() { | ||
RealmRepresentation realm = given() | ||
.when().get("/admin-client/newrealm").as(RealmRepresentation.class); | ||
assertEquals("quarkus2", realm.getRealm()); | ||
} | ||
} |
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