Jwt Tcl Reference Documentation

Jwt

Current Version: 10.1.2

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

Object Creation

set myJwt [new CkJwt]

Properties

AutoCompact
boolean 1/0
set boolVal [CkJwt_get_AutoCompact $myJwt]
CkJwt_put_AutoCompact $myJwt $boolVal
Introduced in version 9.5.0.58

If 1, 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 1.

top
DebugLogFilePath
string
# ckStr is a CkString
CkJwt_get_DebugLogFilePath $myJwt $ckStr
set strVal [CkJwt_get_debugLogFilePath $myJwt]
CkJwt_put_DebugLogFilePath $myJwt $strVal

If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.

Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.

Possible causes of hangs include:

  • A timeout property set to 0, indicating an infinite timeout.
  • A hang occurring within an event callback in the application code.
  • An internal bug in the Chilkat code causing the hang.

More Information and Examples
top
LastErrorHtml
string
# ckStr is a CkString
CkJwt_get_LastErrorHtml $myJwt $ckStr
set strVal [CkJwt_get_lastErrorHtml $myJwt]

Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorText
string
# ckStr is a CkString
CkJwt_get_LastErrorText $myJwt $ckStr
set strVal [CkJwt_get_lastErrorText $myJwt]

Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorXml
string
# ckStr is a CkString
CkJwt_get_LastErrorXml $myJwt $ckStr
set strVal [CkJwt_get_lastErrorXml $myJwt]

Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastMethodSuccess
boolean 1/0
set boolVal [CkJwt_get_LastMethodSuccess $myJwt]
CkJwt_put_LastMethodSuccess $myJwt $boolVal

Indicates the success or failure of the most recent method call: 1 means success, 0 means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages.

top
UncommonOptions
string
# ckStr is a CkString
CkJwt_get_UncommonOptions $myJwt $ckStr
set strVal [CkJwt_get_uncommonOptions $myJwt]
CkJwt_put_UncommonOptions $myJwt $strVal
Introduced in version 10.1.0

This is a catch-all property to be used for uncommon needs. This property defaults to the empty string and should typically remain empty.

top
Utf8
boolean 1/0
set boolVal [CkJwt_get_Utf8 $myJwt]
CkJwt_put_Utf8 $myJwt $boolVal

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

top
VerboseLogging
boolean 1/0
set boolVal [CkJwt_get_VerboseLogging $myJwt]
CkJwt_put_VerboseLogging $myJwt $boolVal

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

top
Version
string
# ckStr is a CkString
CkJwt_get_Version $myJwt $ckStr
set strVal [CkJwt_get_version $myJwt]

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

More Information and Examples
top

Methods

CreateJwt
# header is a string
# payload is a string
# password is a string
# outStr is a CkString (output)
set status [CkJwt_CreateJwt $header $payload $password $outStr]
set retStr [CkJwt_createJwt $myJwt $header $payload $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 1 for success, 0 for failure.

top
CreateJwtCert
# header is a string
# payload is a string
# cert is a CkCert
# outStr is a CkString (output)
set status [CkJwt_CreateJwtCert $header $payload $cert $outStr]
set retStr [CkJwt_createJwtCert $myJwt $header $payload $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", "PS256", "PS384", "PS512", "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, PS256, PS384, PS512, ES256, ES384, or 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.

Note: Support for the PS256, PS384, PS512 algorithms was added in Chilkat v10.0.0.

Returns 1 for success, 0 for failure.

More Information and Examples
top
CreateJwtPk
# header is a string
# payload is a string
# key is a CkPrivateKey
# outStr is a CkString (output)
set status [CkJwt_CreateJwtPk $header $payload $key $outStr]
set retStr [CkJwt_createJwtPk $myJwt $header $payload $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", "PS256", "PS384", "PS512", "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, PS256, PS384, PS512, ES256, ES384, or 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.

Note: Support for the PS256, PS384, PS512 algorithms was added in Chilkat v10.0.0.

Returns 1 for success, 0 for failure.

top
GenNumericDate
# numSecOffset is an integer
set retInt [CkJwt_GenNumericDate $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
# token is a string
# outStr is a CkString (output)
set status [CkJwt_GetHeader $token $outStr]
set retStr [CkJwt_getHeader $myJwt $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 1 for success, 0 for failure.

More Information and Examples
top
GetPayload
# token is a string
# outStr is a CkString (output)
set status [CkJwt_GetPayload $token $outStr]
set retStr [CkJwt_getPayload $myJwt $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 1 for success, 0 for failure.

top
IsTimeValid
# jwt is a string
# leeway is an integer
set retBool [CkJwt_IsTimeValid $jwt $leeway]
Introduced in version 9.5.0.58

Verifies the "exp" and/or "nbf" claims and returns 1 if the current system date/time is within range. Returns 0 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
# token is a string
# password is a string
set status [CkJwt_VerifyJwt $token $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 1 if the signature was verified. Returns 0 if the signature was not successfully verified.

Note: This method will return 0 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 1 is returned.

Returns 1 for success, 0 for failure.

More Information and Examples
top
VerifyJwtPk
# token is a string
# key is a CkPublicKey
set status [CkJwt_VerifyJwtPk $token $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 1 if the signature was verified. Returns 0 if the signature was not successfully verified.

Note: This method will return 0 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 1 is returned.

Returns 1 for success, 0 for failure.

top