Sample code for 30+ languages & platforms
Lua 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 Lua Downloads

Lua
local chilkat = require("chilkat")

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

-- Create public/private key pair (RSA)
local rsa = chilkat.Rsa.new()

-- Generate a 2048-bit key.
local privkey = chilkat.PrivateKey.new()
local success = rsa:gen_key(2048, privkey)
if not success then
    print(rsa.last_error_text)
    return
end

-- Export the key to PEM files.
-- Write one PEM file for the private key, and one for the public key.
success = privkey:save_pem_file("qa_data/pem/abnAmroPrivateKey.pem")

local pubkey = chilkat.PublicKey.new()
privkey:to_public_key(pubkey)
success = pubkey:save_pem_file(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.
local jwt = chilkat.Jwt.new()

-- Create the header:
-- {
--     "typ": "JWT",
--     "alg": "RS256"
-- }
local jsonHeader = chilkat.JsonObject.new()
jsonHeader:update_string("typ", "JWT")
jsonHeader:update_string("alg", "RS256")

-- Create the payload:
-- {
--     "nbf": 1499947668,
--     "exp": 1499948668,
--     "iss": "me",
--     "sub": "anApiKey",
--     "aud": "https://auth-sandbox.abnamro.com/oauth/token"
-- }
local jsonPayload = chilkat.JsonObject.new()

local curDateTime = jwt:gen_numeric_date(0)

-- Set the "not process before" timestamp to now.
success = jsonPayload:add_int_at(-1, "nbf", curDateTime)

-- Set the timestamp defining an expiration time (end time) for the token
-- to be now + 1 hour (3600 seconds)
success = jsonPayload:add_int_at(-1, "exp", curDateTime + 3600)

jsonPayload:update_string("iss", "me")
jsonPayload:update_string("sub", "anApiKey")
jsonPayload:update_string("aud", "https://auth-sandbox.abnamro.com/oauth/token")

-- Produce the smallest possible JWT:
jwt.auto_compact = true

local jwtStr = jwt:create_jwt_pk(jsonHeader:emit(), jsonPayload:emit(), privkey)
if not jwt.last_method_success then
    print(jwt.last_error_text)
    return
end

-- Here is the JWT:
print(jwtStr)