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