Skip to content

Commit 09d07ae

Browse files
committed
Allow supplying a custom map to InMemoryOAuth2AuthorizedClientService
1 parent db3162e commit 09d07ae

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
* @see Authentication
3737
*/
3838
public final class InMemoryOAuth2AuthorizedClientService implements OAuth2AuthorizedClientService {
39-
private final Map<OAuth2AuthorizedClientId, OAuth2AuthorizedClient> authorizedClients = new ConcurrentHashMap<>();
4039
private final ClientRegistrationRepository clientRegistrationRepository;
40+
private Map<OAuth2AuthorizedClientId, OAuth2AuthorizedClient> authorizedClients = new ConcurrentHashMap<>();
4141

4242
/**
4343
* Constructs an {@code InMemoryOAuth2AuthorizedClientService} using the provided parameters.
@@ -49,6 +49,15 @@ public InMemoryOAuth2AuthorizedClientService(ClientRegistrationRepository client
4949
this.clientRegistrationRepository = clientRegistrationRepository;
5050
}
5151

52+
/**
53+
* Sets the map of authorized clients to use.
54+
* @param authorizedClients the map of authorized clients
55+
*/
56+
public void setAuthorizedClients(Map<OAuth2AuthorizedClientId, OAuth2AuthorizedClient> authorizedClients) {
57+
Assert.notNull(authorizedClients, "authorizedClients cannot be null");
58+
this.authorizedClients = authorizedClients;
59+
}
60+
5261
@Override
5362
@SuppressWarnings("unchecked")
5463
public <T extends OAuth2AuthorizedClient> T loadAuthorizedClient(String clientRegistrationId, String principalName) {

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

Lines changed: 33 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.
@@ -15,7 +15,11 @@
1515
*/
1616
package org.springframework.security.oauth2.client;
1717

18+
import java.util.Collections;
19+
import java.util.Map;
20+
1821
import org.junit.Test;
22+
1923
import org.springframework.security.core.Authentication;
2024
import org.springframework.security.oauth2.client.registration.ClientRegistration;
2125
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
@@ -24,13 +28,17 @@
2428
import org.springframework.security.oauth2.core.OAuth2AccessToken;
2529

2630
import static org.assertj.core.api.Assertions.assertThat;
31+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
32+
import static org.mockito.ArgumentMatchers.eq;
33+
import static org.mockito.BDDMockito.given;
2734
import static org.mockito.Mockito.mock;
2835
import static org.mockito.Mockito.when;
2936

3037
/**
3138
* Tests for {@link InMemoryOAuth2AuthorizedClientService}.
3239
*
3340
* @author Joe Grandja
41+
* @author Vedran Pavic
3442
*/
3543
public class InMemoryOAuth2AuthorizedClientServiceTests {
3644
private String principalName1 = "principal-1";
@@ -57,6 +65,30 @@ public void constructorWhenClientRegistrationRepositoryIsNullThenThrowIllegalArg
5765
new InMemoryOAuth2AuthorizedClientService(null);
5866
}
5967

68+
@Test
69+
public void constructorWhenAuthorizedClientsIsNullThenIllegalArgumentException() {
70+
assertThatExceptionOfType(IllegalArgumentException.class)
71+
.isThrownBy(() -> this.authorizedClientService.setAuthorizedClients(null))
72+
.withMessage("authorizedClients cannot be null");
73+
}
74+
75+
@Test
76+
public void constructorWhenAuthorizedClientsIsEmptyMapThenRepositoryUsingSuppliedAuthorizedClients() {
77+
String registrationId = this.registration3.getRegistrationId();
78+
79+
Map<OAuth2AuthorizedClientId, OAuth2AuthorizedClient> authorizedClients = Collections.singletonMap(
80+
OAuth2AuthorizedClientId.create(this.registration3, this.principalName1),
81+
mock(OAuth2AuthorizedClient.class));
82+
ClientRegistrationRepository clientRegistrationRepository = mock(ClientRegistrationRepository.class);
83+
given(clientRegistrationRepository.findByRegistrationId(eq(registrationId))).willReturn(this.registration3);
84+
85+
InMemoryOAuth2AuthorizedClientService authorizedClientService = new InMemoryOAuth2AuthorizedClientService(
86+
this.clientRegistrationRepository);
87+
authorizedClientService.setAuthorizedClients(authorizedClients);
88+
assertThat((OAuth2AuthorizedClient) authorizedClientService.loadAuthorizedClient(
89+
registrationId, this.principalName1)).isNotNull();
90+
}
91+
6092
@Test(expected = IllegalArgumentException.class)
6193
public void loadAuthorizedClientWhenClientRegistrationIdIsNullThenThrowIllegalArgumentException() {
6294
this.authorizedClientService.loadAuthorizedClient(null, this.principalName1);

0 commit comments

Comments
 (0)