|
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 */ |
|
26 |
|
27 package ch.boye.httpclientandroidlib.auth; |
|
28 |
|
29 import java.io.Serializable; |
|
30 import java.security.Principal; |
|
31 import java.util.Locale; |
|
32 |
|
33 import ch.boye.httpclientandroidlib.annotation.Immutable; |
|
34 |
|
35 import ch.boye.httpclientandroidlib.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(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 } |
|
89 |
|
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 } |
|
116 |
|
117 public Principal getUserPrincipal() { |
|
118 return this.principal; |
|
119 } |
|
120 |
|
121 public String getUserName() { |
|
122 return this.principal.getUsername(); |
|
123 } |
|
124 |
|
125 public String getPassword() { |
|
126 return this.password; |
|
127 } |
|
128 |
|
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 } |
|
137 |
|
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 } |
|
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 return hash; |
|
153 } |
|
154 |
|
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 } |
|
167 |
|
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 } |
|
178 |
|
179 } |