Skip to content

Commit d0efd33

Browse files
Add support for Client Registration Model and InMemory Client Repository
Fixes spring-projectsgh-40
1 parent e822fbb commit d0efd33

File tree

6 files changed

+1032
-1
lines changed

6 files changed

+1032
-1
lines changed
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+
* @since 5.3
30+
* @see RegisteredClientRepository
31+
* @see RegisteredClient
32+
*/
33+
public final class InMemoryRegisteredClientRepository implements RegisteredClientRepository {
34+
private final Map<String, RegisteredClient> idRegistrationMap;
35+
private final Map<String, RegisteredClient> clientIdRegistrationMap;
36+
37+
/**
38+
* Constructs an {@code InMemoryRegisteredClientRepository} using the provided parameters.
39+
*
40+
* @param registrations the client registration(s)
41+
*/
42+
public InMemoryRegisteredClientRepository(RegisteredClient... registrations) {
43+
this(Arrays.asList(registrations));
44+
}
45+
46+
/**
47+
* Constructs an {@code InMemoryRegisteredClientRepository} using the provided parameters.
48+
*
49+
* @param registrations the client registration(s)
50+
*/
51+
public InMemoryRegisteredClientRepository(List<RegisteredClient> registrations) {
52+
Assert.notEmpty(registrations, "registrations cannot be empty");
53+
ConcurrentHashMap<String, RegisteredClient> idRegistrationMapResult = new ConcurrentHashMap<>();
54+
ConcurrentHashMap<String, RegisteredClient> clientIdRegistrationMapResult = new ConcurrentHashMap<>();
55+
for (RegisteredClient registration : registrations) {
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)