Sample code for 30+ languages & platforms
Lianja

bitzlato.com whoami

See more JSON Web Token (JWT) Examples

Demonstrates sending a request to the bitzlato.com whoami endpoint using an ES256 JWT token for authentication.

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

//  Use the following ECC key loaded from JWK format.
loJwk = createobject("CkJsonObject")
llSuccess = loJwk.UpdateString("kty","EC")
llSuccess = loJwk.UpdateString("crv","P-256")
llSuccess = loJwk.UpdateString("x","...")
llSuccess = loJwk.UpdateString("y","...")
llSuccess = loJwk.UpdateString("d","...")

loEccKey = createobject("CkPrivateKey")
llSuccess = loEccKey.LoadJwk(loJwk.Emit())
if (llSuccess = .F.) then
    ? loEccKey.LastErrorText
    release loJwk
    release loEccKey
    return
endif

loJwt = createobject("CkJwt")

// Build the JOSE header
loJose = createobject("CkJsonObject")
llSuccess = loJose.AppendString("format","compact")
llSuccess = loJose.AppendString("alg","ES256")

// Now build the JWT claims (also known as the payload)

// Our JWT claims will contain members as shown here:

// 	{
// 	  "email":"your_email@example.com",
// 	  "aud":"usr",
// 	  "iat":"1588286154",
// 	  "jti":"555D9123"
// 	}

loClaims = createobject("CkJsonObject")
loClaims.AppendString("jti","555D9123")
loClaims.AppendString("email","your_email@example.com")

// Set the timestamp of when the JWT was created to now minus 60 seconds
lnCurDateTime = loJwt.GenNumericDate(-60)
llSuccess = loClaims.AddIntAt(-1,"iat",lnCurDateTime)

// Set the "not process before" timestamp to now minus 60 seconds
llSuccess = loClaims.AddIntAt(-1,"nbf",lnCurDateTime)

// Set the timestamp defining an expiration time (end time) for the token
// to be now + 1 hour (3600 seconds)
llSuccess = loClaims.AddIntAt(-1,"exp",lnCurDateTime + 3600)

loClaims.AppendString("aud","usr")

// Produce the smallest possible JWT:
loJwt.AutoCompact = .T.

// Create the JWT token.  This is where the RSA signature is created.
lcJwt_token = loJwt.CreateJwtPk(loJose.Emit(),loClaims.Emit(),loEccKey)

? lcJwt_token

// Send the HTTPS GET with the jwt_token used for Authorization.
loHttp = createobject("CkHttp")
loHttp.AuthToken = lcJwt_token
lcResponseStr = loHttp.QuickGetStr("https://bitzlato.com/api/auth/whoami")
if (loHttp.LastMethodSuccess = .F.) then
    ? loHttp.LastErrorText
    release loJwk
    release loEccKey
    release loJwt
    release loJose
    release loClaims
    release loHttp
    return
endif

? "status code = " + str(loHttp.LastStatus)
? lcResponseStr


release loJwk
release loEccKey
release loJwt
release loJose
release loClaims
release loHttp