Jwt C Reference Documentation

Jwt

Current Version: 10.0.0

A class for signing and verifying JWT's (JSON Web Tokens).

Create/Dispose

HCkJwt instance = CkJwt_Create();
// ...
CkJwt_Dispose(instance);
HCkJwt CkJwt_Create(void);

Creates an instance of the HCkJwt object and returns a handle ("void *" pointer). The handle is passed in the 1st argument for the functions listed on this page.

void CkJwt_Dispose(HCkJwt handle);

Objects created by calling CkJwt_Create must be freed by calling this method. A memory leak occurs if a handle is not disposed by calling this function. Also, any handle returned by a Chilkat "C" function must also be freed by the application by calling the appropriate Dispose method, such as CkJwt_Dispose.

Properties

AutoCompact
BOOL CkJwt_getAutoCompact(HCkJwt cHandle);
void CkJwt_putAutoCompact(HCkJwt cHandle, BOOL newVal);
Introduced in version 9.5.0.58

If TRUE, the JSON passed to CreateJwt and CreateJwtPk will be compacted to remove unnecessary whitespace. This will result in the smallest possible JWT. The default value is TRUE.

top
DebugLogFilePath
void CkJwt_getDebugLogFilePath(HCkJwt cHandle, HCkString retval);
void CkJwt_putDebugLogFilePath(HCkJwt cHandle, const char *newVal);
const char *CkJwt_debugLogFilePath(HCkJwt cHandle);

If set to a file path, causes each Chilkat method or property call to automatically append it's LastErrorText to the specified log file. The information is appended such that if a hang or crash occurs, it is possible to see the context in which the problem occurred, as well as a history of all Chilkat calls up to the point of the problem. The VerboseLogging property can be set to provide more detailed information.

This property is typically used for debugging the rare cases where a Chilkat method call hangs or generates an exception that halts program execution (i.e. crashes). A hang or crash should generally never happen. The typical causes of a hang are:

  1. a timeout related property was set to 0 to explicitly indicate that an infinite timeout is desired,
  2. the hang is actually a hang within an event callback (i.e. it is a hang within the application code), or
  3. there is an internal problem (bug) in the Chilkat code that causes the hang.

More Information and Examples
top
LastErrorHtml
void CkJwt_getLastErrorHtml(HCkJwt cHandle, HCkString retval);
const char *CkJwt_lastErrorHtml(HCkJwt cHandle);

Provides information in HTML format about the last method/property called. If a method call returns a value indicating failure, or behaves unexpectedly, examine this property to get more information.

top
LastErrorText
void CkJwt_getLastErrorText(HCkJwt cHandle, HCkString retval);
const char *CkJwt_lastErrorText(HCkJwt cHandle);

Provides information in plain-text format about the last method/property called. If a method call returns a value indicating failure, or behaves unexpectedly, examine this property to get more information.

top
LastErrorXml
void CkJwt_getLastErrorXml(HCkJwt cHandle, HCkString retval);
const char *CkJwt_lastErrorXml(HCkJwt cHandle);

Provides information in XML format about the last method/property called. If a method call returns a value indicating failure, or behaves unexpectedly, examine this property to get more information.

top
LastMethodSuccess
BOOL CkJwt_getLastMethodSuccess(HCkJwt cHandle);
void CkJwt_putLastMethodSuccess(HCkJwt cHandle, BOOL newVal);

Indicate whether the last method call succeeded or failed. A value of TRUE indicates success, a value of FALSE indicates failure. This property is automatically set for method calls. It is not modified by property accesses. The property is automatically set to indicate success for the following types of method calls:

  • Any method that returns a string.
  • Any method returning a Chilkat object, binary bytes, or a date/time.
  • Any method returning a standard boolean status value where success = TRUE and failure = FALSE.
  • Any method returning an integer where failure is defined by a return value less than zero.

Note: Methods that do not fit the above requirements will always set this property equal to TRUE. For example, a method that returns no value (such as a "void" in C++) will technically always succeed.

