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 package org.apache.hc.client5.http.auth;
28
29 import java.io.Serializable;
30 import java.security.Principal;
31 import java.util.Locale;
32
33 import org.apache.hc.core5.annotation.Contract;
34 import org.apache.hc.core5.annotation.ThreadingBehavior;
35 import org.apache.hc.core5.util.Args;
36 import org.apache.hc.core5.util.LangUtils;
37
38
39
40
41
42
43
44 @Contract(threading = ThreadingBehavior.IMMUTABLE)
45 public class NTCredentials implements Credentials, Serializable {
46
47 private static final long serialVersionUID = -7385699315228907265L;
48
49
50 private final NTUserPrincipal principal;
51
52
53 private final char[] password;
54
55
56 private final String workstation;
57
58
59 private final String netbiosDomain;
60
61
62
63
64
65
66
67
68
69
70 public NTCredentials(
71 final String userName,
72 final char[] password,
73 final String workstation,
74 final String domain) {
75 this(userName, password, convertHost(workstation), domain, convertDomain(domain));
76 }
77
78
79
80
81
82
83
84
85
86
87
88 public NTCredentials(
89 final String userName,
90 final char[] password,
91 final String workstation,
92 final String domain,
93 final String netbiosDomain) {
94 super();
95 Args.notNull(userName, "User name");
96 this.principal = new NTUserPrincipal(domain, userName);
97 this.password = password;
98 if (workstation != null) {
99 this.workstation = workstation.toUpperCase(Locale.ROOT);
100 } else {
101 this.workstation = null;
102 }
103 this.netbiosDomain = netbiosDomain;
104 }
105
106 @Override
107 public Principal getUserPrincipal() {
108 return this.principal;
109 }
110
111 public String getUserName() {
112 return this.principal.getUsername();
113 }
114
115 @Override
116 public char[] getPassword() {
117 return this.password;
118 }
119
120
121
122
123
124
125 public String getDomain() {
126 return this.principal.getDomain();
127 }
128
129
130
131
132
133 public String getNetbiosDomain() {
134 return this.netbiosDomain;
135 }
136
137
138
139
140
141
142 public String getWorkstation() {
143 return this.workstation;
144 }
145
146 @Override
147 public int hashCode() {
148 int hash = LangUtils.HASH_SEED;
149 hash = LangUtils.hashCode(hash, this.principal);
150 hash = LangUtils.hashCode(hash, this.workstation);
151 hash = LangUtils.hashCode(hash, this.netbiosDomain);
152 return hash;
153 }
154
155 @Override
156 public boolean equals(final Object o) {
157 if (this == o) {
158 return true;
159 }
160 if (o instanceof NTCredentials) {
161 final NTCredentials/../../../../org/apache/hc/client5/http/auth/NTCredentials.html#NTCredentials">NTCredentials that = (NTCredentials) o;
162 return LangUtils.equals(this.principal, that.principal)
163 && LangUtils.equals(this.workstation, that.workstation)
164 && LangUtils.equals(this.netbiosDomain, that.netbiosDomain);
165 }
166 return false;
167 }
168
169 @Override
170 public String toString() {
171 final StringBuilder buffer = new StringBuilder();
172 buffer.append("[principal: ");
173 buffer.append(this.principal);
174 buffer.append("][workstation: ");
175 buffer.append(this.workstation);
176 buffer.append("][netbiosDomain: ");
177 buffer.append(this.netbiosDomain);
178 buffer.append("]");
179 return buffer.toString();
180 }
181
182
183 private static String stripDotSuffix(final String value) {
184 if (value == null) {
185 return null;
186 }
187 final int index = value.indexOf('.');
188 if (index != -1) {
189 return value.substring(0, index);
190 }
191 return value;
192 }
193
194
195 private static String convertHost(final String host) {
196 return stripDotSuffix(host);
197 }
198
199
200 private static String convertDomain(final String domain) {
201 final String returnString = stripDotSuffix(domain);
202 return returnString == null ? returnString : returnString.toUpperCase(Locale.ROOT);
203 }
204
205 }