Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
package org.keycloak.admin.client.resource;

import java.util.List;
import java.util.Optional;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
Expand Down Expand Up @@ -60,10 +62,25 @@ List<ClientRepresentation> findAll(@QueryParam("clientId") String clientId,
@QueryParam("first") Integer firstResult,
@QueryParam("max") Integer maxResults);

/** @deprecated use {@link #findClientByClientId(String)} */
@Deprecated
@GET
@Produces(MediaType.APPLICATION_JSON)
List<ClientRepresentation> findByClientId(@QueryParam("clientId") String clientId);

default Optional<ClientRepresentation> findClientByClientId(String clientId) {
List<ClientRepresentation> list = findByClientId(clientId);
if (list.isEmpty()) {
return Optional.<ClientRepresentation>empty();
}
return Optional.of(list.get(0));
}

default ClientResource getByClientId(String id) {
return get(findClientByClientId(id).map(ClientRepresentation::getId)
.orElseThrow(() -> new NotFoundException("client not found " + id)));
}

@GET
@Produces(MediaType.APPLICATION_JSON)
List<ClientRepresentation> query(@QueryParam("q") String searchQuery);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.keycloak.testframework.realm;

import java.util.List;

import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
Expand Down Expand Up @@ -40,11 +38,7 @@ public ManagedClient getValue(InstanceContext<ManagedClient, InjectClient> insta
}
clientRepresentation.setId(ApiUtil.getCreatedId(response));
} else {
List<ClientRepresentation> clients = realm.admin().clients().findByClientId(attachTo);
if (clients.isEmpty()) {
throw new IllegalStateException("No client found with client id: " + attachTo);
}
clientRepresentation = clients.get(0);
clientRepresentation = realm.admin().clients().findClientByClientId(attachTo).orElseThrow();
}

instanceContext.addNote("managed", managed);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.keycloak.test.examples;

import java.util.List;

