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      public NTCredentials(final String usernamePassword) {
65          super();
66          Args.notNull(usernamePassword, "Username:password string");
67          final String username;
68          final int atColon = usernamePassword.indexOf(':');
69          if (atColon >= 0) {
70              username = usernamePassword.substring(0, atColon);
71              this.password = usernamePassword.substring(atColon + 1);
72          } else {
73              username = usernamePassword;
74              this.password = null;
75          }
76          final int atSlash = username.indexOf('/');
77          if (atSlash >= 0) {
78              this.principal = new NTUserPrincipal(
79                      username.substring(0, atSlash).toUpperCase(Locale.ROOT),
80                      username.substring(atSlash + 1));
81          } else {
82              this.principal = new NTUserPrincipal(
83                      null,
84                      username.substring(atSlash + 1));
85          }
86          this.workstation = null;
87      }
88  
89      /**
90       * Constructor.
91       * @param userName The user name.  This should not include the domain to authenticate with.
92       * For example: "user" is correct whereas "DOMAIN&#x5c;user" is not.
93       * @param password The password.
94       * @param workstation The workstation the authentication request is originating from.
95       * Essentially, the computer name for this machine.
96       * @param domain The domain to authenticate within.
97       */
98      public NTCredentials(
99              final String userName,
100             final String password,
101             final String workstation,
102             final String domain) {
103         super();
104         Args.notNull(userName, "User name");
105         this.principal = new NTUserPrincipal(domain, userName);
106         this.password = password;
107         if (workstation != null) {
108             this.workstation = workstation.toUpperCase(Locale.ROOT);
109         } else {
110             this.workstation = null;
111         }
112     }
113 
114     @Override
115     public Principal getUserPrincipal() {
116         return this.principal;
117     }
118 
119     public String getUserName() {
120         return this.principal.getUsername();
121     }
122 
123     @Override
124     public String getPassword() {
125         return this.password;
126     }
127 
128     /**
129      * Retrieves the name to authenticate with.
130      *
131      * @return String the domain these credentials are intended to authenticate with.
132      */
133     public String getDomain() {
134         return this.principal.getDomain();
135     }
136 
137     /**
138      * Retrieves the workstation name of the computer originating the request.
139      *
140      * @return String the 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         return hash;
152     }
153 
154     @Override
155     public boolean equals(final Object o) {
156         if (this == o) {
157             return true;
158         }
159         if (o instanceof NTCredentials) {
160             final NTCredentials that = (NTCredentials) o;
161             if (LangUtils.equals(this.principal, that.principal)
162                     && LangUtils.equals(this.workstation, that.workstation)) {
163                 return true;
164             }
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("]");
177         return buffer.toString();
178     }
179 
180 }