Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoJwk
    Variant vEccKey
    Handle hoEccKey
    Handle hoJwt
    Handle hoJose
    Handle hoClaims
    Integer iCurDateTime
    String sJwt_token
    Handle hoHttp
    String sResponseStr
    String sTemp1
    String sTemp2
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    // 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.
    Get Create (RefClass(cComChilkatJsonObject)) To hoJwk
    If (Not(IsComObjectCreated(hoJwk))) Begin
        Send CreateComObject of hoJwk
    End
    Get ComUpdateString Of hoJwk "kty" "EC" To iSuccess
    Get ComUpdateString Of hoJwk "crv" "P-256" To iSuccess
    Get ComUpdateString Of hoJwk "x" "..." To iSuccess
    Get ComUpdateString Of hoJwk "y" "..." To iSuccess
    Get ComUpdateString Of hoJwk "d" "..." To iSuccess

    Get Create (RefClass(cComChilkatPrivateKey)) To hoEccKey
    If (Not(IsComObjectCreated(hoEccKey))) Begin
        Send CreateComObject of hoEccKey
    End
    Get ComEmit Of hoJwk To sTemp1
    Get ComLoadJwk Of hoEccKey sTemp1 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEccKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJwt)) To hoJwt
    If (Not(IsComObjectCreated(hoJwt))) Begin
        Send CreateComObject of hoJwt
    End

    // Build the JOSE header
    Get Create (RefClass(cComChilkatJsonObject)) To hoJose
    If (Not(IsComObjectCreated(hoJose))) Begin
        Send CreateComObject of hoJose
    End
    Get ComAppendString Of hoJose "format" "compact" To iSuccess
    Get ComAppendString Of hoJose "alg" "ES256" To iSuccess

    // 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"
    // 	}

    Get Create (RefClass(cComChilkatJsonObject)) To hoClaims
    If (Not(IsComObjectCreated(hoClaims))) Begin
        Send CreateComObject of hoClaims
    End
    Get ComAppendString Of hoClaims "jti" "555D9123" To iSuccess
    Get ComAppendString Of hoClaims "email" "your_email@example.com" To iSuccess

    // Set the timestamp of when the JWT was created to now minus 60 seconds
    Get ComGenNumericDate Of hoJwt -60 To iCurDateTime
    Get ComAddIntAt Of hoClaims -1 "iat" iCurDateTime To iSuccess

    // Set the "not process before" timestamp to now minus 60 seconds
    Get ComAddIntAt Of hoClaims -1 "nbf" iCurDateTime To iSuccess

    // Set the timestamp defining an expiration time (end time) for the token
    // to be now + 1 hour (3600 seconds)
    Get ComAddIntAt Of hoClaims -1 "exp" (iCurDateTime + 3600) To iSuccess

    Get ComAppendString Of hoClaims "aud" "usr" To iSuccess

    // Produce the smallest possible JWT:
    Set ComAutoCompact Of hoJwt To True

    // Create the JWT token.  This is where the RSA signature is created.
    Get ComEmit Of hoJose To sTemp1
    Get ComEmit Of hoClaims To sTemp2
    Get pvComObject of hoEccKey to vEccKey
    Get ComCreateJwtPk Of hoJwt sTemp1 sTemp2 vEccKey To sJwt_token

    Showln sJwt_token

    // Send the HTTPS GET with the jwt_token used for Authorization.
    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Set ComAuthToken Of hoHttp To sJwt_token
    Get ComQuickGetStr Of hoHttp "https://bitzlato.com/api/auth/whoami" To sResponseStr
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComLastStatus Of hoHttp To iTemp1
    Showln "status code = " iTemp1
    Showln sResponseStr


End_Procedure