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.Immutable;
34  import org.apache.http.util.Args;
35  import org.apache.http.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       * @deprecated (4.5) will be replaced with {@code String}, {@code char[]} in 5.0
63       */
64      @Deprecated
65      public NTCredentials(final String usernamePassword) {
66          super();
67          Args.notNull(usernamePassword, "Username:password string");
68          final String username;
69          final 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          final int atSlash = username.indexOf('/');
78          if (atSlash >= 0) {
79              this.principal = new NTUserPrincipal(
80                      username.substring(0, atSlash).toUpperCase(Locale.ROOT),
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&#x5c;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         Args.notNull(userName, "User name");
106         this.principal = new NTUserPrincipal(domain, userName);
107         this.password = password;
108         if (workstation != null) {
109             this.workstation = workstation.toUpperCase(Locale.ROOT);
110         } else {
111             this.workstation = null;
112         }
113     }
114 
115     @Override
116     public Principal getUserPrincipal() {
117         return this.principal;
118     }
119 
120     public String getUserName() {
121         return this.principal.getUsername();
122     }
123 
124     @Override
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(final Object o) {
157         if (this == o) {
158             return true;
159         }
160         if (o instanceof NTCredentials) {
161             final NTCredentials that = (NTCredentials) o;
162             if (LangUtils.equals(this.principal, that.principal)
163                     && LangUtils.equals(this.workstation, that.workstation)) {
164                 return true;
165             }
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("]");
178         return buffer.toString();
179     }
180 
181 }