1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.core.authn;
21
22
23 import java.io.Externalizable;
24 import java.io.IOException;
25 import java.io.ObjectInput;
26 import java.io.ObjectOutput;
27 import java.security.Principal;
28
29 import org.apache.directory.shared.ldap.constants.AuthenticationLevel;
30 import org.apache.directory.shared.ldap.name.LdapDN;
31 import org.apache.directory.shared.ldap.util.StringTools;
32
33
34
35
36
37
38
39
40
41 public final class LdapPrincipal implements Principal, Cloneable, Externalizable
42 {
43 private static final long serialVersionUID = 3906650782395676720L;
44
45
46 private LdapDN name;
47
48
49 public static final LdapPrincipal ANONYMOUS = new LdapPrincipal();
50
51
52 private AuthenticationLevel authenticationLevel;
53
54
55
56
57
58 transient private byte[] userPassword;
59
60
61
62
63
64
65
66
67
68
69 public LdapPrincipal( LdapDN name, AuthenticationLevel authenticationLevel )
70 {
71 this.name = name;
72
73 if ( ! name.isNormalized() )
74 {
75 throw new IllegalStateException( "Names used for principals must be normalized!" );
76 }
77
78 this.authenticationLevel = authenticationLevel;
79 this.userPassword = null;
80 }
81
82
83
84
85
86
87
88
89
90
91 public LdapPrincipal( LdapDN name, AuthenticationLevel authenticationLevel, byte[] userPassword )
92 {
93 this.name = name;
94 this.authenticationLevel = authenticationLevel;
95 this.userPassword = new byte[ userPassword.length ];
96 System.arraycopy( userPassword, 0, this.userPassword, 0, userPassword.length );
97 }
98
99
100
101
102
103
104 public LdapPrincipal()
105 {
106 name = new LdapDN();
107 authenticationLevel = AuthenticationLevel.NONE;
108 userPassword = null;
109 }
110
111
112
113
114
115
116
117
118 public LdapDN getJndiName()
119 {
120 return ( LdapDN ) name.clone();
121 }
122
123
124
125
126
127 public String getName()
128 {
129 return name.getNormName();
130 }
131
132
133
134
135
136
137
138 public AuthenticationLevel getAuthenticationLevel()
139 {
140 return authenticationLevel;
141 }
142
143
144
145
146
147
148 public String toString()
149 {
150 return "['" + name.getUpName() + "', '" + StringTools.utf8ToString( userPassword ) +"']'";
151 }
152
153
154 public byte[] getUserPassword()
155 {
156 return userPassword;
157 }
158
159
160 public void setUserPassword( byte[] userPassword )
161 {
162 this.userPassword = new byte[ userPassword.length ];
163 System.arraycopy( userPassword, 0, this.userPassword, 0, userPassword.length );
164 }
165
166
167
168
169
170
171 public Object clone() throws CloneNotSupportedException
172 {
173 LdapPrincipal clone = (LdapPrincipal)super.clone();
174
175 if ( userPassword != null )
176 {
177 clone.setUserPassword( userPassword );
178 }
179
180 return clone;
181 }
182
183
184
185
186
187
188
189
190
191 public void readExternal( ObjectInput in ) throws IOException , ClassNotFoundException
192 {
193
194 name = (LdapDN)in.readObject();
195
196
197 int level = in.readInt();
198
199 authenticationLevel = AuthenticationLevel.getLevel( level );
200 }
201
202
203
204
205
206
207
208
209
210
211 public void writeExternal( ObjectOutput out ) throws IOException
212 {
213
214 if ( name == null )
215 {
216 out.writeObject( LdapDN.EMPTY_LDAPDN );
217 }
218 else
219 {
220 out.writeObject( name );
221 }
222
223
224 if ( authenticationLevel == null )
225 {
226 out.writeInt( AuthenticationLevel.NONE.getLevel() );
227 }
228 else
229 {
230 out.writeInt( authenticationLevel.getLevel() );
231 }
232
233
234
235 }
236 }