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

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

Webmaster