Xbase++
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oJwk
LOCAL oEccKey
LOCAL oJwt
LOCAL oJose
LOCAL oClaims
LOCAL nCurDateTime
LOCAL cJwt_token
LOCAL oHttp
LOCAL cResponseStr
nSuccess := 0
// 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.
oJwk := CreateObject("Chilkat.JsonObject")
nSuccess := oJwk:UpdateString("kty", "EC")
nSuccess := oJwk:UpdateString("crv", "P-256")
nSuccess := oJwk:UpdateString("x", "...")
nSuccess := oJwk:UpdateString("y", "...")
nSuccess := oJwk:UpdateString("d", "...")
oEccKey := CreateObject("Chilkat.PrivateKey")
nSuccess := oEccKey:LoadJwk(oJwk:Emit())
IF (nSuccess == 0)
? oEccKey:LastErrorText
oJwk:destroy()
oEccKey:destroy()
RETURN
ENDIF
oJwt := CreateObject("Chilkat.Jwt")
// Build the JOSE header
oJose := CreateObject("Chilkat.JsonObject")
nSuccess := oJose:AppendString("format", "compact")
nSuccess := oJose: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"
// }
oClaims := CreateObject("Chilkat.JsonObject")
oClaims:AppendString("jti", "555D9123")
oClaims:AppendString("email", "your_email@example.com")
// Set the timestamp of when the JWT was created to now minus 60 seconds
nCurDateTime := oJwt:GenNumericDate(-60)
nSuccess := oClaims:AddIntAt(-1, "iat", nCurDateTime)
// Set the "not process before" timestamp to now minus 60 seconds
nSuccess := oClaims:AddIntAt(-1, "nbf", nCurDateTime)
// Set the timestamp defining an expiration time (end time) for the token
// to be now + 1 hour (3600 seconds)
nSuccess := oClaims:AddIntAt(-1, "exp", nCurDateTime + 3600)
oClaims:AppendString("aud", "usr")
// Produce the smallest possible JWT:
oJwt:AutoCompact := 1
// Create the JWT token. This is where the RSA signature is created.
cJwt_token := oJwt:CreateJwtPk(oJose:Emit(), oClaims:Emit(), oEccKey)
? cJwt_token
// Send the HTTPS GET with the jwt_token used for Authorization.
oHttp := CreateObject("Chilkat.Http")
oHttp:AuthToken := cJwt_token
cResponseStr := oHttp:QuickGetStr("https://bitzlato.com/api/auth/whoami")
IF (oHttp:LastMethodSuccess == 0)
? oHttp:LastErrorText
oJwk:destroy()
oEccKey:destroy()
oJwt:destroy()
oJose:destroy()
oClaims:destroy()
oHttp:destroy()
RETURN
ENDIF
? "status code = " + Str(oHttp:LastStatus)
? cResponseStr
oJwk:destroy()
oEccKey:destroy()
oJwt:destroy()
oJose:destroy()
oClaims:destroy()
oHttp:destroy()