* Refactored UsernamePasswordCredentials and NTCredentials (copied from HttpClient 3.1 code line)

* Added test coverage

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@653864 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2008-05-06 18:12:39 +00:00
parent 9b340a0559
commit b418ac5e2c
5 changed files with 477 additions and 71 deletions

View File

@ -0,0 +1,197 @@
/*
* $HeadURL$
* $Revision$
* $Date$
*
* ====================================================================
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.auth;
import java.util.Locale;
import org.apache.http.util.LangUtils;
/** {@link Credentials} specific to the Windows platform.
*
* @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a>
* @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*
* @since 2.0
*/
public class NTCredentials implements Credentials {
/** User name */
private final String userName;
/** Password */
private final String password;
/** The Domain to authenticate with. */
private final String domain;
/** The host the authentication request is originating from. */
private final String workstation;
/**
* The constructor with the fully qualified username and password combined
* string argument.
*
* @param usernamePassword the domain/username:password formed string
*/
public NTCredentials(String usernamePassword) {
super();
if (usernamePassword == null) {
throw new IllegalArgumentException("Username:password string may not be null");
}
String username;
int atColon = usernamePassword.indexOf(':');
if (atColon >= 0) {
username = usernamePassword.substring(0, atColon);
this.password = usernamePassword.substring(atColon + 1);
} else {
username = usernamePassword;
this.password = null;
}
int atSlash = username.indexOf('/');
if (atSlash >= 0) {
this.domain = username.substring(0, atSlash).toUpperCase(Locale.ENGLISH);
this.userName = username.substring(atSlash + 1);
} else {
this.domain = null;
this.userName = username;
}
this.workstation = null;
}
/**
* Constructor.
* @param userName The user name. This should not include the domain to authenticate with.
* For example: "user" is correct whereas "DOMAIN\\user" is not.
* @param password The password.
* @param workstation The workstation the authentication request is originating from.
* Essentially, the computer name for this machine.
* @param domain The domain to authenticate within.
*/
public NTCredentials(
final String userName,
final String password,
final String workstation,
final String domain) {
super();
if (userName == null) {
throw new IllegalArgumentException("User name may not be null");
}
this.userName = userName;
this.password = password;
if (workstation != null) {
this.workstation = workstation.toUpperCase(Locale.ENGLISH);
} else {
this.workstation = null;
}
if (domain != null) {
this.domain = domain.toUpperCase(Locale.ENGLISH);
} else {
this.domain = null;
}
}
public String getUserName() {
return this.userName;
}
public String getPrincipalName() {
if (this.domain != null && this.domain.length() > 0) {
StringBuilder buffer = new StringBuilder();
buffer.append(this.domain);
buffer.append('/');
buffer.append(this.userName);
return buffer.toString();
} else {
return this.userName;
}
}
public String getPassword() {
return this.password;
}
/**
* Retrieves the name to authenticate with.
*
* @return String the domain these credentials are intended to authenticate with.
*/
public String getDomain() {
return domain;
}
/**
* Retrieves the workstation name of the computer originating the request.
*
* @return String the workstation the user is logged into.
*/
public String getWorkstation() {
return this.workstation;
}
@Override
public int hashCode() {
int hash = LangUtils.HASH_SEED;
hash = LangUtils.hashCode(hash, this.userName);
hash = LangUtils.hashCode(hash, this.workstation);
hash = LangUtils.hashCode(hash, this.domain);
return hash;
}
@Override
public boolean equals(Object o) {
if (o == null) return false;
if (this == o) return true;
if (o instanceof NTCredentials) {
NTCredentials that = (NTCredentials) o;
if (LangUtils.equals(this.userName, that.userName)
&& LangUtils.equals(this.workstation, that.workstation)
&& LangUtils.equals(this.domain, that.domain)) {
return true;
}
}
return false;
}
@Override
public String toString() {
StringBuilder buffer = new StringBuilder();
buffer.append("[username: ");
buffer.append(this.userName);
buffer.append("][workstation: ");
buffer.append(this.workstation);
buffer.append("][domain: ");
buffer.append(this.domain);
buffer.append("]");
return buffer.toString();
}
}

