Skip to content

Commit

Permalink
jwt_dump: Remove these functions
Browse files Browse the repository at this point in the history
The ability to print header and grants in JSON string format is
duplicated with jwt_header_get() and jwt_grant_get() when using the
JWT_VALUE_JSON type.

Added a .pretty flag for the JSON getter.

Reworked tests in jwt_dump

Signed-off-by: Ben Collins <[email protected]>
  • Loading branch information
benmcollins committed Jan 8, 2025
1 parent 626776e commit c7bc8f5
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 271 deletions.
13 changes: 12 additions & 1 deletion examples/main-gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ int main(int argc, char *argv[])
jwt_auto_t *jwt = NULL;
jwk_set_auto_t *jwk_set = NULL;
jwk_item_t *item = NULL;
jwt_value_t jval;
struct kv {
char *key;
char *val;
Expand Down Expand Up @@ -164,7 +165,17 @@ int main(int argc, char *argv[])
}
}

jwt_dump_fp(jwt, stderr, 1);
jwt_set_GET_JSON(&jval, NULL);
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);
if (jwt_grant_get(jwt, &jval) == JWT_VALUE_ERR_NONE) {
fprintf(stderr, "GRANTS: %s\n", jval.json_val);
free(jval.json_val);
}

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

Expand Down
108 changes: 29 additions & 79 deletions include/jwt.h
Original file line number Diff line number Diff line change
Expand Up @@ -522,12 +522,12 @@ jwt_t *jwt_verify_wcb(const char *token, jwt_config_t *config,
* @brief Value types for grants and headers
*/
typedef enum {
JWT_VALUE_NONE = 0, /**< No type (do not use this) */
JWT_VALUE_INT, /**< Integer */
JWT_VALUE_STR, /**< String */
JWT_VALUE_BOOL, /**< Boolean */
JWT_VALUE_JSON, /**< JSON String */
JWT_VALUE_INVALID, /**< Invalid (used internally) */
JWT_VALUE_NONE = 0, /**< No type (do not use this) */
JWT_VALUE_INT, /**< Integer */
JWT_VALUE_STR, /**< String */
JWT_VALUE_BOOL, /**< Boolean */
JWT_VALUE_JSON, /**< JSON String (object format ``{}``) */
JWT_VALUE_INVALID, /**< Invalid (used internally) */
} jwt_value_type_t;

/**
Expand Down Expand Up @@ -562,6 +562,7 @@ typedef struct {
char *json_val;
};
int replace;
int pretty;
jwt_value_error_t error;
} jwt_value_t;

Expand All @@ -571,7 +572,7 @@ typedef struct {
* When adding a value, you must set the type, name, and the specific val for
* the type. If the value already exists, then the function will return
* JWT_VALUE_ERR_EXISTS and value.error will be set the same. If value.replace
* is non-zero, then the exist value will be overwritten if it exists.
* is non-zero, then any existing value will be overwritten.
*
* @remarks When adding a JSON value, you can set value.name = NULL, in which case
* the entire header will be set to the JSON string pointed to by
Expand All @@ -580,6 +581,9 @@ typedef struct {
* updated. There is no indication of which values are or aren't updated in
* either case.
*
* @note The replace flag must be set after calling jwt_set_ADD_*() macro, as
* the macros will reset it back to 0.
*
* @code
* jwt_value_error_t ret;
* jwt_value_t jval;
Expand All @@ -588,7 +592,7 @@ typedef struct {
* ret = jwt_header_add(jwt, &jval);
*
* if (ret == JWT_VALUE_ERR_NONE)
* printf("iss: %s\n", jval.str_val);
* printf("iss updated to: %s\n", jval.str_val);
* @endcode
*
* @param jwt Pointer to a jwt_t token, previously created with jwt_create()
Expand All @@ -603,15 +607,19 @@ jwt_value_error_t jwt_header_add(jwt_t *jwt, jwt_value_t *value);
* @brief Get a value from the header of a JWT
*
* When getting a value, you must set type and name. On a successful return, the
* val in value specific to the type will be filled in. Common errors responses
* for this function are JWT_VALUE_ERR_NOEXIST when the name does not exist, and
* the value specific to the type will be filled in. Common error responses for
* this function are JWT_VALUE_ERR_NOEXIST when the name does not exist, and
* JWT_VALUE_ERR_TYPE, when the named object is not of the type you requested
* (e.g. you requested a string, but it's an integer value).
*
* @remarks When getting a JSON value, you can set value.name = NULL, in which
* case the entire header is returned. Also, the resulting value.json_val
* will be using allocated memory and must be freed by the caller.
*
* @note Normally JSON is retrieved in compact form. If you set
* jwt_value_t.pretty, then you will get a tabbed format suitable for human
* viewing. This must be set after calling jwt_set_GET_JSON().
*
* @code
* jwt_value_error_t ret;
* jwt_value_t jval;
Expand All @@ -633,15 +641,14 @@ jwt_value_error_t jwt_header_get(jwt_t *jwt, jwt_value_t *value);
/**
* @brief Delete a value from the header of a JWT
*
* Deletes the value referenced by value.name from the header. If you set
* value.name = NULL, then the entire header will be cleared of all values. This
* Deletes the value referenced by ``header`` from the header. If you pass NULL
* as the header, then the entire header will be cleared of all values. This
* function will generally return without error.
*
* @param jwt Pointer to a jwt_t token, previously created with jwt_create()
* @param header The name of the header to delete, or NULL to clear the entire
* header
* @return A jwt_value_error_t value, JWT_VALUE_ERR_NONE being success. The
* value.error field will match this return value.
* @return A jwt_value_error_t value, JWT_VALUE_ERR_NONE being success.
*/
JWT_EXPORT
jwt_value_error_t jwt_header_del(jwt_t *jwt, const char *header);
Expand Down Expand Up @@ -679,8 +686,7 @@ jwt_value_error_t jwt_grant_get(jwt_t *jwt, jwt_value_t *value);
*
* @param jwt Pointer to a jwt_t token, previously created with jwt_create()
* @param header The name of the grant to delete, or NULL to clear all grants
* @return A jwt_value_error_t value, JWT_VALUE_ERR_NONE being success. The
* value.error field will match this return value.
* @return A jwt_value_error_t value, JWT_VALUE_ERR_NONE being success.
*/
JWT_EXPORT
jwt_value_error_t jwt_grant_del(jwt_t *jwt, const char *header);
Expand All @@ -692,8 +698,8 @@ jwt_value_error_t jwt_grant_del(jwt_t *jwt, const char *header);
* @param __n Name of the value
* @return No return value
*/
#define jwt_set_GET_INT(__v, __n) ({ \
(__v)->type=JWT_VALUE_INT;(__v)->replace=0; \
#define jwt_set_GET_INT(__v, __n) ({ \
(__v)->type=JWT_VALUE_INT; \
(__v)->name=(__n);(__v)->int_val=0;(__v)->error=0;})

/**
Expand All @@ -703,8 +709,8 @@ jwt_value_error_t jwt_grant_del(jwt_t *jwt, const char *header);
* @param __n Name of the value
* @return No return value
*/
#define jwt_set_GET_STR(__v, __n) ({ \
(__v)->type=JWT_VALUE_STR;(__v)->replace=0; \
#define jwt_set_GET_STR(__v, __n) ({ \
(__v)->type=JWT_VALUE_STR; \
(__v)->name=(__n);(__v)->str_val=NULL;(__v)->error=0;})

/**
Expand All @@ -714,8 +720,8 @@ jwt_value_error_t jwt_grant_del(jwt_t *jwt, const char *header);
* @param __n Name of the value
* @return No return value
*/
#define jwt_set_GET_BOOL(__v, __n) ({ \
(__v)->type=JWT_VALUE_BOOL;(__v)->replace=0; \
#define jwt_set_GET_BOOL(__v, __n) ({ \
(__v)->type=JWT_VALUE_BOOL; \
(__v)->name=(__n);(__v)->bool_val=0;(__v)->error=0;})

/**
Expand All @@ -726,7 +732,7 @@ jwt_value_error_t jwt_grant_del(jwt_t *jwt, const char *header);
* @return No return value
*/
#define jwt_set_GET_JSON(__v, __n) ({ \
(__v)->type=JWT_VALUE_JSON;(__v)->replace=0; \
(__v)->type=JWT_VALUE_JSON;(__v)->pretty=0; \
(__v)->name=(__n);(__v)->json_val=NULL;(__v)->error=0;})

/**
Expand Down Expand Up @@ -789,62 +795,6 @@ jwt_value_error_t jwt_grant_del(jwt_t *jwt, const char *header);
* @{
*/

