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