AutoIt
AutoIt
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 AutoIt Downloads
Local $bSuccess = False
; 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 = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oJwk.UpdateString("kty","EC")
$bSuccess = $oJwk.UpdateString("crv","P-256")
$bSuccess = $oJwk.UpdateString("x","...")
$bSuccess = $oJwk.UpdateString("y","...")
$bSuccess = $oJwk.UpdateString("d","...")
$oEccKey = ObjCreate("Chilkat.PrivateKey")
$bSuccess = $oEccKey.LoadJwk($oJwk.Emit())
If ($bSuccess = False) Then
ConsoleWrite($oEccKey.LastErrorText & @CRLF)
Exit
EndIf
$oJwt = ObjCreate("Chilkat.Jwt")
; Build the JOSE header
$oJose = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oJose.AppendString("format","compact")
$bSuccess = $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 = ObjCreate("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
Local $iCurDateTime = $oJwt.GenNumericDate(-60)
$bSuccess = $oClaims.AddIntAt(-1,"iat",$iCurDateTime)
; Set the "not process before" timestamp to now minus 60 seconds
$bSuccess = $oClaims.AddIntAt(-1,"nbf",$iCurDateTime)
; Set the timestamp defining an expiration time (end time) for the token
; to be now + 1 hour (3600 seconds)
$bSuccess = $oClaims.AddIntAt(-1,"exp",$iCurDateTime + 3600)
$oClaims.AppendString("aud","usr")
; Produce the smallest possible JWT:
$oJwt.AutoCompact = True
; Create the JWT token. This is where the RSA signature is created.
Local $sJwt_token = $oJwt.CreateJwtPk($oJose.Emit(),$oClaims.Emit(),$oEccKey)
ConsoleWrite($sJwt_token & @CRLF)
; Send the HTTPS GET with the jwt_token used for Authorization.
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.AuthToken = $sJwt_token
Local $sResponseStr = $oHttp.QuickGetStr("https://bitzlato.com/api/auth/whoami")
If ($oHttp.LastMethodSuccess = False) Then
ConsoleWrite($oHttp.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("status code = " & $oHttp.LastStatus & @CRLF)
ConsoleWrite($sResponseStr & @CRLF)