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

1.1       bmahe       1: // BasicAuthprincipal.java
1.5     ! ylafon      2: // $Id: BasicAuthPrincipal.java,v 1.4 2000/08/16 21:37:33 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.5     ! ylafon     14:  * @version $Revision: 1.4 $
1.1       bmahe      15:  * @author  Benoît Mahé (bmahe@w3.org)
                     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
        !            20:     protected String      origname = null;
1.2       bmahe      21:     protected String      name     = null;
                     22:     protected String      password = null;
                     23:     protected String      cookie   = null;
1.1       bmahe      24: 
                     25:     protected String getCookie() {
                     26:        return cookie;
                     27:     }
                     28: 
                     29:     public boolean equals(Object another) {
1.2       bmahe      30:        if (another instanceof AclPrincipal) {
                     31:            AclPrincipal aclp = (AclPrincipal) another;
                     32:            if (aclp.matchIP(getInetAddress())) {
                     33:                if (aclp.getPassword() != null) {
1.3       bmahe      34:                    return ((name != null) &&
                     35:                            (password != null) &&
                     36:                            name.equals(aclp.getName()) &&
1.2       bmahe      37:                            password.equals(aclp.getPassword()));
                     38:                } else {
                     39:                    return true;
                     40:                }
                     41:            } else {
1.3       bmahe      42:                return ((name != null) &&
                     43:                        (password != null) &&
                     44:                        name.equals(aclp.getName()) &&
1.2       bmahe      45:                        password.equals(aclp.getPassword()));
                     46:            }
                     47:        } else {
                     48:            return toString().equals(another.toString());
1.1       bmahe      49:        }
                     50:     }
                     51: 
                     52:     public String toString() {
1.3       bmahe      53:        if (name == null)
                     54:            return super.toString();
1.2       bmahe      55:        return name+":"+password;
1.1       bmahe      56:     }
                     57: 
                     58:     public int hashCode() {
1.2       bmahe      59:        return toString().hashCode();
1.1       bmahe      60:     }
                     61: 
                     62:     public String getName() {
                     63:        return name;
                     64:     }
                     65: 
1.5     ! ylafon     66:     public String getOriginalName() {
        !            67:        return (origname == null) ? name : origname;
        !            68:     }
        !            69: 
1.1       bmahe      70:     public BasicAuthPrincipal(Request request) 
                     71:        throws InvalidAuthException
                     72:     {
1.5     ! ylafon     73:        this(request, false);
        !            74:     }
        !            75: 
        !            76:     public BasicAuthPrincipal(Request request, boolean lenient) 
        !            77:        throws InvalidAuthException
        !            78:     {
        !            79:        super(request, lenient);
1.1       bmahe      80:        HttpCredential credential = null;
                     81:        credential = (request.isProxy()
                     82:                      ? request.getProxyAuthorization()
                     83:                      : request.getAuthorization());
1.3       bmahe      84:        if (credential == null) {
                     85:            this.name     = null;
                     86:            this.password = null;
                     87:        } else if ( ! credential.getScheme().equalsIgnoreCase("Basic") ) {
1.1       bmahe      88:            String msg = ("Invalid authentication scheme \""
                     89:                          + credential.getScheme()
                     90:                          + " expecting \"Basic\"");
                     91:            throw new InvalidAuthException (msg) ;
                     92:        } else {
1.3       bmahe      93:            // Decode the credentials:
                     94:            String decoded = null ;
                     95:            this.cookie    = credential.getAuthParameter("cookie");
                     96:            try {
                     97:                Base64Decoder b  = new Base64Decoder (cookie) ;
                     98:                decoded          = b.processString() ;
                     99:            } catch (Base64FormatException e) {
                    100:                String msg = "Invalid BASE64 encoding of credentials." ;
                    101:                throw new InvalidAuthException (msg) ;
                    102:            }
                    103:            // Get user and password:
1.5     ! ylafon    104:            origname = null;
1.3       bmahe     105:            int icolon = decoded.indexOf (':') ;
                    106:            if ( (icolon > 0) && (icolon+1 < decoded.length()) ) {
                    107:                // ok, parse was find, check user:
1.5     ! ylafon    108:                if (lenient) {
        !           109:                    String _name = decoded.substring (0, icolon) ;
        !           110:                    int _slashIdx = _name.lastIndexOf('\\');
        !           111:                    if ( _slashIdx != -1) {
        !           112:                        this.origname = _name;
        !           113:                        this.name = _name.substring(_slashIdx+1);
        !           114:                    } else {
        !           115:                        this.name = _name;
        !           116:                    }
        !           117:                } else {
        !           118:                    this.name     = decoded.substring (0, icolon) ;
        !           119:                }
1.3       bmahe     120:                this.password = decoded.substring (icolon+1) ;
                    121:            } else {
                    122:                String msg = "Invalid credentials syntax in " + decoded ;
                    123:                throw new InvalidAuthException (msg) ;
                    124:            }
1.1       bmahe     125:        }
                    126:     }
                    127: }

Webmaster