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