You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2008/05/20 23:04:27 UTC
svn commit: r658430 - in /httpcomponents/httpclient/trunk/module-client/src:
main/java/org/apache/http/auth/ main/java/org/apache/http/impl/auth/
test/java/org/apache/http/auth/
Author: olegk
Date: Tue May 20 14:04:27 2008
New Revision: 658430
URL: http://svn.apache.org/viewvc?rev=658430&view=rev
Log:
Credentials interface changed to make use of standard java.security.Princinal to identify a user
Added:
httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/BasicUserPrincipal.java (with props)
httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/NTUserPrincipal.java (with props)
Modified:
httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/Credentials.java
httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/NTCredentials.java
httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/UsernamePasswordCredentials.java
httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/auth/BasicScheme.java
httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/auth/DigestScheme.java
httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/auth/TestCredentials.java
Added: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/BasicUserPrincipal.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/BasicUserPrincipal.java?rev=658430&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/BasicUserPrincipal.java (added)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/BasicUserPrincipal.java Tue May 20 14:04:27 2008
@@ -0,0 +1,90 @@
+/*
+ * $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.security.Principal;
+
+import org.apache.http.util.LangUtils;
+
+/**
+ * Basic user principal used for HTTP authentication
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ * @since 4.0
+ */
+public final class BasicUserPrincipal implements Principal {
+
+ private final String username;
+
+ public BasicUserPrincipal(final String username) {
+ super();
+ if (username == null) {
+ throw new IllegalArgumentException("User name may not be null");
+ }
+ this.username = username;
+ }
+
+ public String getName() {
+ return this.username;
+ }
+
+ @Override
+ public int hashCode() {
+ int hash = LangUtils.HASH_SEED;
+ hash = LangUtils.hashCode(hash, this.username);
+ return hash;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (o == null) return false;
+ if (this == o) return true;
+ if (o instanceof BasicUserPrincipal) {
+ BasicUserPrincipal that = (BasicUserPrincipal) o;
+ if (LangUtils.equals(this.username, that.username)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder buffer = new StringBuilder();
+ buffer.append("[principal: ");
+ buffer.append(this.username);
+ buffer.append("]");
+ return buffer.toString();
+ }
+
+}
+
Propchange: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/BasicUserPrincipal.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/BasicUserPrincipal.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/BasicUserPrincipal.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/Credentials.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/Credentials.java?rev=658430&r1=658429&r2=658430&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/Credentials.java (original)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/Credentials.java Tue May 20 14:04:27 2008
@@ -30,6 +30,8 @@
package org.apache.http.auth;
+import java.security.Principal;
+
/**
* User name and password based authentication credentials.
*
@@ -40,7 +42,7 @@
*/
public interface Credentials {
- String getPrincipalName();
+ Principal getUserPrincipal();
String getPassword();
Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/NTCredentials.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/NTCredentials.java?rev=658430&r1=658429&r2=658430&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/NTCredentials.java (original)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/NTCredentials.java Tue May 20 14:04:27 2008
@@ -30,6 +30,7 @@
package org.apache.http.auth;
+import java.security.Principal;
import java.util.Locale;
import org.apache.http.util.LangUtils;
@@ -44,15 +45,12 @@
*/
public class NTCredentials implements Credentials {
- /** User name */
- private final String userName;
+ /** The user principal */
+ private final NTUserPrincipal principal;
/** 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;
@@ -78,11 +76,13 @@
}
int atSlash = username.indexOf('/');
if (atSlash >= 0) {
- this.domain = username.substring(0, atSlash).toUpperCase(Locale.ENGLISH);
- this.userName = username.substring(atSlash + 1);
+ this.principal = new NTUserPrincipal(
+ username.substring(0, atSlash).toUpperCase(Locale.ENGLISH),
+ username.substring(atSlash + 1));
} else {
- this.domain = null;
- this.userName = username;
+ this.principal = new NTUserPrincipal(
+ null,
+ username.substring(atSlash + 1));
}
this.workstation = null;
}
@@ -105,34 +105,21 @@
if (userName == null) {
throw new IllegalArgumentException("User name may not be null");
}
- this.userName = userName;
+ this.principal = new NTUserPrincipal(domain, 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 Principal getUserPrincipal() {
+ return this.principal;
}
- 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 getUserName() {
+ return this.principal.getUsername();
}
public String getPassword() {
@@ -145,7 +132,7 @@
* @return String the domain these credentials are intended to authenticate with.
*/
public String getDomain() {
- return domain;
+ return this.principal.getDomain();
}
/**
@@ -160,9 +147,8 @@
@Override
public int hashCode() {
int hash = LangUtils.HASH_SEED;
- hash = LangUtils.hashCode(hash, this.userName);
+ hash = LangUtils.hashCode(hash, this.principal);
hash = LangUtils.hashCode(hash, this.workstation);
- hash = LangUtils.hashCode(hash, this.domain);
return hash;
}
@@ -172,9 +158,8 @@
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)) {
+ if (LangUtils.equals(this.principal, that.principal)
+ && LangUtils.equals(this.workstation, that.workstation)) {
return true;
}
}
@@ -184,12 +169,10 @@
@Override
public String toString() {
StringBuilder buffer = new StringBuilder();
- buffer.append("[username: ");
- buffer.append(this.userName);
+ buffer.append("[principal: ");
+ buffer.append(this.principal);
buffer.append("][workstation: ");
buffer.append(this.workstation);
- buffer.append("][domain: ");
- buffer.append(this.domain);
buffer.append("]");
return buffer.toString();
}
Added: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/NTUserPrincipal.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/NTUserPrincipal.java?rev=658430&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/NTUserPrincipal.java (added)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/NTUserPrincipal.java Tue May 20 14:04:27 2008
@@ -0,0 +1,113 @@
+/*
+ * $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.security.Principal;
+import java.util.Locale;
+
+import org.apache.http.util.LangUtils;
+
+/** NT (MS Windows specific) user principal used for HTTP authentication
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ * @since 4.0
+ */
+public class NTUserPrincipal implements Principal {
+
+ private final String username;
+ private final String domain;
+ private final String ntname;
+
+ public NTUserPrincipal(
+ final String domain,
+ final String username) {
+ super();
+ if (username == null) {
+ throw new IllegalArgumentException("User name may not be null");
+ }
+ this.username = username;
+ if (domain != null) {
+ this.domain = domain.toUpperCase(Locale.ENGLISH);
+ } else {
+ this.domain = null;
+ }
+ if (this.domain != null && this.domain.length() > 0) {
+ StringBuilder buffer = new StringBuilder();
+ buffer.append(this.domain);
+ buffer.append('/');
+ buffer.append(this.username);
+ this.ntname = buffer.toString();
+ } else {
+ this.ntname = this.username;
+ }
+ }
+
+ public String getName() {
+ return this.ntname;
+ }
+
+ public String getDomain() {
+ return this.domain;
+ }
+
+ public String getUsername() {
+ return this.username;
+ }
+
+ @Override
+ public int hashCode() {
+ int hash = LangUtils.HASH_SEED;
+ hash = LangUtils.hashCode(hash, this.username);
+ 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 NTUserPrincipal) {
+ NTUserPrincipal that = (NTUserPrincipal) o;
+ if (LangUtils.equals(this.username, that.username)
+ && LangUtils.equals(this.domain, that.domain)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public String toString() {
+ return this.ntname;
+ }
+
+}
Propchange: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/NTUserPrincipal.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/NTUserPrincipal.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/NTUserPrincipal.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/UsernamePasswordCredentials.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/UsernamePasswordCredentials.java?rev=658430&r1=658429&r2=658430&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/UsernamePasswordCredentials.java (original)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/auth/UsernamePasswordCredentials.java Tue May 20 14:04:27 2008
@@ -30,6 +30,8 @@
package org.apache.http.auth;
+import java.security.Principal;
+
import org.apache.http.util.LangUtils;
/**
@@ -45,7 +47,7 @@
*/
public class UsernamePasswordCredentials implements Credentials {
- private final String userName;
+ private final BasicUserPrincipal principal;
private final String password;
/**
@@ -61,10 +63,10 @@
}
int atColon = usernamePassword.indexOf(':');
if (atColon >= 0) {
- this.userName = usernamePassword.substring(0, atColon);
+ this.principal = new BasicUserPrincipal(usernamePassword.substring(0, atColon));
this.password = usernamePassword.substring(atColon + 1);
} else {
- this.userName = usernamePassword;
+ this.principal = new BasicUserPrincipal(usernamePassword);
this.password = null;
}
}
@@ -81,16 +83,16 @@
if (userName == null) {
throw new IllegalArgumentException("Username may not be null");
}
- this.userName = userName;
+ this.principal = new BasicUserPrincipal(userName);
this.password = password;
}
- public String getPrincipalName() {
- return userName;
+ public Principal getUserPrincipal() {
+ return this.principal;
}
public String getUserName() {
- return userName;
+ return this.principal.getName();
}
public String getPassword() {
@@ -99,9 +101,7 @@
@Override
public int hashCode() {
- int hash = LangUtils.HASH_SEED;
- hash = LangUtils.hashCode(hash, this.userName);
- return hash;
+ return this.principal.hashCode();
}
@Override
@@ -110,7 +110,7 @@
if (this == o) return true;
if (o instanceof UsernamePasswordCredentials) {
UsernamePasswordCredentials that = (UsernamePasswordCredentials) o;
- if (LangUtils.equals(this.userName, that.userName)) {
+ if (LangUtils.equals(this.principal, that.principal)) {
return true;
}
}
@@ -119,11 +119,7 @@
@Override
public String toString() {
- StringBuilder buffer = new StringBuilder();
- buffer.append("[username: ");
- buffer.append(this.userName);
- buffer.append("]");
- return buffer.toString();
+ return this.principal.toString();
}
}
Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/auth/BasicScheme.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/auth/BasicScheme.java?rev=658430&r1=658429&r2=658430&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/auth/BasicScheme.java (original)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/auth/BasicScheme.java Tue May 20 14:04:27 2008
@@ -163,7 +163,7 @@
}
StringBuilder tmp = new StringBuilder();
- tmp.append(credentials.getPrincipalName());
+ tmp.append(credentials.getUserPrincipal().getName());
tmp.append(":");
tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword());
Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/auth/DigestScheme.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/auth/DigestScheme.java?rev=658430&r1=658429&r2=658430&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/auth/DigestScheme.java (original)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/auth/DigestScheme.java Tue May 20 14:04:27 2008
@@ -270,7 +270,7 @@
MessageDigest md5Helper = createMessageDigest("MD5");
- String uname = credentials.getPrincipalName();
+ String uname = credentials.getUserPrincipal().getName();
String pwd = credentials.getPassword();
// 3.2.2.2: Calculating digest
@@ -372,8 +372,10 @@
String response = digest;
String algorithm = getParameter("algorithm");
+ String uname = credentials.getUserPrincipal().getName();
+
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(20);
- params.add(new BasicNameValuePair("username", credentials.getPrincipalName()));
+ params.add(new BasicNameValuePair("username", uname));
params.add(new BasicNameValuePair("realm", realm));
params.add(new BasicNameValuePair("nonce", nonce));
params.add(new BasicNameValuePair("uri", uri));
Modified: httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/auth/TestCredentials.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/auth/TestCredentials.java?rev=658430&r1=658429&r2=658430&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/auth/TestCredentials.java (original)
+++ httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/auth/TestCredentials.java Tue May 20 14:04:27 2008
@@ -57,59 +57,67 @@
UsernamePasswordCredentials creds1 = new UsernamePasswordCredentials(
"name", "pwd");
assertEquals("name", creds1.getUserName());
- assertEquals("name", creds1.getPrincipalName());
+ assertEquals(new BasicUserPrincipal("name"),
+ creds1.getUserPrincipal());
assertEquals("pwd", creds1.getPassword());
- assertEquals("[username: name]", creds1.toString());
+ assertEquals("[principal: name]", creds1.toString());
UsernamePasswordCredentials creds2 = new UsernamePasswordCredentials(
"name:pwd");
assertEquals("name", creds2.getUserName());
- assertEquals("name", creds2.getPrincipalName());
+ assertEquals(new BasicUserPrincipal("name"),
+ creds2.getUserPrincipal());
assertEquals("pwd", creds2.getPassword());
- assertEquals("[username: name]", creds2.toString());
+ assertEquals("[principal: name]", creds2.toString());
UsernamePasswordCredentials creds3 = new UsernamePasswordCredentials(
"name");
assertEquals("name", creds3.getUserName());
- assertEquals("name", creds3.getPrincipalName());
+ assertEquals(new BasicUserPrincipal("name"),
+ creds3.getUserPrincipal());
assertEquals(null, creds3.getPassword());
- assertEquals("[username: name]", creds3.toString());
+ assertEquals("[principal: name]", creds3.toString());
}
public void testNTCredentialsBasics() {
NTCredentials creds1 = new NTCredentials(
"name", "pwd", "localhost", "domain");
assertEquals("name", creds1.getUserName());
- assertEquals("DOMAIN/name", creds1.getPrincipalName());
+ assertEquals(new NTUserPrincipal("DOMAIN", "name"),
+ creds1.getUserPrincipal());
assertEquals("pwd", creds1.getPassword());
- assertEquals("[username: name][workstation: LOCALHOST]" +
- "[domain: DOMAIN]", creds1.toString());
+ assertEquals("[principal: DOMAIN/name][workstation: LOCALHOST]",
+ creds1.toString());
NTCredentials creds2 = new NTCredentials(
"name", null, null, null);
assertEquals("name", creds2.getUserName());
- assertEquals("name", creds2.getPrincipalName());
+ assertEquals(new NTUserPrincipal(null, "name"),
+ creds2.getUserPrincipal());
assertEquals(null, creds2.getPassword());
- assertEquals("[username: name][workstation: null]" +
- "[domain: null]", creds2.toString());
+ assertEquals("[principal: name][workstation: null]",
+ creds2.toString());
NTCredentials creds3 = new NTCredentials(
"domain/name:pwd");
assertEquals("name", creds3.getUserName());
- assertEquals("DOMAIN/name", creds3.getPrincipalName());
+ assertEquals(new NTUserPrincipal("DOMAIN", "name"),
+ creds3.getUserPrincipal());
assertEquals("pwd", creds3.getPassword());
- assertEquals("[username: name][workstation: null]" +
- "[domain: DOMAIN]", creds3.toString());
+ assertEquals("[principal: DOMAIN/name][workstation: null]",
+ creds3.toString());
NTCredentials creds4 = new NTCredentials(
"domain/name");
assertEquals("name", creds4.getUserName());
- assertEquals("DOMAIN/name", creds4.getPrincipalName());
+ assertEquals(new NTUserPrincipal("DOMAIN", "name"),
+ creds4.getUserPrincipal());
assertEquals(null, creds4.getPassword());
- assertEquals("[username: name][workstation: null]" +
- "[domain: DOMAIN]", creds4.toString());
+ assertEquals("[principal: DOMAIN/name][workstation: null]",
+ creds4.toString());
NTCredentials creds5 = new NTCredentials(
"name");
assertEquals("name", creds5.getUserName());
- assertEquals("name", creds5.getPrincipalName());
+ assertEquals(new NTUserPrincipal(null, "name"),
+ creds5.getUserPrincipal());
assertEquals(null, creds5.getPassword());
- assertEquals("[username: name][workstation: null]" +
- "[domain: null]", creds5.toString());
+ assertEquals("[principal: name][workstation: null]",
+ creds5.toString());
}
public void testUsernamePasswordCredentialsHashCode() {