Skip to content

Commit

Permalink
jwt_set_*(): Setup so these can be passed directly to the get/set fun…
Browse files Browse the repository at this point in the history
…ctions

Signed-off-by: Ben Collins <[email protected]>
  • Loading branch information
benmcollins committed Jan 15, 2025
1 parent 3919e7f commit d745462
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 39 deletions.
58 changes: 21 additions & 37 deletions include/jwt.h
Original file line number Diff line number Diff line change
Expand Up @@ -663,9 +663,9 @@ int jwt_checker_verify(jwt_checker_t *checker, const char *token);
* While there are certain claims that are defined by the RFCs related to JWT,
* what they actually control are application defined.
*
* There are two groups of claims functions. Ones for @ref jwt_builder_grp and
* ones for @ref jwt_checker_grp. While they are functionally the same, their
* use is very different.
* There are three groups of claims functions. Ones for @ref jwt_builder_grp,
* for @ref jwt_checker_grp, and finally, @ref jwt_object_grp. While they are
* functionally the same, their use is very different.
* @{
*/

Expand All @@ -685,7 +685,8 @@ int jwt_checker_verify(jwt_checker_t *checker, const char *token);
*/
#define jwt_set_GET_INT(__v, __n) ({ \
(__v)->type=JWT_VALUE_INT; \
(__v)->name=(__n);(__v)->int_val=0;(__v)->error=0;})
(__v)->name=(__n);(__v)->int_val=0;(__v)->error=0;\
(__v);})

/**
* @brief Setup a jwt_value_t to get a string value
Expand All @@ -696,7 +697,8 @@ int jwt_checker_verify(jwt_checker_t *checker, const char *token);
*/
#define jwt_set_GET_STR(__v, __n) ({ \
(__v)->type=JWT_VALUE_STR; \
(__v)->name=(__n);(__v)->str_val=NULL;(__v)->error=0;})
(__v)->name=(__n);(__v)->str_val=NULL;(__v)->error=0;\
(__v);})

/**
* @brief Setup a jwt_value_t to get a boolean value
Expand All @@ -707,7 +709,8 @@ int jwt_checker_verify(jwt_checker_t *checker, const char *token);
*/
#define jwt_set_GET_BOOL(__v, __n) ({ \
(__v)->type=JWT_VALUE_BOOL; \
(__v)->name=(__n);(__v)->bool_val=0;(__v)->error=0;})
(__v)->name=(__n);(__v)->bool_val=0;(__v)->error=0;\
(__v);})

/**
* @brief Setup a jwt_value_t to get an JSON string
Expand All @@ -718,7 +721,8 @@ int jwt_checker_verify(jwt_checker_t *checker, const char *token);
*/
#define jwt_set_GET_JSON(__v, __n) ({ \
(__v)->type=JWT_VALUE_JSON;(__v)->pretty=0; \
(__v)->name=(__n);(__v)->json_val=NULL;(__v)->error=0;})
(__v)->name=(__n);(__v)->json_val=NULL;(__v)->error=0;\
(__v);})

/**
* @brief Setup a jwt_value_t to add an integer value
Expand All @@ -730,7 +734,8 @@ int jwt_checker_verify(jwt_checker_t *checker, const char *token);
*/
#define jwt_set_ADD_INT(__v, __n, __x) ({ \
(__v)->type=JWT_VALUE_INT;(__v)->replace=0; \
(__v)->name=(__n);(__v)->int_val=(__x);(__v)->error=0;})
(__v)->name=(__n);(__v)->int_val=(__x);(__v)->error=0;\
(__v);})

/**
* @brief Setup a jwt_value_t to add a string value
Expand All @@ -742,7 +747,8 @@ int jwt_checker_verify(jwt_checker_t *checker, const char *token);
*/
#define jwt_set_ADD_STR(__v, __n, __x) ({ \
(__v)->type=JWT_VALUE_STR;(__v)->replace=0; \
(__v)->name=(__n);(__v)->str_val=(__x);(__v)->error=0;})
(__v)->name=(__n);(__v)->str_val=(__x);(__v)->error=0;\
(__v);})

/**
* @brief Setup a jwt_value_t to add a boolean value
Expand All @@ -754,7 +760,8 @@ int jwt_checker_verify(jwt_checker_t *checker, const char *token);
*/
#define jwt_set_ADD_BOOL(__v, __n, __x) ({ \
(__v)->type=JWT_VALUE_BOOL;(__v)->replace=0; \
(__v)->name=(__n);(__v)->bool_val=(__x);(__v)->error=0;})
(__v)->name=(__n);(__v)->bool_val=(__x);(__v)->error=0;\
(__v);})

/**
* @brief Setup a jwt_value_t to add a JSON string
Expand All @@ -764,33 +771,10 @@ int jwt_checker_verify(jwt_checker_t *checker, const char *token);
* @param __x Value to add
* @return No return value
*/
#define jwt_set_ADD_JSON(__v, __n, __x) ({ \
(__v)->type=JWT_VALUE_JSON;(__v)->replace=0; \
(__v)->name=(__n);(__v)->json_val=(__x);(__v)->error=0;})

/**
* @brief Setup a jwt_value_t to add a boolean value
*
* @param __v Pointer to a jwt_value_t object
* @param __n Name of the value
* @param __x Value to add
* @return No return value
*/
#define jwt_set_ADD_BOOL(__v, __n, __x) ({ \
(__v)->type=JWT_VALUE_BOOL;(__v)->replace=0; \
(__v)->name=(__n);(__v)->bool_val=(__x);(__v)->error=0;})

/**
* @brief Setup a jwt_value_t to add a JSON string
*
* @param __v Pointer to a jwt_value_t object
* @param __n Name of the value
* @param __x Value to add
* @return No return value
*/
#define jwt_set_ADD_JSON(__v, __n, __x) ({ \
(__v)->type=JWT_VALUE_JSON;(__v)->replace=0; \
(__v)->name=(__n);(__v)->json_val=(__x);(__v)->error=0;})
#define jwt_set_ADD_JSON(__v, __n, __x) ({ \
(__v)->type=JWT_VALUE_JSON;(__v)->replace=0; \
(__v)->name=(__n);(__v)->json_val=(__x);(__v)->error=0; \
(__v);})

/**
* @}
Expand Down
4 changes: 2 additions & 2 deletions tools/jwt-generate.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ int main(int argc, char *argv[])
}

if (json) {
jwt_set_ADD_JSON(&jval, NULL, json);
if (jwt_builder_claim_add(builder, &jval)) {
if (jwt_builder_claim_add(builder,
jwt_set_ADD_JSON(&jval, NULL, json))) {
fprintf(stderr, "Error adding JSON (%d)\n",
jval.error);
exit(EXIT_FAILURE);
Expand Down

0 comments on commit d745462

Please sign in to comment.