Skip to content

Commit e1b095d

Browse files
vpavicjgrandja
authored andcommitted
Allow in-memory client registration repos to be constructed with a map
Fixes gh-5918
1 parent 8f49ca8 commit e1b095d

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/registration/InMemoryClientRegistrationRepository.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@
3434
*
3535
* @author Joe Grandja
3636
* @author Rob Winch
37+
* @author Vedran Pavic
3738
* @since 5.0
3839
* @see ClientRegistrationRepository
3940
* @see ClientRegistration
@@ -56,11 +57,26 @@ public InMemoryClientRegistrationRepository(ClientRegistration... registrations)
5657
* @param registrations the client registration(s)
5758
*/
5859
public InMemoryClientRegistrationRepository(List<ClientRegistration> registrations) {
60+
this(createRegistrationsMap(registrations));
61+
}
62+
63+
private static Map<String, ClientRegistration> createRegistrationsMap(List<ClientRegistration> registrations) {
5964
Assert.notEmpty(registrations, "registrations cannot be empty");
6065
Collector<ClientRegistration, ?, ConcurrentMap<String, ClientRegistration>> collector =
61-
toConcurrentMap(ClientRegistration::getRegistrationId, Function.identity());
62-
this.registrations = registrations.stream()
63-
.collect(collectingAndThen(collector, Collections::unmodifiableMap));
66+
toConcurrentMap(ClientRegistration::getRegistrationId, Function.identity());
67+
return registrations.stream().collect(collectingAndThen(collector, Collections::unmodifiableMap));
68+
}
69+
70+
/**
71+
* Constructs an {@code InMemoryClientRegistrationRepository} using the provided {@code Map}
72+
* of {@link ClientRegistration#getRegistrationId() registration id} to {@link ClientRegistration}.
73+
*
74+
* @since 5.2
75+
* @param registrations the {@code Map} of client registration(s)
76+
*/
77+
public InMemoryClientRegistrationRepository(Map<String, ClientRegistration> registrations) {
78+
Assert.notNull(registrations, "registrations cannot be null");
79+
this.registrations = registrations;
6480
}
6581

6682
@Override

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/registration/InMemoryReactiveClientRegistrationRepository.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* A Reactive {@link ClientRegistrationRepository} that stores {@link ClientRegistration}(s) in-memory.
3131
*
3232
* @author Rob Winch
33+
* @author Vedran Pavic
3334
* @since 5.1
3435
* @see ClientRegistrationRepository
3536
* @see ClientRegistration
@@ -64,6 +65,18 @@ public InMemoryReactiveClientRegistrationRepository(List<ClientRegistration> reg
6465
.collect(Collectors.toConcurrentMap(ClientRegistration::getRegistrationId, Function.identity()));
6566
}
6667

68+
/**
69+
* Constructs an {@code InMemoryReactiveClientRegistrationRepository} using the provided {@code Map}
70+
* of {@link ClientRegistration#getRegistrationId() registration id} to {@link ClientRegistration}.
71+
* <b>NOTE:</b> The supplied {@code Map} must be a non-blocking {@code Map}.
72+
*
73+
* @since 5.2
74+
* @param registrations the {@code Map} of client registration(s)
75+
*/
76+
public InMemoryReactiveClientRegistrationRepository(Map<String, ClientRegistration> registrations) {
77+
Assert.notNull(registrations, "registrations cannot be null");
78+
this.clientIdToClientRegistration = registrations;
79+
}
6780

6881
@Override
6982
public Mono<ClientRegistration> findByRegistrationId(String registrationId) {

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/registration/InMemoryClientRegistrationRepositoryTests.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,14 +20,17 @@
2020

2121
import java.util.Arrays;
2222
import java.util.Collections;
23+
import java.util.HashMap;
2324
import java.util.List;
25+
import java.util.Map;
2426

2527
import static org.assertj.core.api.Assertions.assertThat;
2628

2729
/**
2830
* Tests for {@link InMemoryClientRegistrationRepository}.
2931
*
3032
* @author Rob Winch
33+
* @author Vedran Pavic
3134
* @since 5.0
3235
*/
3336
public class InMemoryClientRegistrationRepositoryTests {
@@ -53,6 +56,17 @@ public void constructorListClientRegistrationWhenDuplicateIdThenIllegalArgumentE
5356
new InMemoryClientRegistrationRepository(registrations);
5457
}
5558

59+
@Test(expected = IllegalArgumentException.class)
60+
public void constructorMapClientRegistrationWhenNullThenIllegalArgumentException() {
61+
new InMemoryClientRegistrationRepository((Map<String, ClientRegistration>) null);
62+
}
63+
64+
@Test
65+
public void constructorMapClientRegistrationWhenEmptyMapThenRepositoryIsEmpty() {
66+
InMemoryClientRegistrationRepository clients = new InMemoryClientRegistrationRepository(new HashMap<>());
67+
assertThat(clients).isEmpty();
68+
}
69+
5670
@Test
5771
public void findByRegistrationIdWhenFoundThenFound() {
5872
String id = this.registration.getRegistrationId();

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/registration/InMemoryReactiveClientRegistrationRepositoryTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import static org.assertj.core.api.Assertions.assertThat;
2020
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2121

22+
import java.util.HashMap;
2223
import java.util.List;
24+
import java.util.Map;
2325

2426
import org.junit.Before;
2527
import org.junit.Test;
@@ -28,6 +30,7 @@
2830

2931
/**
3032
* @author Rob Winch
33+
* @author Vedran Pavic
3134
* @since 5.1
3235
*/
3336
public class InMemoryReactiveClientRegistrationRepositoryTests {
@@ -68,6 +71,20 @@ public void constructorWhenClientRegistrationIsNullThenIllegalArgumentException(
6871
.isInstanceOf(IllegalArgumentException.class);
6972
}
7073

74+
@Test
75+
public void constructorWhenClientRegistrationMapIsNullThenIllegalArgumentException() {
76+
Map<String, ClientRegistration> registrations = null;
77+
assertThatThrownBy(() -> new InMemoryReactiveClientRegistrationRepository(registrations))
78+
.isInstanceOf(IllegalArgumentException.class);
79+
}
80+
81+
@Test
82+
public void constructorWhenClientRegistrationMapIsEmptyThenRepositoryIsEmpty() {
83+
InMemoryReactiveClientRegistrationRepository repository = new InMemoryReactiveClientRegistrationRepository(
84+
new HashMap<>());
85+
assertThat(repository).isEmpty();
86+
}
87+
7188
@Test
7289
public void findByRegistrationIdWhenValidIdThenFound() {
7390
StepVerifier.create(this.repository.findByRegistrationId(this.registration.getRegistrationId()))

0 commit comments

Comments
 (0)