Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
1 /*
2 * ====================================================================
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ====================================================================
19 *
20 * This software consists of voluntary contributions made by many
21 * individuals on behalf of the Apache Software Foundation. For more
22 * information on the Apache Software Foundation, please see
23 * <http://www.apache.org/>.
24 *
25 */
27 package ch.boye.httpclientandroidlib.auth;
29 import java.io.Serializable;
30 import java.security.Principal;
31 import java.util.Locale;
33 import ch.boye.httpclientandroidlib.annotation.Immutable;
35 import ch.boye.httpclientandroidlib.util.LangUtils;
37 /**
38 * {@link Credentials} implementation for Microsoft Windows platforms that includes
39 * Windows specific attributes such as name of the domain the user belongs to.
40 *
41 * @since 4.0
42 */
43 @Immutable
44 public class NTCredentials implements Credentials, Serializable {
46 private static final long serialVersionUID = -7385699315228907265L;
48 /** The user principal */
49 private final NTUserPrincipal principal;
51 /** Password */
52 private final String password;
54 /** The host the authentication request is originating from. */
55 private final String workstation;
57 /**
58 * The constructor with the fully qualified username and password combined
59 * string argument.
60 *
61 * @param usernamePassword the domain/username:password formed string
62 */
63 public NTCredentials(String usernamePassword) {
64 super();
65 if (usernamePassword == null) {
66 throw new IllegalArgumentException("Username:password string may not be null");
67 }
68 String username;
69 int atColon = usernamePassword.indexOf(':');
70 if (atColon >= 0) {
71 username = usernamePassword.substring(0, atColon);
72 this.password = usernamePassword.substring(atColon + 1);
73 } else {
74 username = usernamePassword;
75 this.password = null;
76 }
77 int atSlash = username.indexOf('/');
78 if (atSlash >= 0) {
79 this.principal = new NTUserPrincipal(
80 username.substring(0, atSlash).toUpperCase(Locale.ENGLISH),
81 username.substring(atSlash + 1));
82 } else {
83 this.principal = new NTUserPrincipal(
84 null,
85 username.substring(atSlash + 1));
86 }
87 this.workstation = null;
88 }
90 /**
91 * Constructor.
92 * @param userName The user name. This should not include the domain to authenticate with.
93 * For example: "user" is correct whereas "DOMAIN\\user" is not.
94 * @param password The password.
95 * @param workstation The workstation the authentication request is originating from.
96 * Essentially, the computer name for this machine.
97 * @param domain The domain to authenticate within.
98 */
99 public NTCredentials(
100 final String userName,
101 final String password,
102 final String workstation,
103 final String domain) {
104 super();
105 if (userName == null) {
106 throw new IllegalArgumentException("User name may not be null");
107 }
108 this.principal = new NTUserPrincipal(domain, userName);
109 this.password = password;
110 if (workstation != null) {
111 this.workstation = workstation.toUpperCase(Locale.ENGLISH);
112 } else {
113 this.workstation = null;
114 }
115 }
117 public Principal getUserPrincipal() {
118 return this.principal;
119 }
121 public String getUserName() {
122 return this.principal.getUsername();
123 }
125 public String getPassword() {
126 return this.password;
127 }
129 /**
130 * Retrieves the name to authenticate with.
131 *
132 * @return String the domain these credentials are intended to authenticate with.
133 */
134 public String getDomain() {
135 return this.principal.getDomain();
136 }
138 /**
139 * Retrieves the workstation name of the computer originating the request.
140 *
141 * @return String the workstation the user is logged into.
142 */
143 public String getWorkstation() {
144 return this.workstation;
145 }
147 @Override
148 public int hashCode() {
149 int hash = LangUtils.HASH_SEED;
150 hash = LangUtils.hashCode(hash, this.principal);
151 hash = LangUtils.hashCode(hash, this.workstation);
152 return hash;
153 }
155 @Override
156 public boolean equals(Object o) {
157 if (this == o) return true;
158 if (o instanceof NTCredentials) {
159 NTCredentials that = (NTCredentials) o;
160 if (LangUtils.equals(this.principal, that.principal)
161 && LangUtils.equals(this.workstation, that.workstation)) {
162 return true;
163 }
164 }
165 return false;
166 }
168 @Override
169 public String toString() {
170 StringBuilder buffer = new StringBuilder();
171 buffer.append("[principal: ");
172 buffer.append(this.principal);
173 buffer.append("][workstation: ");
174 buffer.append(this.workstation);
175 buffer.append("]");
176 return buffer.toString();
177 }
179 }