jsmn_token_init

Synopsis

int jsmn_token_init(
    jsmn_token_encode_s* t,
    JSMN_ALG alg,
    const char* claims,
    ...);

Convert JSON data into a signed encoded JWT object in dot notation format. Internally this method will populate a JSON header "{\"alg\":\"%s\",\"typ\":\"JWT\"}" where %s is determined by the algorithm argument. Then, a string of JSON “claims” is prepended to the header.

Warning

Your “claims” is expected to be a valid JSON string, and validation is not performed for the caller. Caller should treat the last two arguments as common for typical printf or snprintf family of functions. Internally this routine will use snprintf to build your JSON payload. If the result of your snprintf routine is not a valid JSON string then you will have a properly encoded and signed token that will not parse! (AKA a junky token) =P. This is not a hand holding library but a tiny library for constrained enviorments.

Parameters

Parameter Description
jsmn_token_encode_s *token A context to manage encoder state
JSMN_ALG alg An algorithm for to sign token with
const char *claims format string JSON encoded format string
Arguments to format string variadic arguments to the format string

Examples

int err;
jsmn_token_encode_s token;

// Create token
err = jsmn_token_init(&token, JSMN_ALG_HS256, "{\"sub\":\"%s\",\"iat\":%d}", "user", time(NULL));

See Also

  1. jsmn_token_encode_s
  2. JSMN_ALG