xref: /OpenGrok/plugins/src/main/java/opengrok/auth/plugin/ldap/FakeLdapFacade.java (revision 9d74bf96c386aa7bedff220480c7da5f7bfead79)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * See LICENSE.txt included in this distribution for the specific
9  * language governing permissions and limitations under the License.
10  *
11  * When distributing Covered Code, include this CDDL HEADER in each
12  * file and include the License file at LICENSE.txt.
13  * If applicable, add the following below this CDDL HEADER, with the
14  * fields enclosed by brackets "[]" replaced with your own identifying
15  * information: Portions Copyright [yyyy] [name of copyright owner]
16  *
17  * CDDL HEADER END
18  */
19 
20 /*
21  * Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
22  */
23 package opengrok.auth.plugin.ldap;
24 
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.TreeMap;
30 import java.util.TreeSet;
31 
32 /**
33  * LDAP facade for testing.
34  * @author Krystof Tulinger
35  */
36 public class FakeLdapFacade extends AbstractLdapProvider {
37 
38     @Override
lookupLdapContent(String dn, String filter, String[] values)39     public LdapSearchResult<Map<String, Set<String>>> lookupLdapContent(String dn, String filter, String[] values) {
40         Map<String, Set<String>> map = new TreeMap<>();
41 
42         filter = filter == null ? "objectclass=*" : filter;
43         values = values == null ? new String[]{"dn", "ou"} : values;
44 
45         if ("objectclass=*".equals(filter)) {
46             List<String> v = Arrays.asList(values);
47             if (v.isEmpty()) {
48                 map.put("mail", new TreeSet<>(List.of("james@bond.com")));
49                 map.put("ou", new TreeSet<>(List.of("MI6")));
50             } else {
51                 for (String x : v) {
52                     if (x.equals("uid")) {
53                         map.put("uid", new TreeSet<>(List.of("bondjame")));
54                     }
55                 }
56             }
57             return new LdapSearchResult<>("fakedn", map);
58         }
59 
60         if (filter.contains("objectclass")) {
61             map.put("dn", new TreeSet<>(Arrays.asList("cn=mi6,cn=mi6,cn=james,dc=bond,dc=com",
62                     "cn=mi7,cn=mi7,cn=james,dc=bond,dc=com")));
63         }
64 
65         return new LdapSearchResult<>("fakedn", map);
66     }
67 
68     @Override
isConfigured()69     public boolean isConfigured() {
70         return true;
71     }
72 
73     @Override
close()74     public void close() {
75         // No need to close anything as this is fake plugin.
76     }
77 }
78