]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.core/src/org/argeo/osgi/useradmin/UserAdminConf.java
Move packages to CMS
[lgpl/argeo-commons.git] / org.argeo.security.core / src / org / argeo / osgi / useradmin / UserAdminConf.java
1 package org.argeo.osgi.useradmin;
2
3 import java.io.UnsupportedEncodingException;
4 import java.net.URI;
5 import java.net.URISyntaxException;
6 import java.net.URLDecoder;
7 import java.util.Dictionary;
8 import java.util.Enumeration;
9 import java.util.Hashtable;
10 import java.util.LinkedHashMap;
11 import java.util.LinkedList;
12 import java.util.List;
13 import java.util.Map;
14
15 import javax.naming.Context;
16
17 /** Properties used to configure user admins. */
18 public enum UserAdminConf {
19 /** Base DN (cannot be configured externally) */
20 baseDn("dc=example,dc=com"),
21
22 /** URI of the underlying resource (cannot be configured externally) */
23 uri("ldap://localhost:10389"),
24
25 /** User objectClass */
26 userObjectClass("inetOrgPerson"),
27
28 /** Relative base DN for users */
29 userBase("ou=users"),
30
31 /** Groups objectClass */
32 groupObjectClass("groupOfNames"),
33
34 /** Relative base DN for users */
35 groupBase("ou=groups"),
36
37 /** Read-only source */
38 readOnly(null);
39
40 public final static String PREFIX = "argeo.useradmin";
41
42 /** The default value. */
43 private Object def;
44
45 UserAdminConf(Object def) {
46 this.def = def;
47 }
48
49 public Object getDefault() {
50 return def;
51 }
52
53 /** For use as Java property. */
54 public String property() {
55 return getPrefix() + '.' + name();
56 }
57
58 public String getPrefix() {
59 return PREFIX;
60 }
61
62 public String getValue(Dictionary<String, ?> properties) {
63 Object res = getRawValue(properties);
64 if (res == null)
65 return null;
66 return res.toString();
67 }
68
69 @SuppressWarnings("unchecked")
70 public <T> T getRawValue(Dictionary<String, ?> properties) {
71 Object res = properties.get(property());
72 if (res == null)
73 res = getDefault();
74 return (T) res;
75 }
76
77 public static UserAdminConf local(String property) {
78 return UserAdminConf.valueOf(property.substring(PREFIX.length() + 1));
79 }
80
81 /** Hides host and credentials. */
82 public static URI propertiesAsUri(Dictionary<String, ?> properties) {
83 StringBuilder query = new StringBuilder();
84
85 boolean first = true;
86 for (Enumeration<String> keys = properties.keys(); keys
87 .hasMoreElements();) {
88 String key = keys.nextElement();
89 if (key.startsWith(PREFIX) && !key.equals(baseDn.property())
90 && !key.equals(uri.property())) {
91 if (first)
92 first = false;
93 else
94 query.append('&');
95 query.append(local(key).name());
96 query.append('=').append(properties.get(key).toString());
97 }
98 }
99
100 String bDn = (String) properties.get(baseDn.property());
101 try {
102 return new URI(null, null, bDn != null ? '/' + bDn : null,
103 query.length() != 0 ? query.toString() : null, null);
104 } catch (URISyntaxException e) {
105 throw new UserDirectoryException(
106 "Cannot create URI from properties", e);
107 }
108 }
109
110 public static Dictionary<String, ?> uriAsProperties(String uriStr) {
111 try {
112 Hashtable<String, Object> res = new Hashtable<String, Object>();
113 URI u = new URI(uriStr);
114 String scheme = u.getScheme();
115 String path = u.getPath();
116 String bDn = path.substring(path.lastIndexOf('/') + 1,
117 path.length());
118 if (bDn.endsWith(".ldif"))
119 bDn = bDn.substring(0, bDn.length() - ".ldif".length());
120
121 String principal = null;
122 String credentials = null;
123 if (scheme != null)
124 if (scheme.equals("ldap") || scheme.equals("ldaps")) {
125 // TODO additional checks
126 String[] userInfo = u.getUserInfo().split(":");
127 principal = userInfo.length > 0 ? userInfo[0] : null;
128 credentials = userInfo.length > 1 ? userInfo[1] : null;
129 } else if (scheme.equals("file")) {
130 } else
131 throw new UserDirectoryException("Unsupported scheme "
132 + scheme);
133 Map<String, List<String>> query = splitQuery(u.getQuery());
134 for (String key : query.keySet()) {
135 UserAdminConf ldapProp = UserAdminConf.valueOf(key);
136 List<String> values = query.get(key);
137 if (values.size() == 1) {
138 res.put(ldapProp.property(), values.get(0));
139 } else {
140 throw new UserDirectoryException(
141 "Only single values are supported");
142 }
143 }
144 res.put(baseDn.property(), bDn);
145 if (principal != null)
146 res.put(Context.SECURITY_PRINCIPAL, principal);
147 if (credentials != null)
148 res.put(Context.SECURITY_CREDENTIALS, credentials);
149 if (scheme != null) {
150 URI bareUri = new URI(scheme, null, u.getHost(), u.getPort(),
151 scheme.equals("file") ? u.getPath() : null, null, null);
152 res.put(uri.property(), bareUri.toString());
153 }
154 return res;
155 } catch (Exception e) {
156 throw new UserDirectoryException("Cannot convert " + uri
157 + " to properties", e);
158 }
159 }
160
161 private static Map<String, List<String>> splitQuery(String query)
162 throws UnsupportedEncodingException {
163 final Map<String, List<String>> query_pairs = new LinkedHashMap<String, List<String>>();
164 if (query == null)
165 return query_pairs;
166 final String[] pairs = query.split("&");
167 for (String pair : pairs) {
168 final int idx = pair.indexOf("=");
169 final String key = idx > 0 ? URLDecoder.decode(
170 pair.substring(0, idx), "UTF-8") : pair;
171 if (!query_pairs.containsKey(key)) {
172 query_pairs.put(key, new LinkedList<String>());
173 }
174 final String value = idx > 0 && pair.length() > idx + 1 ? URLDecoder
175 .decode(pair.substring(idx + 1), "UTF-8") : null;
176 query_pairs.get(key).add(value);
177 }
178 return query_pairs;
179 }
180
181 public static void main(String[] args) {
182 Dictionary<String, ?> props = uriAsProperties("ldap://"
183 + "uid=admin,ou=system:secret@localhost:10389"
184 + "/dc=example,dc=com"
185 + "?readOnly=false&userObjectClass=person");
186 System.out.println(props);
187 System.out.println(propertiesAsUri(props));
188
189 System.out
190 .println(uriAsProperties("file://some/dir/dc=example,dc=com.ldif"));
191
192 props = uriAsProperties("/dc=example,dc=com.ldif?readOnly=true"
193 + "&userBase=ou=CoWorkers,ou=People&groupBase=ou=Roles");
194 System.out.println(props);
195 System.out.println(propertiesAsUri(props));
196 }
197 }