PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkHttp.pb"
IncludeFile "CkJwt.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkPrivateKey.pb"
Procedure ChilkatExample()
success.i = 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.
jwk.i = CkJsonObject::ckCreate()
If jwk.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckUpdateString(jwk,"kty","EC")
success = CkJsonObject::ckUpdateString(jwk,"crv","P-256")
success = CkJsonObject::ckUpdateString(jwk,"x","...")
success = CkJsonObject::ckUpdateString(jwk,"y","...")
success = CkJsonObject::ckUpdateString(jwk,"d","...")
eccKey.i = CkPrivateKey::ckCreate()
If eccKey.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPrivateKey::ckLoadJwk(eccKey,CkJsonObject::ckEmit(jwk))
If success = 0
Debug CkPrivateKey::ckLastErrorText(eccKey)
CkJsonObject::ckDispose(jwk)
CkPrivateKey::ckDispose(eccKey)
ProcedureReturn
EndIf
jwt.i = CkJwt::ckCreate()
If jwt.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Build the JOSE header
jose.i = CkJsonObject::ckCreate()
If jose.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckAppendString(jose,"format","compact")
success = CkJsonObject::ckAppendString(jose,"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"
; }
claims.i = CkJsonObject::ckCreate()
If claims.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckAppendString(claims,"jti","555D9123")
CkJsonObject::ckAppendString(claims,"email","your_email@example.com")
; Set the timestamp of when the JWT was created to now minus 60 seconds
curDateTime.i = CkJwt::ckGenNumericDate(jwt,-60)
success = CkJsonObject::ckAddIntAt(claims,-1,"iat",curDateTime)
; Set the "not process before" timestamp to now minus 60 seconds
success = CkJsonObject::ckAddIntAt(claims,-1,"nbf",curDateTime)
; Set the timestamp defining an expiration time (end time) for the token
; to be now + 1 hour (3600 seconds)
success = CkJsonObject::ckAddIntAt(claims,-1,"exp",curDateTime + 3600)
CkJsonObject::ckAppendString(claims,"aud","usr")
; Produce the smallest possible JWT:
CkJwt::setCkAutoCompact(jwt, 1)
; Create the JWT token. This is where the RSA signature is created.
jwt_token.s = CkJwt::ckCreateJwtPk(jwt,CkJsonObject::ckEmit(jose),CkJsonObject::ckEmit(claims),eccKey)
Debug jwt_token
; Send the HTTPS GET with the jwt_token used for Authorization.
http.i = CkHttp::ckCreate()
If http.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttp::setCkAuthToken(http, jwt_token)
responseStr.s = CkHttp::ckQuickGetStr(http,"https://bitzlato.com/api/auth/whoami")
If CkHttp::ckLastMethodSuccess(http) = 0
Debug CkHttp::ckLastErrorText(http)
CkJsonObject::ckDispose(jwk)
CkPrivateKey::ckDispose(eccKey)
CkJwt::ckDispose(jwt)
CkJsonObject::ckDispose(jose)
CkJsonObject::ckDispose(claims)
CkHttp::ckDispose(http)
ProcedureReturn
EndIf
Debug "status code = " + Str(CkHttp::ckLastStatus(http))
Debug responseStr
CkJsonObject::ckDispose(jwk)
CkPrivateKey::ckDispose(eccKey)
CkJwt::ckDispose(jwt)
CkJsonObject::ckDispose(jose)
CkJsonObject::ckDispose(claims)
CkHttp::ckDispose(http)
ProcedureReturn
EndProcedure