001: // JigEditAclFilter.java
002: // $Id: JigEditAclFilter.java,v 1.1 2001/01/16 15:05:05 bmahe Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 2000.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigedit.filters;
007:
008: import java.net.*;
009:
010: import org.w3c.tools.resources.ProtocolException;
011: import org.w3c.tools.resources.*;
012:
013: import org.w3c.jigsaw.acl.*;
014: import org.w3c.jigsaw.auth.IPMatcher;
015: import org.w3c.jigsaw.auth.IPTemplatesAttribute;
016:
017: import org.w3c.jigsaw.http.Request;
018: import org.w3c.jigsaw.http.Client;
019:
020: /**
021: * @version $Revision: 1.1 $
022: * @author Benoît Mahé (bmahe@w3.org)
023: */
024: public class JigEditAclFilter extends AclFilter {
025:
026: /**
027: * Attribute Index - Secure ip array
028: */
029: public static int ATTR_SECURE_IPS = -1;
030:
031: static {
032: Attribute a = null;
033: Class c = null;
034: try {
035: c = Class
036: .forName("org.w3c.jigedit.filters.JigEditAclFilter");
037: } catch (Exception ex) {
038: ex.printStackTrace();
039: System.exit(1);
040: }
041: a = new IPTemplatesAttribute("secure-ips", null,
042: Attribute.EDITABLE);
043: ATTR_SECURE_IPS = AttributeRegistry.registerAttribute(c, a);
044: }
045:
046: /**
047: * The IPMatcher to match secure IP
048: */
049: protected IPMatcher ipmatcher = null;
050:
051: protected IPMatcher getIPMatcher() {
052: if (ipmatcher == null) {
053: ipmatcher = new IPMatcher();
054: short[][] ips = getSecureIPs();
055: if (ips != null) {
056: for (int i = 0; i < ips.length; i++)
057: ipmatcher.add(ips[i], Boolean.TRUE);
058: }
059: }
060: return ipmatcher;
061: }
062:
063: /**
064: * Get the secure ip address
065: */
066: public short[][] getSecureIPs() {
067: return (short[][]) getValue(ATTR_SECURE_IPS, null);
068: }
069:
070: /**
071: * Catch set value on the filter, to maintain cached values.
072: */
073: public void setValue(int idx, Object value) {
074: super .setValue(idx, value);
075: if (idx == ATTR_SECURE_IPS) {
076: ipmatcher = null;
077: }
078: }
079:
080: /**
081: * Authenticate the given request.
082: * @param request The request to be authentified.
083: * @param acls The Access Control List array.
084: * @exception org.w3c.tools.resources.ProtocolException if authentication
085: * failed
086: */
087: protected void authenticate(Request request, JAcl acls[])
088: throws ProtocolException {
089: Client client = request.getClient();
090: InetAddress inetaddress = client.getInetAddress();
091: IPMatcher ipmatcher = getIPMatcher();
092: System.out.println(">>> " + inetaddress);
093: if (ipmatcher.lookup(inetaddress) != null) {
094: // secure ip, basic auth
095: setValue(ATTR_SECURITY_LEVEL, new Integer(0));
096: } else {
097: // unsecure ip, digest auth
098: setValue(ATTR_SECURITY_LEVEL, new Integer(1));
099: }
100: super.authenticate(request, acls);
101: }
102:
103: }
|