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.ws.security;
21
22 import java.security.Principal;
23
24 /**
25 * This class implements the <code>Principal</code> interface and
26 * represents a UsernameToken user.
27 * <p/>In addition to the principal's name
28 * this principal object also contains the nonce and created time of the
29 * UsernameToken (refer to the OASIS WS Security specification, UsernameToken
30 * profile). These values are set only if the password of UsernameToken was of
31 * type <code>PasswordDigest</code>.
32 * <p/>Furthermore the password type is
33 * provided to the application. The password type is the string of the type
34 * attribute of the password element inside the username token. Refer to the
35 * OASIS WSS specification for predefined password types. <p/>The
36 * <code>equals()</code> method use the prinicipal's name only and does not
37 * compare nonce or created time.
38 * <p/>Modelled according to the example provided
39 * by JAAS documentation
40 * <p/>
41 *
42 * @author Davanum Srinivas (dims@yahoo.com).
43 * @author Werner Dittmann (Werner.Dittmann@siemens.com).
44 * @see java.security.Principal
45 * @see javax.security.auth.Subject
46 */
47 public class WSUsernameTokenPrincipal implements Principal, java.io.Serializable {
48
49 /**
50 *
51 */
52 private static final long serialVersionUID = 5608648208455259722L;
53 private String name;
54 private String nonce;
55 private String password;
56 private String createdTime;
57 private String passwordType;
58 private boolean digest = false;
59
60 /**
61 * Create a WSUsernameTokenPrincipal with a WSUsernameToken username.
62 *
63 * @param name the WSUsernameToken username for this user.
64 */
65 public WSUsernameTokenPrincipal(String name, boolean digest) {
66 this.name = name;
67 this.digest = digest;
68 }
69
70 /**
71 * Return the WSUsernameToken username for this <code>WSUsernameTokenPrincipal</code>.
72 *
73 * @return the WSUsernameToken username for this <code>WSUsernameTokenPrincipal</code>
74 */
75 public String getName() {
76 return name;
77 }
78
79 /**
80 * Return the WSUsernameToken password type for this <code>WSUsernameTokenPrincipal</code>.
81 *
82 * @return true if the password type was <code>PassowrdDigest</code>
83 */
84 public boolean isPasswordDigest() {
85 return digest;
86 }
87
88 /**
89 * Set the WSUsernameToken password for this <code>WSUsernameTokenPrincipal</code>.
90 *
91 * @param password
92 */
93 public void setPassword(String password) {
94 this.password = password;
95 }
96
97 /**
98 * Return the WSUsernameToken password for this <code>WSUsernameTokenPrincipal</code>.
99 *
100 * @return the WSUsernameToken password for this <code>WSUsernameTokenPrincipal</code>
101 */
102 public String getPassword() {
103 return password;
104 }
105
106 /**
107 * Set the WSUsernameToken nonce for this <code>WSUsernameTokenPrincipal</code>.
108 *
109 * @param nonce
110 */
111 public void setNonce(String nonce) {
112 this.nonce = nonce;
113 }
114
115 /**
116 * Return the WSUsernameToken nonce for this <code>WSUsernameTokenPrincipal</code>.
117 *
118 * @return the WSUsernameToken nonce for this <code>WSUsernameTokenPrincipal</code>
119 */
120 public String getNonce() {
121 return nonce;
122 }
123
124 /**
125 * Set the WSUsernameToken created time for this <code>WSUsernameTokenPrincipal</code>.
126 *
127 * @param createdTime
128 */
129 public void setCreatedTime(String createdTime) {
130 this.createdTime = createdTime;
131 }
132
133 /**
134 * Return the WSUsernameToken created time for this <code>WSUsernameTokenPrincipal</code>.
135 *
136 * @return the WSUsernameToken created time for this <code>WSUsernameTokenPrincipal</code>
137 */
138 public String getCreatedTime() {
139 return createdTime;
140 }
141
142 /**
143 * Return a string representation of this <code>WSUsernameTokenPrincipal</code>.
144 *
145 * @return a string representation of this <code>WSUsernameTokenPrincipal</code>.
146 */
147 public String toString() {
148 return ("WSUsernameTokenPrincipal: " + name);
149 }
150
151 /**
152 * @return Returns the passwordType.
153 */
154 public String getPasswordType() {
155 return passwordType;
156 }
157
158 /**
159 * @param passwordType The passwordType to set.
160 */
161 public void setPasswordType(String passwordType) {
162 this.passwordType = passwordType;
163 }
164
165 /**
166 * Compares the specified Object with this <code>WSUsernameTokenPrincipal</code>
167 * for equality. Returns true if the given object is also a
168 * <code>WSUsernameTokenPrincipal</code> and the two WSUsernameTokenPrincipals
169 * have the same username.
170 *
171 * @param o Object to be compared for equality with this
172 * <code>WSUsernameTokenPrincipal</code>.
173 * @return true if the specified Object is equal equal to this
174 * <code>WSUsernameTokenPrincipal</code>.
175 */
176 @Override
177 public boolean equals(Object o) {
178 if (o == null) {
179 return false;
180 }
181 if (this == o) {
182 return true;
183 }
184 if (!(o instanceof WSUsernameTokenPrincipal)) {
185 return false;
186 }
187 WSUsernameTokenPrincipal that = (WSUsernameTokenPrincipal) o;
188 if (this.digest != that.digest) {
189 return false;
190 }
191 if (this.name == null ? that.name != null : !this.name.equals(that.name)) {
192 return false;
193 }
194 if (this.nonce == null ? that.nonce != null : !this.nonce.equals(that.nonce)) {
195 return false;
196 }
197 if (this.password == null ? that.password != null : !this.password.equals(that.password)) {
198 return false;
199 }
200 if (this.createdTime == null ? that.createdTime != null
201 : !this.createdTime.equals(that.createdTime)) {
202 return false;
203 }
204 if (this.passwordType == null ? that.passwordType != null
205 : !this.passwordType.equals(that.passwordType)) {
206 return false;
207 }
208 return true;
209 }
210
211 /**
212 * Return a hash code for this <code>WSUsernameTokenPrincipal</code>.
213 *
214 * @return a hash code for this <code>WSUsernameTokenPrincipal</code>.
215 */
216 @Override
217 public int hashCode() {
218 int hashcode = 17;
219 hashcode = 31 * hashcode + (digest ? 1 : 0);
220 hashcode = 31 * hashcode + (name == null ? 0 : name.hashCode());
221 hashcode = 31 * hashcode + (nonce == null ? 0 : nonce.hashCode());
222 hashcode = 31 * hashcode + (password == null ? 0 : password.hashCode());
223 hashcode = 31 * hashcode + (createdTime == null ? 0 : createdTime.hashCode());
224 hashcode = 31 * hashcode + (passwordType == null ? 0 : passwordType.hashCode());
225
226 return hashcode;
227 }
228
229 }