Skip to content

Commit fb1292e

Browse files
authored
Fix HTTP Basic Auth key handling (#15)
1 parent 01c5903 commit fb1292e

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
- OCPP 2.0.1 BasicAuthPassword integration ([#13](https://github.com/matth-x/MicroOcppMongoose/pull/13))
88

99
### Fixed
10-
- AuthorizationKey hex conversion ([#12](https://github.com/matth-x/MicroOcppMongoose/pull/12))
10+
- AuthorizationKey hex conversion ([#12](https://github.com/matth-x/MicroOcppMongoose/pull/12), [#15](https://github.com/matth-x/MicroOcppMongoose/pull/15))
1111

1212
## [v1.1.0] - 2024-05-21
1313

src/MicroOcppMongooseClient.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ void MOcppMongooseClient::maintainWsConn() {
274274
#if MO_MG_USE_VERSION <= MO_MG_V708
275275
mg_base64_encode(token, len, base64);
276276
#else
277-
mg_base64_encode(token, len, base64, sizeof(base64));
277+
mg_base64_encode(token, len, base64, base64_length + 1);
278278
#endif
279279
delete[] token;
280280

@@ -452,16 +452,24 @@ void MOcppMongooseClient::reloadConfigs() {
452452
{
453453
if (setting_auth_key_hex_str) {
454454
auto auth_key_hex = setting_auth_key_hex_str->getString();
455+
auto auth_key_hex_len = strlen(setting_auth_key_hex_str->getString());
456+
if (!validateAuthorizationKeyHex(auth_key_hex)) {
457+
MO_DBG_ERR("AuthorizationKey stored with format error. Disable Basic Auth");
458+
auth_key_hex_len = 0;
459+
}
460+
461+
auth_key_len = auth_key_hex_len / 2;
455462

456463
#if MO_MG_VERSION_614
457-
cs_from_hex((char*)auth_key, auth_key_hex, strlen(auth_key_hex));
464+
cs_from_hex((char*)auth_key, auth_key_hex, auth_key_hex_len);
458465
#elif MO_MG_USE_VERSION <= MO_MG_V713
459-
mg_unhex(auth_key_hex, strlen(auth_key_hex), auth_key);
466+
mg_unhex(auth_key_hex, auth_key_hex_len, auth_key);
460467
#else
461-
mg_str_to_num(mg_str(auth_key_hex), 16, auth_key, MO_AUTHKEY_LEN_MAX);
468+
for (size_t i = 0; i < auth_key_len; i++) {
469+
mg_str_to_num(mg_str_n(auth_key_hex + 2*i, 2), 16, auth_key + i, sizeof(uint8_t));
470+
}
462471
#endif
463472

464-
auth_key_len = strlen(setting_auth_key_hex_str->getString()) / 2;
465473
auth_key[auth_key_len] = '\0'; //need null-termination as long as deprecated `const char *getAuthKey()` exists
466474
}
467475
}

0 commit comments

Comments
 (0)