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  
33  import org.apache.hc.core5.annotation.Contract;
34  import org.apache.hc.core5.annotation.ThreadingBehavior;
35  import org.apache.hc.core5.util.Args;
36  import org.apache.hc.core5.util.LangUtils;
37  
38  /**
39   * Microsoft Windows specific {@link Credentials} representation 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 char[] password;
54  
55      /** The netbios hostname the authentication request is originating from.  */
56      private final String workstation;
57  
58      /** The netbios domain the authentication request is against */
59      private final String netbiosDomain;
60  
61      /**
62       * Constructor.
63       * @param userName The user name.  This should not include the domain to authenticate with.
64       * For example: "user" is correct whereas "DOMAIN&#x5c;user" is not.
65       * @param password The password.
66       * @param workstation The workstation the authentication request is originating from.
67       * Essentially, the computer name for this machine.
68       * @param domain The domain to authenticate within.
69       */
70      public NTCredentials(
71              final String userName,
72              final char[] password,
73              final String workstation,
74              final String domain) {
75          this(userName, password, convertHost(workstation), domain, convertDomain(domain));
76      }
77  
78      /**
79       * Constructor.
80       * @param userName The user name.  This should not include the domain to authenticate with.
81       * For example: "user" is correct whereas "DOMAIN&#x5c;user" is not.
82       * @param password The password.
83       * @param workstation The netbios workstation name that the authentication request is originating from.
84       * Essentially, the computer name for this machine.
85       * @param domain The domain to authenticate within.
86       * @param netbiosDomain The netbios version of the domain name.
87       */
88      public NTCredentials(
89              final String userName,
90              final char[] password,
91              final String workstation,
92              final String domain,
93              final String netbiosDomain) {
94          super();
95          Args.notNull(userName, "User name");
96          this.principal = new NTUserPrincipal(domain, userName);
97          this.password = password;
98          if (workstation != null) {
99              this.workstation = workstation.toUpperCase(Locale.ROOT);
100         } else {
101             this.workstation = null;
102         }
103         this.netbiosDomain = netbiosDomain;
104     }
105 
106     @Override
107     public Principal getUserPrincipal() {
108         return this.principal;
109     }
110 
111     public String getUserName() {
112         return this.principal.getUsername();
113     }
114 
115     @Override
116     public char[] getPassword() {
117         return this.password;
118     }
119 
120     /**
121      * Retrieves the name to authenticate with.
122      *
123      * @return String the domain these credentials are intended to authenticate with.
124      */
125     public String getDomain() {
126         return this.principal.getDomain();
127     }
128 
129     /**
130     * Retrieves the netbios domain to authenticate with.
131     * @return String the netbios domain name.
132     */
133     public String getNetbiosDomain() {
134         return this.netbiosDomain;
135     }
136 
137     /**
138      * Retrieves the netbios workstation name of the computer originating the request.
139      *
140      * @return String the netbios workstation the user is logged into.
141      */
142     public String getWorkstation() {
143         return this.workstation;
144     }
145 
146     @Override
147     public int hashCode() {
148         int hash = LangUtils.HASH_SEED;
149         hash = LangUtils.hashCode(hash, this.principal);
150         hash = LangUtils.hashCode(hash, this.workstation);
151         hash = LangUtils.hashCode(hash, this.netbiosDomain);
152         return hash;
153     }
154 
155     @Override
156     public boolean equals(final Object o) {
157         if (this == o) {
158             return true;
159         }
160         if (o instanceof NTCredentials) {
161             final NTCredentials that = (NTCredentials) o;
162             return LangUtils.equals(this.principal, that.principal)
163                     && LangUtils.equals(this.workstation, that.workstation)
164                     && LangUtils.equals(this.netbiosDomain, that.netbiosDomain);
165         }
166         return false;
167     }
168 
169     @Override
170     public String toString() {
171         final StringBuilder buffer = new StringBuilder();
172         buffer.append("[principal: ");
173         buffer.append(this.principal);
174         buffer.append("][workstation: ");
175         buffer.append(this.workstation);
176         buffer.append("][netbiosDomain: ");
177         buffer.append(this.netbiosDomain);
178         buffer.append("]");
179         return buffer.toString();
180     }
181 
182     /** Strip dot suffix from a name */
183     private static String stripDotSuffix(final String value) {
184         if (value == null) {
185             return null;
186         }
187         final int index = value.indexOf('.');
188         if (index != -1) {
189             return value.substring(0, index);
190         }
191         return value;
192     }
193 
194     /** Convert host to standard form */
195     private static String convertHost(final String host) {
196         return stripDotSuffix(host);
197     }
198 
199     /** Convert domain to standard form */
200     private static String convertDomain(final String domain) {
201         final String returnString = stripDotSuffix(domain);
202         return returnString == null ? returnString : returnString.toUpperCase(Locale.ROOT);
203     }
204 
205 }