From 561c49226b14e9e042ddb26889b620b0d5822c22 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 21 Sep 2024 20:16:44 +0200 Subject: [PATCH] Ready to build JWT --- esp32_device/.vscode/settings.json | 3 ++- esp32_device/main/CMakeLists.txt | 2 +- esp32_device/main/jwt.c | 9 +++++++++ esp32_device/main/jwt.h | 24 ++++++++++++++++++++++++ esp32_device/main/secure_api.c | 22 ++++++++++++++++++++++ 5 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 esp32_device/main/jwt.c create mode 100644 esp32_device/main/jwt.h diff --git a/esp32_device/.vscode/settings.json b/esp32_device/.vscode/settings.json index 7b9add5..2669f47 100644 --- a/esp32_device/.vscode/settings.json +++ b/esp32_device/.vscode/settings.json @@ -46,6 +46,7 @@ "string_view": "c", "format": "c", "span": "c", - "regex": "c" + "regex": "c", + "stdlib.h": "c" } } diff --git a/esp32_device/main/CMakeLists.txt b/esp32_device/main/CMakeLists.txt index 82ce304..f7bdb83 100755 --- a/esp32_device/main/CMakeLists.txt +++ b/esp32_device/main/CMakeLists.txt @@ -1,4 +1,4 @@ -idf_component_register(SRCS "secure_api.c" "http_client.c" "ethernet.c" "unsecure_api.c" "system.c" "crypto.c" "random.c" "storage.c" "main.c" +idf_component_register(SRCS "jwt.c" "secure_api.c" "http_client.c" "ethernet.c" "unsecure_api.c" "system.c" "crypto.c" "random.c" "storage.c" "main.c" "dev_name.c" INCLUDE_DIRS ".") diff --git a/esp32_device/main/jwt.c b/esp32_device/main/jwt.c new file mode 100644 index 0000000..288fccc --- /dev/null +++ b/esp32_device/main/jwt.c @@ -0,0 +1,9 @@ + +#include + +#include "jwt.h" + +char *jwt_gen(cJSON *payload) +{ + return strdup("TODO:)"); +} \ No newline at end of file diff --git a/esp32_device/main/jwt.h b/esp32_device/main/jwt.h new file mode 100644 index 0000000..16e75d3 --- /dev/null +++ b/esp32_device/main/jwt.h @@ -0,0 +1,24 @@ +/** + * JWT utilities + */ + +#pragma once + +#include +#include "cJSON.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * Sign JWT token + * + * Return encoded JWT or NULL in case of failure + */ + char *jwt_gen(cJSON *payload); + +#ifdef __cplusplus +} +#endif diff --git a/esp32_device/main/secure_api.c b/esp32_device/main/secure_api.c index 7200cc7..5f1e0a7 100644 --- a/esp32_device/main/secure_api.c +++ b/esp32_device/main/secure_api.c @@ -10,6 +10,7 @@ #include "dev_name.h" #include "storage.h" #include "http_client.h" +#include "jwt.h" #include "esp_log.h" @@ -196,6 +197,27 @@ char *secure_api_get_dev_certificate() void *secure_api_sync_device() { + cJSON *obj = cJSON_CreateObject(); + if (!obj) + { + ESP_LOGE(TAG, "Failed allocate memory to store JSON object!"); + return NULL; + } + + cJSON_AddItemToObject(obj, "info", genDevInfo()); + + char *encoded_req = jwt_gen(obj); + cJSON_Delete(obj); + + if (!encoded_req) + { + ESP_LOGE(TAG, "Failed to encode JWT!"); + return NULL; + } + + printf("JWT: %s\n", encoded_req); + free(encoded_req); + printf("here implement sync device logic\n"); return NULL; } \ No newline at end of file