SolarEnergy/esp32_device/main/jwt.c

53 lines
1.1 KiB
C
Raw Normal View History

2024-09-21 18:16:44 +00:00
#include <string.h>
2024-09-21 18:24:33 +00:00
#include <stdio.h>
2024-09-21 18:16:44 +00:00
#include "jwt.h"
2024-09-21 18:24:33 +00:00
#include "dev_name.h"
2024-09-21 18:43:02 +00:00
#include "crypto.h"
2024-09-21 18:24:33 +00:00
#include "esp_log.h"
static const char *TAG = "jwt";
2024-09-21 18:16:44 +00:00
char *jwt_gen(cJSON *payload)
{
2024-09-21 18:24:33 +00:00
char *kid = dev_name();
if (!kid)
{
ESP_LOGE(TAG, "Failed to allocated memory to store device name!");
return NULL;
}
cJSON *header_json = cJSON_CreateObject();
if (!header_json)
return NULL;
cJSON_AddStringToObject(header_json, "alg", "ES256");
cJSON_AddStringToObject(header_json, "typ", "JWT");
cJSON_AddStringToObject(header_json, "kid", kid);
char *header = cJSON_PrintUnformatted(header_json);
free(kid);
cJSON_Delete(header_json);
if (!header)
{
ESP_LOGE(TAG, "Failed to generate JSON header!");
return NULL;
}
2024-09-21 18:43:02 +00:00
char *header_b64 = crypto_encode_base64_safe_url(header, strlen(header));
free(header);
2024-09-21 18:24:33 +00:00
2024-09-21 18:43:02 +00:00
if (!header_b64)
{
ESP_LOGE(TAG, "Failed to encode header to base64!");
return NULL;
}
printf("header = %s\n", header_b64);
free(header_b64);
// TODO : continue
2024-09-21 18:16:44 +00:00
return strdup("TODO:)");
}