Credentials interface changed to make use of standard java.security.Princinal to identify a user
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@658430 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
c9a4970c10
commit
db8947ac50
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -30,6 +30,8 @@
|
|||
|
||||
package org.apache.http.auth;
|
||||
|
||||
import java.security.Principal;
|
||||
|
||||
/**
|
||||
* User name and password based authentication credentials.
|
||||
*
|
||||
|
@ -40,7 +42,7 @@ package org.apache.http.auth;
|
|||
*/
|
||||
public interface Credentials {
|
||||
|
||||
String getPrincipalName();
|
||||
Principal getUserPrincipal();
|
||||
|
||||
String getPassword();
|
||||
|
||||
|
|
|
@ -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 @@ import org.apache.http.util.LangUtils;
|
|||
*/
|
||||
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 @@ public class NTCredentials implements Credentials {
|
|||
}
|
||||
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 @@ public class NTCredentials implements Credentials {
|
|||
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 Principal getUserPrincipal() {
|
||||
return this.principal;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
return this.principal.getUsername();
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
|
@ -145,7 +132,7 @@ public class NTCredentials implements Credentials {
|
|||
* @return String the domain these credentials are intended to authenticate with.
|
||||
*/
|
||||
public String getDomain() {
|
||||
return domain;
|
||||
return this.principal.getDomain();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -160,9 +147,8 @@ public class NTCredentials implements Credentials {
|
|||
@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 @@ public class NTCredentials implements Credentials {
|
|||
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 @@ public class NTCredentials implements Credentials {
|
|||
@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();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -30,6 +30,8 @@
|
|||
|
||||
package org.apache.http.auth;
|
||||
|
||||
import java.security.Principal;
|
||||
|
||||
import org.apache.http.util.LangUtils;
|
||||
|
||||
/**
|
||||
|
@ -45,7 +47,7 @@ import org.apache.http.util.LangUtils;
|
|||
*/
|
||||
public class UsernamePasswordCredentials implements Credentials {
|
||||
|
||||
private final String userName;
|
||||
private final BasicUserPrincipal principal;
|
||||
private final String password;
|
||||
|
||||
/**
|
||||
|
@ -61,10 +63,10 @@ public class UsernamePasswordCredentials implements Credentials {
|
|||
}
|
||||
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 @@ public class UsernamePasswordCredentials implements Credentials {
|
|||
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 @@ public class UsernamePasswordCredentials implements Credentials {
|
|||
|
||||
@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 @@ public class UsernamePasswordCredentials implements Credentials {
|
|||
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 @@ public class UsernamePasswordCredentials implements Credentials {
|
|||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
buffer.append("[username: ");
|
||||
buffer.append(this.userName);
|
||||
buffer.append("]");
|
||||
return buffer.toString();
|
||||
return this.principal.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -163,7 +163,7 @@ public class BasicScheme extends RFC2617Scheme {
|
|||
}
|
||||
|
||||
StringBuilder tmp = new StringBuilder();
|
||||
tmp.append(credentials.getPrincipalName());
|
||||
tmp.append(credentials.getUserPrincipal().getName());
|
||||
tmp.append(":");
|
||||
tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword());
|
||||
|
||||
|
|
|
@ -270,7 +270,7 @@ public class DigestScheme extends RFC2617Scheme {
|
|||
|
||||
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 @@ public class DigestScheme extends RFC2617Scheme {
|
|||
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));
|
||||
|
|
|
@ -57,59 +57,67 @@ public class TestCredentials extends TestCase {
|
|||
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() {
|
||||
|
|
Loading…
Reference in New Issue