Ready to build JWT

This commit is contained in:
Pierre HUBERT 2024-09-21 20:16:44 +02:00
parent 5db7593a4f
commit 561c49226b
5 changed files with 58 additions and 2 deletions

View File

@ -46,6 +46,7 @@
"string_view": "c", "string_view": "c",
"format": "c", "format": "c",
"span": "c", "span": "c",
"regex": "c" "regex": "c",
"stdlib.h": "c"
} }
} }

View File

@ -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" "dev_name.c"
INCLUDE_DIRS ".") INCLUDE_DIRS ".")

9
esp32_device/main/jwt.c Normal file
View File

@ -0,0 +1,9 @@
#include <string.h>
#include "jwt.h"
char *jwt_gen(cJSON *payload)
{
return strdup("TODO:)");
}

24
esp32_device/main/jwt.h Normal file
View File

@ -0,0 +1,24 @@
/**
* JWT utilities
*/
#pragma once
#include <stddef.h>
#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

View File

@ -10,6 +10,7 @@
#include "dev_name.h" #include "dev_name.h"
#include "storage.h" #include "storage.h"
#include "http_client.h" #include "http_client.h"
#include "jwt.h"
#include "esp_log.h" #include "esp_log.h"
@ -196,6 +197,27 @@ char *secure_api_get_dev_certificate()
void *secure_api_sync_device() 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"); printf("here implement sync device logic\n");
return NULL; return NULL;
} }