Skip to content

Commit

Permalink
Merge pull request #428 from guruchandru/jwt_write
Browse files Browse the repository at this point in the history
Write JWT claims to a file
  • Loading branch information
sadhyama committed Aug 3, 2023
2 parents 85a0d9b + 66e72b3 commit 695c097
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,9 @@ int parseCommandLine(int argc,char **argv,ParodusCfg * cfg)
{"close-reason-file", required_argument, 0, 'R'},
{"mtls-client-key-path", required_argument, 0, 'K'},
{"mtls-client-cert-path", required_argument, 0,'M'},
#ifdef FEATURE_DNS_QUERY
{"record-jwt-payload", required_argument, 0,'W'},
#endif
{0, 0, 0, 0}
};
int c;
Expand All @@ -478,6 +481,9 @@ int parseCommandLine(int argc,char **argv,ParodusCfg * cfg)
cfg->token_server_url = NULL;
cfg->cloud_status = NULL;
cfg->cloud_disconnect = NULL;
#ifdef FEATURE_DNS_QUERY
cfg->record_jwt_file = NULL;
#endif
optind = 1; /* We need this if parseCommandLine is called again */
while (1)
{
Expand Down Expand Up @@ -669,6 +675,12 @@ int parseCommandLine(int argc,char **argv,ParodusCfg * cfg)
ParodusInfo("mtls_client_cert_path is %s\n", cfg->mtls_client_cert_path);
break;

#ifdef FEATURE_DNS_QUERY
case 'W':
cfg->record_jwt_file = strdup(optarg);
ParodusInfo("record_jwt_file is %s\n", cfg->record_jwt_file);
break;
#endif
case '?':
/* getopt_long already printed an error message. */
break;
Expand Down Expand Up @@ -755,6 +767,13 @@ void free_cfg(ParodusCfg *cfg)
free(cfg->cloud_disconnect);
cfg->cloud_disconnect = NULL;
}
#ifdef FEATURE_DNS_QUERY
if(cfg->record_jwt_file != NULL)
{
free(cfg->record_jwt_file);
cfg->record_jwt_file = NULL;
}
#endif
}
}

Expand Down Expand Up @@ -793,6 +812,9 @@ void setDefaultValuesToCfg(ParodusCfg *cfg)
cfg->close_reason_file = NULL;
cfg->client_cert_path = NULL;
cfg->token_server_url = NULL;
#ifdef FEATURE_DNS_QUERY
cfg->record_jwt_file = NULL;
#endif

cfg->cloud_status = CLOUD_STATUS_OFFLINE;
ParodusInfo("Default cloud_status is %s\n", cfg->cloud_status);
Expand Down Expand Up @@ -988,6 +1010,16 @@ void loadParodusCfg(ParodusCfg * config,ParodusCfg *cfg)
{
ParodusPrint("token_server_url is NULL. set to empty\n");
}
#ifdef FEATURE_DNS_QUERY
if(config->record_jwt_file != NULL)
{
cfg->record_jwt_file = strdup(config->record_jwt_file);
}
else
{
ParodusPrint("record_jwt_file is NULL. set to empty\n");
}
#endif
}

#ifdef WAN_FAILOVER_SUPPORTED
Expand Down
3 changes: 3 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ typedef struct
char *cloud_status;
char *cloud_disconnect;
unsigned int boot_retry_wait;
#ifdef FEATURE_DNS_QUERY
char *record_jwt_file;
#endif
} ParodusCfg;

#define FLAGS_IPV6_ONLY (1 << 0)
Expand Down
29 changes: 28 additions & 1 deletion src/token.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,25 @@ extern int __res_nquery(res_state statp,
/* Internal functions */
/*----------------------------------------------------------------------------*/


void write_jwt_file(char* record_jwt_file, char* jwt_data)
{
if(jwt_data != NULL)
{
FILE* fp = fopen(record_jwt_file, "w+");
if (fp == NULL)
{
ParodusError("Error opening %s\n", record_jwt_file);
return;
}
fprintf(fp, "%s\n", jwt_data);
ParodusInfo("JWT claims is Successfully written to %s\n", record_jwt_file);
fclose(fp);
}
else
{
ParodusError("JWT claims is Empty or NULL\n");
}
}
static void show_times (time_t exp_time, time_t cur_time)
{
char exp_buf[30];
Expand Down Expand Up @@ -529,8 +547,17 @@ int allow_insecure_conn(char **server_addr, unsigned int *port)
}

if (insecure >= 0) {
ParodusCfg *cfg = get_parodus_cfg();
char *claim_str = cJSON_Print (jwt->private_claims);
ParodusInfo ("JWT claims: %s\n", claim_str);
if(cfg->record_jwt_file != NULL)
{
write_jwt_file(cfg->record_jwt_file, claim_str);
}
else
{
ParodusError("The record_jwt_file is NULL, failed to write jwt content to file\n");
}
free (claim_str);
}
cjwt_destroy(&jwt);
Expand Down

0 comments on commit 695c097

Please sign in to comment.