1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45:
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51:
52:
55: public class PlainClient
56: extends ClientMechanism
57: implements SaslClient
58: {
59: public PlainClient()
60: {
61: super(Registry.SASL_PLAIN_MECHANISM);
62: }
63:
64: protected void initMechanism() throws SaslException
65: {
66: }
67:
68: protected void resetMechanism() throws SaslException
69: {
70: }
71:
72: public boolean hasInitialResponse()
73: {
74: return true;
75: }
76:
77: public byte[] evaluateChallenge(final byte[] challenge) throws SaslException
78: {
79: try
80: {
81: final String username;
82: final char[] password;
83: Callback[] callbacks;
84: if ((! properties.containsKey(Registry.SASL_USERNAME))
85: && (! properties.containsKey(Registry.SASL_PASSWORD)))
86: {
87: callbacks = new Callback[2];
88: final NameCallback nameCB;
89: final String defaultName = System.getProperty("user.name");
90: if (defaultName == null)
91: nameCB = new NameCallback("username: ");
92: else
93: nameCB = new NameCallback("username: ", defaultName);
94: final PasswordCallback pwdCB = new PasswordCallback("password: ",
95: false);
96: callbacks[0] = nameCB;
97: callbacks[1] = pwdCB;
98: this.handler.handle(callbacks);
99: username = nameCB.getName();
100: password = pwdCB.getPassword();
101: }
102: else
103: {
104: if (properties.containsKey(Registry.SASL_USERNAME))
105: username = (String) properties.get(Registry.SASL_USERNAME);
106: else
107: {
108: callbacks = new Callback[1];
109: final NameCallback nameCB;
110: final String defaultName = System.getProperty("user.name");
111: if (defaultName == null)
112: nameCB = new NameCallback("username: ");
113: else
114: nameCB = new NameCallback("username: ", defaultName);
115: callbacks[0] = nameCB;
116: this.handler.handle(callbacks);
117: username = nameCB.getName();
118: }
119: if (properties.containsKey(Registry.SASL_PASSWORD))
120: password = ((String) properties.get(Registry.SASL_PASSWORD)).toCharArray();
121: else
122: {
123: callbacks = new Callback[1];
124: final PasswordCallback pwdCB = new PasswordCallback("password: ",
125: false);
126: callbacks[0] = pwdCB;
127: this.handler.handle(callbacks);
128: password = pwdCB.getPassword();
129: }
130: }
131: if (password == null)
132: throw new SaslException("null password supplied");
133: final CPStringBuilder sb = new CPStringBuilder();
134: if (authorizationID != null)
135: sb.append(authorizationID);
136: sb.append('\0');
137: sb.append(username);
138: sb.append('\0');
139: sb.append(password);
140: this.complete = true;
141: final byte[] response = sb.toString().getBytes("UTF-8");
142: return response;
143: }
144: catch (Exception x)
145: {
146: if (x instanceof SaslException)
147: throw (SaslException) x;
148: throw new SaslException("evaluateChallenge()", x);
149: }
150: }
151:
152: protected String getNegotiatedQOP()
153: {
154: return Registry.QOP_AUTH;
155: }
156: }