View Javadoc
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.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   * {@link Credentials} implementation for Microsoft Windows platforms that includes
40   * Windows specific attributes such as name of the domain the user belongs to.
41   *
42   * @since 4.0
43   */
44  @Contract(threading = ThreadingBehavior.IMMUTABLE)
45  public class NTCredentials implements Credentials, Serializable {
46  
47      private static final long serialVersionUID = -7385699315228907265L;
48  
49      /** The user principal  */
50      private final NTUserPrincipal principal;
51  
52      /** Password */
53      private final String password;
54  
55      /** The host the authentication request is originating from.  */
56      private final String workstation;
57  
58      /**
59       * The constructor with the fully qualified username and password combined
60       * string argument.
61       *
62       * @param usernamePassword the domain/username:password formed string
63       * @deprecated (4.5) will be replaced with {@code String}, {@code char[]} in 5.0
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       * Constructor.
93       * @param userName The user name.  This should not include the domain to authenticate with.
94       * For example: "user" is correct whereas "DOMAIN&#x5c;user" is not.
95       * @param password The password.
96       * @param workstation The workstation the authentication request is originating from.
97       * Essentially, the computer name for this machine.
98       * @param domain The domain to authenticate within.
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      * Retrieves the name to authenticate with.
132      *
133      * @return String the domain these credentials are intended to authenticate with.
134      */
135     public String getDomain() {
136         return this.principal.getDomain();
137     }
138 
139     /**
140      * Retrieves the workstation name of the computer originating the request.
141      *
142      * @return String the workstation the user is logged into.
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 }