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.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.Contract;
34 import org.apache.http.annotation.ThreadingBehavior;
35 import org.apache.http.util.Args;
36 import org.apache.http.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 String password;
54
55
56 private final String workstation;
57
58
59
60
61
62
63
64
65 @Deprecated
66 public NTCredentials(final String usernamePassword) {
67 super();
68 Args.notNull(usernamePassword, "Username:password string");
69 final String username;
70 final int atColon = usernamePassword.indexOf(':');
71 if (atColon >= 0) {
72 username = usernamePassword.substring(0, atColon);
73 this.password = usernamePassword.substring(atColon + 1);
74 } else {
75 username = usernamePassword;
76 this.password = null;
77 }
78 final int atSlash = username.indexOf('/');
79 if (atSlash >= 0) {
80 this.principal = new NTUserPrincipal(
81 username.substring(0, atSlash).toUpperCase(Locale.ROOT),
82 username.substring(atSlash + 1));
83 } else {
84 this.principal = new NTUserPrincipal(
85 null,
86 username.substring(atSlash + 1));
87 }
88 this.workstation = null;
89 }
90
91
92
93
94
95
96
97
98
99
100 public NTCredentials(
101 final String userName,
102 final String password,
103 final String workstation,
104 final String domain) {
105 super();
106 Args.notNull(userName, "User name");
107 this.principal = new NTUserPrincipal(domain, userName);
108 this.password = password;
109 if (workstation != null) {
110 this.workstation = workstation.toUpperCase(Locale.ROOT);
111 } else {
112 this.workstation = null;
113 }
114 }
115
116 @Override
117 public Principal getUserPrincipal() {
118 return this.principal;
119 }
120
121 public String getUserName() {
122 return this.principal.getUsername();
123 }
124
125 @Override
126 public String getPassword() {
127 return this.password;
128 }
129
130
131
132
133
134
135 public String getDomain() {
136 return this.principal.getDomain();
137 }
138
139
140
141
142
143
144 public String getWorkstation() {
145 return this.workstation;
146 }
147
148 @Override
149 public int hashCode() {
150 int hash = LangUtils.HASH_SEED;
151 hash = LangUtils.hashCode(hash, this.principal);
152 hash = LangUtils.hashCode(hash, this.workstation);
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/../../org/apache/http/auth/NTCredentials.html#NTCredentials">NTCredentials that = (NTCredentials) o;
163 if (LangUtils.equals(this.principal, that.principal)
164 && LangUtils.equals(this.workstation, that.workstation)) {
165 return true;
166 }
167 }
168 return false;
169 }
170
171 @Override
172 public String toString() {
173 final StringBuilder buffer = new StringBuilder();
174 buffer.append("[principal: ");
175 buffer.append(this.principal);
176 buffer.append("][workstation: ");
177 buffer.append(this.workstation);
178 buffer.append("]");
179 return buffer.toString();
180 }
181
182 }