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

1.1     ! bmahe       1: // BasicAuthprincipal.java
        !             2: // $Id$
        !             3: // (c) COPYRIGHT MIT, INRIA and Keio, 1999.
        !             4: // Please first read the full copyright statement in file COPYRIGHT.html
        !             5:  
        !             6: package org.w3c.jigsaw.acl;
        !             7: 
        !             8: import java.security.Principal;
        !             9: 
        !            10: import org.w3c.jigsaw.http.Request;
        !            11: import org.w3c.tools.codec.Base64Decoder;
        !            12: import org.w3c.tools.codec.Base64FormatException;
        !            13: import org.w3c.www.http.HttpCredential;
        !            14: 
        !            15: /**
        !            16:  * @version $Revision$
        !            17:  * @author  Benoît Mahé (bmahe@w3.org)
        !            18:  */
        !            19: public class BasicAuthPrincipal implements Principal {
        !            20:     
        !            21:     protected String name     = null;
        !            22:     protected String password = null;
        !            23:     protected String cookie   = null;
        !            24: 
        !            25:     protected String getCookie() {
        !            26:        return cookie;
        !            27:     }
        !            28: 
        !            29:     public boolean equals(Object another) {
        !            30:        if (another instanceof BasicAuthPrincipal) {
        !            31:            return (cookie.equals(((BasicAuthPrincipal)another).getCookie()));
        !            32:        }
        !            33:        return false;
        !            34:     }
        !            35: 
        !            36:     public String toString() {
        !            37:        return cookie;
        !            38:     }
        !            39: 
        !            40:     public int hashCode() {
        !            41:        return cookie.hashCode();
        !            42:     }
        !            43: 
        !            44:     public String getName() {
        !            45:        return name;
        !            46:     }
        !            47: 
        !            48:     public BasicAuthPrincipal(Request request) 
        !            49:        throws InvalidAuthException
        !            50:     {
        !            51:        HttpCredential credential = null;
        !            52:        credential = (request.isProxy()
        !            53:                      ? request.getProxyAuthorization()
        !            54:                      : request.getAuthorization());
        !            55:        if ( ! credential.getScheme().equalsIgnoreCase("Basic") ) {
        !            56:            String msg = ("Invalid authentication scheme \""
        !            57:                          + credential.getScheme()
        !            58:                          + " expecting \"Basic\"");
        !            59:            throw new InvalidAuthException (msg) ;
        !            60:        }
        !            61:        // Decode the credentials:
        !            62:        String decoded = null ;
        !            63:        this.cookie    = credential.getAuthParameter("cookie");
        !            64:        try {
        !            65:            Base64Decoder b  = new Base64Decoder (cookie) ;
        !            66:            decoded          = b.processString() ;
        !            67:        } catch (Base64FormatException e) {
        !            68:            String msg = "Invalid BASE64 encoding of credentials." ;
        !            69:            throw new InvalidAuthException (msg) ;
        !            70:        }
        !            71:        // Get user and password:
        !            72:        int icolon = decoded.indexOf (':') ;
        !            73:        if ( (icolon > 0) && (icolon+1 < decoded.length()) ) {
        !            74:            // ok, parse was find, check user:
        !            75:            this.name     = decoded.substring (0, icolon) ;
        !            76:            this.password = decoded.substring (icolon+1) ;
        !            77:        } else {
        !            78:            String msg = "Invalid credentials syntax in " + decoded ;
        !            79:            throw new InvalidAuthException (msg) ;
        !            80:        }
        !            81:     }
        !            82: 
        !            83: }

Webmaster