1 /*
2 * ====================================================================
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with 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,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 * ====================================================================
20 *
21 * This software consists of voluntary contributions made by many
22 * individuals on behalf of the Apache Software Foundation. For more
23 * information on the Apache Software Foundation, please see
24 * <http://www.apache.org/>.
25 *
26 */
27 package org.apache.http.auth;
28
29 import java.io.Serializable;
30 import java.security.Principal;
31 import java.util.Locale;
32
33 import org.apache.http.annotation.Immutable;
34 import org.apache.http.util.Args;
35 import org.apache.http.util.LangUtils;
36
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 {
45
46 private static final long serialVersionUID = -7385699315228907265L;
47
48 /** The user principal */
49 private final NTUserPrincipal principal;
50
51 /** Password */
52 private final String password;
53
54 /** The host the authentication request is originating from. */
55 private final String workstation;
56
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(final String usernamePassword) {
64 super();
65 Args.notNull(usernamePassword, "Username:password string");
66 String username;
67 final int atColon = usernamePassword.indexOf(':');
68 if (atColon >= 0) {
69 username = usernamePassword.substring(0, atColon);
70 this.password = usernamePassword.substring(atColon + 1);
71 } else {
72 username = usernamePassword;
73 this.password = null;
74 }
75 final int atSlash = username.indexOf('/');
76 if (atSlash >= 0) {
77 this.principal = new NTUserPrincipal(
78 username.substring(0, atSlash).toUpperCase(Locale.ENGLISH),
79 username.substring(atSlash + 1));
80 } else {
81 this.principal = new NTUserPrincipal(
82 null,
83 username.substring(atSlash + 1));
84 }
85 this.workstation = null;
86 }
87
88 /**
89 * Constructor.
90 * @param userName The user name. This should not include the domain to authenticate with.
91 * For example: "user" is correct whereas "DOMAIN\\user" is not.
92 * @param password The password.
93 * @param workstation The workstation the authentication request is originating from.
94 * Essentially, the computer name for this machine.
95 * @param domain The domain to authenticate within.
96 */
97 public NTCredentials(
98 final String userName,
99 final String password,
100 final String workstation,
101 final String domain) {
102 super();
103 Args.notNull(userName, "User name");
104 this.principal = new NTUserPrincipal(domain, userName);
105 this.password = password;
106 if (workstation != null) {
107 this.workstation = workstation.toUpperCase(Locale.ENGLISH);
108 } else {
109 this.workstation = null;
110 }
111 }
112
113 public Principal getUserPrincipal() {
114 return this.principal;
115 }
116
117 public String getUserName() {
118 return this.principal.getUsername();
119 }
120
121 public String getPassword() {
122 return this.password;
123 }
124
125 /**
126 * Retrieves the name to authenticate with.
127 *
128 * @return String the domain these credentials are intended to authenticate with.
129 */
130 public String getDomain() {
131 return this.principal.getDomain();
132 }
133
134 /**
135 * Retrieves the workstation name of the computer originating the request.
136 *
137 * @return String the workstation the user is logged into.
138 */
139 public String getWorkstation() {
140 return this.workstation;
141 }
142
143 @Override
144 public int hashCode() {
145 int hash = LangUtils.HASH_SEED;
146 hash = LangUtils.hashCode(hash, this.principal);
147 hash = LangUtils.hashCode(hash, this.workstation);
148 return hash;
149 }
150
151 @Override
152 public boolean equals(final Object o) {
153 if (this == o) {
154 return true;
155 }
156 if (o instanceof NTCredentials) {
157 final NTCredentials that = (NTCredentials) o;
158 if (LangUtils.equals(this.principal, that.principal)
159 && LangUtils.equals(this.workstation, that.workstation)) {
160 return true;
161 }
162 }
163 return false;
164 }
165
166 @Override
167 public String toString() {
168 final StringBuilder buffer = new StringBuilder();
169 buffer.append("[principal: ");
170 buffer.append(this.principal);
171 buffer.append("][workstation: ");
172 buffer.append(this.workstation);
173 buffer.append("]");
174 return buffer.toString();
175 }
176
177 }