1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.apache.wss4j.common.principal;
21
22
23 import java.util.Arrays;
24
25 /**
26 * This class implements the <code>Principal</code> interface and
27 * represents a UsernameToken user.
28 * <p/>In addition to the principal's name
29 * this principal object also contains the nonce and created time of the
30 * UsernameToken (refer to the OASIS WS Security specification, UsernameToken
31 * profile). These values are set only if the password of UsernameToken was of
32 * type <code>PasswordDigest</code>.
33 * <p/>Furthermore the password type is
34 * provided to the application. The password type is the string of the type
35 * attribute of the password element inside the username token. Refer to the
36 * OASIS WSS specification for predefined password types. <p/>The
37 * <code>equals()</code> method use the prinicipal's name only and does not
38 * compare nonce or created time.
39 * <p/>Modelled according to the example provided
40 * by JAAS documentation
41 * <p/>
42 *
43 * @see java.security.Principal
44 * @see javax.security.auth.Subject
45 */
46 public class WSUsernameTokenPrincipalImpl implements java.io.Serializable, UsernameTokenPrincipal {
47
48 private static final long serialVersionUID = 5608648208455259722L;
49 private String name;
50 private byte[] nonce;
51 private transient String password;
52 private String createdTime;
53 private String passwordType;
54 private boolean digest = false;
55
56 /**
57 * Create a WSUsernameTokenPrincipalImpl with a WSUsernameToken username.
58 *
59 * @param name the WSUsernameToken username for this user.
60 */
61 public WSUsernameTokenPrincipalImpl(String name, boolean digest) {
62 this.name = name;
63 this.digest = digest;
64 }
65
66 /**
67 * Return the WSUsernameToken username for this <code>WSUsernameTokenPrincipalImpl</code>.
68 *
69 * @return the WSUsernameToken username for this <code>WSUsernameTokenPrincipalImpl</code>
70 */
71 public String getName() {
72 return name;
73 }
74
75 /**
76 * Return the WSUsernameToken password type for this <code>WSUsernameTokenPrincipalImpl</code>.
77 *
78 * @return true if the password type was <code>PassowrdDigest</code>
79 */
80 @Override
81 public boolean isPasswordDigest() {
82 return digest;
83 }
84
85 /**
86 * Set the WSUsernameToken password for this <code>WSUsernameTokenPrincipalImpl</code>.
87 *
88 * @param password
89 */
90 public void setPassword(String password) {
91 this.password = password;
92 }
93
94 /**
95 * Return the WSUsernameToken password for this <code>WSUsernameTokenPrincipalImpl</code>.
96 *
97 * @return the WSUsernameToken password for this <code>WSUsernameTokenPrincipalImpl</code>
98 */
99 @Override
100 public String getPassword() {
101 return password;
102 }
103
104 /**
105 * Set the WSUsernameToken nonce for this <code>WSUsernameTokenPrincipalImpl</code>.
106 *
107 * @param nonce
108 */
109 public void setNonce(byte[] nonce) {
110 this.nonce = nonce;
111 }
112
113 /**
114 * Return the WSUsernameToken nonce for this <code>WSUsernameTokenPrincipalImpl</code>.
115 *
116 * @return the WSUsernameToken nonce for this <code>WSUsernameTokenPrincipalImpl</code>
117 */
118 @Override
119 public byte[] getNonce() {
120 return nonce;
121 }
122
123 /**
124 * Set the WSUsernameToken created time for this <code>WSUsernameTokenPrincipalImpl</code>.
125 *
126 * @param createdTime
127 */
128 public void setCreatedTime(String createdTime) {
129 this.createdTime = createdTime;
130 }
131
132 /**
133 * Return the WSUsernameToken created time for this <code>WSUsernameTokenPrincipalImpl</code>.
134 *
135 * @return the WSUsernameToken created time for this <code>WSUsernameTokenPrincipalImpl</code>
136 */
137 @Override
138 public String getCreatedTime() {
139 return createdTime;
140 }
141
142 /**
143 * Return a string representation of this <code>WSUsernameTokenPrincipalImpl</code>.
144 *
145 * @return a string representation of this <code>WSUsernameTokenPrincipalImpl</code>.
146 */
147 public String toString() {
148 return "WSUsernameTokenPrincipalImpl: " + name;
149 }
150
151 /**
152 * @return Returns the passwordType.
153 */
154 @Override
155 public String getPasswordType() {
156 return passwordType;
157 }
158
159 /**
160 * @param passwordType The passwordType to set.
161 */
162 public void setPasswordType(String passwordType) {
163 this.passwordType = passwordType;
164 }
165
166 /**
167 * Compares the specified Object with this <code>WSUsernameTokenPrincipal</code>
168 * for equality. Returns true if the given object is also a
169 * <code>WSUsernameTokenPrincipal</code> and the two WSUsernameTokenPrincipals
170 * have the same username.
171 *
172 * @param o Object to be compared for equality with this
173 * <code>WSUsernameTokenPrincipal</code>.
174 * @return true if the specified Object is equal equal to this
175 * <code>WSUsernameTokenPrincipal</code>.
176 */
177 @Override
178 public boolean equals(Object o) {
179 if (o == null) {
180 return false;
181 }
182 if (this == o) {
183 return true;
184 }
185 if (!(o instanceof UsernameTokenPrincipal)) {
186 return false;
187 }
188 UsernameTokenPrincipal that = (UsernameTokenPrincipal) o;
189 if (this.digest != that.isPasswordDigest()) {
190 return false;
191 }
192 if (this.name == null ? that.getName() != null : !this.name.equals(that.getName())) {
193 return false;
194 }
195 if (this.nonce == null ? that.getNonce() != null : !Arrays.equals(this.nonce, that.getNonce())) {
196 return false;
197 }
198 if (this.password == null ? that.getPassword() != null : !this.password.equals(that.getPassword())) {
199 return false;
200 }
201 if (this.createdTime == null ? that.getCreatedTime() != null
202 : !this.createdTime.equals(that.getCreatedTime())) {
203 return false;
204 }
205 return this.passwordType == null ? that.getPasswordType() == null
206 : this.passwordType.equals(that.getPasswordType());
207 }
208
209 /**
210 * Return a hash code for this <code>WSUsernameTokenPrincipalImpl</code>.
211 *
212 * @return a hash code for this <code>WSUsernameTokenPrincipalImpl</code>.
213 */
214 @Override
215 public int hashCode() {
216 int hashcode = 17;
217 hashcode = 31 * hashcode + (digest ? 1 : 0);
218 hashcode = 31 * hashcode + (name == null ? 0 : name.hashCode());
219 hashcode = 31 * hashcode + (nonce == null ? 0 : Arrays.hashCode(nonce));
220 hashcode = 31 * hashcode + (password == null ? 0 : password.hashCode());
221 hashcode = 31 * hashcode + (createdTime == null ? 0 : createdTime.hashCode());
222 hashcode = 31 * hashcode + (passwordType == null ? 0 : passwordType.hashCode());
223
224 return hashcode;
225 }
226
227 }