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@ephox.com">Adrian Sutton</a> 41 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> 42 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> 43 * 44 * @since 2.0 45 */ 46 public class NTCredentials implements Credentials { 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 */ NTCredentials(String usernamePassword)63 public NTCredentials(String usernamePassword) { 64 super(); 65 if (usernamePassword == null) { 66 throw new IllegalArgumentException("Username:password string may not be null"); 67 } 68 String username; 69 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 int atSlash = username.indexOf('/'); 78 if (atSlash >= 0) { 79 this.principal = new NTUserPrincipal( 80 username.substring(0, atSlash).toUpperCase(Locale.ENGLISH), 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\\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 */ NTCredentials( final String userName, final String password, final String workstation, final String domain)99 public NTCredentials( 100 final String userName, 101 final String password, 102 final String workstation, 103 final String domain) { 104 super(); 105 if (userName == null) { 106 throw new IllegalArgumentException("User name may not be null"); 107 } 108 this.principal = new NTUserPrincipal(domain, userName); 109 this.password = password; 110 if (workstation != null) { 111 this.workstation = workstation.toUpperCase(Locale.ENGLISH); 112 } else { 113 this.workstation = null; 114 } 115 } 116 getUserPrincipal()117 public Principal getUserPrincipal() { 118 return this.principal; 119 } 120 getUserName()121 public String getUserName() { 122 return this.principal.getUsername(); 123 } 124 getPassword()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 */ getDomain()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 */ getWorkstation()143 public String getWorkstation() { 144 return this.workstation; 145 } 146 147 @Override hashCode()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 equals(Object o)156 public boolean equals(Object o) { 157 if (o == null) return false; 158 if (this == o) return true; 159 if (o instanceof NTCredentials) { 160 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 toString()170 public String toString() { 171 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 } 181