top
Utf8
BOOL CkJwt_getUtf8(HCkJwt cHandle);
void CkJwt_putUtf8(HCkJwt cHandle, BOOL newVal);

When set to TRUE, all "const char *" arguments are interpreted as utf-8 strings. If set to FALSE (the default), then "const char *" arguments are interpreted as ANSI strings. Also, when set to TRUE, and Chilkat method returning a "const char *" is returning the utf-8 representation. If set to FALSE, all "const char *" return values are ANSI strings.

top
VerboseLogging
BOOL CkJwt_getVerboseLogging(HCkJwt cHandle);
void CkJwt_putVerboseLogging(HCkJwt cHandle, BOOL newVal);

If set to TRUE, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is FALSE. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.

top
Version
void CkJwt_getVersion(HCkJwt cHandle, HCkString retval);
const char *CkJwt_version(HCkJwt cHandle);

Version of the component/library, such as "9.5.0.94"

More Information and Examples
top

Methods

CreateJwt
BOOL CkJwt_CreateJwt(HCkJwt cHandle, const char *header, const char *payload, const char *password, HCkString outStr);
const char *CkJwt_createJwt(HCkJwt cHandle, const char *header, const char *payload, const char *password);
Introduced in version 9.5.0.58

Creates a JWT. The header is the JOSE JSON header. It can be the full JOSE JSON, or it can be a shorthand string such as "HS256", "HS384", or "HS512", in which case the standard JOSE header for the given algorithm will be used.

The payload is the JSON payload that contains the claims. The password is the secret. Given that the secret is a shared passwod string, this method should only be called for creating JWT's where the JOSE header's "alg" is HS256, HS384, or HS512. For RS256, RS384, RS512, ES256, ES384, and ES512, call CreateJwtPk instead.

When successful, this method returns a JWT with the format xxxxx.yyyyy.zzzzz, where xxxxx is the base64url encoded JOSE header, yyyyy is the base64url encoded payload, and zzzzz is the base64url signature.

Returns TRUE for success, FALSE for failure.

top
CreateJwtCert
BOOL CkJwt_CreateJwtCert(HCkJwt cHandle, const char *header, const char *payload, HCkCert cert, HCkString outStr);
const char *CkJwt_createJwtCert(HCkJwt cHandle, const char *header, const char *payload, HCkCert cert);
Introduced in version 9.5.0.99

Creates a JWT using a certificate's private key. The header is the JOSE JSON header. It can be the full JOSE JSON, or it can be a shorthand string such as "RS256", "RS384", "RS512", "ES256", "ES384", or "ES512", in which case the standard JOSE header for the given algorithm will be used.

The payload is the JSON payload that contains the claims. The cert is the certificate with private key. This method should only be called for creating JWT's where the JOSE header's "alg" is RS256, RS384, RS512, ES256, ES384, and ES512. If the secret is a shared password string, then call CreateJwt instead.

When successful, this method returns a JWT with the format xxxxx.yyyyy.zzzzz, where xxxxx is the base64url encoded JOSE header, yyyyy is the base64url encoded payload, and zzzzz is the base64url signature.

Returns TRUE for success, FALSE for failure.

top
CreateJwtPk
BOOL CkJwt_CreateJwtPk(HCkJwt cHandle, const char *header, const char *payload, HCkPrivateKey key, HCkString outStr);
const char *CkJwt_createJwtPk(HCkJwt cHandle, const char *header, const char *payload, HCkPrivateKey key);
Introduced in version 9.5.0.58

Creates a JWT using an RSA or ECC private key. The header is the JOSE JSON header. It can be the full JOSE JSON, or it can be a shorthand string such as "RS256", "RS384", "RS512", "ES256", "ES384", or "ES512", in which case the standard JOSE header for the given algorithm will be used.

The payload is the JSON payload that contains the claims. The key is the private key. This method should only be called for creating JWT's where the JOSE header's "alg" is RS256, RS384, RS512, ES256, ES384, and ES512. If the secret is a shared password string, then call CreateJwt instead.

