Generate unsigned JWT

This commit is contained in:
Pierre HUBERT 2024-09-21 20:56:51 +02:00
parent c55c55d56d
commit fa431857ee

View File

@ -12,6 +12,7 @@ static const char *TAG = "jwt";
char *jwt_gen(cJSON *payload) char *jwt_gen(cJSON *payload)
{ {
// Generate header
char *kid = dev_name(); char *kid = dev_name();
if (!kid) if (!kid)
{ {
@ -44,9 +45,41 @@ char *jwt_gen(cJSON *payload)
return NULL; return NULL;
} }
printf("header = %s\n", header_b64); // Encode body to JSON
char *body_json = cJSON_PrintUnformatted(payload);
if (!body_json)
{
ESP_LOGE(TAG, "Failed to encode body to JSON!");
free(header_b64); free(header_b64);
return NULL;
}
char *body_b64 = crypto_encode_base64_safe_url(body_json, strlen(body_json));
free(body_json);
if (!body_b64)
{
ESP_LOGE(TAG, "Failed to encode body to base64!");
free(header_b64);
return NULL;
}
// Assemble unsigned JWT parts
char *unsigned_jwt = calloc(1, strlen(header_b64) + strlen(body_b64) + 2);
if (!unsigned_jwt)
{
ESP_LOGE(TAG, "Failed to allocate memory to store unsigned JWT!");
free(header_b64);
free(body_b64);
return NULL;
}
sprintf(unsigned_jwt, "%s.%s", header_b64, body_b64);
free(header_b64);
free(body_b64);
// TODO : wip
printf("unsigned = %s\n", unsigned_jwt);
free(unsigned_jwt);
// TODO : continue // TODO : continue
return strdup("TODO:)"); return strdup("TODO:)");