Skip to content

Commit

Permalink
Code cleanups for jwtgen tool
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Collins <[email protected]>
  • Loading branch information
benmcollins committed Jan 12, 2025
1 parent f5c32dc commit 39b3539
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
43 changes: 26 additions & 17 deletions examples/main-gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,28 @@ void usage(const char *name)
exit(0);
}

static int __gen_wcb(jwt_t *jwt, jwt_config_t *config)
{
jwt_value_t jval;
int ret;

if (config == NULL)
return 1;

jwt_set_GET_JSON(&jval, NULL);
jval.pretty = 1;
ret = jwt_grant_get(jwt, &jval);
if (!ret) {
printf("PAYLOAD:\n%s\n", jval.json_val);
free(jval.json_val);
}
return 0;
}

int main(int argc, char *argv[])
{
char *opt_key_name = NULL;
jwt_alg_t opt_alg = JWT_ALG_NONE;
time_t iat = time(NULL);

int oc = 0;
char *optstr = "hk:a:c:j:";
Expand Down Expand Up @@ -125,8 +142,8 @@ int main(int argc, char *argv[])
}
}

fprintf(stderr, "jwtgen: privkey %s algorithm %s\n",
opt_key_name, jwt_alg_str(opt_alg));
printf("jwtgen: privkey %s algorithm %s\n",
opt_key_name, jwt_alg_str(opt_alg));

if (opt_alg != JWT_ALG_NONE && opt_key_name == NULL)
usage(basename(argv[0]));
Expand Down Expand Up @@ -154,25 +171,18 @@ int main(int argc, char *argv[])
}
}

jwt_set_ADD_INT(&jval, "iat", iat);
if (jwt_builder_claim_add(builder, &jval)) {
fprintf(stderr, "Error adding iat\n");
exit(EXIT_FAILURE);
}

if (opt_json) {
jwt_set_ADD_JSON(&jval, NULL, opt_json);
if (jwt_builder_claim_add(builder, &jval)) {
fprintf(stderr, "Error adding iat\n");
fprintf(stderr, "Error adding json\n");
exit(EXIT_FAILURE);
}
}

jwt_set_GET_JSON(&jval, NULL);
jval.pretty = 1;
if (jwt_builder_claim_get(builder, &jval) == JWT_VALUE_ERR_NONE) {
fprintf(stderr, "PAYLOAD: %s\n", jval.json_val);
free(jval.json_val);
if (jwt_builder_setcb(builder, __gen_wcb, NULL)) {
fprintf(stderr, "ERR setting callback: %s\n",
jwt_builder_error_msg(builder));
exit(EXIT_FAILURE);
}

out = jwt_builder_generate(builder);
Expand All @@ -182,12 +192,11 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}

fprintf(stderr, "jwt algo %s!\n", jwt_alg_str(opt_alg));
printf("jwt algo %s!\n", jwt_alg_str(opt_alg));

printf("%s\n", out);

free(out);

return 0;
}

6 changes: 4 additions & 2 deletions libjwt/jwt-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,11 @@ char *FUNC(generate)(jwt_common_t *__cmd)
jwt->alg = config.alg;
jwt->key = config.key;

if (jwt_head_setup(jwt))
return NULL;

out = jwt_encode_str(jwt);
__cmd->error = jwt->error;
strcpy(__cmd->error_msg, jwt->error_msg);
jwt_copy_error(__cmd, jwt);

return out;
}
Expand Down
6 changes: 3 additions & 3 deletions libjwt/jwt-encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static int write_js(const json_t *js, char **buf)
return *buf == NULL ? 1 : 0;
}

static int jwt_write_head(jwt_t *jwt, char **buf)
int jwt_head_setup(jwt_t *jwt)
{
jwt_value_t jval;

Expand All @@ -50,7 +50,7 @@ static int jwt_write_head(jwt_t *jwt, char **buf)
return 1;
}

return write_js(jwt->headers, buf);
return 0;
}

static int jwt_encode(jwt_t *jwt, char **out)
Expand All @@ -67,7 +67,7 @@ static int jwt_encode(jwt_t *jwt, char **out)
*out = NULL;

/* First the header. */
ret = jwt_write_head(jwt, &buf);
ret = write_js(jwt->headers, &buf);
if (ret)
return 1;
/* Encode it */
Expand Down
3 changes: 3 additions & 0 deletions libjwt/jwt-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ int jwt_checker_setkey_check(jwt_checker_t *checker, const jwt_alg_t alg,
JWT_NO_EXPORT
char *jwt_encode_str(jwt_t *jwt);

JWT_NO_EXPORT
int jwt_head_setup(jwt_t *jwt);

#define __trace() fprintf(stderr, "%s:%d\n", __func__, __LINE__)

#endif /* JWT_PRIVATE_H */

0 comments on commit 39b3539

Please sign in to comment.