-
Notifications
You must be signed in to change notification settings - Fork 23
/
mqtt_authenticate_variable_header.dart
181 lines (161 loc) · 5.79 KB
/
mqtt_authenticate_variable_header.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* Package : mqtt5_client
* Author : S. Hamblett <steve.hamblett@linux.com>
* Date : 10/05/2020
* Copyright : S.Hamblett
*/
part of mqtt5_client;
/// The variable Header of the authentication message contains the following fields in
/// the order: authenticate reason code and properties.
class MqttAuthenticateVariableHeader implements MqttIVariableHeader {
/// Initializes a new instance of the MqttAuthenticateVariableHeader class.
MqttAuthenticateVariableHeader(this.header);
/// Initializes a new instance of the MqttAuthenticateVariableHeader class.
MqttAuthenticateVariableHeader.fromByteBuffer(
this.header, MqttByteBuffer variableHeaderStream) {
readFrom(variableHeaderStream);
}
/// Standard header
MqttHeader? header;
int _length = 0;
/// The length of the variable header as received.
/// To get the write length use[getWriteLength].
@override
int get length => _length;
/// Reason code
MqttAuthenticateReasonCode? reasonCode = MqttAuthenticateReasonCode.notSet;
// Properties
final _propertySet = MqttPropertyContainer();
/// Authentication Method
///
/// A UTF-8 Encoded String containing the name of the authentication method.
/// It is a protocol error to omit the authentication method.
String? _authenticationMethod;
String? get authenticationMethod => _authenticationMethod;
set authenticationMethod(String? method) {
var property =
MqttUtf8StringProperty(MqttPropertyIdentifier.authenticationMethod);
property.value = method;
_propertySet.add(property);
_authenticationMethod = method;
}
/// Authentication Data
///
/// Binary data containing authentication data.
/// The contents of this data are defined by the authentication method.
typed.Uint8Buffer _authenticationData = typed.Uint8Buffer();
typed.Uint8Buffer get authenticationData => _authenticationData;
set authenticationData(typed.Uint8Buffer data) {
var property =
MqttBinaryDataProperty(MqttPropertyIdentifier.authenticationData);
property.addBytes(data);
_propertySet.add(property);
_authenticationData.clear();
_authenticationData.addAll(data);
}
/// Reason String.
///
/// The Reason String is a human readable string designed for diagnostics only.
String? _reasonString;
String? get reasonString => _reasonString;
set reasonString(String? reason) {
var property = MqttUtf8StringProperty(MqttPropertyIdentifier.reasonString);
property.value = reason;
_propertySet.add(property);
_reasonString = reason;
}
/// User property
///
/// The User Property is allowed to appear multiple times to represent
/// multiple name, value pairs. The same name is allowed to appear
/// more than once.
List<MqttUserProperty> _userProperty = <MqttUserProperty>[];
List<MqttUserProperty> get userProperty => _userProperty;
set userProperty(List<MqttUserProperty> properties) {
for (var userProperty in properties) {
_propertySet.add(userProperty);
}
_userProperty.addAll(properties);
}
void _processProperties() {
if (!_propertySet.propertiesAreValid()) {
throw FormatException(
'MqttAuthenticateVariableHeader::_processProperties, message properties received are invalid');
}
final properties = _propertySet.toList();
for (final property in properties) {
switch (property.identifier) {
case MqttPropertyIdentifier.authenticationMethod:
_authenticationMethod = property.value;
break;
case MqttPropertyIdentifier.reasonString:
_reasonString = property.value;
break;
case MqttPropertyIdentifier.authenticationData:
_authenticationData = typed.Uint8Buffer()..addAll(property.value);
break;
default:
if (property.identifier != MqttPropertyIdentifier.userProperty) {
MqttLogger.log(
'MqttAuthenticateVariableHeader::_processProperties, unexpected property type'
'received, identifier is ${property.identifier}, ignoring');
}
}
_userProperty = _propertySet.userProperties;
}
}
/// Creates a variable header from the specified header stream.
@override
void readFrom(MqttByteBuffer variableHeaderStream) {
if (header!.messageSize > 1) {
readReasonCode(variableHeaderStream);
_length += 1;
// Properties
variableHeaderStream.shrink();
_propertySet.readFrom(variableHeaderStream);
_length += _propertySet.getWriteLength();
_processProperties();
variableHeaderStream.shrink();
} else {
reasonCode = MqttAuthenticateReasonCode.success;
}
}
/// Writes the variable header to the supplied stream.
@override
void writeTo(MqttByteBuffer variableHeaderStream) {
if (isValid) {
writeReasonCode(variableHeaderStream);
_propertySet.writeTo(variableHeaderStream);
}
}
/// Gets the length of the write data when WriteTo will be called.
@override
int getWriteLength() {
var headerLength = 0;
if (isValid) {
headerLength += _propertySet.getWriteLength() + 1;
}
return headerLength;
}
/// Reason code.
void readReasonCode(MqttByteBuffer stream) {
reasonCode = mqttAuthenticateReasonCode.fromInt(stream.readByte());
}
/// Reason code.
void writeReasonCode(MqttByteBuffer stream) {
stream.writeByte(mqttAuthenticateReasonCode.asInt(reasonCode));
}
/// Is valid.
/// Reason code must be set and authentication method must be assigned.
bool get isValid =>
reasonCode != MqttAuthenticateReasonCode.notSet &&
authenticationMethod != null;
@override
String toString() {
final sb = StringBuffer();
sb.writeln(
'Reason Code = ${mqttAuthenticateReasonCode.asString(reasonCode)}');
sb.writeln('Properties = ${_propertySet.toString()}');
return sb.toString();
}
}