Annotation of java/classes/org/w3c/jigsaw/acl/BasicAuthPrincipal.java, revision 1.6

1.1       bmahe       1: // BasicAuthprincipal.java
1.6     ! ylafon      2: // $Id: BasicAuthPrincipal.java,v 1.5 2005/02/18 17:35:13 ylafon Exp $
1.1       bmahe       3: // (c) COPYRIGHT MIT, INRIA and Keio, 1999.
                      4: // Please first read the full copyright statement in file COPYRIGHT.html
1.4       ylafon      5: 
1.1       bmahe       6: package org.w3c.jigsaw.acl;
                      7: 
                      8: import org.w3c.jigsaw.http.Request;
                      9: import org.w3c.tools.codec.Base64Decoder;
                     10: import org.w3c.tools.codec.Base64FormatException;
                     11: import org.w3c.www.http.HttpCredential;
                     12: 
                     13: /**
1.6     ! ylafon     14:  * @author Benoît Mahé (bmahe@w3.org)
        !            15:  * @version $Revision: 1.5 $
1.1       bmahe      16:  */
1.2       bmahe      17: public class BasicAuthPrincipal extends HTTPPrincipal {
1.4       ylafon     18: 
1.5       ylafon     19:     // original name is there to cope with a MS weirdness on MiniRedir
1.6     ! ylafon     20:     protected String origname = null;
        !            21:     protected String name = null;
        !            22:     protected String password = null;
        !            23:     protected String cookie = null;
1.1       bmahe      24: 
                     25:     protected String getCookie() {
1.6     ! ylafon     26:         return cookie;
1.1       bmahe      27:     }
                     28: 
                     29:     public boolean equals(Object another) {
1.6     ! ylafon     30:         if (another instanceof AclPrincipal) {
        !            31:             AclPrincipal aclp = (AclPrincipal) another;
        !            32:             if (aclp.matchIP(getInetAddress())) {
        !            33:                 if (aclp.getPassword() != null) {
        !            34:                     return ((name != null) &&
        !            35:                             (password != null) &&
        !            36:                             name.equals(aclp.getName()) &&
        !            37:                             password.equals(aclp.getPassword()));
        !            38:                 } else {
        !            39:                     return true;
        !            40:                 }
        !            41:             } else {
        !            42:                 return ((name != null) &&
        !            43:                         (password != null) &&
        !            44:                         name.equals(aclp.getName()) &&
        !            45:                         password.equals(aclp.getPassword()));
        !            46:             }
        !            47:         } else {
        !            48:             return toString().equals(another.toString());
        !            49:         }
1.1       bmahe      50:     }
                     51: 
                     52:     public String toString() {
1.6     ! ylafon     53:         if (name == null)
        !            54:             return super.toString();
        !            55:         return name + ":" + password;
1.1       bmahe      56:     }
                     57: 
                     58:     public int hashCode() {
1.6     ! ylafon     59:         return toString().hashCode();
1.1       bmahe      60:     }
                     61: 
                     62:     public String getName() {
1.6     ! ylafon     63:         return name;
1.1       bmahe      64:     }
                     65: 
1.5       ylafon     66:     public String getOriginalName() {
1.6     ! ylafon     67:         return (origname == null) ? name : origname;
1.5       ylafon     68:     }
                     69: 
1.6     ! ylafon     70:     public BasicAuthPrincipal(Request request)
        !            71:             throws InvalidAuthException {
        !            72:         this(request, false);
        !            73:     }
        !            74: 
        !            75:     public BasicAuthPrincipal(Request request, boolean lenient)
        !            76:             throws InvalidAuthException {
        !            77:         super(request, lenient);
        !            78:         HttpCredential credential = null;
        !            79:         credential = (request.isProxy()
        !            80:                 ? request.getProxyAuthorization()
        !            81:                 : request.getAuthorization());
        !            82:         if (credential == null) {
        !            83:             this.name = null;
        !            84:             this.password = null;
        !            85:         } else if (!credential.getScheme().equalsIgnoreCase("Basic")) {
        !            86:             String msg = ("Invalid authentication scheme \""
        !            87:                     + credential.getScheme()
        !            88:                     + " expecting \"Basic\"");
        !            89:             throw new InvalidAuthException(msg);
        !            90:         } else {
        !            91:             // Decode the credentials:
        !            92:             String decoded = null;
        !            93:             this.cookie = credential.getAuthParameter("cookie");
        !            94:             try {
        !            95:                 Base64Decoder b = new Base64Decoder(cookie);
        !            96:                 decoded = b.processString();
        !            97:             } catch (Base64FormatException e) {
        !            98:                 String msg = "Invalid BASE64 encoding of credentials.";
        !            99:                 throw new InvalidAuthException(msg);
        !           100:             }
        !           101:             // Get user and password:
        !           102:             origname = null;
        !           103:             int icolon = decoded.indexOf(':');
        !           104:             if ((icolon > 0) && (icolon + 1 < decoded.length())) {
        !           105:                 // ok, parse was find, check user:
        !           106:                 if (lenient) {
        !           107:                     String _name = decoded.substring(0, icolon);
        !           108:                     int _slashIdx = _name.lastIndexOf('\\');
        !           109:                     if (_slashIdx != -1) {
        !           110:                         this.origname = _name;
        !           111:                         this.name = _name.substring(_slashIdx + 1);
        !           112:                     } else {
        !           113:                         this.name = _name;
        !           114:                     }
        !           115:                 } else {
        !           116:                     this.name = decoded.substring(0, icolon);
        !           117:                 }
        !           118:                 this.password = decoded.substring(icolon + 1);
        !           119:             } else {
        !           120:                 String msg = "Invalid credentials syntax in " + decoded;
        !           121:                 throw new InvalidAuthException(msg);
        !           122:             }
        !           123:         }
1.1       bmahe     124:     }
                    125: }

Webmaster