import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.testframework.annotations.InjectClient;
import org.keycloak.testframework.annotations.InjectRealm;
Expand Down Expand Up @@ -33,8 +31,8 @@ public void testCreatedRealm() {
public void testCreatedClient() {
Assertions.assertEquals("default", client.getClientId());

List<ClientRepresentation> clients = realm.admin().clients().findByClientId("default");
Assertions.assertEquals(1, clients.size());
ClientRepresentation client = realm.admin().clients().findClientByClientId("default").orElse(null);
Assertions.assertNotNull(client);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ public void testAdminClientIssuers() throws JWSInputException {
public void testRealmWithClientAndUser() {
RealmResource realmResource = realmAdminClient.realms().realm(realm.getName());

List<ClientRepresentation> clients = realmResource.clients().findByClientId("myclient");
Assertions.assertEquals(1, clients.size());

ClientRepresentation client = clients.get(0);
ClientRepresentation client = realmResource.clients().findClientByClientId("myclient").orElseThrow();
Assertions.assertTrue(client.isEnabled());
Assertions.assertTrue(client.isDirectAccessGrantsEnabled());
Assertions.assertEquals("mysecret", client.getSecret());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public class TestAppTest {

@Test
public void testPushNotBefore() throws InterruptedException {
String clientUuid = managedRealm.admin().clients().findByClientId("test-app").stream().findFirst().get().getId();
managedRealm.admin().clients().get(clientUuid).pushRevocation();
managedRealm.admin().clients().getByClientId("test-app").pushRevocation();

PushNotBeforeAction adminPushNotBefore = testApp.kcAdmin().getAdminPushNotBefore();
Assertions.assertNotNull(adminPushNotBefore);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void beforeEach() { // todo rewrite
String roleUserUuid = ApiUtil.getCreatedId(response);
managedMasterRealm.cleanup().add(r -> r.users().delete(roleUserUuid));

String clientUuid = managedMasterRealm.admin().clients().findByClientId(REALM_NAME + "-realm").get(0).getId();
String clientUuid = managedMasterRealm.admin().clients().findClientByClientId(REALM_NAME + "-realm").orElseThrow().getId();
RoleRepresentation roleRep = managedMasterRealm.admin().clients().get(clientUuid).roles().get(role).toRepresentation();
managedMasterRealm.admin().users().get(roleUserUuid).roles().clientLevel(clientUuid).add(Collections.singletonList(roleRep));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class AuthzCleanupTest {
@Test
public void testCreate() throws Exception {
ClientsResource clients = managedRealm.admin().clients();
ClientRepresentation client = clients.findByClientId(clientId).get(0);
ClientRepresentation client = clients.findClientByClientId(clientId).orElseThrow();
ResourceServerRepresentation settings = JsonSerialization.readValue(AuthzCleanupTest.class.getResourceAsStream("authz/acme-resource-server-cleanup-test.json"), ResourceServerRepresentation.class);

clients.get(client.getId()).authorization().importSettings(settings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ public void scopes() {

AdminEventAssertion.assertEvent(adminEvents.poll(), OperationType.CREATE, AdminEventPaths.roleResourceCompositesPath("realm-composite"), List.of(roleRep2), ResourceType.REALM_ROLE);

String accountMgmtId = managedRealm.admin().clients().findByClientId(Constants.ACCOUNT_MANAGEMENT_CLIENT_ID).get(0).getId();
String accountMgmtId = managedRealm.admin().clients().findClientByClientId(Constants.ACCOUNT_MANAGEMENT_CLIENT_ID).orElseThrow().getId();
RoleRepresentation viewAccountRoleRep = managedRealm.admin().clients().get(accountMgmtId).roles().get(AccountRoles.VIEW_PROFILE).toRepresentation();

scopesResource.realmLevel().add(List.of(roleRep1));
Expand Down Expand Up @@ -970,6 +970,12 @@ public void updateClientWithProtocolMapper() {
ClientRepresentation storedClient = managedRealm.admin().clients().get(client.getId()).toRepresentation();
assertClient(client, storedClient);
}
@Test
@SuppressWarnings("deprecation")
void findByClientListResult() {
assertTrue( managedRealm.admin().clients().findByClientId("client-0").isEmpty());
assertEquals(1, managedRealm.admin().clients().findByClientId(Constants.ACCOUNT_MANAGEMENT_CLIENT_ID).size());
}

public static void assertClient(ClientRepresentation client, ClientRepresentation storedClient) {
if (client.getClientId() != null) Assert.assertEquals(client.getClientId(), storedClient.getClientId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public void testRetrieveConsentsForUserWithClientsWithGrantedOfflineAccess() thr
providerRealm.admin().update(providerRealmRep);
providerRealm.admin().clients().create(ClientConfigBuilder.create().clientId("test-app").redirectUris("*").publicClient(true).webOrigins("*").build());

ClientRepresentation providerAccountRep = providerRealm.admin().clients().findByClientId("test-app").get(0);
ClientRepresentation providerAccountRep = providerRealm.admin().clients().findClientByClientId("test-app").orElseThrow();

// add offline_scope to default account-console client scope
ClientScopeRepresentation offlineAccessScope = providerRealm.admin().getDefaultOptionalClientScopes().stream()
Expand Down Expand Up @@ -314,7 +314,7 @@ public void clientConsentRequiredAfterLogin() {
userRealm.updateWithCleanup(r -> r.enabledEventTypes("REFRESH_TOKEN_ERROR"));
String sessionId = loginEvent.getSessionId();

ClientRepresentation clientRepresentation = userRealm.admin().clients().findByClientId("test-app").get(0);
ClientRepresentation clientRepresentation = userRealm.admin().clients().findClientByClientId("test-app").orElseThrow();
try {
clientRepresentation.setConsentRequired(true);
userRealm.admin().clients().get(clientRepresentation.getId()).update(clientRepresentation);
Expand Down
Loading
Loading