001: // BasicAuthprincipal.java
002: // $Id: BasicAuthPrincipal.java,v 1.5 2005/02/18 17:35:13 ylafon Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 1999.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.acl;
007:
008: import org.w3c.jigsaw.http.Request;
009: import org.w3c.tools.codec.Base64Decoder;
010: import org.w3c.tools.codec.Base64FormatException;
011: import org.w3c.www.http.HttpCredential;
012:
013: /**
014: * @version $Revision: 1.5 $
015: * @author Benoît Mahé (bmahe@w3.org)
016: */
017: public class BasicAuthPrincipal extends HTTPPrincipal {
018:
019: // original name is there to cope with a MS weirdness on MiniRedir
020: protected String origname = null;
021: protected String name = null;
022: protected String password = null;
023: protected String cookie = null;
024:
025: protected String getCookie() {
026: return cookie;
027: }
028:
029: public boolean equals(Object another) {
030: if (another instanceof AclPrincipal) {
031: AclPrincipal aclp = (AclPrincipal) another;
032: if (aclp.matchIP(getInetAddress())) {
033: if (aclp.getPassword() != null) {
034: return ((name != null) && (password != null)
035: && name.equals(aclp.getName()) && password
036: .equals(aclp.getPassword()));
037: } else {
038: return true;
039: }
040: } else {
041: return ((name != null) && (password != null)
042: && name.equals(aclp.getName()) && password
043: .equals(aclp.getPassword()));
044: }
045: } else {
046: return toString().equals(another.toString());
047: }
048: }
049:
050: public String toString() {
051: if (name == null)
052: return super .toString();
053: return name + ":" + password;
054: }
055:
056: public int hashCode() {
057: return toString().hashCode();
058: }
059:
060: public String getName() {
061: return name;
062: }
063:
064: public String getOriginalName() {
065: return (origname == null) ? name : origname;
066: }
067:
068: public BasicAuthPrincipal(Request request)
069: throws InvalidAuthException {
070: this (request, false);
071: }
072:
073: public BasicAuthPrincipal(Request request, boolean lenient)
074: throws InvalidAuthException {
075: super (request, lenient);
076: HttpCredential credential = null;
077: credential = (request.isProxy() ? request
078: .getProxyAuthorization() : request.getAuthorization());
079: if (credential == null) {
080: this .name = null;
081: this .password = null;
082: } else if (!credential.getScheme().equalsIgnoreCase("Basic")) {
083: String msg = ("Invalid authentication scheme \""
084: + credential.getScheme() + " expecting \"Basic\"");
085: throw new InvalidAuthException(msg);
086: } else {
087: // Decode the credentials:
088: String decoded = null;
089: this .cookie = credential.getAuthParameter("cookie");
090: try {
091: Base64Decoder b = new Base64Decoder(cookie);
092: decoded = b.processString();
093: } catch (Base64FormatException e) {
094: String msg = "Invalid BASE64 encoding of credentials.";
095: throw new InvalidAuthException(msg);
096: }
097: // Get user and password:
098: origname = null;
099: int icolon = decoded.indexOf(':');
100: if ((icolon > 0) && (icolon + 1 < decoded.length())) {
101: // ok, parse was find, check user:
102: if (lenient) {
103: String _name = decoded.substring(0, icolon);
104: int _slashIdx = _name.lastIndexOf('\\');
105: if (_slashIdx != -1) {
106: this .origname = _name;
107: this .name = _name.substring(_slashIdx + 1);
108: } else {
109: this .name = _name;
110: }
111: } else {
112: this .name = decoded.substring(0, icolon);
113: }
114: this .password = decoded.substring(icolon + 1);
115: } else {
116: String msg = "Invalid credentials syntax in " + decoded;
117: throw new InvalidAuthException(msg);
118: }
119: }
120: }
121: }
|