1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.apache.commons.httpclient;
31
32 import org.apache.commons.httpclient.util.LangUtils;
33
34 /*** {@link Credentials} for use with the NTLM authentication scheme which requires additional
35 * information.
36 *
37 * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a>
38 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
39 *
40 * @version $Revision: 326707 $ $Date: 2005-10-19 16:51:46 -0400 (Wed, 19 Oct 2005) $
41 *
42 * @since 2.0
43 */
44 public class NTCredentials extends UsernamePasswordCredentials {
45
46
47
48 /*** The Domain to authenticate with. */
49 private String domain;
50
51 /*** The host the authentication request is originating from. */
52 private String host;
53
54
55
56
57 /***
58 * Default constructor.
59 *
60 * @deprecated Do not use. Null user name, domain & host no longer allowed
61 */
62 public NTCredentials() {
63 super();
64 }
65
66 /***
67 * Constructor.
68 * @param userName The user name. This should not include the domain to authenticate with.
69 * For example: "user" is correct whereas "DOMAIN//user" is not.
70 * @param password The password.
71 * @param host The host the authentication request is originating from. Essentially, the
72 * computer name for this machine.
73 * @param domain The domain to authenticate within.
74 */
75 public NTCredentials(String userName, String password, String host,
76 String domain) {
77 super(userName, password);
78 if (domain == null) {
79 throw new IllegalArgumentException("Domain may not be null");
80 }
81 this.domain = domain;
82 if (host == null) {
83 throw new IllegalArgumentException("Host may not be null");
84 }
85 this.host = host;
86 }
87
88
89
90 /***
91 * Sets the domain to authenticate with. The domain may not be null.
92 *
93 * @param domain the NT domain to authenticate in.
94 *
95 * @see #getDomain()
96 *
97 * @deprecated Do not use. The NTCredentials objects should be immutable
98 */
99 public void setDomain(String domain) {
100 if (domain == null) {
101 throw new IllegalArgumentException("Domain may not be null");
102 }
103 this.domain = domain;
104 }
105
106 /***
107 * Retrieves the name to authenticate with.
108 *
109 * @return String the domain these credentials are intended to authenticate with.
110 *
111 * @see #setDomain(String)
112 *
113 */
114 public String getDomain() {
115 return domain;
116 }
117
118 /***
119 * Sets the host name of the computer originating the request. The host name may
120 * not be null.
121 *
122 * @param host the Host the user is logged into.
123 *
124 * @deprecated Do not use. The NTCredentials objects should be immutable
125 */
126 public void setHost(String host) {
127 if (host == null) {
128 throw new IllegalArgumentException("Host may not be null");
129 }
130 this.host = host;
131 }
132
133 /***
134 * Retrieves the host name of the computer originating the request.
135 *
136 * @return String the host the user is logged into.
137 */
138 public String getHost() {
139 return this.host;
140 }
141
142 /***
143 * Return a string representation of this object.
144 * @return A string represenation of this object.
145 */
146 public String toString() {
147 final StringBuffer sbResult = new StringBuffer(super.toString());
148
149 sbResult.append("@");
150 sbResult.append(this.host);
151 sbResult.append(".");
152 sbResult.append(this.domain);
153
154 return sbResult.toString();
155 }
156
157 /***
158 * Computes a hash code based on all the case-sensitive parts of the credentials object.
159 *
160 * @return The hash code for the credentials.
161 */
162 public int hashCode() {
163 int hash = super.hashCode();
164 hash = LangUtils.hashCode(hash, this.host);
165 hash = LangUtils.hashCode(hash, this.domain);
166 return hash;
167 }
168
169 /***
170 * Performs a case-sensitive check to see if the components of the credentials
171 * are the same.
172 *
173 * @param o The object to match.
174 *
175 * @return <code>true</code> if all of the credentials match.
176 */
177 public boolean equals(Object o) {
178 if (o == null) return false;
179 if (this == o) return true;
180 if (super.equals(o) ) {
181 if (o instanceof NTCredentials) {
182 NTCredentials that = (NTCredentials) o;
183
184 return LangUtils.equals(this.domain, that.domain)
185 && LangUtils.equals(this.host, that.host);
186 }
187 }
188
189 return false;
190 }
191 }