Skip to content

Commit de2e868

Browse files
committed
Add ANONYMOUS SASL mechanism
Fixes #1405 (cherry picked from commit c2849c2)
1 parent 09e7cc2 commit de2e868

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

src/main/java/com/rabbitmq/client/DefaultSaslConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
package com.rabbitmq.client;
1717

18+
import com.rabbitmq.client.impl.AnonymousMechanism;
1819
import com.rabbitmq.client.impl.ExternalMechanism;
1920
import com.rabbitmq.client.impl.PlainMechanism;
2021

@@ -30,6 +31,7 @@ public class DefaultSaslConfig implements SaslConfig {
3031

3132
public static final DefaultSaslConfig PLAIN = new DefaultSaslConfig("PLAIN");
3233
public static final DefaultSaslConfig EXTERNAL = new DefaultSaslConfig("EXTERNAL");
34+
public static final DefaultSaslConfig ANONYMOUS = new DefaultSaslConfig("ANONYMOUS");
3335

3436
/**
3537
* Create a DefaultSaslConfig with an explicit mechanism to use.
@@ -50,6 +52,8 @@ public SaslMechanism getSaslMechanism(String[] serverMechanisms) {
5052
}
5153
else if (mechanism.equals("EXTERNAL")) {
5254
return new ExternalMechanism();
55+
} else if (mechanism.equals("ANONYMOUS")) {
56+
return new AnonymousMechanism();
5357
}
5458
}
5559
return null;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2007-2023 Broadcom. All Rights Reserved. The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
2+
//
3+
// This software, the RabbitMQ Java client library, is triple-licensed under the
4+
// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2
5+
// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see
6+
// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL,
7+
// please see LICENSE-APACHE2.
8+
//
9+
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
10+
// either express or implied. See the LICENSE file for specific language governing
11+
// rights and limitations of this software.
12+
//
13+
// If you have any questions regarding licensing, please contact us at
14+
15+
16+
package com.rabbitmq.client.impl;
17+
18+
import com.rabbitmq.client.LongString;
19+
import com.rabbitmq.client.SaslMechanism;
20+
21+
/**
22+
* The ANONYMOUS auth mechanism
23+
*/
24+
public class AnonymousMechanism implements SaslMechanism {
25+
@Override
26+
public String getName() {
27+
return "ANONYMOUS";
28+
}
29+
30+
@Override
31+
public LongString handleChallenge(LongString challenge, String username, String password) {
32+
return LongStringHelper.asLongString("");
33+
}
34+
}

src/test/java/com/rabbitmq/client/test/TestUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,8 @@ private BrokerVersionAtLeast310Condition() {
485485

486486
public enum BrokerVersion {
487487
RABBITMQ_3_8("3.8.0"),
488-
RABBITMQ_3_10("3.10.0");
488+
RABBITMQ_3_10("3.10.0"),
489+
RABBITMQ_4_0("4.0.0");
489490

490491
final String value;
491492

src/test/java/com/rabbitmq/client/test/functional/SaslMechanisms.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,10 @@
2222
import java.util.Map;
2323
import java.util.concurrent.TimeoutException;
2424

25+
import com.rabbitmq.client.*;
2526
import com.rabbitmq.client.test.TestUtils;
2627
import org.junit.jupiter.api.Test;
2728

28-
import com.rabbitmq.client.AuthenticationFailureException;
29-
import com.rabbitmq.client.Connection;
30-
import com.rabbitmq.client.ConnectionFactory;
31-
import com.rabbitmq.client.LongString;
32-
import com.rabbitmq.client.PossibleAuthenticationFailureException;
33-
import com.rabbitmq.client.SaslConfig;
34-
import com.rabbitmq.client.SaslMechanism;
3529
import com.rabbitmq.client.impl.AMQConnection;
3630
import com.rabbitmq.client.impl.LongStringHelper;
3731
import com.rabbitmq.client.test.BrokerTestCase;
@@ -105,6 +99,15 @@ public SaslMechanism getSaslMechanism(String[] mechanisms) {
10599
connectionCloseAuthFailure(connectionFactory.getUsername(), "incorrect-password");
106100
}
107101

102+
@Test
103+
@TestUtils.BrokerVersionAtLeast(TestUtils.BrokerVersion.RABBITMQ_4_0)
104+
public void anonymousShouldSucceed() throws Exception {
105+
ConnectionFactory factory = TestUtils.connectionFactory();
106+
factory.setSaslConfig(DefaultSaslConfig.ANONYMOUS);
107+
Connection connection = factory.newConnection();
108+
connection.close();
109+
}
110+
108111
public void connectionCloseAuthFailure(String username, String password) throws IOException, TimeoutException {
109112
String failDetail = "for username " + username + " and password " + password;
110113
try {

0 commit comments

Comments
 (0)