Skip to content

Commit 45855e6

Browse files
author
Simon MacMullen
committed
Remove round parameter, introduce factories.
1 parent 6e15917 commit 45855e6

File tree

8 files changed

+75
-43
lines changed

8 files changed

+75
-43
lines changed

src/com/rabbitmq/client/AuthMechanism.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,15 @@
55
import java.io.IOException;
66

77
/**
8-
* A pluggable authentication mechanism. Should be stateless.
8+
* A pluggable authentication mechanism.
99
*/
1010
public interface AuthMechanism {
1111
/**
1212
* Handle one round of challenge-response
13-
* @param round zero-based counter of the current round
14-
* @param challenge the challenge this round, or null on round 0.
13+
* @param challenge the challenge this round, or null on first round.
1514
* @param factory for reference to e.g. username and password.
1615
* @return response
1716
* @throws IOException
1817
*/
19-
LongString handleChallenge(int round, LongString challenge, ConnectionFactory factory);
20-
21-
/**
22-
* The name of the authentication mechanism, as negotiated on the wire
23-
*/
24-
String getName();
18+
LongString handleChallenge(LongString challenge, ConnectionFactory factory);
2519
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.rabbitmq.client;
2+
3+
/**
4+
*
5+
*/
6+
public interface AuthMechanismFactory {
7+
/**
8+
* @return a new authentication mechanism implementation
9+
*/
10+
public AuthMechanism getInstance();
11+
12+
/**
13+
* The name of the authentication mechanism, as negotiated on the wire
14+
*/
15+
public String getName();
16+
}

src/com/rabbitmq/client/ConnectionFactory.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
import com.rabbitmq.client.impl.AMQConnection;
4848
import com.rabbitmq.client.impl.FrameHandler;
49-
import com.rabbitmq.client.impl.PlainMechanism;
49+
import com.rabbitmq.client.impl.PlainMechanismFactory;
5050
import com.rabbitmq.client.impl.SocketFrameHandler;
5151

5252
/**
@@ -87,8 +87,8 @@ public class ConnectionFactory implements Cloneable {
8787
public static final int DEFAULT_AMQP_OVER_SSL_PORT = 5671;
8888

8989
/** The default list of authentication mechanisms to use */
90-
public static final AuthMechanism[] DEFAULT_AUTH_MECHANISMS =
91-
new AuthMechanism[] { new PlainMechanism() };
90+
public static final AuthMechanismFactory[] DEFAULT_AUTH_MECHANISMS =
91+
new AuthMechanismFactory[] { new PlainMechanismFactory() };
9292

9393
/**
9494
* The default SSL protocol (currently "SSLv3").
@@ -103,9 +103,9 @@ public class ConnectionFactory implements Cloneable {
103103
private int requestedChannelMax = DEFAULT_CHANNEL_MAX;
104104
private int requestedFrameMax = DEFAULT_FRAME_MAX;
105105
private int requestedHeartbeat = DEFAULT_HEARTBEAT;
106-
private AuthMechanism[] authMechanisms = DEFAULT_AUTH_MECHANISMS;
107106
private Map<String, Object> _clientProperties = AMQConnection.defaultClientProperties();
108107
private SocketFactory factory = SocketFactory.getDefault();
108+
private AuthMechanismFactory[] authMechanismFactories = DEFAULT_AUTH_MECHANISMS;
109109

110110
/**
111111
* Instantiate a ConnectionFactory with a default set of parameters.
@@ -273,11 +273,11 @@ public void setClientProperties(Map<String, Object> clientProperties) {
273273
* @param serverMechanisms
274274
* @return
275275
*/
276-
public AuthMechanism getAuthMechanism(List<String> serverMechanisms) {
276+
public AuthMechanismFactory getAuthMechanismFactory(List<String> serverMechanisms) {
277277
// Our list is in order of preference, the server one is not.
278-
for (AuthMechanism mechanism : authMechanisms) {
279-
if (serverMechanisms.contains(mechanism.getName())) {
280-
return mechanism;
278+
for (AuthMechanismFactory f : authMechanismFactories) {
279+
if (serverMechanisms.contains(f.getName())) {
280+
return f;
281281
}
282282
}
283283

@@ -286,11 +286,11 @@ public AuthMechanism getAuthMechanism(List<String> serverMechanisms) {
286286

287287
/**
288288
* Set authentication mechanisms to use (in descending preference order)
289-
* @param mechanisms
289+
* @param factories
290290
* @see #DEFAULT_AUTH_MECHANISMS
291291
*/
292-
public void setAuthMechanisms(AuthMechanism[] mechanisms) {
293-
this.authMechanisms = mechanisms;
292+
public void setAuthMechanismFactories(AuthMechanismFactory[] factories) {
293+
this.authMechanismFactories = factories;
294294
}
295295

296296
/**

src/com/rabbitmq/client/impl/AMQConnection.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,11 @@
3939
import java.util.Map;
4040
import java.util.HashMap;
4141
import java.util.concurrent.TimeoutException;
42-
import java.util.concurrent.ScheduledExecutorService;
43-
import java.util.concurrent.Executors;
44-
import java.util.concurrent.TimeUnit;
4542

4643
import com.rabbitmq.client.AMQP;
47-
import com.rabbitmq.client.Address;
4844
import com.rabbitmq.client.AlreadyClosedException;
4945
import com.rabbitmq.client.AuthMechanism;
46+
import com.rabbitmq.client.AuthMechanismFactory;
5047
import com.rabbitmq.client.Channel;
5148
import com.rabbitmq.client.Command;
5249
import com.rabbitmq.client.Connection;
@@ -278,21 +275,21 @@ public void start()
278275

279276
List<String> mechanisms = Arrays.asList(
280277
connStart.getMechanisms().toString().split(" "));
281-
AuthMechanism mechanism = _factory.getAuthMechanism(mechanisms);
282-
if (mechanism == null) {
278+
AuthMechanismFactory mechanismFactory = _factory.getAuthMechanismFactory(mechanisms);
279+
if (mechanismFactory == null) {
283280
throw new IOException("No compatible authentication mechanism found - " +
284281
"server offered [" + connStart.getMechanisms() + "]");
285282
}
283+
AuthMechanism mechanism = mechanismFactory.getInstance();
286284

287-
int round = 0;
288285
LongString challenge = null;
289286
AMQP.Connection.Tune connTune = null;
290287
do {
291-
LongString response = mechanism.handleChallenge(round, challenge, _factory);
292-
Method method = (round == 0)
288+
LongString response = mechanism.handleChallenge(challenge, _factory);
289+
Method method = (challenge == null)
293290
? new AMQImpl.Connection.StartOk(_clientProperties,
294-
mechanism.getName(), response,
295-
"en_US")
291+
mechanismFactory.getName(),
292+
response, "en_US")
296293
: new AMQImpl.Connection.SecureOk(response);
297294

298295
try {
@@ -301,7 +298,6 @@ public void start()
301298
connTune = (AMQP.Connection.Tune) serverResponse;
302299
} else {
303300
challenge = ((AMQP.Connection.Secure) serverResponse).getChallenge();
304-
round++;
305301
}
306302
} catch (ShutdownSignalException e) {
307303
throw AMQChannel.wrap(e, "Possibly caused by authentication failure");

src/com/rabbitmq/client/impl/PlainMechanism.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,9 @@
77
* The PLAIN auth mechanism
88
*/
99
public class PlainMechanism implements AuthMechanism {
10-
public LongString handleChallenge(int round, LongString challenge,
10+
public LongString handleChallenge(LongString challenge,
1111
ConnectionFactory factory) {
1212
return LongStringHelper.asLongString("\0" + factory.getUsername() +
1313
"\0" + factory.getPassword());
1414
}
15-
16-
public String getName() {
17-
return "PLAIN";
18-
}
1915
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.rabbitmq.client.impl;
2+
3+
import com.rabbitmq.client.AuthMechanism;
4+
import com.rabbitmq.client.AuthMechanismFactory;
5+
6+
/**
7+
*
8+
*/
9+
public class PlainMechanismFactory implements AuthMechanismFactory {
10+
public AuthMechanism getInstance() {
11+
return new PlainMechanism();
12+
}
13+
14+
public String getName() {
15+
return "PLAIN";
16+
}
17+
}

src/com/rabbitmq/client/impl/ScramMD5Mechanism.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ This is only somewhat improved security over PLAIN (if you can
2323
*/
2424

2525
public class ScramMD5Mechanism implements AuthMechanism {
26-
public LongString handleChallenge(int round, LongString challengeStr,
26+
public LongString handleChallenge(LongString challengeStr,
2727
ConnectionFactory factory) {
28-
if (round == 0) {
28+
if (challengeStr == null) {
2929
return LongStringHelper.asLongString(factory.getUsername());
3030
} else {
3131
try {
@@ -43,10 +43,6 @@ public LongString handleChallenge(int round, LongString challengeStr,
4343
}
4444
}
4545

46-
public String getName() {
47-
return "RABBIT-SCRAM-MD5";
48-
}
49-
5046
private static byte[] digest(byte[] arr1, byte[] arr2) {
5147
try {
5248
MessageDigest digest = MessageDigest.getInstance("MD5");
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.rabbitmq.client.impl;
2+
3+
import com.rabbitmq.client.AuthMechanism;
4+
import com.rabbitmq.client.AuthMechanismFactory;
5+
6+
/**
7+
*
8+
*/
9+
public class ScramMD5MechanismFactory implements AuthMechanismFactory {
10+
public AuthMechanism getInstance() {
11+
return new ScramMD5Mechanism();
12+
}
13+
14+
public String getName() {
15+
return "RABBIT-SCRAM-MD5";
16+
}
17+
}

0 commit comments

Comments
 (0)