001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jgroups.auth;
023:
024: import org.jgroups.Message;
025: import org.jgroups.util.Util;
026:
027: import java.util.Properties;
028: import java.util.ArrayList;
029: import java.util.List;
030: import java.util.StringTokenizer;
031: import java.io.DataOutputStream;
032: import java.io.IOException;
033: import java.io.DataInputStream;
034:
035: /**
036: * <p>
037: * The FixedMemberShipToken object predefines a list of IP addresses and Ports that can join the group.
038: *</p>
039: * <p>
040: * Configuration parameters for this example are shown below:
041: * </p>
042: * <ul>
043: * <li>fixed_members_value (required) = List of IP addresses & ports (optionally) - ports must be seperated by a '/' e.g. 127.0.0.1/1010*127.0.0.1/4567</li>
044: * <li>fixed_members_seperator (required) = The seperator used between IP addresses - e.g. *</li>
045: * </ul>
046: * @author Chris Mills (millsy@jboss.com)
047: */
048: public class FixedMembershipToken extends AuthToken {
049: private static final String FIXED_MEMBERS_ATTR = "fixed_members_value";
050: private static final String FIXED_MEMBERS_SEPERATOR_ATTR = "fixed_members_seperator";
051:
052: private List memberList = null;
053: private String token = "emptyToken";
054:
055: public FixedMembershipToken() {
056: }
057:
058: public String getName() {
059: return "org.jgroups.auth.FixedMembershipToken";
060: }
061:
062: public boolean authenticate(AuthToken token, Message msg) {
063: if ((token != null) && (token instanceof FixedMembershipToken)
064: && (this .memberList != null)) {
065: //Found a valid Token to authenticate against
066: FixedMembershipToken serverToken = (FixedMembershipToken) token;
067:
068: String sourceAddressWithPort = msg.getSrc().toString();
069: String sourceAddressWithoutPort = sourceAddressWithPort
070: .substring(0, sourceAddressWithPort.indexOf(":"));
071:
072: if (log.isDebugEnabled()) {
073: log.debug("AUTHToken received from "
074: + sourceAddressWithPort);
075: }
076:
077: if ((this .memberList.contains(sourceAddressWithPort))
078: || (this .memberList
079: .contains(sourceAddressWithoutPort))) {
080: //validated
081: if (log.isDebugEnabled()) {
082: log.debug("FixedMembershipToken match");
083: }
084: return true;
085: } else {
086: if (log.isWarnEnabled()) {
087: log
088: .warn("Authentication failed on FixedMembershipToken");
089: }
090: return false;
091: }
092: }
093:
094: if (log.isWarnEnabled()) {
095: log.warn("Invalid AuthToken instance - wrong type or null");
096: }
097: return false;
098: }
099:
100: public void setValue(Properties properties) {
101: memberList = new ArrayList();
102: StringTokenizer memberListTokenizer = new StringTokenizer(
103: (String) properties
104: .get(FixedMembershipToken.FIXED_MEMBERS_ATTR),
105: (String) properties
106: .get(FixedMembershipToken.FIXED_MEMBERS_SEPERATOR_ATTR));
107: while (memberListTokenizer.hasMoreTokens()) {
108: memberList.add(memberListTokenizer.nextToken().replace('/',
109: ':'));
110: }
111: properties.remove(FixedMembershipToken.FIXED_MEMBERS_ATTR);
112: properties
113: .remove(FixedMembershipToken.FIXED_MEMBERS_SEPERATOR_ATTR);
114: }
115:
116: /**
117: * Required to serialize the object to pass across the wire
118: * @param out
119: * @throws java.io.IOException
120: */
121: public void writeTo(DataOutputStream out) throws IOException {
122: if (log.isDebugEnabled()) {
123: log.debug("SimpleToken writeTo()");
124: }
125: Util.writeString(this .token, out);
126: }
127:
128: /**
129: * Required to deserialize the object when read in from the wire
130: * @param in
131: * @throws IOException
132: * @throws IllegalAccessException
133: * @throws InstantiationException
134: */
135: public void readFrom(DataInputStream in) throws IOException,
136: IllegalAccessException, InstantiationException {
137: if (log.isDebugEnabled()) {
138: log.debug("SimpleToken readFrom()");
139: }
140: this.token = Util.readString(in);
141: }
142: }
|