jwt_builder_grp(3)
π jwt_builder_grp(3) β the Builder, bestie
okay so this whole page lives in the Library Functions Manual, and it's all about jwt_builder_grp, aka the Builder. no cap. let's get into it π
πΈ NAME
jwt_builder_grp β Builder. that's the whole name section, she's brief and she's iconic. π
β¨ SYNOPSIS
π Typedefs
typedef struct jwt_builder jwt_builder_tβ Opaque Builder Object.
π¦ Functions
jwt_builder_t * jwt_builder_new (void)β the function that spawns a fresh new builder instance β¨void jwt_builder_free (jwt_builder_t *builder)β frees a builder object you made earlier π§Ήint jwt_builder_error (const jwt_builder_t *builder)β checks the error state of a builder objectconst char * jwt_builder_error_msg (const jwt_builder_t *builder)β grabs the error message hanging out inside a builder objectvoid jwt_builder_error_clear (jwt_builder_t *builder)β clears the error state in a builder object π§Όint jwt_builder_setkey (jwt_builder_t *builder, const jwt_alg_t alg, const jwk_item_t *key)β sets a key and an algorithm for a builder πint jwt_builder_enable_iat (jwt_builder_t *builder, int enable)β sets whether the IssuedAt claim gets used on the builderint jwt_builder_setcb (jwt_builder_t *builder, jwt_callback_t cb, void *ctx)β sets a callback for generating tokensvoid * jwt_builder_getctx (jwt_builder_t *builder)β retrieves the callback context you set earlierchar * jwt_builder_generate (jwt_builder_t *builder)β generates a token πͺ
π Detailed Description
So making a JWT token is a whole multi-step thing, bestie, it's giving process. First up you create a jwt_builder_t object β think of it as a JWT factory π. Once it's configured, you use it to make tokens with pre-defined claims. that's the whole vibe.
β Typedef Documentation
typedef struct jwt_builder jwt_builder_t
Opaque Builder Object. she's opaque, she's mysterious, we don't peek inside. π«Ά
π₯ Function Documentation
int jwt_builder_enable_iat (jwt_builder_t * builder, int enable)
Set IssuedAt usage on builder. By default, the builder sets the iat claim on all the tokens it makes. You can flip that on or off with this function, whenever you feel like it. β¨
Parameters
- builder β Pointer to a builder object
- enable β
0to disable it, any other value to enable it
Returns
- Previous value (
0or1), or-1on error π
int jwt_builder_error (const jwt_builder_t * builder)
Checks error state of builder object. the little vibe check of the builder world π
Parameters
- builder β Pointer to a builder object
Returns
0if no errors exist, non-zero otherwise
void jwt_builder_error_clear (jwt_builder_t * builder)
Clear error state in a builder object. fresh start, clean slate, new me π§
Parameters
- builder β Pointer to a builder object
const char * jwt_builder_error_msg (const jwt_builder_t * builder)
Get the error message contained in a builder object.
Parameters
- builder β Pointer to a builder object
Returns
- Pointer to a string with the error message. It can be an empty string if there's no error. It never returns
NULL, so you're safe, don't stress. π
void jwt_builder_free (jwt_builder_t * builder)
Frees a previously created builder object. let her rest, she's done her job π
Parameters
- builder β Pointer to a builder object
char * jwt_builder_generate (jwt_builder_t * builder)
Generate a token. What this function does is spit out a string containing a JWT. A token is represented by 3 parts: header.payload.sig. Each part is Base64url Encoded. An example would be, and yes this is literally it:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3MzY0MzI0MzR9.iDn6N9JsAdUPF11ow0skIfc9eJc2wGRIq30RSRZ8_68
When decoded, the header and payload would look like this (excluding the signature block):
{"alg":"HS256","typ":"JWT"}.{"iat":1736432434}
If pretty printed:
{
"alg": "HS256",
"typ": "JWT"
}
.
{
"iat": 1736432434
}
The signature block is a cryptographic hash π. Its length and format depend on the algorithm being used.
A simple usage with no signature or payload would be:
jwt_builder_t *builder = NULL;
builder = jwt_builder_new();
if (builder) {
char *out = jwt_builder_generate(builder);
if (out) {
printf("%s\n", out);
free(out);
}
}
jwt_builder_free(builder);
Note π β If you set a callback for this builder, this is when it will be called.
Parameters
- builder β Pointer to a builder object
Returns
- A string containing a JWT. The caller is responsible for freeing the memory for this string. On error,
NULLis returned and the error is set in the builder object.
void * jwt_builder_getctx (jwt_builder_t * builder)
Retrieve the callback context that was previously set. This is useful for accessing the context that was previously passed in the setcb function. lowkey handy π«Ά
Parameters
- builder β Pointer to a builder object
Returns
- Pointer to the context or
NULL
jwt_builder_t * jwt_builder_new (void )
Function to create a new builder instance. newborn builder, so precious π
Returns
- Pointer to a builder object on success,
NULLon failure
int jwt_builder_setcb (jwt_builder_t * builder, jwt_callback_t cb, void * ctx)
Set a callback for generating tokens. When a token is being generated, this callback runs after jwt_t has been created, but before the token is encoded. During that window the callback can set, change, or remove claims and header attributes. It can also use the jwt_value_t structure to set a key and alg to use when signing the token. she's got range π
The ctx value is also passed to the callback as part of the jwt_value_t struct.
Note π β Calling this with aNULLcb param and a new ctx param after already setting the callback will let you update the ctx passed to the callback. Calling with both values asNULLdisables the callback completely.
Parameters
- builder β Pointer to a builder object
- cb β Pointer to a callback function
- ctx β Pointer to data to pass to the callback function
Returns
0on success, non-zero otherwise with error set in the builder
int jwt_builder_setkey (jwt_builder_t * builder, const jwt_alg_t alg, const jwk_item_t * key)
Sets a key and algorithm for a builder. The values here must make sense, bestie, we're not doing chaos today. This is what will or won't pass as far as algorithm matching between the alg param and the alg inside jwk_item_t. Where alg-A means one specific algorithm (not none) and alg-B represents another one (also not none). The none is used to mean no algorithm is set. NULL means the jwk_item_t pointer is NULL.
alg β jwt_item_t β Result
alg-Aβalg-Aβ βnoneβalg-Aβ βalg-Aβnoneβ βnoneβNULLβ β οΈalg-Aβalg-Bβ βalg-AβNULLβ β
Warning β οΈ β That warning row means an insecure token. Insecure tokens are not very useful and are strongly discouraged. do not do it, i am begging π
Parameters
- builder β Pointer to a builder object
- alg β A valid
jwt_alg_ttype - key β A JWK key object
Returns
0on success, non-zero otherwise with error set in the builder
π Author
Generated automatically by Doxygen for LibJWT from the source code.
and that's the whole page, bestie, you ate it π this was LibJWT, Version 3.3.3, page jwt_builder_grp(3) β go forth and build tokens, main character energy only π
π¦ where this page comes from
- ubuntu stonking: libjwt14-doc 3.3.3-1build1 Β· MPL-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms