Sample code for 30+ languages & platforms
Unicode C

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 Unicode C Downloads

Unicode C
#include <C_CkJsonObjectW.h>
#include <C_CkPrivateKeyW.h>
#include <C_CkJwtW.h>
#include <C_CkHttpW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObjectW jwk;
    HCkPrivateKeyW eccKey;
    HCkJwtW jwt;
    HCkJsonObjectW jose;
    HCkJsonObjectW claims;
    int curDateTime;
    const wchar_t *jwt_token;
    HCkHttpW http;
    const wchar_t *responseStr;

    success = 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.
    jwk = CkJsonObjectW_Create();
    success = CkJsonObjectW_UpdateString(jwk,L"kty",L"EC");
    success = CkJsonObjectW_UpdateString(jwk,L"crv",L"P-256");
    success = CkJsonObjectW_UpdateString(jwk,L"x",L"...");
    success = CkJsonObjectW_UpdateString(jwk,L"y",L"...");
    success = CkJsonObjectW_UpdateString(jwk,L"d",L"...");

    eccKey = CkPrivateKeyW_Create();
    success = CkPrivateKeyW_LoadJwk(eccKey,CkJsonObjectW_emit(jwk));
    if (success == FALSE) {
        wprintf(L"%s\n",CkPrivateKeyW_lastErrorText(eccKey));
        CkJsonObjectW_Dispose(jwk);
        CkPrivateKeyW_Dispose(eccKey);
        return;
    }

    jwt = CkJwtW_Create();

    // Build the JOSE header
    jose = CkJsonObjectW_Create();
    success = CkJsonObjectW_AppendString(jose,L"format",L"compact");
    success = CkJsonObjectW_AppendString(jose,L"alg",L"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 = CkJsonObjectW_Create();
    CkJsonObjectW_AppendString(claims,L"jti",L"555D9123");
    CkJsonObjectW_AppendString(claims,L"email",L"your_email@example.com");

    // Set the timestamp of when the JWT was created to now minus 60 seconds
    curDateTime = CkJwtW_GenNumericDate(jwt,-60);
    success = CkJsonObjectW_AddIntAt(claims,-1,L"iat",curDateTime);

    // Set the "not process before" timestamp to now minus 60 seconds
    success = CkJsonObjectW_AddIntAt(claims,-1,L"nbf",curDateTime);

    // Set the timestamp defining an expiration time (end time) for the token
    // to be now + 1 hour (3600 seconds)
    success = CkJsonObjectW_AddIntAt(claims,-1,L"exp",curDateTime + 3600);

    CkJsonObjectW_AppendString(claims,L"aud",L"usr");

    // Produce the smallest possible JWT:
    CkJwtW_putAutoCompact(jwt,TRUE);

    // Create the JWT token.  This is where the RSA signature is created.
    jwt_token = CkJwtW_createJwtPk(jwt,CkJsonObjectW_emit(jose),CkJsonObjectW_emit(claims),eccKey);

    wprintf(L"%s\n",jwt_token);

    // Send the HTTPS GET with the jwt_token used for Authorization.
    http = CkHttpW_Create();
    CkHttpW_putAuthToken(http,jwt_token);
    responseStr = CkHttpW_quickGetStr(http,L"https://bitzlato.com/api/auth/whoami");
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkJsonObjectW_Dispose(jwk);
        CkPrivateKeyW_Dispose(eccKey);
        CkJwtW_Dispose(jwt);
        CkJsonObjectW_Dispose(jose);
        CkJsonObjectW_Dispose(claims);
        CkHttpW_Dispose(http);
        return;
    }

    wprintf(L"status code = %d\n",CkHttpW_getLastStatus(http));
    wprintf(L"%s\n",responseStr);


    CkJsonObjectW_Dispose(jwk);
    CkPrivateKeyW_Dispose(eccKey);
    CkJwtW_Dispose(jwt);
    CkJsonObjectW_Dispose(jose);
    CkJsonObjectW_Dispose(claims);
    CkHttpW_Dispose(http);

    }