1 package org
.argeo
.osgi
.useradmin
;
3 import java
.io
.UnsupportedEncodingException
;
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
;
15 import javax
.naming
.Context
;
17 /** Properties used to configure user admins. */
18 public enum UserAdminConf
{
20 baseDn("dc=example,dc=com"),
22 /** URI of the underlying resource */
23 uri("ldap://localhost:10389"),
25 /** User objectClass */
26 userObjectClass("inetOrgPerson"),
28 /** Relative base DN for users */
31 /** Groups objectClass */
32 groupObjectClass("groupOfNames"),
34 /** Relative base DN for users */
35 groupBase("ou=groups"),
37 /** Read-only source */
40 public final static String PREFIX
= "argeo.useradmin";
42 /** The default value. */
45 UserAdminConf(Object def
) {
49 public Object
getDefault() {
53 /** For use as Java property. */
54 public String
property() {
55 return getPrefix() + name();
58 public String
getPrefix() {
62 public String
getValue(Dictionary
<String
, ?
> properties
) {
63 Object res
= getRawValue(properties
);
66 return res
.toString();
69 @SuppressWarnings("unchecked")
70 public <T
> T
getRawValue(Dictionary
<String
, ?
> properties
) {
71 Object res
= properties
.get(property());
77 public static UserAdminConf
local(String property
) {
78 return UserAdminConf
.valueOf(property
.substring(PREFIX
.length()));
81 /** Hides host and credentials. */
82 public static URI
propertiesAsUri(Dictionary
<String
, ?
> properties
) {
83 StringBuilder query
= new StringBuilder();
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())) {
95 query
.append(local(key
).name());
96 query
.append('=').append(properties
.get(key
).toString());
100 String bDn
= (String
) properties
.get(baseDn
.property());
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
);
110 public static Dictionary
<String
, ?
> uriAsProperties(String uriStr
) {
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,
118 if (bDn
.endsWith(".ldif"))
119 bDn
= bDn
.substring(0, bDn
.length() - ".ldif".length());
121 String principal
= null;
122 String credentials
= 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")) {
131 throw new UserDirectoryException("Unsupported 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));
140 throw new UserDirectoryException(
141 "Only single values are supported");
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());
155 } catch (Exception e
) {
156 throw new UserDirectoryException("Cannot convert " + uri
157 + " to properties", e
);
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
>>();
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
>());
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
);
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
));
190 .println(uriAsProperties("file://some/dir/dc=example,dc=com.ldif"));
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
));