Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

ABN AMRO Create Signed JSON Web Token

See more ABN AMRO Examples

Demonstrates how to create a signed JWT to be used for authenticating requests to the ABN AMRO REST API's.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // Create public/private key pair (RSA)
    const rsa = try chilkat.Rsa.init();
    defer rsa.deinit();

    // Generate a 2048-bit key.
    const privkey = try chilkat.PrivateKey.init();
    defer privkey.deinit();
    rsa.genKey(2048, privkey) catch {
        std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
        return;
    };

    // Export the key to PEM files.
    // Write one PEM file for the private key, and one for the public key.
    try privkey.savePemFile("qa_data/pem/abnAmroPrivateKey.pem");

    const pubkey = try chilkat.PublicKey.init();
    defer pubkey.deinit();
    privkey.toPublicKey(pubkey) catch {};
    try pubkey.savePemFile(true, "qa_data/pem/abnAmroPublicKey.pem");
    // Note: Please share your public key along with your app name and developer email id at api.support@nl.abnamro.com.
    // Token generation will not work unless public key is associated with your app.

    // Create the JWT.
    const jwt = try chilkat.Jwt.init();
    defer jwt.deinit();

    // Create the header:
    // {
    //     "typ": "JWT",
    //     "alg": "RS256"
    // }
    const json_header = try chilkat.JsonObject.init();
    defer json_header.deinit();
    json_header.updateString("typ", "JWT") catch {};
    json_header.updateString("alg", "RS256") catch {};

    // Create the payload:
    // {
    //     "nbf": 1499947668,
    //     "exp": 1499948668,
    //     "iss": "me",
    //     "sub": "anApiKey",
    //     "aud": "https://auth-sandbox.abnamro.com/oauth/token"
    // }
    const json_payload = try chilkat.JsonObject.init();
    defer json_payload.deinit();

    const cur_date_time = jwt.genNumericDate(0);

    // Set the "not process before" timestamp to now.
    try json_payload.addIntAt(-1, "nbf", cur_date_time);

    // Set the timestamp defining an expiration time (end time) for the token
    // to be now + 1 hour (3600 seconds)
    try json_payload.addIntAt(-1, "exp", cur_date_time + 3600);

    json_payload.updateString("iss", "me") catch {};
    json_payload.updateString("sub", "anApiKey") catch {};
    json_payload.updateString("aud", "https://auth-sandbox.abnamro.com/oauth/token") catch {};

    // Produce the smallest possible JWT:
    jwt.setAutoCompact(true);

    const jwt_str = jwt.createJwtPk(alloc, try json_header.emit(alloc), try json_payload.emit(alloc), privkey) catch {
        std.debug.print("{s}\n", .{try jwt.getLastErrorText(alloc)});
        return;
    };

    // Here is the JWT:
    std.debug.print("{s}\n", .{jwt_str});
}