Skip to content

Commit 52822fd

Browse files
Add implementation for in memory RegisteredClientRepository
Fixes spring-projectsgh-40
1 parent 0ae7e32 commit 52822fd

File tree

8 files changed

+1054
-3
lines changed

8 files changed

+1054
-3
lines changed

core/spring-authorization-server-core.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ dependencies {
1818

1919
provided 'javax.servlet:javax.servlet-api'
2020
}
21+
22+
jacoco {
23+
toolVersion = '0.8.5'
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.oauth2.server.authorization;
17+
18+
/**
19+
* Internal class used for serialization across Spring Security Authorization Server classes.
20+
*
21+
* @author Anoop Garlapati
22+
*/
23+
public class Version {
24+
/**
25+
* Global Serialization value for Spring Security Authorization Server classes.
26+
*/
27+
public static final long SERIAL_VERSION_UID = "0.0.1".hashCode();
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.oauth2.server.authorization.client;
17+
18+
import org.springframework.util.Assert;
19+
20+
import java.util.Arrays;
21+
import java.util.List;
22+
import java.util.Map;
23+
import java.util.concurrent.ConcurrentHashMap;
24+
25+
/**
26+
* A {@link RegisteredClientRepository} that stores {@link RegisteredClient}(s) in-memory.
27+
*
28+
* @author Anoop Garlapati
29+
* @see RegisteredClientRepository
30+
* @see RegisteredClient
31+
*/
32+
public final class InMemoryRegisteredClientRepository implements RegisteredClientRepository {
33+
private final Map<String, RegisteredClient> idRegistrationMap;
34+
private final Map<String, RegisteredClient> clientIdRegistrationMap;
35+
36+
/**
37+
* Constructs an {@code InMemoryRegisteredClientRepository} using the provided parameters.
38+
*
39+
* @param registrations the client registration(s)
40+
*/
41+
public InMemoryRegisteredClientRepository(RegisteredClient... registrations) {
42+
this(Arrays.asList(registrations));
43+
}
44+
45+
/**
46+
* Constructs an {@code InMemoryRegisteredClientRepository} using the provided parameters.
47+
*
48+
* @param registrations the client registration(s)
49+
*/
50+
public InMemoryRegisteredClientRepository(List<RegisteredClient> registrations) {
51+
Assert.notEmpty(registrations, "registrations cannot be empty");
52+
ConcurrentHashMap<String, RegisteredClient> idRegistrationMapResult = new ConcurrentHashMap<>();
53+
ConcurrentHashMap<String, RegisteredClient> clientIdRegistrationMapResult = new ConcurrentHashMap<>();
54+
for (RegisteredClient registration : registrations) {
55+
Assert.notNull(registration, "registration cannot be null");
56+
String id = registration.getId();
57+
if (idRegistrationMapResult.containsKey(id)) {
58+
throw new IllegalArgumentException("Registered client must be unique. " +
59+
"Found duplicate identifier: " + id);
60+
}
61+
String clientId = registration.getClientId();
62+
if (clientIdRegistrationMapResult.containsKey(clientId)) {
63+
throw new IllegalArgumentException("Registered client must be unique. " +
64+
"Found duplicate client identifier: " + clientId);
65+
}
66+
idRegistrationMapResult.put(id, registration);
67+
clientIdRegistrationMapResult.put(clientId, registration);
68+
}
69+
idRegistrationMap = idRegistrationMapResult;
70+
clientIdRegistrationMap = clientIdRegistrationMapResult;
71+
}
72+
73+
@Override
74+
public RegisteredClient findById(String id) {
75+
Assert.hasText(id, "id cannot be empty");
76+
return this.idRegistrationMap.get(id);
77+
}
78+
79+
@Override
80+
public RegisteredClient findByClientId(String clientId) {
81+
Assert.hasText(clientId, "clientId cannot be empty");
82+
return this.clientIdRegistrationMap.get(clientId);
83+
}
84+
}

0 commit comments

Comments
 (0)