Skip to content

Commit

Permalink
Fix complilation of example programs and add them to the auto builds
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Collins <[email protected]>
  • Loading branch information
benmcollins committed Jan 9, 2025
1 parent f4504a9 commit aaa942d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
with:
options: |
WITH_TESTS=YES
WITH_EXAMPLES=YES
build-args: |
--
check
Expand All @@ -50,6 +51,7 @@ jobs:
with:
options: |
ENABLE_COVERAGE=YES
WITH_EXAMPLES=YES
build-args: |
--
check
Expand Down
29 changes: 22 additions & 7 deletions examples/main-auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ int main(int argc, char *argv[])
JWT_CONFIG_DECLARE(config);
jwt_auto_t *jwt = NULL;
jwk_set_auto_t *jwk_set = NULL;
jwk_item_t *item = NULL;
const jwk_item_t *item = NULL;
FILE *key_fp = NULL;
char key_data[BUFSIZ];
jwt_value_t jval;
int key_len;
char oc, ret;

Expand Down Expand Up @@ -114,20 +115,20 @@ int main(int argc, char *argv[])
}
/* Get the first key */
item = jwks_item_get(jwk_set, 0);
if (item->error) {
if (jwks_item_error(item)) {
fprintf(stderr, "ERR: Could not read JWK: %s\n",
item->error_msg);
jwks_item_error_msg(item));
exit(EXIT_FAILURE);
}

if (item->alg == JWT_ALG_NONE && opt_alg == JWT_ALG_NONE) {
if (jwks_item_alg(item) == JWT_ALG_NONE && opt_alg == JWT_ALG_NONE) {
fprintf(stderr, "Cannot find a valid algorithm in the "
" JWK. You need to set it with --alg\n");
exit(EXIT_FAILURE);
}

if (item->alg != JWT_ALG_NONE && opt_alg != JWT_ALG_NONE &&
item->alg != opt_alg) {
if (jwks_item_alg(item) != JWT_ALG_NONE && opt_alg != JWT_ALG_NONE &&
jwks_item_alg(item) != opt_alg) {
fprintf(stderr, "Key algorithm does not match --alg argument\n");
exit(EXIT_FAILURE);
}
Expand All @@ -146,7 +147,21 @@ int main(int argc, char *argv[])
fprintf(stderr, "JWT %s successfully!\n",
token ? "verified" : "decoded");

jwt_dump_fp(jwt, stdout, 1);
jwt_set_GET_JSON(&jval, NULL);
jval.pretty = 1;
ret = jwt_header_get(jwt, &jval);
if (!ret) {
fprintf(stderr, "HEADER:\n%s\n", jval.json_val);
free(jval.json_val);
}

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

exit(EXIT_SUCCESS);
}
Expand Down
38 changes: 22 additions & 16 deletions examples/main-gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int main(int argc, char *argv[])
int ret = 0;
jwt_auto_t *jwt = NULL;
jwk_set_auto_t *jwk_set = NULL;
jwk_item_t *item = NULL;
const jwk_item_t *item = NULL;
jwt_value_t jval;
struct kv {
char *key;
Expand Down Expand Up @@ -124,20 +124,20 @@ int main(int argc, char *argv[])
}
/* Get the first key */
item = jwks_item_get(jwk_set, 0);
if (item->error) {
if (jwks_item_error(item)) {
fprintf(stderr, "ERR: Could not read JWK: %s\n",
item->error_msg);
jwks_item_error_msg(item));
exit(EXIT_FAILURE);
}

if (item->alg == JWT_ALG_NONE && opt_alg == JWT_ALG_NONE) {
if (jwks_item_alg(item) == JWT_ALG_NONE && opt_alg == JWT_ALG_NONE) {
fprintf(stderr, "Cannot find a valid algorithm in the "
" JWK. You need to set it with --alg\n");
exit(EXIT_FAILURE);
}

if (item->alg != JWT_ALG_NONE && opt_alg != JWT_ALG_NONE &&
item->alg != opt_alg) {
if (jwks_item_alg(item) != JWT_ALG_NONE && opt_alg != JWT_ALG_NONE &&
jwks_item_alg(item) != opt_alg) {
fprintf(stderr, "Key algorithm does not match --alg argument\n");
exit(EXIT_FAILURE);
}
Expand All @@ -151,39 +151,45 @@ int main(int argc, char *argv[])
goto finish;
}

ret = jwt_add_grant_int(jwt, "iat", iat);
jwt_set_ADD_INT(&jval, "iat", iat);
jwt_grant_add(jwt, &jval);
for (i = 0; i < claims_count; i++) {
fprintf(stderr, "Adding claim %s with value %s\n", opt_claims[i].key, opt_claims[i].val);
jwt_add_grant(jwt, opt_claims[i].key, opt_claims[i].val);
fprintf(stderr, "Adding claim %s with value %s\n",
opt_claims[i].key, opt_claims[i].val);

jwt_set_ADD_STR(&jval, opt_claims[i].key, opt_claims[i].val);
jwt_grant_add(jwt, &jval);
}

if (opt_json != NULL) {
ret = jwt_add_grants_json(jwt, opt_json);
jwt_set_ADD_JSON(&jval, NULL, opt_json);
ret = jwt_grant_add(jwt, &jval);
if (ret != 0) {
fprintf(stderr, "Input json is invalid\n");
goto finish;
}
}

char *out = jwt_encode_str(jwt);
printf("Token: %s\n", out);
free(out);

jwt_set_GET_JSON(&jval, NULL);
jval.pretty = 1;
if (jwt_header_get(jwt, &jval) == JWT_VALUE_ERR_NONE) {
fprintf(stderr, "HEADER: %s\n", jval.json_val);
free(jval.json_val);
}

jwt_set_GET_JSON(&jval, NULL);
jval.pretty = 1;
if (jwt_grant_get(jwt, &jval) == JWT_VALUE_ERR_NONE) {
fprintf(stderr, "GRANTS: %s\n", jval.json_val);
fprintf(stderr, "PAYLOAD: %s\n", jval.json_val);
free(jval.json_val);
}

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

char *out = jwt_encode_str(jwt);
printf("%s\n", out);

free(out);

finish:
return 0;
}
Expand Down

0 comments on commit aaa942d

Please sign in to comment.