1 package org
.argeo
.osgi
.useradmin
;
3 import java
.io
.UnsupportedEncodingException
;
4 import java
.nio
.ByteBuffer
;
5 import java
.nio
.CharBuffer
;
6 import java
.nio
.charset
.Charset
;
7 import java
.util
.ArrayList
;
8 import java
.util
.Arrays
;
9 import java
.util
.Base64
;
10 import java
.util
.Collections
;
11 import java
.util
.Dictionary
;
12 import java
.util
.Enumeration
;
13 import java
.util
.HashSet
;
14 import java
.util
.Iterator
;
15 import java
.util
.List
;
18 import javax
.naming
.NamingEnumeration
;
19 import javax
.naming
.NamingException
;
20 import javax
.naming
.directory
.Attribute
;
21 import javax
.naming
.directory
.Attributes
;
22 import javax
.naming
.directory
.BasicAttribute
;
23 import javax
.naming
.ldap
.LdapName
;
25 /** Directory user implementation */
26 class LdifUser
implements DirectoryUser
{
27 private final AbstractUserDirectory userAdmin
;
29 private final LdapName dn
;
31 private final boolean frozen
;
32 private Attributes publishedAttributes
;
34 private final AttributeDictionary properties
;
35 private final AttributeDictionary credentials
;
37 LdifUser(AbstractUserDirectory userAdmin
, LdapName dn
, Attributes attributes
) {
38 this(userAdmin
, dn
, attributes
, false);
41 private LdifUser(AbstractUserDirectory userAdmin
, LdapName dn
, Attributes attributes
, boolean frozen
) {
42 this.userAdmin
= userAdmin
;
44 this.publishedAttributes
= attributes
;
45 properties
= new AttributeDictionary(false);
46 credentials
= new AttributeDictionary(true);
51 public String
getName() {
56 public int getType() {
61 public Dictionary
<String
, Object
> getProperties() {
66 public Dictionary
<String
, Object
> getCredentials() {
71 public boolean hasCredential(String key
, Object value
) {
73 // TODO check other sources (like PKCS12)
74 char[] password
= toChars(value
);
75 byte[] hashedPassword
= hash(password
);
76 return hasCredential(LdifName
.userPassword
.name(), hashedPassword
);
79 Object storedValue
= getCredentials().get(key
);
80 if (storedValue
== null || value
== null)
82 if (!(value
instanceof String
|| value
instanceof byte[]))
84 if (storedValue
instanceof String
&& value
instanceof String
)
85 return storedValue
.equals(value
);
86 if (storedValue
instanceof byte[] && value
instanceof byte[])
87 return Arrays
.equals((byte[]) storedValue
, (byte[]) value
);
91 /** Hash and clear the password */
92 private byte[] hash(char[] password
) {
93 byte[] hashedPassword
= ("{SHA}" + Base64
.getEncoder().encodeToString(DigestUtils
.sha1(toBytes(password
))))
95 Arrays
.fill(password
, '\u0000');
96 return hashedPassword
;
99 private byte[] toBytes(char[] chars
) {
100 CharBuffer charBuffer
= CharBuffer
.wrap(chars
);
101 ByteBuffer byteBuffer
= Charset
.forName("UTF-8").encode(charBuffer
);
102 byte[] bytes
= Arrays
.copyOfRange(byteBuffer
.array(), byteBuffer
.position(), byteBuffer
.limit());
103 Arrays
.fill(charBuffer
.array(), '\u0000'); // clear sensitive data
104 Arrays
.fill(byteBuffer
.array(), (byte) 0); // clear sensitive data
108 private char[] toChars(Object obj
) {
109 if (obj
instanceof char[])
111 if (!(obj
instanceof byte[]))
112 throw new IllegalArgumentException(obj
.getClass() + " is not a byte array");
113 ByteBuffer fromBuffer
= ByteBuffer
.wrap((byte[]) obj
);
114 CharBuffer toBuffer
= Charset
.forName("UTF-8").decode(fromBuffer
);
115 char[] res
= Arrays
.copyOfRange(toBuffer
.array(), toBuffer
.position(), toBuffer
.limit());
116 Arrays
.fill(fromBuffer
.array(), (byte) 0); // clear sensitive data
117 Arrays
.fill((byte[]) obj
, (byte) 0); // clear sensitive data
118 Arrays
.fill(toBuffer
.array(), '\u0000'); // clear sensitive data
123 public LdapName
getDn() {
128 public synchronized Attributes
getAttributes() {
129 return isEditing() ?
getModifiedAttributes() : publishedAttributes
;
132 /** Should only be called from working copy thread. */
133 private synchronized Attributes
getModifiedAttributes() {
134 assert getWc() != null;
135 return getWc().getAttributes(getDn());
138 protected synchronized boolean isEditing() {
139 return getWc() != null && getModifiedAttributes() != null;
142 private synchronized UserDirectoryWorkingCopy
getWc() {
143 return userAdmin
.getWorkingCopy();
146 protected synchronized void startEditing() {
148 throw new UserDirectoryException("Cannot edit frozen view");
149 if (getUserAdmin().isReadOnly())
150 throw new UserDirectoryException("User directory is read-only");
151 assert getModifiedAttributes() == null;
152 getWc().startEditing(this);
153 // modifiedAttributes = (Attributes) publishedAttributes.clone();
156 public synchronized void publishAttributes(Attributes modifiedAttributes
) {
157 publishedAttributes
= modifiedAttributes
;
160 public DirectoryUser
getPublished() {
161 return new LdifUser(userAdmin
, dn
, publishedAttributes
, true);
165 public int hashCode() {
166 return dn
.hashCode();
170 public boolean equals(Object obj
) {
173 if (obj
instanceof LdifUser
) {
174 LdifUser that
= (LdifUser
) obj
;
175 return this.dn
.equals(that
.dn
);
181 public String
toString() {
182 return dn
.toString();
185 protected AbstractUserDirectory
getUserAdmin() {
189 private class AttributeDictionary
extends Dictionary
<String
, Object
> {
190 private final List
<String
> effectiveKeys
= new ArrayList
<String
>();
191 private final List
<String
> attrFilter
;
192 private final Boolean includeFilter
;
194 public AttributeDictionary(Boolean includeFilter
) {
195 this.attrFilter
= userAdmin
.getCredentialAttributeIds();
196 this.includeFilter
= includeFilter
;
198 NamingEnumeration
<String
> ids
= getAttributes().getIDs();
199 while (ids
.hasMore()) {
200 String id
= ids
.next();
201 if (includeFilter
&& attrFilter
.contains(id
))
202 effectiveKeys
.add(id
);
203 else if (!includeFilter
&& !attrFilter
.contains(id
))
204 effectiveKeys
.add(id
);
206 } catch (NamingException e
) {
207 throw new UserDirectoryException("Cannot initialise attribute dictionary", e
);
213 return effectiveKeys
.size();
217 public boolean isEmpty() {
218 return effectiveKeys
.size() == 0;
222 public Enumeration
<String
> keys() {
223 return Collections
.enumeration(effectiveKeys
);
227 public Enumeration
<Object
> elements() {
228 final Iterator
<String
> it
= effectiveKeys
.iterator();
229 return new Enumeration
<Object
>() {
232 public boolean hasMoreElements() {
237 public Object
nextElement() {
238 String key
= it
.next();
246 public Object
get(Object key
) {
248 Attribute attr
= getAttributes().get(key
.toString());
251 Object value
= attr
.get();
252 if (value
instanceof byte[]) {
253 if (key
.equals(LdifName
.userPassword
.name()))
254 // TODO other cases (certificates, images)
256 value
= new String((byte[]) value
, Charset
.forName("UTF-8"));
258 if (attr
.size() == 1)
260 if (!attr
.getID().equals(LdifName
.objectClass
.name()))
262 // special case for object class
263 NamingEnumeration
<?
> en
= attr
.getAll();
264 Set
<String
> objectClasses
= new HashSet
<String
>();
265 while (en
.hasMore()) {
266 String objectClass
= en
.next().toString();
267 objectClasses
.add(objectClass
);
270 if (objectClasses
.contains(userAdmin
.getUserObjectClass()))
271 return userAdmin
.getUserObjectClass();
272 else if (objectClasses
.contains(userAdmin
.getGroupObjectClass()))
273 return userAdmin
.getGroupObjectClass();
276 } catch (NamingException e
) {
277 throw new UserDirectoryException("Cannot get value for attribute " + key
, e
);
282 public Object
put(String key
, Object value
) {
284 // TODO persist to other sources (like PKCS12)
285 char[] password
= toChars(value
);
286 byte[] hashedPassword
= hash(password
);
287 return put(LdifName
.userPassword
.name(), hashedPassword
);
290 userAdmin
.checkEdit();
294 if (!(value
instanceof String
|| value
instanceof byte[]))
295 throw new IllegalArgumentException("Value must be String or byte[]");
297 if (includeFilter
&& !attrFilter
.contains(key
))
298 throw new IllegalArgumentException("Key " + key
+ " not included");
299 else if (!includeFilter
&& attrFilter
.contains(key
))
300 throw new IllegalArgumentException("Key " + key
+ " excluded");
303 Attribute attribute
= getModifiedAttributes().get(key
.toString());
304 attribute
= new BasicAttribute(key
.toString());
305 if (value
instanceof String
&& !isAsciiPrintable(((String
) value
)))
307 attribute
.add(((String
) value
).getBytes("UTF-8"));
308 } catch (UnsupportedEncodingException e
) {
309 throw new UserDirectoryException("Cannot encode " + value
, e
);
312 attribute
.add(value
);
313 Attribute previousAttribute
= getModifiedAttributes().put(attribute
);
314 if (previousAttribute
!= null)
315 return previousAttribute
.get();
318 } catch (NamingException e
) {
319 throw new UserDirectoryException("Cannot get value for attribute " + key
, e
);
324 public Object
remove(Object key
) {
325 userAdmin
.checkEdit();
329 if (includeFilter
&& !attrFilter
.contains(key
))
330 throw new IllegalArgumentException("Key " + key
+ " not included");
331 else if (!includeFilter
&& attrFilter
.contains(key
))
332 throw new IllegalArgumentException("Key " + key
+ " excluded");
335 Attribute attr
= getModifiedAttributes().remove(key
.toString());
340 } catch (NamingException e
) {
341 throw new UserDirectoryException("Cannot remove attribute " + key
, e
);
346 private static boolean isAsciiPrintable(String str
) {
350 int sz
= str
.length();
351 for (int i
= 0; i
< sz
; i
++) {
352 if (isAsciiPrintable(str
.charAt(i
)) == false) {
359 private static boolean isAsciiPrintable(char ch
) {
360 return ch
>= 32 && ch
< 127;