View File

@ -33,7 +33,7 @@ package org.apache.http.auth;
import org.apache.http.util.LangUtils;
/**
* <p>Username and password {@link Credentials}.</p>
* Username and password {@link Credentials}
*
* @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
* @author Sean C. Sullivan
@ -45,8 +45,9 @@ import org.apache.http.util.LangUtils;
*/
public class UsernamePasswordCredentials implements Credentials {
// ----------------------------------------------------------- Constructors
private final String userName;
private final String password;
/**
* The constructor with the username and password combined string argument.
*
@ -84,99 +85,46 @@ public class UsernamePasswordCredentials implements Credentials {
this.password = password;
}
// ----------------------------------------------------- Instance Variables
/**
* User name.
*/
private final String userName;
/**
* Password.
*/
private final String password;
// ------------------------------------------------------------- Properties
/**
* User name property getter.
*
* @return the userName
*/
public String getPrincipalName() {
return userName;
}
public String getUserName() {
return userName;
}
/**
* Password property getter.
*
* @return the password
*/
public String getPassword() {
return password;
}
public String toText() {
return toString();
}
/**
* Get this object string.
*
* @return the username:password formed string
*/
@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append(this.userName);
result.append(':');
result.append((this.password == null) ? "null" : this.password);
return result.toString();
}
/**
* Does a hash of both user name and password.
*
* @return The hash code including user name and password.
*/
@Override
public int hashCode() {
int hash = LangUtils.HASH_SEED;
hash = LangUtils.hashCode(hash, this.userName);
hash = LangUtils.hashCode(hash, this.password);
return hash;
}
/**
* These credentials are assumed equal if the username and password are the
* same.
*
* @param o The other object to compare with.
*
* @return <code>true</code> if the object is equivalent.
*/
@Override
public boolean equals(Object o) {
if (o == null) return false;
if (this == o) return true;
// note - to allow for sub-classing, this checks that class is the same
// rather than do "instanceof".
if (this.getClass().equals(o.getClass())) {
if (o instanceof UsernamePasswordCredentials) {
UsernamePasswordCredentials that = (UsernamePasswordCredentials) o;
if (LangUtils.equals(this.userName, that.userName)
&& LangUtils.equals(this.password, that.password) ) {
if (LangUtils.equals(this.userName, that.userName)) {
return true;
}
}
return false;
}
@Override
public String toString() {
StringBuilder buffer = new StringBuilder();
buffer.append("[username: ");
buffer.append(this.userName);
buffer.append("]");
return buffer.toString();
}
}

View File

@ -0,0 +1,54 @@
/*
* $HeadURL$
* $Revision$
* $Date$
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.auth;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class TestAllAuth extends TestCase {
public TestAllAuth(String testName) {
super(testName);
}
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(TestCredentials.suite());
return suite;
}
public static void main(String args[]) {
String[] testCaseName = { TestAllAuth.class.getName() };
junit.textui.TestRunner.main(testCaseName);
}
}

View File

@ -0,0 +1,203 @@
/*
* $HeadURL$
* $Revision$
* $Date$
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.auth;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class TestCredentials extends TestCase {
// ------------------------------------------------------------ Constructor
public TestCredentials(final String testName) {
super(testName);
}
// ------------------------------------------------------------------- Main
public static void main(String args[]) {
String[] testCaseName = { TestCredentials.class.getName() };
junit.textui.TestRunner.main(testCaseName);
}
// ------------------------------------------------------- TestCase Methods
public static Test suite() {
return new TestSuite(TestCredentials.class);
}
public void testUsernamePasswordCredentialsBasics() {
UsernamePasswordCredentials creds1 = new UsernamePasswordCredentials(
"name", "pwd");
assertEquals("name", creds1.getUserName());
assertEquals("name", creds1.getPrincipalName());
assertEquals("pwd", creds1.getPassword());
assertEquals("[username: name]", creds1.toString());
UsernamePasswordCredentials creds2 = new UsernamePasswordCredentials(
"name:pwd");
assertEquals("name", creds2.getUserName());
assertEquals("name", creds2.getPrincipalName());
assertEquals("pwd", creds2.getPassword());
assertEquals("[username: name]", creds2.toString());
UsernamePasswordCredentials creds3 = new UsernamePasswordCredentials(
"name");
assertEquals("name", creds3.getUserName());
assertEquals("name", creds3.getPrincipalName());
assertEquals(null, creds3.getPassword());
assertEquals("[username: name]", creds3.toString());
}
public void testNTCredentialsBasics() {
NTCredentials creds1 = new NTCredentials(
"name", "pwd", "localhost", "domain");
assertEquals("name", creds1.getUserName());
assertEquals("DOMAIN/name", creds1.getPrincipalName());
assertEquals("pwd", creds1.getPassword());
assertEquals("[username: name][workstation: LOCALHOST]" +
"[domain: DOMAIN]", creds1.toString());
NTCredentials creds2 = new NTCredentials(
"name", null, null, null);
assertEquals("name", creds2.getUserName());
assertEquals("name", creds2.getPrincipalName());
assertEquals(null, creds2.getPassword());
assertEquals("[username: name][workstation: null]" +
"[domain: null]", creds2.toString());
NTCredentials creds3 = new NTCredentials(
"domain/name:pwd");
assertEquals("name", creds3.getUserName());
assertEquals("DOMAIN/name", creds3.getPrincipalName());
assertEquals("pwd", creds3.getPassword());
assertEquals("[username: name][workstation: null]" +
"[domain: DOMAIN]", creds3.toString());
NTCredentials creds4 = new NTCredentials(
"domain/name");
assertEquals("name", creds4.getUserName());
assertEquals("DOMAIN/name", creds4.getPrincipalName());
assertEquals(null, creds4.getPassword());
assertEquals("[username: name][workstation: null]" +
"[domain: DOMAIN]", creds4.toString());
NTCredentials creds5 = new NTCredentials(
"name");
assertEquals("name", creds5.getUserName());
assertEquals("name", creds5.getPrincipalName());
assertEquals(null, creds5.getPassword());
assertEquals("[username: name][workstation: null]" +
"[domain: null]", creds5.toString());
}
public void testUsernamePasswordCredentialsHashCode() {
UsernamePasswordCredentials creds1 = new UsernamePasswordCredentials(
"name", "pwd");
UsernamePasswordCredentials creds2 = new UsernamePasswordCredentials(
"othername", "pwd");
UsernamePasswordCredentials creds3 = new UsernamePasswordCredentials(
"name", "otherpwd");
assertTrue(creds1.hashCode() == creds1.hashCode());
assertTrue(creds1.hashCode() != creds2.hashCode());
assertTrue(creds1.hashCode() == creds3.hashCode());
}
public void testUsernamePasswordCredentialsEquals() {
UsernamePasswordCredentials creds1 = new UsernamePasswordCredentials(
"name", "pwd");
UsernamePasswordCredentials creds2 = new UsernamePasswordCredentials(
"othername", "pwd");
UsernamePasswordCredentials creds3 = new UsernamePasswordCredentials(
"name", "otherpwd");
assertTrue(creds1.equals(creds1));
assertFalse(creds1.equals(creds2));
assertTrue(creds1.equals(creds3));
}
public void testNTCredentialsHashCode() {
NTCredentials creds1 = new NTCredentials(
"name", "pwd", "somehost", "domain");
NTCredentials creds2 = new NTCredentials(
"othername", "pwd", "somehost", "domain");
NTCredentials creds3 = new NTCredentials(
"name", "otherpwd", "SomeHost", "Domain");
NTCredentials creds4 = new NTCredentials(
"name", "pwd", "otherhost", "domain");
NTCredentials creds5 = new NTCredentials(
"name", "pwd", null, "domain");
NTCredentials creds6 = new NTCredentials(
"name", "pwd", "somehost", "ms");
NTCredentials creds7 = new NTCredentials(
"name", "pwd", "somehost", null);
NTCredentials creds8 = new NTCredentials(
"name", "pwd", null, "domain");
NTCredentials creds9 = new NTCredentials(
"name", "pwd", "somehost", null);
assertTrue(creds1.hashCode() == creds1.hashCode());
assertTrue(creds1.hashCode() != creds2.hashCode());
assertTrue(creds1.hashCode() == creds3.hashCode());
assertFalse(creds1.hashCode() == creds4.hashCode());
assertFalse(creds1.hashCode() == creds5.hashCode());
assertFalse(creds1.hashCode() == creds6.hashCode());
assertFalse(creds1.hashCode() == creds7.hashCode());
assertTrue(creds8.hashCode() == creds5.hashCode());
assertTrue(creds9.hashCode() == creds7.hashCode());
}
public void testNTCredentialsEquals() {
NTCredentials creds1 = new NTCredentials(
"name", "pwd", "somehost", "domain");
NTCredentials creds2 = new NTCredentials(
"othername", "pwd", "somehost", "domain");
NTCredentials creds3 = new NTCredentials(
"name", "otherpwd", "SomeHost", "Domain");
NTCredentials creds4 = new NTCredentials(
"name", "pwd", "otherhost", "domain");
NTCredentials creds5 = new NTCredentials(
"name", "pwd", null, "domain");
NTCredentials creds6 = new NTCredentials(
"name", "pwd", "somehost", "ms");
NTCredentials creds7 = new NTCredentials(
"name", "pwd", "somehost", null);
NTCredentials creds8 = new NTCredentials(
"name", "pwd", null, "domain");
NTCredentials creds9 = new NTCredentials(
"name", "pwd", "somehost", null);
assertTrue(creds1.equals(creds1));
assertFalse(creds1.equals(creds2));
assertTrue(creds1.equals(creds3));
assertFalse(creds1.equals(creds4));
assertFalse(creds1.equals(creds5));
assertFalse(creds1.equals(creds6));
assertFalse(creds1.equals(creds7));
assertTrue(creds8.equals(creds5));
assertTrue(creds9.equals(creds7));
}
}

View File

@ -34,10 +34,12 @@ import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.http.auth.TestAllAuth;
import org.apache.http.client.methods.TestAllMethods;
import org.apache.http.client.protocol.TestAllProtocol;
import org.apache.http.conn.TestAllConn;
import org.apache.http.cookie.TestAllCookie;
import org.apache.http.impl.auth.TestAllAuthImpl;
import org.apache.http.impl.client.TestAllHttpClientImpl;
import org.apache.http.impl.conn.TestAllConnImpl;
import org.apache.http.impl.conn.tsccm.TestAllTSCCM;
@ -53,12 +55,14 @@ public class TestAll extends TestCase {
TestSuite suite = new TestSuite();
suite.addTest(TestAllCookie.suite());
suite.addTest(TestAllCookieImpl.suite());
suite.addTest(TestAllHttpClientImpl.suite());
suite.addTest(TestAllAuth.suite());
suite.addTest(TestAllAuthImpl.suite());
suite.addTest(TestAllConn.suite());
suite.addTest(TestAllConnImpl.suite());
suite.addTest(TestAllTSCCM.suite());
suite.addTest(TestAllProtocol.suite());
suite.addTest(TestAllMethods.suite());
suite.addTest(TestAllHttpClientImpl.suite());
return suite;
}