From cec0ce5d0fd2d8243863444314e34263d3876b42 Mon Sep 17 00:00:00 2001 From: chinglee-iot <61685396+chinglee-iot@users.noreply.github.com> Date: Mon, 23 Oct 2023 15:16:21 +0800 Subject: [PATCH] Fix mqtt_demo_mutual_auth username parameters for client authentication (#1893) * Support CLIENT_USERNAME with parameters and metrics --- .../mqtt_demo_mutual_auth.c | 45 ++++++++++++++++--- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/demos/mqtt/mqtt_demo_mutual_auth/mqtt_demo_mutual_auth.c b/demos/mqtt/mqtt_demo_mutual_auth/mqtt_demo_mutual_auth.c index ff9cf65af9..44a157b838 100644 --- a/demos/mqtt/mqtt_demo_mutual_auth/mqtt_demo_mutual_auth.c +++ b/demos/mqtt/mqtt_demo_mutual_auth/mqtt_demo_mutual_auth.c @@ -241,15 +241,24 @@ #define TRANSPORT_SEND_RECV_TIMEOUT_MS ( 500 ) /** - * @brief The MQTT metrics string expected by AWS IoT. + * @brief The MQTT metrics parameters expected by AWS IoT. */ -#define METRICS_STRING "?SDK=" OS_NAME "&Version=" OS_VERSION "&Platform=" HARDWARE_PLATFORM_NAME "&MQTTLib=" MQTT_LIB +#define METRICS_PARAMETERS "SDK=" OS_NAME "&Version=" OS_VERSION "&Platform=" HARDWARE_PLATFORM_NAME "&MQTTLib=" MQTT_LIB + +/** + * @brief The MQTT metrics string to be appended if #CLIENT_USERNAME doesn't contain parameter. + */ +#define METRICS_STRING "?" METRICS_PARAMETERS /** * @brief The length of the MQTT metrics string expected by AWS IoT. */ #define METRICS_STRING_LENGTH ( ( uint16_t ) ( sizeof( METRICS_STRING ) - 1 ) ) +/** + * @brief The MQTT metrics string to be appended if #CLIENT_USERNAME contains parameters. + */ +#define METRICS_STRING_APPEND "&" METRICS_PARAMETERS #ifdef CLIENT_USERNAME @@ -259,7 +268,16 @@ * This is to support both metrics reporting and username/password based client * authentication by AWS IoT. */ - #define CLIENT_USERNAME_WITH_METRICS CLIENT_USERNAME METRICS_STRING + #define CLIENT_USERNAME_WITH_METRICS CLIENT_USERNAME METRICS_STRING + +/** + * @brief Append the username with the metrics string if #CLIENT_USERNAME contains parameter. + * + * #CLIENT_USERNAME can be appended with extra parameters like authorizer, token + * and signature. Use the #METRICS_STRING_APPEND if parameters are already appended + * in #CLIENT_USERNAME. + */ + #define CLIENT_USERNAME_APPEND_METRICS CLIENT_USERNAME METRICS_STRING_APPEND #endif /** @@ -1105,6 +1123,10 @@ static int establishMqttSession( MQTTContext_t * pMqttContext, MQTTStatus_t mqttStatus; MQTTConnectInfo_t connectInfo = { 0 }; + #ifdef CLIENT_USERNAME + void * pMemchrPtr; + #endif + assert( pMqttContext != NULL ); assert( pSessionPresent != NULL ); @@ -1147,11 +1169,22 @@ static int establishMqttSession( MQTTContext_t * pMqttContext, * the metrics string is appended to the username to support both client * authentication and metrics collection. */ #ifdef CLIENT_USERNAME - connectInfo.pUserName = CLIENT_USERNAME_WITH_METRICS; - connectInfo.userNameLength = strlen( CLIENT_USERNAME_WITH_METRICS ); + pMemchrPtr = memchr( CLIENT_USERNAME, '?', strlen( CLIENT_USERNAME ) ); + + if( pMemchrPtr != NULL ) + { + connectInfo.pUserName = CLIENT_USERNAME_APPEND_METRICS; + connectInfo.userNameLength = strlen( CLIENT_USERNAME_APPEND_METRICS ); + } + else + { + connectInfo.pUserName = CLIENT_USERNAME_WITH_METRICS; + connectInfo.userNameLength = strlen( CLIENT_USERNAME_WITH_METRICS ); + } + connectInfo.pPassword = CLIENT_PASSWORD; connectInfo.passwordLength = strlen( CLIENT_PASSWORD ); - #else + #else /* ifdef CLIENT_USERNAME */ connectInfo.pUserName = METRICS_STRING; connectInfo.userNameLength = METRICS_STRING_LENGTH; /* Password for authentication is not used. */