Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ class MqttConnectMessage extends MqttMessage {
}

/// Sets the authentication
/// Note that although passed as a string the password can be any binary data up to a length of
/// 65535 bytes.
MqttConnectMessage authenticateAs(String? username, String? password) {
if (username != null) {
variableHeader!.connectFlags.usernameFlag = username.isNotEmpty;
Expand Down
31 changes: 18 additions & 13 deletions lib/src/mqtt_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -606,28 +606,33 @@ class MqttClient {
}

/// Check the username and password validity
@protected
void checkCredentials(String? username, String? password) {
if (username != null) {
MqttLogger.log(
"Authenticating with username '{$username}' "
"and password '{$password}'",
);
if (username.trim().length >
MqttClientConstants.recommendedMaxUsernamePasswordLength) {
MqttLogger.log(
'MqttClient::checkCredentials - Username length (${username.trim().length}) '
'exceeds the max recommended in the MQTT spec. ',
);
if (Protocol.version == MqttClientConstants.mqttV31ProtocolVersion) {
if (username.trim().length >
MqttClientConstants.recommendedMaxUsernamePasswordLength) {
MqttLogger.log(
'MqttClient::checkCredentials - Advisory - Username length (${username.trim().length}) '
'exceeds the max recommended in the MQTT 3.1 spec. ',
);
}
}
}
if (password != null &&
password.trim().length >

if (password != null) {
if (Protocol.version == MqttClientConstants.mqttV31ProtocolVersion) {
if (password.trim().length >
MqttClientConstants.recommendedMaxUsernamePasswordLength) {
MqttLogger.log(
'MqttClient::checkCredentials - Password length (${password.trim().length}) '
'exceeds the max recommended in the MQTT spec. ',
);
MqttLogger.log(
'MqttClient::checkCredentials - Advisory - Password length (${password.trim().length}) '
'exceeds the max recommended in the MQTT 3.1 spec. ',
);
}
}
}
}

Expand Down
13 changes: 10 additions & 3 deletions lib/src/utility/mqtt_client_logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ class MqttLogger {
/// Test output
static String testOutput = '';

/// Test mode
static bool testMode = false;

/// Log publish message payload data
static bool logPayloads = true;

static bool _testMode = false;

/// Test mode
static bool get testMode => _testMode;

static set testMode(bool state) {
_testMode = state;
testOutput = '';
}

/// Log method
/// If the optimise parameter is supplied it must have a toString method,
/// this allows large objects such as lots of payload data not to be
Expand Down
61 changes: 61 additions & 0 deletions test/mqtt_client_base_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,67 @@ void main() {
client.connectTimeoutPeriod = 500;
expect(client.connectTimeoutPeriod, 5000);
});
test('Check Credentials OK for 3.1', () {
final client = MqttClient('aaaa', 'bbbb');
client.logging(on: true);
MqttLogger.testMode = true;
client.setProtocolV31();
client.checkCredentials('username', 'password');
expect(
MqttLogger.testOutput.contains(
"Authenticating with username '{username}' "
"and password '{password}",
),
isTrue,
);
MqttLogger.testMode = false;
client.logging(on: false);
});
test('Check Credentials username bad for 3.1', () {
final client = MqttClient('aaaa', 'bbbb');
client.logging(on: true);
MqttLogger.testMode = true;
client.setProtocolV31();
client.checkCredentials('usernameffffffffffffff', 'password');
expect(
MqttLogger.testOutput.contains('Advisory - Username length'),
isTrue,
);
MqttLogger.testMode = false;
client.logging(on: false);
});
test('Check Credentials password bad for 3.1', () {
final client = MqttClient('aaaa', 'bbbb');
client.logging(on: true);
MqttLogger.testMode = true;
client.setProtocolV31();
client.checkCredentials('username', 'passwordkkkkkkkkkkkkkkkk');
expect(
MqttLogger.testOutput.contains('Advisory - Password length'),
isTrue,
);
MqttLogger.testMode = false;
client.logging(on: false);
});
test('Check Credentials always OK for 3.1.1', () {
final client = MqttClient('aaaa', 'bbbb');
client.logging(on: true);
MqttLogger.testMode = true;
client.setProtocolV311();
client.checkCredentials(
'usernamedddddddddddddddd',
'passwordfffffffffffffff',
);
expect(
MqttLogger.testOutput.contains(
"Authenticating with username '{usernamedddddddddddddddd}' "
"and password '{passwordfffffffffffffff}",
),
isTrue,
);
MqttLogger.testMode = false;
client.logging(on: false);
});
});

group('Logging', () {
Expand Down