Skip to content

Commit cec0ce5

Browse files
authored
Fix mqtt_demo_mutual_auth username parameters for client authentication (#1893)
* Support CLIENT_USERNAME with parameters and metrics
1 parent 3cd7d57 commit cec0ce5

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

demos/mqtt/mqtt_demo_mutual_auth/mqtt_demo_mutual_auth.c

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,24 @@
241241
#define TRANSPORT_SEND_RECV_TIMEOUT_MS ( 500 )
242242

243243
/**
244-
* @brief The MQTT metrics string expected by AWS IoT.
244+
* @brief The MQTT metrics parameters expected by AWS IoT.
245245
*/
246-
#define METRICS_STRING "?SDK=" OS_NAME "&Version=" OS_VERSION "&Platform=" HARDWARE_PLATFORM_NAME "&MQTTLib=" MQTT_LIB
246+
#define METRICS_PARAMETERS "SDK=" OS_NAME "&Version=" OS_VERSION "&Platform=" HARDWARE_PLATFORM_NAME "&MQTTLib=" MQTT_LIB
247+
248+
/**
249+
* @brief The MQTT metrics string to be appended if #CLIENT_USERNAME doesn't contain parameter.
250+
*/
251+
#define METRICS_STRING "?" METRICS_PARAMETERS
247252

248253
/**
249254
* @brief The length of the MQTT metrics string expected by AWS IoT.
250255
*/
251256
#define METRICS_STRING_LENGTH ( ( uint16_t ) ( sizeof( METRICS_STRING ) - 1 ) )
252257

258+
/**
259+
* @brief The MQTT metrics string to be appended if #CLIENT_USERNAME contains parameters.
260+
*/
261+
#define METRICS_STRING_APPEND "&" METRICS_PARAMETERS
253262

254263
#ifdef CLIENT_USERNAME
255264

@@ -259,7 +268,16 @@
259268
* This is to support both metrics reporting and username/password based client
260269
* authentication by AWS IoT.
261270
*/
262-
#define CLIENT_USERNAME_WITH_METRICS CLIENT_USERNAME METRICS_STRING
271+
#define CLIENT_USERNAME_WITH_METRICS CLIENT_USERNAME METRICS_STRING
272+
273+
/**
274+
* @brief Append the username with the metrics string if #CLIENT_USERNAME contains parameter.
275+
*
276+
* #CLIENT_USERNAME can be appended with extra parameters like authorizer, token
277+
* and signature. Use the #METRICS_STRING_APPEND if parameters are already appended
278+
* in #CLIENT_USERNAME.
279+
*/
280+
#define CLIENT_USERNAME_APPEND_METRICS CLIENT_USERNAME METRICS_STRING_APPEND
263281
#endif
264282

265283
/**
@@ -1105,6 +1123,10 @@ static int establishMqttSession( MQTTContext_t * pMqttContext,
11051123
MQTTStatus_t mqttStatus;
11061124
MQTTConnectInfo_t connectInfo = { 0 };
11071125

1126+
#ifdef CLIENT_USERNAME
1127+
void * pMemchrPtr;
1128+
#endif
1129+
11081130
assert( pMqttContext != NULL );
11091131
assert( pSessionPresent != NULL );
11101132

@@ -1147,11 +1169,22 @@ static int establishMqttSession( MQTTContext_t * pMqttContext,
11471169
* the metrics string is appended to the username to support both client
11481170
* authentication and metrics collection. */
11491171
#ifdef CLIENT_USERNAME
1150-
connectInfo.pUserName = CLIENT_USERNAME_WITH_METRICS;
1151-
connectInfo.userNameLength = strlen( CLIENT_USERNAME_WITH_METRICS );
1172+
pMemchrPtr = memchr( CLIENT_USERNAME, '?', strlen( CLIENT_USERNAME ) );
1173+
1174+
if( pMemchrPtr != NULL )
1175+
{
1176+
connectInfo.pUserName = CLIENT_USERNAME_APPEND_METRICS;
1177+
connectInfo.userNameLength = strlen( CLIENT_USERNAME_APPEND_METRICS );
1178+
}
1179+
else
1180+
{
1181+
connectInfo.pUserName = CLIENT_USERNAME_WITH_METRICS;
1182+
connectInfo.userNameLength = strlen( CLIENT_USERNAME_WITH_METRICS );
1183+
}
1184+
11521185
connectInfo.pPassword = CLIENT_PASSWORD;
11531186
connectInfo.passwordLength = strlen( CLIENT_PASSWORD );
1154-
#else
1187+
#else /* ifdef CLIENT_USERNAME */
11551188
connectInfo.pUserName = METRICS_STRING;
11561189
connectInfo.userNameLength = METRICS_STRING_LENGTH;
11571190
/* Password for authentication is not used. */

0 commit comments

Comments
 (0)