diff --git a/core/sources.cmake b/core/sources.cmake index 81c6633..c540693 100644 --- a/core/sources.cmake +++ b/core/sources.cmake @@ -30,6 +30,7 @@ function(tedge_core_sources out prefix) _on(TEDGE_TRANSPORT_C8Y tedge_c8y.c tedge_codec.c + tedge_jwt.c tedge_mqtt.c tedge_smartrest.c ) diff --git a/core/src/tedge_c8y.c b/core/src/tedge_c8y.c index 78c8066..fa60f50 100644 --- a/core/src/tedge_c8y.c +++ b/core/src/tedge_c8y.c @@ -40,7 +40,10 @@ TEDGE_LOG_DECLARE(); #define CONNACK_TIMEOUT_MS 15000 #define SUBACK_TIMEOUT_MS 3000 #define SUBACK_RETRY_MAX 5 +/* Cumulocity's tokens last an hour by default: refresh well before that, + * and ask again when a request gets no answer. */ #define JWT_REFRESH_MS (50 * 60 * 1000) +#define JWT_RETRY_MS (60 * 1000) /* Three broker closes this soon after CONNACK look like another client using * the same ID (c8y-direct-spikes, Spike B finding 6). */ #define TAKEOVER_WINDOW_MS 10000 @@ -73,7 +76,8 @@ static uint8_t packet_buf[TEDGE_C8Y_PAYLOAD_BYTES + PACKET_OVERHEAD]; static bool session_open; static bool ca_registered; static char jwt[1024]; -static int64_t jwt_at; +static int64_t jwt_at; /* when the token arrived; 0: none yet */ +static int64_t jwt_asked_at; /* when one was last requested; 0: never */ static char device_id[64]; static int64_t connected_at; @@ -705,6 +709,14 @@ static void dispatch_operation(const char *line) TEDGE_LOG_DBG("unhandled downstream message: %.40s", line); } +/* Ask for a new token on s/uat; it arrives on s/dat. */ +static void request_jwt(void) +{ + /* Never 0, which means "never asked". */ + jwt_asked_at = tedge_port_uptime_ms() | 1; + (void)publish("s/uat", "", QOS_0); +} + static void handle_message(const char *topic, const char *payload, size_t len) { if (strcmp(topic, "s/dat") == 0) { @@ -712,7 +724,7 @@ static void handle_message(const char *topic, const char *payload, size_t len) if (len > 3 && len - 3 < sizeof(jwt)) { memcpy(jwt, payload + 3, len - 3); jwt[len - 3] = '\0'; - jwt_at = tedge_port_uptime_ms(); + jwt_at = tedge_port_uptime_ms() | 1; TEDGE_LOG_DBG("token received (%zu B)", len - 3); } return; @@ -1135,8 +1147,9 @@ static int session_start(void) } } if (tedge_auth_username() == NULL) { - /* Only certificate devices get a token. */ - (void)publish("s/uat", "", QOS_0); + /* Only certificate devices get a token. A new session always + * asks for one. */ + request_jwt(); } /* c8y_Firmware, in every build: the running image's name and version. @@ -1399,10 +1412,10 @@ static int c8y_poll(int timeout_ms) /* Renew the certificate while there is still plenty of time. */ tedge_cert_renew_tick(); #endif - if (tedge_auth_username() == NULL && jwt_at != 0 && - tedge_port_uptime_ms() - jwt_at > JWT_REFRESH_MS) { - jwt_at = tedge_port_uptime_ms(); /* one request per interval */ - (void)publish("s/uat", "", QOS_0); + if (tedge_auth_username() == NULL && + tedge_jwt_request_due(tedge_port_uptime_ms(), jwt_at, jwt_asked_at, + JWT_REFRESH_MS, JWT_RETRY_MS)) { + request_jwt(); } return 0; } diff --git a/core/src/tedge_internal.h b/core/src/tedge_internal.h index d37ab7f..69587a4 100644 --- a/core/src/tedge_internal.h +++ b/core/src/tedge_internal.h @@ -151,6 +151,18 @@ int tedge_c8y_publish_sr(const char *line); /** The latest JWT from s/dat, or an empty string. */ const char *tedge_c8y_jwt(void); +/** + * Whether to ask for a new token now (tedge_jwt.c). Times are uptimes in + * milliseconds, and 0 means never: @p got_at_ms is when the last token + * arrived, @p asked_at_ms when one was last requested. A token is refreshed + * @p refresh_ms after it arrived; a request that gets no answer is repeated + * after @p retry_ms, so one lost reply does not leave the device with an + * expired token until it reconnects. + */ +bool tedge_jwt_request_due(int64_t now_ms, int64_t got_at_ms, + int64_t asked_at_ms, int64_t refresh_ms, + int64_t retry_ms); + /* --- Downloads (tedge_http_download.c) ----------------------------------- */ /** Receives each body segment as the parser reports it. */ diff --git a/core/src/tedge_jwt.c b/core/src/tedge_jwt.c new file mode 100644 index 0000000..6718e4e --- /dev/null +++ b/core/src/tedge_jwt.c @@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: Apache-2.0 + * + * When to ask Cumulocity for a new token (s/uat). Pure, so the cases that + * matter (no answer to a request, a token that is about to expire) are + * unit-tested rather than reasoned about. + */ + +#include "tedge_internal.h" + +bool tedge_jwt_request_due(int64_t now_ms, int64_t got_at_ms, + int64_t asked_at_ms, int64_t refresh_ms, + int64_t retry_ms) +{ + /* A request is out: give its answer time to arrive. */ + if (asked_at_ms != 0 && now_ms - asked_at_ms < retry_ms) { + return false; + } + /* No token yet, or the request for one went unanswered. */ + if (got_at_ms == 0) { + return true; + } + return now_ms - got_at_ms >= refresh_ms; +} diff --git a/ports/zephyr/tests/unit/CMakeLists.txt b/ports/zephyr/tests/unit/CMakeLists.txt index ea3127c..2b61681 100644 --- a/ports/zephyr/tests/unit/CMakeLists.txt +++ b/ports/zephyr/tests/unit/CMakeLists.txt @@ -19,6 +19,7 @@ target_sources(app PRIVATE ../../../../core/src/tedge_ra_target.c ../../../../core/src/tedge_url.c ../../../../core/src/tedge_cert_decision.c + ../../../../core/src/tedge_jwt.c ../../../../core/src/tedge_telemetry.c ../../../../core/src/tedge_json.c ../../../../core/src/tedge_shell_allow_list.c diff --git a/ports/zephyr/tests/unit/src/main.c b/ports/zephyr/tests/unit/src/main.c index 01177f0..48d3688 100644 --- a/ports/zephyr/tests/unit/src/main.c +++ b/ports/zephyr/tests/unit/src/main.c @@ -175,6 +175,59 @@ ZTEST(tedge_backoff, test_jitter_spreads_attempts) ZTEST_SUITE(tedge_backoff, NULL, NULL, NULL, NULL, NULL); +/* --- tedge_jwt.c --------------------------------------------------------- */ + +#define REFRESH (50 * 60 * 1000) +#define RETRY (60 * 1000) + +ZTEST(tedge_jwt, test_first_request) +{ + /* No token and never asked. */ + zassert_true(tedge_jwt_request_due(5000, 0, 0, REFRESH, RETRY)); +} + +ZTEST(tedge_jwt, test_waits_for_the_answer) +{ + /* Asked at 10 s: not again within the retry interval. */ + zassert_false(tedge_jwt_request_due(10000 + RETRY - 1, 0, 10000, + REFRESH, RETRY)); +} + +ZTEST(tedge_jwt, test_unanswered_request_is_repeated) +{ + /* The first answer was lost: ask again once the retry interval is up, + * instead of waiting for the next session. */ + zassert_true(tedge_jwt_request_due(10000 + RETRY, 0, 10000, + REFRESH, RETRY)); +} + +ZTEST(tedge_jwt, test_fresh_token_is_kept) +{ + zassert_false(tedge_jwt_request_due(20000 + REFRESH - 1, 20000, 10000, + REFRESH, RETRY)); +} + +ZTEST(tedge_jwt, test_refresh_after_the_interval) +{ + zassert_true(tedge_jwt_request_due(20000 + REFRESH, 20000, 10000, + REFRESH, RETRY)); +} + +ZTEST(tedge_jwt, test_lost_refresh_is_repeated) +{ + /* The refresh was asked for at 50 min and never answered: the old + * token is about to expire, so ask again a minute later, and not + * before. */ + int64_t got = 20000, asked = got + REFRESH; + + zassert_false(tedge_jwt_request_due(asked + RETRY - 1, got, asked, + REFRESH, RETRY)); + zassert_true(tedge_jwt_request_due(asked + RETRY, got, asked, + REFRESH, RETRY)); +} + +ZTEST_SUITE(tedge_jwt, NULL, NULL, NULL, NULL, NULL); + /* ------------------------------------------------------------------------ */ /* Remote access */ /* ------------------------------------------------------------------------ */