]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/org/argeo/osgi/useradmin/AuthenticatingUser.java
Directory as a hierarchy unit.
[lgpl/argeo-commons.git] / org.argeo.util / src / org / argeo / osgi / useradmin / AuthenticatingUser.java
1 package org.argeo.osgi.useradmin;
2
3 import java.util.Dictionary;
4 import java.util.Hashtable;
5
6 import javax.naming.ldap.LdapName;
7
8 import org.argeo.util.directory.DirectoryDigestUtils;
9 import org.osgi.service.useradmin.User;
10
11 /**
12 * A special user type used during authentication in order to provide the
13 * credentials required for scoping the user admin.
14 */
15 public class AuthenticatingUser implements User {
16 /** From com.sun.security.auth.module.*LoginModule */
17 public final static String SHARED_STATE_NAME = "javax.security.auth.login.name";
18 /** From com.sun.security.auth.module.*LoginModule */
19 public final static String SHARED_STATE_PWD = "javax.security.auth.login.password";
20
21 private final String name;
22 private final Dictionary<String, Object> credentials;
23
24 public AuthenticatingUser(LdapName name) {
25 if (name == null)
26 throw new NullPointerException("Provided name cannot be null.");
27 this.name = name.toString();
28 this.credentials = new Hashtable<>();
29 }
30
31 public AuthenticatingUser(String name, Dictionary<String, Object> credentials) {
32 this.name = name;
33 this.credentials = credentials;
34 }
35
36 public AuthenticatingUser(String name, char[] password) {
37 if (name == null)
38 throw new NullPointerException("Provided name cannot be null.");
39 this.name = name;
40 credentials = new Hashtable<>();
41 credentials.put(SHARED_STATE_NAME, name);
42 byte[] pwd = DirectoryDigestUtils.charsToBytes(password);
43 credentials.put(SHARED_STATE_PWD, pwd);
44 }
45
46 @Override
47 public String getName() {
48 return name;
49 }
50
51 @Override
52 public int getType() {
53 return User.USER;
54 }
55
56 @SuppressWarnings({ "rawtypes", "unchecked" })
57 @Override
58 public Dictionary getProperties() {
59 throw new UnsupportedOperationException();
60 }
61
62 @SuppressWarnings({ "rawtypes", "unchecked" })
63 @Override
64 public Dictionary getCredentials() {
65 return credentials;
66 }
67
68 @Override
69 public boolean hasCredential(String key, Object value) {
70 throw new UnsupportedOperationException();
71 }
72
73 @Override
74 public int hashCode() {
75 return name.hashCode();
76 }
77
78 @Override
79 public String toString() {
80 return "Authenticating user " + name;
81 }
82
83 }