Skip to content

Commit 23d61d4

Browse files
committed
Polish #5994
1 parent 9432670 commit 23d61d4

File tree

5 files changed

+86
-103
lines changed

5 files changed

+86
-103
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/InMemoryOAuth2AuthorizedClientService.java

+18-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -32,12 +32,13 @@
3232
* @since 5.0
3333
* @see OAuth2AuthorizedClientService
3434
* @see OAuth2AuthorizedClient
35+
* @see OAuth2AuthorizedClientId
3536
* @see ClientRegistration
3637
* @see Authentication
3738
*/
3839
public final class InMemoryOAuth2AuthorizedClientService implements OAuth2AuthorizedClientService {
40+
private final Map<OAuth2AuthorizedClientId, OAuth2AuthorizedClient> authorizedClients;
3941
private final ClientRegistrationRepository clientRegistrationRepository;
40-
private Map<OAuth2AuthorizedClientId, OAuth2AuthorizedClient> authorizedClients = new ConcurrentHashMap<>();
4142

4243
/**
4344
* Constructs an {@code InMemoryOAuth2AuthorizedClientService} using the provided parameters.
@@ -47,15 +48,22 @@ public final class InMemoryOAuth2AuthorizedClientService implements OAuth2Author
4748
public InMemoryOAuth2AuthorizedClientService(ClientRegistrationRepository clientRegistrationRepository) {
4849
Assert.notNull(clientRegistrationRepository, "clientRegistrationRepository cannot be null");
4950
this.clientRegistrationRepository = clientRegistrationRepository;
51+
this.authorizedClients = new ConcurrentHashMap<>();
5052
}
5153

5254
/**
53-
* Sets the map of authorized clients to use.
54-
* @param authorizedClients the map of authorized clients
55+
* Constructs an {@code InMemoryOAuth2AuthorizedClientService} using the provided parameters.
56+
*
57+
* @since 5.2
58+
* @param clientRegistrationRepository the repository of client registrations
59+
* @param authorizedClients the initial {@code Map} of authorized client(s) keyed by {@link OAuth2AuthorizedClientId}
5560
*/
56-
public void setAuthorizedClients(Map<OAuth2AuthorizedClientId, OAuth2AuthorizedClient> authorizedClients) {
57-
Assert.notNull(authorizedClients, "authorizedClients cannot be null");
58-
this.authorizedClients = authorizedClients;
61+
public InMemoryOAuth2AuthorizedClientService(ClientRegistrationRepository clientRegistrationRepository,
62+
Map<OAuth2AuthorizedClientId, OAuth2AuthorizedClient> authorizedClients) {
63+
Assert.notNull(clientRegistrationRepository, "clientRegistrationRepository cannot be null");
64+
Assert.notEmpty(authorizedClients, "authorizedClients cannot be empty");
65+
this.clientRegistrationRepository = clientRegistrationRepository;
66+
this.authorizedClients = new ConcurrentHashMap<>(authorizedClients);
5967
}
6068

6169
@Override
@@ -67,14 +75,14 @@ public <T extends OAuth2AuthorizedClient> T loadAuthorizedClient(String clientRe
6775
if (registration == null) {
6876
return null;
6977
}
70-
return (T) this.authorizedClients.get(OAuth2AuthorizedClientId.create(registration, principalName));
78+
return (T) this.authorizedClients.get(new OAuth2AuthorizedClientId(clientRegistrationId, principalName));
7179
}
7280

7381
@Override
7482
public void saveAuthorizedClient(OAuth2AuthorizedClient authorizedClient, Authentication principal) {
7583
Assert.notNull(authorizedClient, "authorizedClient cannot be null");
7684
Assert.notNull(principal, "principal cannot be null");
77-
this.authorizedClients.put(OAuth2AuthorizedClientId.create(authorizedClient.getClientRegistration(),
85+
this.authorizedClients.put(new OAuth2AuthorizedClientId(authorizedClient.getClientRegistration().getRegistrationId(),
7886
principal.getName()), authorizedClient);
7987
}
8088

@@ -84,8 +92,7 @@ public void removeAuthorizedClient(String clientRegistrationId, String principal
8492
Assert.hasText(principalName, "principalName cannot be empty");
8593
ClientRegistration registration = this.clientRegistrationRepository.findByRegistrationId(clientRegistrationId);
8694
if (registration != null) {
87-
this.authorizedClients.remove(OAuth2AuthorizedClientId.create(registration, principalName));
95+
this.authorizedClients.remove(new OAuth2AuthorizedClientId(clientRegistrationId, principalName));
8896
}
8997
}
90-
9198
}

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/InMemoryReactiveOAuth2AuthorizedClientService.java

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -15,16 +15,15 @@
1515
*/
1616
package org.springframework.security.oauth2.client;
1717

18-
import java.util.Map;
19-
import java.util.concurrent.ConcurrentHashMap;
20-
2118
import org.springframework.security.core.Authentication;
2219
import org.springframework.security.oauth2.client.registration.ClientRegistration;
2320
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
2421
import org.springframework.util.Assert;
25-
2622
import reactor.core.publisher.Mono;
2723

24+
import java.util.Map;
25+
import java.util.concurrent.ConcurrentHashMap;
26+
2827
/**
2928
* An {@link OAuth2AuthorizedClientService} that stores
3029
* {@link OAuth2AuthorizedClient Authorized Client(s)} in-memory.
@@ -38,11 +37,11 @@
3837
* @see Authentication
3938
*/
4039
public final class InMemoryReactiveOAuth2AuthorizedClientService implements ReactiveOAuth2AuthorizedClientService {
41-
private final Map<OAuth2AuthorizedClientId, OAuth2AuthorizedClient> authorizedClients = new ConcurrentHashMap<>();;
40+
private final Map<OAuth2AuthorizedClientId, OAuth2AuthorizedClient> authorizedClients = new ConcurrentHashMap<>();
4241
private final ReactiveClientRegistrationRepository clientRegistrationRepository;
4342

4443
/**
45-
* Constructs an {@code InMemoryOAuth2AuthorizedClientService} using the provided parameters.
44+
* Constructs an {@code InMemoryReactiveOAuth2AuthorizedClientService} using the provided parameters.
4645
*
4746
* @param clientRegistrationRepository the repository of client registrations
4847
*/
@@ -57,7 +56,7 @@ public <T extends OAuth2AuthorizedClient> Mono<T> loadAuthorizedClient(String cl
5756
Assert.hasText(clientRegistrationId, "clientRegistrationId cannot be empty");
5857
Assert.hasText(principalName, "principalName cannot be empty");
5958
return (Mono<T>) this.clientRegistrationRepository.findByRegistrationId(clientRegistrationId)
60-
.map(clientRegistration -> OAuth2AuthorizedClientId.create(clientRegistration, principalName))
59+
.map(clientRegistration -> new OAuth2AuthorizedClientId(clientRegistrationId, principalName))
6160
.flatMap(identifier -> Mono.justOrEmpty(this.authorizedClients.get(identifier)));
6261
}
6362

@@ -66,8 +65,8 @@ public Mono<Void> saveAuthorizedClient(OAuth2AuthorizedClient authorizedClient,
6665
Assert.notNull(authorizedClient, "authorizedClient cannot be null");
6766
Assert.notNull(principal, "principal cannot be null");
6867
return Mono.fromRunnable(() -> {
69-
OAuth2AuthorizedClientId identifier = OAuth2AuthorizedClientId.create(
70-
authorizedClient.getClientRegistration(), principal.getName());
68+
OAuth2AuthorizedClientId identifier = new OAuth2AuthorizedClientId(
69+
authorizedClient.getClientRegistration().getRegistrationId(), principal.getName());
7170
this.authorizedClients.put(identifier, authorizedClient);
7271
});
7372
}
@@ -77,9 +76,8 @@ public Mono<Void> removeAuthorizedClient(String clientRegistrationId, String pri
7776
Assert.hasText(clientRegistrationId, "clientRegistrationId cannot be empty");
7877
Assert.hasText(principalName, "principalName cannot be empty");
7978
return this.clientRegistrationRepository.findByRegistrationId(clientRegistrationId)
80-
.map(clientRegistration -> OAuth2AuthorizedClientId.create(clientRegistration, principalName))
79+
.map(clientRegistration -> new OAuth2AuthorizedClientId(clientRegistrationId, principalName))
8180
.doOnNext(this.authorizedClients::remove)
8281
.then(Mono.empty());
8382
}
84-
8583
}
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.springframework.security.oauth2.client;
1817

18+
import org.springframework.security.core.SpringSecurityCoreVersion;
19+
import org.springframework.util.Assert;
20+
1921
import java.io.Serializable;
2022
import java.util.Objects;
2123

22-
import org.springframework.security.oauth2.client.registration.ClientRegistration;
23-
import org.springframework.util.Assert;
24-
2524
/**
2625
* The identifier for {@link OAuth2AuthorizedClient}.
2726
*
@@ -31,37 +30,29 @@
3130
* @see OAuth2AuthorizedClientService
3231
*/
3332
public final class OAuth2AuthorizedClientId implements Serializable {
34-
33+
private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
3534
private final String clientRegistrationId;
36-
3735
private final String principalName;
3836

39-
private OAuth2AuthorizedClientId(String clientRegistrationId, String principalName) {
40-
Assert.notNull(clientRegistrationId, "clientRegistrationId cannot be null");
41-
Assert.notNull(principalName, "principalName cannot be null");
42-
this.clientRegistrationId = clientRegistrationId;
43-
this.principalName = principalName;
44-
}
45-
4637
/**
47-
* Factory method for creating new {@link OAuth2AuthorizedClientId} using
48-
* {@link ClientRegistration} and principal name.
49-
* @param clientRegistration the client registration
50-
* @param principalName the principal name
51-
* @return the new authorized client id
38+
* Constructs an {@code OAuth2AuthorizedClientId} using the provided parameters.
39+
*
40+
* @param clientRegistrationId the identifier for the client's registration
41+
* @param principalName the name of the End-User {@code Principal} (Resource Owner)
5242
*/
53-
public static OAuth2AuthorizedClientId create(ClientRegistration clientRegistration,
54-
String principalName) {
55-
return new OAuth2AuthorizedClientId(clientRegistration.getRegistrationId(),
56-
principalName);
43+
public OAuth2AuthorizedClientId(String clientRegistrationId, String principalName) {
44+
Assert.hasText(clientRegistrationId, "clientRegistrationId cannot be empty");
45+
Assert.hasText(principalName, "principalName cannot be empty");
46+
this.clientRegistrationId = clientRegistrationId;
47+
this.principalName = principalName;
5748
}
5849

5950
@Override
6051
public boolean equals(Object obj) {
6152
if (this == obj) {
6253
return true;
6354
}
64-
if (obj == null || getClass() != obj.getClass()) {
55+
if (obj == null || this.getClass() != obj.getClass()) {
6556
return false;
6657
}
6758
OAuth2AuthorizedClientId that = (OAuth2AuthorizedClientId) obj;
@@ -73,5 +64,4 @@ public boolean equals(Object obj) {
7364
public int hashCode() {
7465
return Objects.hash(this.clientRegistrationId, this.principalName);
7566
}
76-
7767
}

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/InMemoryOAuth2AuthorizedClientServiceTests.java

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -15,22 +15,20 @@
1515
*/
1616
package org.springframework.security.oauth2.client;
1717

18-
import java.util.Collections;
19-
import java.util.Map;
20-
2118
import org.junit.Test;
22-
2319
import org.springframework.security.core.Authentication;
2420
import org.springframework.security.oauth2.client.registration.ClientRegistration;
2521
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
2622
import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository;
2723
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
2824
import org.springframework.security.oauth2.core.OAuth2AccessToken;
2925

26+
import java.util.Collections;
27+
import java.util.Map;
28+
3029
import static org.assertj.core.api.Assertions.assertThat;
31-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
30+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3231
import static org.mockito.ArgumentMatchers.eq;
33-
import static org.mockito.BDDMockito.given;
3432
import static org.mockito.Mockito.mock;
3533
import static org.mockito.Mockito.when;
3634

@@ -66,25 +64,24 @@ public void constructorWhenClientRegistrationRepositoryIsNullThenThrowIllegalArg
6664
}
6765

6866
@Test
69-
public void constructorWhenAuthorizedClientsIsNullThenIllegalArgumentException() {
70-
assertThatExceptionOfType(IllegalArgumentException.class)
71-
.isThrownBy(() -> this.authorizedClientService.setAuthorizedClients(null))
72-
.withMessage("authorizedClients cannot be null");
67+
public void constructorWhenAuthorizedClientsIsNullThenThrowIllegalArgumentException() {
68+
assertThatThrownBy(() -> new InMemoryOAuth2AuthorizedClientService(this.clientRegistrationRepository, null))
69+
.isInstanceOf(IllegalArgumentException.class)
70+
.hasMessage("authorizedClients cannot be empty");
7371
}
7472

7573
@Test
76-
public void constructorWhenAuthorizedClientsIsEmptyMapThenRepositoryUsingSuppliedAuthorizedClients() {
74+
public void constructorWhenAuthorizedClientsProvidedThenUseProvidedAuthorizedClients() {
7775
String registrationId = this.registration3.getRegistrationId();
7876

7977
Map<OAuth2AuthorizedClientId, OAuth2AuthorizedClient> authorizedClients = Collections.singletonMap(
80-
OAuth2AuthorizedClientId.create(this.registration3, this.principalName1),
78+
new OAuth2AuthorizedClientId(this.registration3.getRegistrationId(), this.principalName1),
8179
mock(OAuth2AuthorizedClient.class));
8280
ClientRegistrationRepository clientRegistrationRepository = mock(ClientRegistrationRepository.class);
83-
given(clientRegistrationRepository.findByRegistrationId(eq(registrationId))).willReturn(this.registration3);
81+
when(clientRegistrationRepository.findByRegistrationId(eq(registrationId))).thenReturn(this.registration3);
8482

8583
InMemoryOAuth2AuthorizedClientService authorizedClientService = new InMemoryOAuth2AuthorizedClientService(
86-
this.clientRegistrationRepository);
87-
authorizedClientService.setAuthorizedClients(authorizedClients);
84+
clientRegistrationRepository, authorizedClients);
8885
assertThat((OAuth2AuthorizedClient) authorizedClientService.loadAuthorizedClient(
8986
registrationId, this.principalName1)).isNotNull();
9087
}

0 commit comments

Comments
 (0)