01: package org.jgroups.auth;
02:
03: import org.jgroups.util.Util;
04: import org.jgroups.Message;
05:
06: import java.io.DataOutputStream;
07: import java.io.IOException;
08: import java.io.DataInputStream;
09: import java.util.Properties;
10:
11: /**
12: * <p>
13: * This is an example of using a preshared token for authentication purposes. All members of the group have to have the same string value in the JGroups config.
14: * </p>
15: * <p>JGroups config parameters:</p>
16: *<ul>
17: * <li>auth_value (required) = the string to encrypt</li>
18: * </ul>
19: * @see org.jgroups.auth.AuthToken
20: * @author Chris Mills
21: */
22: public class SimpleToken extends AuthToken {
23:
24: public static final String TOKEN_ATTR = "auth_value";
25: private String token = null;
26:
27: public SimpleToken() {
28: //need an empty constructor
29: }
30:
31: public SimpleToken(String token) {
32: this .token = token;
33: }
34:
35: public void setValue(Properties properties) {
36: this .token = (String) properties.get(SimpleToken.TOKEN_ATTR);
37: properties.remove(SimpleToken.TOKEN_ATTR);
38: }
39:
40: public String getName() {
41: return "org.jgroups.auth.SimpleToken";
42: }
43:
44: public boolean authenticate(AuthToken token, Message msg) {
45: if ((token != null) && (token instanceof SimpleToken)) {
46: //Found a valid Token to authenticate against
47: SimpleToken serverToken = (SimpleToken) token;
48:
49: if ((this .token != null) && (serverToken.token != null)
50: && (this .token.equalsIgnoreCase(serverToken.token))) {
51: //validated
52: if (log.isDebugEnabled()) {
53: log.debug("SimpleToken match");
54: }
55: return true;
56: } else {
57: if (log.isWarnEnabled()) {
58: log.warn("Authentication failed on SimpleToken");
59: }
60: return false;
61: }
62: }
63:
64: if (log.isWarnEnabled()) {
65: log.warn("Invalid AuthToken instance - wrong type or null");
66: }
67: return false;
68: }
69:
70: /**
71: * Required to serialize the object to pass across the wire
72: * @param out
73: * @throws IOException
74: */
75: public void writeTo(DataOutputStream out) throws IOException {
76: if (log.isDebugEnabled()) {
77: log.debug("SimpleToken writeTo()");
78: }
79: Util.writeString(this .token, out);
80: }
81:
82: /**
83: * Required to deserialize the object when read in from the wire
84: * @param in
85: * @throws IOException
86: * @throws IllegalAccessException
87: * @throws InstantiationException
88: */
89: public void readFrom(DataInputStream in) throws IOException,
90: IllegalAccessException, InstantiationException {
91: if (log.isDebugEnabled()) {
92: log.debug("SimpleToken readFrom()");
93: }
94: this.token = Util.readString(in);
95: }
96: }
|