PowerBuilder
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Jwk
oleobject loo_EccKey
oleobject loo_Jwt
oleobject loo_Jose
oleobject loo_Claims
integer li_CurDateTime
string ls_Jwt_token
oleobject loo_Http
string ls_ResponseStr
li_Success = 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.
loo_Jwk = create oleobject
li_rc = loo_Jwk.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
destroy loo_Jwk
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_Jwk.UpdateString("kty","EC")
li_Success = loo_Jwk.UpdateString("crv","P-256")
li_Success = loo_Jwk.UpdateString("x","...")
li_Success = loo_Jwk.UpdateString("y","...")
li_Success = loo_Jwk.UpdateString("d","...")
loo_EccKey = create oleobject
li_rc = loo_EccKey.ConnectToNewObject("Chilkat.PrivateKey")
li_Success = loo_EccKey.LoadJwk(loo_Jwk.Emit())
if li_Success = 0 then
Write-Debug loo_EccKey.LastErrorText
destroy loo_Jwk
destroy loo_EccKey
return
end if
loo_Jwt = create oleobject
li_rc = loo_Jwt.ConnectToNewObject("Chilkat.Jwt")
// Build the JOSE header
loo_Jose = create oleobject
li_rc = loo_Jose.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Jose.AppendString("format","compact")
li_Success = loo_Jose.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"
// }
loo_Claims = create oleobject
li_rc = loo_Claims.ConnectToNewObject("Chilkat.JsonObject")
loo_Claims.AppendString("jti","555D9123")
loo_Claims.AppendString("email","your_email@example.com")
// Set the timestamp of when the JWT was created to now minus 60 seconds
li_CurDateTime = loo_Jwt.GenNumericDate(-60)
li_Success = loo_Claims.AddIntAt(-1,"iat",li_CurDateTime)
// Set the "not process before" timestamp to now minus 60 seconds
li_Success = loo_Claims.AddIntAt(-1,"nbf",li_CurDateTime)
// Set the timestamp defining an expiration time (end time) for the token
// to be now + 1 hour (3600 seconds)
li_Success = loo_Claims.AddIntAt(-1,"exp",li_CurDateTime + 3600)
loo_Claims.AppendString("aud","usr")
// Produce the smallest possible JWT:
loo_Jwt.AutoCompact = 1
// Create the JWT token. This is where the RSA signature is created.
ls_Jwt_token = loo_Jwt.CreateJwtPk(loo_Jose.Emit(),loo_Claims.Emit(),loo_EccKey)
Write-Debug ls_Jwt_token
// Send the HTTPS GET with the jwt_token used for Authorization.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
loo_Http.AuthToken = ls_Jwt_token
ls_ResponseStr = loo_Http.QuickGetStr("https://bitzlato.com/api/auth/whoami")
if loo_Http.LastMethodSuccess = 0 then
Write-Debug loo_Http.LastErrorText
destroy loo_Jwk
destroy loo_EccKey
destroy loo_Jwt
destroy loo_Jose
destroy loo_Claims
destroy loo_Http
return
end if
Write-Debug "status code = " + string(loo_Http.LastStatus)
Write-Debug ls_ResponseStr
destroy loo_Jwk
destroy loo_EccKey
destroy loo_Jwt
destroy loo_Jose
destroy loo_Claims
destroy loo_Http