|
| 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