From e1b095df3260c45c53408ef0a3360a7aa7c5073b Mon Sep 17 00:00:00 2001 From: Vedran Pavic Date: Thu, 4 Oct 2018 07:41:35 +0200 Subject: [PATCH] Allow in-memory client registration repos to be constructed with a map Fixes gh-5918 --- .../InMemoryClientRegistrationRepository.java | 24 +++++++++++++++---- ...yReactiveClientRegistrationRepository.java | 13 ++++++++++ ...moryClientRegistrationRepositoryTests.java | 16 ++++++++++++- ...tiveClientRegistrationRepositoryTests.java | 17 +++++++++++++ 4 files changed, 65 insertions(+), 5 deletions(-) diff --git a/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/registration/InMemoryClientRegistrationRepository.java b/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/registration/InMemoryClientRegistrationRepository.java index fa810e432b..ba61c9f172 100644 --- a/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/registration/InMemoryClientRegistrationRepository.java +++ b/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/registration/InMemoryClientRegistrationRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,7 @@ import static java.util.stream.Collectors.toConcurrentMap; * * @author Joe Grandja * @author Rob Winch + * @author Vedran Pavic * @since 5.0 * @see ClientRegistrationRepository * @see ClientRegistration @@ -56,11 +57,26 @@ public final class InMemoryClientRegistrationRepository implements ClientRegistr * @param registrations the client registration(s) */ public InMemoryClientRegistrationRepository(List registrations) { + this(createRegistrationsMap(registrations)); + } + + private static Map createRegistrationsMap(List registrations) { Assert.notEmpty(registrations, "registrations cannot be empty"); Collector> collector = - toConcurrentMap(ClientRegistration::getRegistrationId, Function.identity()); - this.registrations = registrations.stream() - .collect(collectingAndThen(collector, Collections::unmodifiableMap)); + toConcurrentMap(ClientRegistration::getRegistrationId, Function.identity()); + return registrations.stream().collect(collectingAndThen(collector, Collections::unmodifiableMap)); + } + + /** + * Constructs an {@code InMemoryClientRegistrationRepository} using the provided {@code Map} + * of {@link ClientRegistration#getRegistrationId() registration id} to {@link ClientRegistration}. + * + * @since 5.2 + * @param registrations the {@code Map} of client registration(s) + */ + public InMemoryClientRegistrationRepository(Map registrations) { + Assert.notNull(registrations, "registrations cannot be null"); + this.registrations = registrations; } @Override diff --git a/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/registration/InMemoryReactiveClientRegistrationRepository.java b/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/registration/InMemoryReactiveClientRegistrationRepository.java index fa291c6a4f..3697445d29 100644 --- a/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/registration/InMemoryReactiveClientRegistrationRepository.java +++ b/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/registration/InMemoryReactiveClientRegistrationRepository.java @@ -30,6 +30,7 @@ import reactor.core.publisher.Mono; * A Reactive {@link ClientRegistrationRepository} that stores {@link ClientRegistration}(s) in-memory. * * @author Rob Winch + * @author Vedran Pavic * @since 5.1 * @see ClientRegistrationRepository * @see ClientRegistration @@ -64,6 +65,18 @@ public final class InMemoryReactiveClientRegistrationRepository .collect(Collectors.toConcurrentMap(ClientRegistration::getRegistrationId, Function.identity())); } + /** + * Constructs an {@code InMemoryReactiveClientRegistrationRepository} using the provided {@code Map} + * of {@link ClientRegistration#getRegistrationId() registration id} to {@link ClientRegistration}. + * NOTE: The supplied {@code Map} must be a non-blocking {@code Map}. + * + * @since 5.2 + * @param registrations the {@code Map} of client registration(s) + */ + public InMemoryReactiveClientRegistrationRepository(Map registrations) { + Assert.notNull(registrations, "registrations cannot be null"); + this.clientIdToClientRegistration = registrations; + } @Override public Mono findByRegistrationId(String registrationId) { diff --git a/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/registration/InMemoryClientRegistrationRepositoryTests.java b/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/registration/InMemoryClientRegistrationRepositoryTests.java index efae7de7a9..5628f307b6 100644 --- a/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/registration/InMemoryClientRegistrationRepositoryTests.java +++ b/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/registration/InMemoryClientRegistrationRepositoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,9 @@ import org.junit.Test; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; @@ -28,6 +30,7 @@ import static org.assertj.core.api.Assertions.assertThat; * Tests for {@link InMemoryClientRegistrationRepository}. * * @author Rob Winch + * @author Vedran Pavic * @since 5.0 */ public class InMemoryClientRegistrationRepositoryTests { @@ -53,6 +56,17 @@ public class InMemoryClientRegistrationRepositoryTests { new InMemoryClientRegistrationRepository(registrations); } + @Test(expected = IllegalArgumentException.class) + public void constructorMapClientRegistrationWhenNullThenIllegalArgumentException() { + new InMemoryClientRegistrationRepository((Map) null); + } + + @Test + public void constructorMapClientRegistrationWhenEmptyMapThenRepositoryIsEmpty() { + InMemoryClientRegistrationRepository clients = new InMemoryClientRegistrationRepository(new HashMap<>()); + assertThat(clients).isEmpty(); + } + @Test public void findByRegistrationIdWhenFoundThenFound() { String id = this.registration.getRegistrationId(); diff --git a/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/registration/InMemoryReactiveClientRegistrationRepositoryTests.java b/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/registration/InMemoryReactiveClientRegistrationRepositoryTests.java index ba2ef432a4..359e384ff2 100644 --- a/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/registration/InMemoryReactiveClientRegistrationRepositoryTests.java +++ b/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/registration/InMemoryReactiveClientRegistrationRepositoryTests.java @@ -19,7 +19,9 @@ package org.springframework.security.oauth2.client.registration; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.junit.Before; import org.junit.Test; @@ -28,6 +30,7 @@ import reactor.test.StepVerifier; /** * @author Rob Winch + * @author Vedran Pavic * @since 5.1 */ public class InMemoryReactiveClientRegistrationRepositoryTests { @@ -68,6 +71,20 @@ public class InMemoryReactiveClientRegistrationRepositoryTests { .isInstanceOf(IllegalArgumentException.class); } + @Test + public void constructorWhenClientRegistrationMapIsNullThenIllegalArgumentException() { + Map registrations = null; + assertThatThrownBy(() -> new InMemoryReactiveClientRegistrationRepository(registrations)) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + public void constructorWhenClientRegistrationMapIsEmptyThenRepositoryIsEmpty() { + InMemoryReactiveClientRegistrationRepository repository = new InMemoryReactiveClientRegistrationRepository( + new HashMap<>()); + assertThat(repository).isEmpty(); + } + @Test public void findByRegistrationIdWhenValidIdThenFound() { StepVerifier.create(this.repository.findByRegistrationId(this.registration.getRegistrationId()))