When successful, this method returns a JWT with the format xxxxx.yyyyy.zzzzz, where xxxxx is the base64url encoded JOSE header, yyyyy is the base64url encoded payload, and zzzzz is the base64url signature.

Returns TRUE for success, FALSE for failure.

top
GenNumericDate
int CkJwt_GenNumericDate(HCkJwt cHandle, int numSecOffset);
Introduced in version 9.5.0.58

Generates a JSON numeric value representing the number of seconds from 1970-01-01T00:00:00Z UTC until the specified UTC date/time, ignoring leap seconds. The date/time generated is equal to the current system time plus the number of seconds specified by numSecOffset. The numSecOffset can be negative.

More Information and Examples
top
GetHeader
BOOL CkJwt_GetHeader(HCkJwt cHandle, const char *token, HCkString outStr);
const char *CkJwt_getHeader(HCkJwt cHandle, const char *token);
Introduced in version 9.5.0.58

Decodes the first part of a JWT (the "xxxxx" part of the "xxxxx.yyyyy.zzzzz" JWT) and returns the JSON string. This is the JOSE header of the JWT.

Returns TRUE for success, FALSE for failure.

More Information and Examples
top
GetPayload
BOOL CkJwt_GetPayload(HCkJwt cHandle, const char *token, HCkString outStr);
const char *CkJwt_getPayload(HCkJwt cHandle, const char *token);
Introduced in version 9.5.0.58

Decodes the second part of a JWT (the "yyyyy" part of the "xxxxx.yyyyy.zzzzz" JWT) and returns the JSON string. This is the claims payload of the JWT.

Returns TRUE for success, FALSE for failure.

top
IsTimeValid
BOOL CkJwt_IsTimeValid(HCkJwt cHandle, const char *jwt, int leeway);
Introduced in version 9.5.0.58

Verifies the "exp" and/or "nbf" claims and returns TRUE if the current system date/time is within range. Returns FALSE if the current system date/time is outside the allowed range of time. The leeway may be set to a non-zero number of seconds to allow for some small leeway (usually no more than a few minutes) to account for clock skew.

More Information and Examples
top
VerifyJwt
BOOL CkJwt_VerifyJwt(HCkJwt cHandle, const char *token, const char *password);
Introduced in version 9.5.0.58

Verifies a JWT that requires a shared password string for verification. The token should be a JWT with the format xxxxx.yyyyy.zzzzz. This method should only be called for JWT's using the HS256, HS384, or HS512 algorithms. The VerifyJwtPk method should be called for verifying JWT's requiring an RSA or ECC key.

Returns TRUE if the signature was verified. Returns FALSE if the signature was not successfully verified.

Note: This method will return FALSE if the "alg" in the JOSE header is anything other than the algorithms specifically for HMAC, namely "hs256, "hs384", and "hs512". For example, if the "alg" is "none", then this method immediately returns a failed status.

Further Explanation: This method calculates the signature using the password provided by the application, and compares it against the signature found in the JWT. If the signatures are equal, then the password is correct, and TRUE is returned.

Returns TRUE for success, FALSE for failure.

More Information and Examples
top
VerifyJwtPk
BOOL CkJwt_VerifyJwtPk(HCkJwt cHandle, const char *token, HCkPublicKey key);
Introduced in version 9.5.0.58

Verifies a JWT that requires an RSA or ECC public key for verification. The token should be a JWT with the format xxxxx.yyyyy.zzzzz. This method should only be called for JWT's using the RS256, RS384, RS512, ES256, ES384, or ES512 algorithms.

Returns TRUE if the signature was verified. Returns FALSE if the signature was not successfully verified.

Note: This method will return FALSE if the "alg" in the JOSE header is anything other than the algorithms specifically for RSA and ECC. For example, if the "alg" is "none", then this method immediately returns a failed status.

Further Explanation: This method calculates the signature using the key provided by the application, and compares it against the signature found in the JWT. If the signatures are equal, then the key corresponds to the private key used to sign, and TRUE is returned.

Returns TRUE for success, FALSE for failure.

top