version 1.5, 2005/02/18 17:35:13
|
version 1.6, 2013/02/01 13:02:03
|
Line 11 import org.w3c.tools.codec.Base64FormatE
|
Line 11 import org.w3c.tools.codec.Base64FormatE
|
import org.w3c.www.http.HttpCredential; |
import org.w3c.www.http.HttpCredential; |
|
|
/** |
/** |
|
* @author Benoît Mahé (bmahe@w3.org) |
* @version $Revision$ |
* @version $Revision$ |
* @author Benoît Mahé (bmahe@w3.org) |
|
*/ |
*/ |
public class BasicAuthPrincipal extends HTTPPrincipal { |
public class BasicAuthPrincipal extends HTTPPrincipal { |
|
|
// original name is there to cope with a MS weirdness on MiniRedir |
// original name is there to cope with a MS weirdness on MiniRedir |
protected String origname = null; |
protected String origname = null; |
protected String name = null; |
protected String name = null; |
protected String password = null; |
protected String password = null; |
protected String cookie = null; |
protected String cookie = null; |
|
|
protected String getCookie() { |
protected String getCookie() { |
return cookie; |
return cookie; |
} |
} |
|
|
public boolean equals(Object another) { |
public boolean equals(Object another) { |
if (another instanceof AclPrincipal) { |
if (another instanceof AclPrincipal) { |
AclPrincipal aclp = (AclPrincipal) another; |
AclPrincipal aclp = (AclPrincipal) another; |
if (aclp.matchIP(getInetAddress())) { |
if (aclp.matchIP(getInetAddress())) { |
if (aclp.getPassword() != null) { |
if (aclp.getPassword() != null) { |
return ((name != null) && |
return ((name != null) && |
(password != null) && |
(password != null) && |
name.equals(aclp.getName()) && |
name.equals(aclp.getName()) && |
password.equals(aclp.getPassword())); |
password.equals(aclp.getPassword())); |
} else { |
} else { |
return true; |
return true; |
} |
} |
} else { |
} else { |
return ((name != null) && |
return ((name != null) && |
(password != null) && |
(password != null) && |
name.equals(aclp.getName()) && |
name.equals(aclp.getName()) && |
password.equals(aclp.getPassword())); |
password.equals(aclp.getPassword())); |
} |
} |
} else { |
} else { |
return toString().equals(another.toString()); |
return toString().equals(another.toString()); |
} |
} |
} |
} |
|
|
public String toString() { |
public String toString() { |
if (name == null) |
if (name == null) |
return super.toString(); |
return super.toString(); |
return name+":"+password; |
return name + ":" + password; |
} |
} |
|
|
public int hashCode() { |
public int hashCode() { |
return toString().hashCode(); |
return toString().hashCode(); |
} |
} |
|
|
public String getName() { |
public String getName() { |
return name; |
return name; |
} |
} |
|
|
public String getOriginalName() { |
public String getOriginalName() { |
return (origname == null) ? name : origname; |
return (origname == null) ? name : origname; |
} |
} |
|
|
public BasicAuthPrincipal(Request request) |
public BasicAuthPrincipal(Request request) |
throws InvalidAuthException |
throws InvalidAuthException { |
{ |
this(request, false); |
this(request, false); |
} |
} |
|
|
public BasicAuthPrincipal(Request request, boolean lenient) |
public BasicAuthPrincipal(Request request, boolean lenient) |
throws InvalidAuthException { |
throws InvalidAuthException |
super(request, lenient); |
{ |
HttpCredential credential = null; |
super(request, lenient); |
credential = (request.isProxy() |
HttpCredential credential = null; |
? request.getProxyAuthorization() |
credential = (request.isProxy() |
: request.getAuthorization()); |
? request.getProxyAuthorization() |
if (credential == null) { |
: request.getAuthorization()); |
this.name = null; |
if (credential == null) { |
this.password = null; |
this.name = null; |
} else if (!credential.getScheme().equalsIgnoreCase("Basic")) { |
this.password = null; |
String msg = ("Invalid authentication scheme \"" |
} else if ( ! credential.getScheme().equalsIgnoreCase("Basic") ) { |
+ credential.getScheme() |
String msg = ("Invalid authentication scheme \"" |
+ " expecting \"Basic\""); |
+ credential.getScheme() |
throw new InvalidAuthException(msg); |
+ " expecting \"Basic\""); |
} else { |
throw new InvalidAuthException (msg) ; |
// Decode the credentials: |
} else { |
String decoded = null; |
// Decode the credentials: |
this.cookie = credential.getAuthParameter("cookie"); |
String decoded = null ; |
try { |
this.cookie = credential.getAuthParameter("cookie"); |
Base64Decoder b = new Base64Decoder(cookie); |
try { |
decoded = b.processString(); |
Base64Decoder b = new Base64Decoder (cookie) ; |
} catch (Base64FormatException e) { |
decoded = b.processString() ; |
String msg = "Invalid BASE64 encoding of credentials."; |
} catch (Base64FormatException e) { |
throw new InvalidAuthException(msg); |
String msg = "Invalid BASE64 encoding of credentials." ; |
} |
throw new InvalidAuthException (msg) ; |
// Get user and password: |
} |
origname = null; |
// Get user and password: |
int icolon = decoded.indexOf(':'); |
origname = null; |
if ((icolon > 0) && (icolon + 1 < decoded.length())) { |
int icolon = decoded.indexOf (':') ; |
// ok, parse was find, check user: |
if ( (icolon > 0) && (icolon+1 < decoded.length()) ) { |
if (lenient) { |
// ok, parse was find, check user: |
String _name = decoded.substring(0, icolon); |
if (lenient) { |
int _slashIdx = _name.lastIndexOf('\\'); |
String _name = decoded.substring (0, icolon) ; |
if (_slashIdx != -1) { |
int _slashIdx = _name.lastIndexOf('\\'); |
this.origname = _name; |
if ( _slashIdx != -1) { |
this.name = _name.substring(_slashIdx + 1); |
this.origname = _name; |
} else { |
this.name = _name.substring(_slashIdx+1); |
this.name = _name; |
} else { |
} |
this.name = _name; |
} else { |
} |
this.name = decoded.substring(0, icolon); |
} else { |
} |
this.name = decoded.substring (0, icolon) ; |
this.password = decoded.substring(icolon + 1); |
} |
} else { |
this.password = decoded.substring (icolon+1) ; |
String msg = "Invalid credentials syntax in " + decoded; |
} else { |
throw new InvalidAuthException(msg); |
String msg = "Invalid credentials syntax in " + decoded ; |
} |
throw new InvalidAuthException (msg) ; |
} |
} |
|
} |
|
} |
} |
} |
} |