Skip to content

Commit

Permalink
Fix mqtt_demo_mutual_auth username parameters for client authenticati…
Browse files Browse the repository at this point in the history
…on (#1893)

* Support CLIENT_USERNAME with parameters and metrics
  • Loading branch information
chinglee-iot authored Oct 23, 2023
1 parent 3cd7d57 commit cec0ce5
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions demos/mqtt/mqtt_demo_mutual_auth/mqtt_demo_mutual_auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

/**
Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -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. */
Expand Down

0 comments on commit cec0ce5

Please sign in to comment.