Enhance CommonCLI and JWTHelper to support MQTT email functionality
- Added loading and saving of MQTT email preferences in CommonCLI. - Updated command handling in CommonCLI to retrieve and set the MQTT email. - Modified JWTHelper to include an optional email parameter in JWT token creation. - Adjusted MQTTBridge to utilize the email from preferences when creating JWT tokens.
This commit is contained in:
1 parent
e5dbd56b5d
commit
1ed18f9e3b
4 files changed
+54
-9
No files matched your search
@@ -130,6 +130,7 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
|
||||
file.read((uint8_t *)&_prefs->mqtt_analyzer_us_enabled, sizeof(_prefs->mqtt_analyzer_us_enabled)); // 344
|
||||
file.read((uint8_t *)&_prefs->mqtt_analyzer_eu_enabled, sizeof(_prefs->mqtt_analyzer_eu_enabled)); // 345
|
||||
file.read((uint8_t *)&_prefs->mqtt_owner_public_key, sizeof(_prefs->mqtt_owner_public_key)); // 346
|
||||
file.read((uint8_t *)&_prefs->mqtt_email, sizeof(_prefs->mqtt_email)); // 347
|
||||
// 209
|
||||
>>>>>>> 6f42dc3 (Implement Let's Mesh Analyzer integration in MQTT Bridge)
|
||||
|
||||
@@ -266,6 +267,7 @@ void CommonCLI::savePrefs(FILESYSTEM* fs) {
|
||||
file.write((uint8_t *)&_prefs->mqtt_analyzer_us_enabled, sizeof(_prefs->mqtt_analyzer_us_enabled)); // 344
|
||||
file.write((uint8_t *)&_prefs->mqtt_analyzer_eu_enabled, sizeof(_prefs->mqtt_analyzer_eu_enabled)); // 345
|
||||
file.write((uint8_t *)&_prefs->mqtt_owner_public_key, sizeof(_prefs->mqtt_owner_public_key)); // 346
|
||||
file.write((uint8_t *)&_prefs->mqtt_email, sizeof(_prefs->mqtt_email)); // 347
|
||||
// 209
|
||||
>>>>>>> 6f42dc3 (Implement Let's Mesh Analyzer integration in MQTT Bridge)
|
||||
|
||||
@@ -502,6 +504,12 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
|
||||
} else {
|
||||
strcpy(reply, "> (not set)");
|
||||
}
|
||||
} else if (memcmp(config, "mqtt.email", 10) == 0) {
|
||||
if (_prefs->mqtt_email[0] != '\0') {
|
||||
sprintf(reply, "> %s", _prefs->mqtt_email);
|
||||
} else {
|
||||
strcpy(reply, "> (not set)");
|
||||
}
|
||||
} else if (memcmp(config, "mqtt.config.valid", 17) == 0) {
|
||||
// Check if MQTT configuration is valid using static method
|
||||
bool valid = MQTTBridge::isConfigValid(_prefs);
|
||||
@@ -810,6 +818,10 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
|
||||
} else {
|
||||
strcpy(reply, "Error: public key must be 64 hex characters (32 bytes)");
|
||||
}
|
||||
} else if (memcmp(config, "mqtt.email ", 11) == 0) {
|
||||
StrHelper::strncpy(_prefs->mqtt_email, &config[11], sizeof(_prefs->mqtt_email));
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
#endif
|
||||
} else {
|
||||
sprintf(reply, "unknown config: %s", config);
|
||||
|
||||
@@ -16,7 +16,8 @@ bool JWTHelper::createAuthToken(
|
||||
char* token,
|
||||
size_t tokenSize,
|
||||
const char* owner,
|
||||
const char* client
|
||||
const char* client,
|
||||
const char* email
|
||||
) {
|
||||
Serial.printf("JWTHelper: Starting JWT creation for audience: %s\n", audience);
|
||||
|
||||
@@ -54,7 +55,7 @@ bool JWTHelper::createAuthToken(
|
||||
|
||||
// Create payload with HEX public key (not base64!)
|
||||
char payload[512];
|
||||
size_t payloadLen = createPayload(publicKeyHex, audience, issuedAt, expiresIn, payload, sizeof(payload), owner, client);
|
||||
size_t payloadLen = createPayload(publicKeyHex, audience, issuedAt, expiresIn, payload, sizeof(payload), owner, client, email);
|
||||
if (payloadLen == 0) {
|
||||
Serial.printf("JWTHelper: Failed to create payload\n");
|
||||
return false;
|
||||
@@ -223,7 +224,8 @@ size_t JWTHelper::createPayload(
|
||||
char* output,
|
||||
size_t outputSize,
|
||||
const char* owner,
|
||||
const char* client
|
||||
const char* client,
|
||||
const char* email
|
||||
) {
|
||||
Serial.printf("JWTHelper: createPayload called with outputSize: %d\n", (int)outputSize);
|
||||
Serial.printf("JWTHelper: publicKey: %s, audience: %s, issuedAt: %lu, expiresIn: %lu\n",
|
||||
@@ -234,6 +236,9 @@ size_t JWTHelper::createPayload(
|
||||
if (client) {
|
||||
Serial.printf("JWTHelper: client: %s\n", client);
|
||||
}
|
||||
if (email) {
|
||||
Serial.printf("JWTHelper: email: %s\n", email);
|
||||
}
|
||||
|
||||
// Create JWT payload
|
||||
DynamicJsonDocument doc(512);
|
||||
@@ -255,6 +260,11 @@ size_t JWTHelper::createPayload(
|
||||
doc["client"] = client;
|
||||
}
|
||||
|
||||
// Add optional email field if provided
|
||||
if (email && strlen(email) > 0) {
|
||||
doc["email"] = email;
|
||||
}
|
||||
|
||||
// Use temporary buffer for JSON
|
||||
char jsonBuffer[512];
|
||||
size_t len = serializeJson(doc, jsonBuffer, sizeof(jsonBuffer));
|
||||
|
||||
@@ -22,6 +22,7 @@ public:
|
||||
* @param tokenSize Size of the token buffer
|
||||
* @param owner Optional owner public key in hex format (nullptr if not set)
|
||||
* @param client Optional client string (nullptr if not set)
|
||||
* @param email Optional email address (nullptr if not set)
|
||||
* @return true if token was created successfully
|
||||
*/
|
||||
static bool createAuthToken(
|
||||
@@ -32,7 +33,8 @@ public:
|
||||
char* token = nullptr,
|
||||
size_t tokenSize = 0,
|
||||
const char* owner = nullptr,
|
||||
const char* client = nullptr
|
||||
const char* client = nullptr,
|
||||
const char* email = nullptr
|
||||
);
|
||||
|
||||
private:
|
||||
@@ -67,6 +69,7 @@ private:
|
||||
* @param outputSize Size of output buffer
|
||||
* @param owner Optional owner public key in hex format (nullptr if not set)
|
||||
* @param client Optional client string (nullptr if not set)
|
||||
* @param email Optional email address (nullptr if not set)
|
||||
* @return Length of payload, or 0 on error
|
||||
*/
|
||||
static size_t createPayload(
|
||||
@@ -77,7 +80,8 @@ private:
|
||||
char* output,
|
||||
size_t outputSize,
|
||||
const char* owner = nullptr,
|
||||
const char* client = nullptr
|
||||
const char* client = nullptr,
|
||||
const char* email = nullptr
|
||||
);
|
||||
|
||||
};
|
||||
@@ -1021,13 +1021,20 @@ bool MQTTBridge::createAuthToken() {
|
||||
char client_version[64];
|
||||
snprintf(client_version, sizeof(client_version), "meshcoretomqtt/%s", _build_date);
|
||||
|
||||
// Get email from preferences (if set)
|
||||
const char* email = nullptr;
|
||||
if (_prefs->mqtt_email[0] != '\0') {
|
||||
email = _prefs->mqtt_email;
|
||||
MQTT_DEBUG_PRINTLN("Using email: %s", email);
|
||||
}
|
||||
|
||||
// Create JWT token for US server
|
||||
if (_analyzer_us_enabled) {
|
||||
MQTT_DEBUG_PRINTLN("Creating JWT token for US server...");
|
||||
if (JWTHelper::createAuthToken(
|
||||
*_identity, "mqtt-us-v1.letsmesh.net",
|
||||
0, expires_in, _auth_token_us, sizeof(_auth_token_us),
|
||||
owner_key, client_version)) {
|
||||
owner_key, client_version, email)) {
|
||||
MQTT_DEBUG_PRINTLN("Created auth token for US server");
|
||||
us_token_created = true;
|
||||
} else {
|
||||
@@ -1041,7 +1048,7 @@ bool MQTTBridge::createAuthToken() {
|
||||
if (JWTHelper::createAuthToken(
|
||||
*_identity, "mqtt-eu-v1.letsmesh.net",
|
||||
0, expires_in, _auth_token_eu, sizeof(_auth_token_eu),
|
||||
owner_key, client_version)) {
|
||||
owner_key, client_version, email)) {
|
||||
MQTT_DEBUG_PRINTLN("Created auth token for EU server");
|
||||
eu_token_created = true;
|
||||
} else {
|
||||
@@ -1384,11 +1391,17 @@ void MQTTBridge::maintainAnalyzerConnections() {
|
||||
char client_version[64];
|
||||
snprintf(client_version, sizeof(client_version), "meshcoretomqtt/%s", _build_date);
|
||||
|
||||
// Get email from preferences (if set)
|
||||
const char* email = nullptr;
|
||||
if (_prefs->mqtt_email[0] != '\0') {
|
||||
email = _prefs->mqtt_email;
|
||||
}
|
||||
|
||||
// Renew the token
|
||||
if (JWTHelper::createAuthToken(
|
||||
*_identity, "mqtt-us-v1.letsmesh.net",
|
||||
0, 86400, _auth_token_us, sizeof(_auth_token_us),
|
||||
owner_key, client_version)) {
|
||||
owner_key, client_version, email)) {
|
||||
unsigned long expires_in = 86400; // 24 hours
|
||||
_token_us_expires_at = current_time + expires_in;
|
||||
MQTT_DEBUG_PRINTLN("US token renewed, new expiration: %lu", _token_us_expires_at);
|
||||
@@ -1469,11 +1482,17 @@ void MQTTBridge::maintainAnalyzerConnections() {
|
||||
char client_version[64];
|
||||
snprintf(client_version, sizeof(client_version), "meshcoretomqtt/%s", _build_date);
|
||||
|
||||
// Get email from preferences (if set)
|
||||
const char* email = nullptr;
|
||||
if (_prefs->mqtt_email[0] != '\0') {
|
||||
email = _prefs->mqtt_email;
|
||||
}
|
||||
|
||||
// Renew the token
|
||||
if (JWTHelper::createAuthToken(
|
||||
*_identity, "mqtt-eu-v1.letsmesh.net",
|
||||
0, 86400, _auth_token_eu, sizeof(_auth_token_eu),
|
||||
owner_key, client_version)) {
|
||||
owner_key, client_version, email)) {
|
||||
unsigned long expires_in = 86400; // 24 hours
|
||||
_token_eu_expires_at = current_time + expires_in;
|
||||
MQTT_DEBUG_PRINTLN("EU token renewed, new expiration: %lu", _token_eu_expires_at);
|
||||
|
||||
Reference in new issue
Block a user