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.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   * Microsoft Windows specific {@link Credentials} representation that includes
41   * Windows specific attributes such as name of the domain the user belongs to.
42   *
43   * @since 4.0
44   */
45  @Contract(threading = ThreadingBehavior.IMMUTABLE)
46  public class NTCredentials implements Credentials, Serializable {
47  
48      private static final long serialVersionUID = -7385699315228907265L;
49  
50      /** The user principal  */
51      private final NTUserPrincipal principal;
52  
53      /** Password */
54      private final char[] password;
55  
56      /** The netbios hostname the authentication request is originating from.  */
57      private final String workstation;
58  
59      /** The netbios domain the authentication request is against */
60      private final String netbiosDomain;
61  
62      /**
63       * Constructor.
64       * @param userName The user name.  This should not include the domain to authenticate with.
65       * For example: "user" is correct whereas "DOMAIN&#x5c;user" is not.
66       * @param password The password.
67       * @param workstation The workstation the authentication request is originating from.
68       * Essentially, the computer name for this machine.
69       * @param domain The domain to authenticate within.
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       * Constructor.
81       * @param userName The user name.  This should not include the domain to authenticate with.
82       * For example: "user" is correct whereas "DOMAIN&#x5c;user" is not.
83       * @param password The password.
84       * @param workstation The netbios workstation name that the authentication request is originating from.
85       * Essentially, the computer name for this machine.
86       * @param domain The domain to authenticate within.
87       * @param netbiosDomain The netbios version of the domain name.
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      * Retrieves the name to authenticate with.
123      *
124      * @return String the domain these credentials are intended to authenticate with.
125      */
126     public String getDomain() {
127         return this.principal.getDomain();
128     }
129 
130     /**
131     * Retrieves the netbios domain to authenticate with.
132     * @return String the netbios domain name.
133     */
134     public String getNetbiosDomain() {
135         return this.netbiosDomain;
136     }
137 
138     /**
139      * Retrieves the netbios workstation name of the computer originating the request.
140      *
141      * @return String the netbios 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         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     /** Strip dot suffix from a name */
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     /** Convert host to standard form */
196     private static String convertHost(final String host) {
197         return stripDotSuffix(host);
198     }
199 
200     /** Convert domain to standard form */
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 }