Browse Source

Updated BasicMqttCredentials processing

pull/7596/head
imbeacon 4 years ago
parent
commit
05f92d7222
  1. 11
      application/src/test/java/org/thingsboard/server/transport/mqtt/mqttv3/credentials/BasicMqttCredentialsTest.java
  2. 20
      common/transport/mqtt/src/main/java/org/thingsboard/server/transport/mqtt/MqttTransportHandler.java

11
application/src/test/java/org/thingsboard/server/transport/mqtt/mqttv3/credentials/BasicMqttCredentialsTest.java

@ -16,7 +16,8 @@
package org.thingsboard.server.transport.mqtt.mqttv3.credentials; package org.thingsboard.server.transport.mqtt.mqttv3.credentials;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import org.eclipse.paho.client.mqttv3.MqttSecurityException; import org.eclipse.paho.client.mqttv3.MqttException;
import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.common.util.JacksonUtil;
@ -114,11 +115,15 @@ public class BasicMqttCredentialsTest extends AbstractMqttIntegrationTest {
testTelemetryIsDelivered(accessToken2Device, mqttTestClient5); testTelemetryIsDelivered(accessToken2Device, mqttTestClient5);
} }
@Test(expected = MqttSecurityException.class) @Test
public void testCorrectClientIdAndUserNameButWrongPassword() throws Exception { public void testCorrectClientIdAndUserNameButWrongPassword() throws Exception {
// Not correct. Correct clientId and username, but wrong password // Not correct. Correct clientId and username, but wrong password
MqttTestClient mqttTestClient = new MqttTestClient(CLIENT_ID); MqttTestClient mqttTestClient = new MqttTestClient(CLIENT_ID);
mqttTestClient.connectAndWait(USER_NAME3, "WRONG PASSWORD"); try {
mqttTestClient.connectAndWait(USER_NAME3, "WRONG PASSWORD");
} catch (MqttException e) {
Assert.assertEquals(4, e.getReasonCode()); // 4 - Reason code for bad username or password in MQTT v3
}
testTelemetryIsNotDelivered(clientIdAndUserNameAndPasswordDevice3, mqttTestClient); testTelemetryIsNotDelivered(clientIdAndUserNameAndPasswordDevice3, mqttTestClient);
} }

20
common/transport/mqtt/src/main/java/org/thingsboard/server/transport/mqtt/MqttTransportHandler.java

@ -1010,17 +1010,19 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement
private void onValidateDeviceResponse(ValidateDeviceCredentialsResponse msg, ChannelHandlerContext ctx, MqttConnectMessage connectMessage) { private void onValidateDeviceResponse(ValidateDeviceCredentialsResponse msg, ChannelHandlerContext ctx, MqttConnectMessage connectMessage) {
if (!msg.hasDeviceInfo()) { if (!msg.hasDeviceInfo()) {
context.onAuthFailure(address); context.onAuthFailure(address);
if (MqttVersion.MQTT_5.equals(deviceSessionCtx.getMqttVersion())) { ReturnCode returnCode = ReturnCode.NOT_AUTHORIZED_5;
ReturnCode returnCode = ReturnCode.NOT_AUTHORIZED_5; if (sslHandler == null || getX509Certificate() == null) {
if (sslHandler == null || getX509Certificate() == null) { String username = connectMessage.payload().userName();
if (connectMessage.payload().userName() == null ^ connectMessage.payload().passwordInBytes() == null) { byte[] passwordBytes = connectMessage.payload().passwordInBytes();
returnCode = ReturnCode.BAD_USERNAME_OR_PASSWORD; String clientId = connectMessage.payload().clientIdentifier();
} else if (!StringUtils.isBlank(connectMessage.payload().clientIdentifier())) { if ((username != null && passwordBytes != null && clientId != null)
returnCode = ReturnCode.CLIENT_IDENTIFIER_NOT_VALID; || (username == null ^ passwordBytes == null)) {
} returnCode = ReturnCode.BAD_USERNAME_OR_PASSWORD;
} else if (!StringUtils.isBlank(clientId)) {
returnCode = ReturnCode.CLIENT_IDENTIFIER_NOT_VALID;
} }
ctx.writeAndFlush(createMqttConnAckMsg(returnCode, connectMessage));
} }
ctx.writeAndFlush(createMqttConnAckMsg(returnCode, connectMessage));
ctx.close(); ctx.close();
} else { } else {
context.onAuthSuccess(address); context.onAuthSuccess(address);

Loading…
Cancel
Save