From 1df60461a996f0fa6bfd49cfd6f4e77661e3875c Mon Sep 17 00:00:00 2001 From: Giuseppe Graziano Date: Tue, 18 Jun 2024 14:18:09 +0200 Subject: [PATCH] Avoid race condition when using initial-access-token Closes #27294 Signed-off-by: Giuseppe Graziano --- .../org/keycloak/models/jpa/RealmAdapter.java | 4 +- .../client/ClientRegistrationTest.java | 65 +++++++++++++++++-- 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/RealmAdapter.java b/model/jpa/src/main/java/org/keycloak/models/jpa/RealmAdapter.java index 73caea59e9..53b4a9693f 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/RealmAdapter.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/RealmAdapter.java @@ -2138,7 +2138,7 @@ public class RealmAdapter implements StorageProviderRealmModel, JpaModel> futures = new LinkedList<>(); + for (int i = 0; i < iterations; i ++) { + final int j = i; + + Callable f = () -> { + ClientRegistration client = ClientRegistration.create().url(suiteContext.getAuthServerInfo().getContextRoot() + "/auth", "test").build(); + client.auth(Auth.token(response)); + ClientRepresentation rep = new ClientRepresentation(); + rep.setClientId("test-" + j); + rep = client.create(rep); + if(rep.getId() != null && rep.getClientId().equals("test-" + j)) { + createdCount.getAndIncrement(); + } + return null; + }; + futures.add(f); + } + threadPool.invokeAll(futures); + + } catch (Exception ex) { + fail(ex.getMessage()); + } + + //controls the number of uses of the initial access token + assertEquals(initialTokenCounts, createdCount.get()); + } }