From e17295d04acb8d0c3c8e7f3cd18c832b3b555855 Mon Sep 17 00:00:00 2001 From: rmartinc Date: Thu, 23 Nov 2023 13:30:30 +0100 Subject: [PATCH] Allow duplicated keys in the HardcodedKeyLocator Closes https://github.com/keycloak/keycloak/issues/24961 Signed-off-by: rmartinc --- .../java/org/keycloak/rotation/HardcodedKeyLocator.java | 4 ++-- .../org/keycloak/rotation/HardcodedKeyLocatorTest.java | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/saml-core/src/main/java/org/keycloak/rotation/HardcodedKeyLocator.java b/saml-core/src/main/java/org/keycloak/rotation/HardcodedKeyLocator.java index c7797ec5f5..74bfa1f18d 100644 --- a/saml-core/src/main/java/org/keycloak/rotation/HardcodedKeyLocator.java +++ b/saml-core/src/main/java/org/keycloak/rotation/HardcodedKeyLocator.java @@ -46,14 +46,14 @@ public class HardcodedKeyLocator implements KeyLocator, Iterable { Objects.requireNonNull(keys, "Keys must not be null"); this.byName = Collections.emptyMap(); this.byKey = Collections.unmodifiableMap(keys.stream().collect( - Collectors.toMap(k -> new KeyHash(k), k -> k))); + Collectors.toMap(k -> new KeyHash(k), k -> k, (k1, k2) -> k1))); } public HardcodedKeyLocator(Map keys) { Objects.requireNonNull(keys, "Keys must not be null"); this.byName = Collections.unmodifiableMap(keys); this.byKey = Collections.unmodifiableMap(keys.values().stream().collect( - Collectors.toMap(k -> new KeyHash(k), k -> k))); + Collectors.toMap(k -> new KeyHash(k), k -> k, (k1, k2) -> k1))); } @Override diff --git a/saml-core/src/test/java/org/keycloak/rotation/HardcodedKeyLocatorTest.java b/saml-core/src/test/java/org/keycloak/rotation/HardcodedKeyLocatorTest.java index aaaa935cf3..3ea6c21f73 100644 --- a/saml-core/src/test/java/org/keycloak/rotation/HardcodedKeyLocatorTest.java +++ b/saml-core/src/test/java/org/keycloak/rotation/HardcodedKeyLocatorTest.java @@ -139,4 +139,13 @@ public class HardcodedKeyLocatorTest { Assert.assertNotNull(found); Assert.assertEquals(cert1.getPublicKey(), found); } + + @Test + public void testDuplicateKey() throws Exception { + KeyLocator locator = createLocatorWithoutName(cert1, cert1); + KeyInfo info = XMLSignatureUtil.createKeyInfo(null, null, cert1); + Key found = locator.getKey(info); + Assert.assertNotNull(found); + Assert.assertEquals(cert1.getPublicKey(), found); + } }