/**
* Output plain text representation to a FILE pointer.
*
* This function will write a JSON string representation of this JWT object
* without Base64 encoding. This only writes the header and body, and does
* not compute the signature or encryption (if such an algorithm were being
* used).
*
* @note This is only useful for debugging and is not intended to be used for
* encoding an actual JWT.
*
* @param jwt Pointer to a JWT object.
* @param fp Valid FILE pointer to write data to.
* @param pretty Enables better visual formatting of output. Generally only
* used for debugging.
* @return Returns 0 on success, valid errno otherwise.
*/
JWT_EXPORT
int jwt_dump_fp(jwt_t *jwt, FILE *fp, int pretty);

/**
* Return plain text representation as a string.
*
* Similar to jwt_dump_fp() except that a string is returned. The string
* must be freed by the caller.
*
* @note This is only useful for debugging and is not intended to be used for
* encoding an actual JWT.
*
* @param jwt Pointer to a JWT object.
* @param pretty Enables better visual formatting of output. Generally only
* used for debugging.
* @return A nul terminated string on success, NULL on error with errno
* set appropriately.
*/
JWT_EXPORT
char *jwt_dump_str(jwt_t *jwt, int pretty);

/**
* Return plain text representation of grants as a string.
*
* Similar to jwt_dump_str() except that only a string containing the
* grants string is returned. The string must be freed by the caller.
*
* @note This is only useful for debugging and is not intended to be used for
* encoding an actual JWT.
*
* @param jwt Pointer to a JWT object.
* @param pretty Enables better visual formatting of output. Generally only
* used for debugging.
* @return A nul terminated string on success, NULL on error with errno
* set appropriately.
*/
JWT_EXPORT
char *jwt_dump_grants_str(jwt_t *jwt, int pretty);

/**
* Fully encode a JWT object and write it to FILE.
*
Expand Down
51 changes: 0 additions & 51 deletions libjwt/jwt-encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,57 +76,6 @@ static int jwt_write_body(jwt_t *jwt, char **buf, int pretty)
return write_js(jwt->grants, buf, pretty);
}

static int jwt_dump(jwt_t *jwt, char **buf, int pretty)
{
int ret;

ret = jwt_write_head(jwt, buf, pretty);

if (ret == 0)
ret = __append_str(buf, ".");

if (ret == 0)
ret = jwt_write_body(jwt, buf, pretty);

return ret;
}

char *jwt_dump_grants_str(jwt_t *jwt, int pretty)
{
char *out = NULL;

errno = jwt_write_body(jwt, &out, pretty);

if (errno)
jwt_freemem(out);

return out;
}

int jwt_dump_fp(jwt_t *jwt, FILE *fp, int pretty)
{
char_auto *out = NULL;

errno = jwt_dump(jwt, &out, pretty);

if (errno == 0)
fputs(out, fp);

return errno;
}

char *jwt_dump_str(jwt_t *jwt, int pretty)
{
char *out = NULL;

errno = jwt_dump(jwt, &out, pretty);

if (errno)
jwt_freemem(out);

return out;
}

static int jwt_encode(jwt_t *jwt, char **out)
{
char_auto *head = NULL, *body = NULL, *sig = NULL;
Expand Down
9 changes: 7 additions & 2 deletions libjwt/jwt-setget.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ static jwt_value_error_t jwt_get_json(const jwt_t *jwt, json_t *which,
jwt_value_t *jval)
{
json_t *json_val = NULL;
size_t flags = JSON_SORT_KEYS | JSON_ENCODE_ANY;

if (jval->pretty)
flags |= JSON_INDENT(4);
else
flags |= JSON_COMPACT;

if (jval->name && strlen(jval->name))
json_val = json_object_get(which, jval->name);
Expand All @@ -85,8 +91,7 @@ static jwt_value_error_t jwt_get_json(const jwt_t *jwt, json_t *which,
if (json_val == NULL)
return jval->error = JWT_VALUE_ERR_NOEXIST;

jval->json_val = json_dumps(json_val, JSON_SORT_KEYS |
JSON_COMPACT | JSON_ENCODE_ANY);
jval->json_val = json_dumps(json_val, flags);
if (jval->json_val == NULL)
jval->error = JWT_VALUE_ERR_NOMEM;

Expand Down
Loading

0 comments on commit c7bc8f5

Please sign in to comment.