Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mqtt_demo_mutual_auth username parameters for client authentication #1893

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Changes from 2 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
42 changes: 36 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 @@ -1104,6 +1122,7 @@ static int establishMqttSession( MQTTContext_t * pMqttContext,
int returnStatus = EXIT_SUCCESS;
MQTTStatus_t mqttStatus;
MQTTConnectInfo_t connectInfo = { 0 };
void * pMemchrPtr;

assert( pMqttContext != NULL );
assert( pSessionPresent != NULL );
Expand Down Expand Up @@ -1147,11 +1166,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;
chinglee-iot marked this conversation as resolved.
Show resolved Hide resolved
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
Loading