Home | History | Annotate | Download | only in auth
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/NTCredentials.java $
      3  * $Revision: 658430 $
      4  * $Date: 2008-05-20 14:04:27 -0700 (Tue, 20 May 2008) $
      5  *
      6  * ====================================================================
      7  *
      8  *  Licensed to the Apache Software Foundation (ASF) under one or more
      9  *  contributor license agreements.  See the NOTICE file distributed with
     10  *  this work for additional information regarding copyright ownership.
     11  *  The ASF licenses this file to You under the Apache License, Version 2.0
     12  *  (the "License"); you may not use this file except in compliance with
     13  *  the License.  You may obtain a copy of the License at
     14  *
     15  *      http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  *  Unless required by applicable law or agreed to in writing, software
     18  *  distributed under the License is distributed on an "AS IS" BASIS,
     19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     20  *  See the License for the specific language governing permissions and
     21  *  limitations under the License.
     22  * ====================================================================
     23  *
     24  * This software consists of voluntary contributions made by many
     25  * individuals on behalf of the Apache Software Foundation.  For more
     26  * information on the Apache Software Foundation, please see
     27  * <http://www.apache.org/>.
     28  *
     29  */
     30 
     31 package org.apache.http.auth;
     32 
     33 import java.security.Principal;
     34 import java.util.Locale;
     35 
     36 import org.apache.http.util.LangUtils;
     37 
     38 /** {@link Credentials} specific to the Windows platform.
     39  *
     40  * @author <a href="mailto:adrian (at) ephox.com">Adrian Sutton</a>
     41  * @author <a href="mailto:mbowler (at) GargoyleSoftware.com">Mike Bowler</a>
     42  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
     43  *
     44  * @since 2.0
     45  *
     46  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     47  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     48  *     for further details.
     49  */
     50 @Deprecated
     51 public class NTCredentials implements Credentials {
     52 
     53     /** The user principal  */
     54     private final NTUserPrincipal principal;
     55 
     56     /** Password */
     57     private final String password;
     58 
     59     /** The host the authentication request is originating from.  */
     60     private final String workstation;
     61 
     62     /**
     63      * The constructor with the fully qualified username and password combined
     64      * string argument.
     65      *
     66      * @param usernamePassword the domain/username:password formed string
     67      */
     68     public NTCredentials(String usernamePassword) {
     69         super();
     70         if (usernamePassword == null) {
     71             throw new IllegalArgumentException("Username:password string may not be null");
     72         }
     73         String username;
     74         int atColon = usernamePassword.indexOf(':');
     75         if (atColon >= 0) {
     76             username = usernamePassword.substring(0, atColon);
     77             this.password = usernamePassword.substring(atColon + 1);
     78         } else {
     79             username = usernamePassword;
     80             this.password = null;
     81         }
     82         int atSlash = username.indexOf('/');
     83         if (atSlash >= 0) {
     84             this.principal = new NTUserPrincipal(
     85                     username.substring(0, atSlash).toUpperCase(Locale.ENGLISH),
     86                     username.substring(atSlash + 1));
     87         } else {
     88             this.principal = new NTUserPrincipal(
     89                     null,
     90                     username.substring(atSlash + 1));
     91         }
     92         this.workstation = null;
     93     }
     94 
     95     /**
     96      * Constructor.
     97      * @param userName The user name.  This should not include the domain to authenticate with.
     98      * For example: "user" is correct whereas "DOMAIN\\user" is not.
     99      * @param password The password.
    100      * @param workstation The workstation the authentication request is originating from.
    101      * Essentially, the computer name for this machine.
    102      * @param domain The domain to authenticate within.
    103      */
    104     public NTCredentials(
    105             final String userName,
    106             final String password,
    107             final String workstation,
    108             final String domain) {
    109         super();
    110         if (userName == null) {
    111             throw new IllegalArgumentException("User name may not be null");
    112         }
    113         this.principal = new NTUserPrincipal(domain, userName);
    114         this.password = password;
    115         if (workstation != null) {
    116             this.workstation = workstation.toUpperCase(Locale.ENGLISH);
    117         } else {
    118             this.workstation = null;
    119         }
    120     }
    121 
    122     public Principal getUserPrincipal() {
    123         return this.principal;
    124     }
    125 
    126     public String getUserName() {
    127         return this.principal.getUsername();
    128     }
    129 
    130     public String getPassword() {
    131         return this.password;
    132     }
    133 
    134     /**
    135      * Retrieves the name to authenticate with.
    136      *
    137      * @return String the domain these credentials are intended to authenticate with.
    138      */
    139     public String getDomain() {
    140         return this.principal.getDomain();
    141     }
    142 
    143     /**
    144      * Retrieves the workstation name of the computer originating the request.
    145      *
    146      * @return String the workstation the user is logged into.
    147      */
    148     public String getWorkstation() {
    149         return this.workstation;
    150     }
    151 
    152     @Override
    153     public int hashCode() {
    154         int hash = LangUtils.HASH_SEED;
    155         hash = LangUtils.hashCode(hash, this.principal);
    156         hash = LangUtils.hashCode(hash, this.workstation);
    157         return hash;
    158     }
    159 
    160     @Override
    161     public boolean equals(Object o) {
    162         if (o == null) return false;
    163         if (this == o) return true;
    164         if (o instanceof NTCredentials) {
    165             NTCredentials that = (NTCredentials) o;
    166             if (LangUtils.equals(this.principal, that.principal)
    167                     && LangUtils.equals(this.workstation, that.workstation)) {
    168                 return true;
    169             }
    170         }
    171         return false;
    172     }
    173 
    174     @Override
    175     public String toString() {
    176         StringBuilder buffer = new StringBuilder();
    177         buffer.append("[principal: ");
    178         buffer.append(this.principal);
    179         buffer.append("][workstation: ");
    180         buffer.append(this.workstation);
    181         buffer.append("]");
    182         return buffer.toString();
    183     }
    184 
    185 }
    186