Lua Requires Chilkat v11.0.0+
Lua
PRODA Get OAuth2 Access Token using JWT
See more PRODA Examples
Demonstrates how to get an OAuth2 access token for the PRODA Australian Government Online Services using a JWT.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- First create a JWT to be sent in the POST to https://vnd.proda.humanservices.gov.au/mga/sps/oauth/oauth20/token
local privKey = chilkat.PrivateKey.new()
-- Load an RSA private key from a PEM file.
-- Chilkat provides alternative methods to load from other formats, or to load from a string or binary data.
local success = privKey:load_encrypted_pem_file("qa_data/pem/rsa_passwd.pem", "passwd")
if not success then
print(privKey.last_error_text)
return
end
local jwt = chilkat.Jwt.new()
-- Build the JOSE header
local jose = chilkat.JsonObject.new()
-- Use RS256. Pass the string "RS384" or "RS512" to use RSA with SHA-384 or SHA-512.
success = jose:append_string("alg", "RS256")
success = jose:append_string("typ", "JWT")
success = jose:append_string("kid", "test-device")
-- Now build the JWT claims (also known as the payload)
local claims = chilkat.JsonObject.new()
success = claims:append_string("iss", "9646844092")
success = claims:append_string("sub", "test-device")
success = claims:append_string("aud", "https://proda.humanservices.gov.au")
-- Set the timestamp of when the JWT was created to now.
local curDateTime = jwt:gen_numeric_date(0)
success = claims:add_int_at(-1, "iat", curDateTime)
-- Set the timestamp defining an expiration time (end time) for the token
-- to be now + 1 hour (3600 seconds)
success = claims:add_int_at(-1, "exp", curDateTime + 3600)
-- Produce the smallest possible JWT:
jwt.auto_compact = true
-- Create the JWT token. This is where the RSA signature is created.
local jwtToken = jwt:create_jwt_pk(jose:emit(), claims:emit(), privKey)
-- ---------------------------------------------------------------------
-- Build and send the POST, which should look something like this:
-- POST https://vnd.proda.humanservices.gov.au/mga/sps/oauth/oauth20/token HTTP/1.1
-- Content-Type: application/x-www-form-urlencoded
-- Content-Length: 666
-- Host: vnd.proda.humanservices.gov.au
--
-- grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=<jwt>&client_id=VendorClient03
local http = chilkat.Http.new()
local req = chilkat.HttpRequest.new()
req.http_verb = "POST"
req.content_type = "application/x-www-form-urlencoded"
-- Add the request params.
req:add_param("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer")
req:add_param("assertion", jwtToken)
req:add_param("client_id", "VendorClient03")
local resp = chilkat.HttpResponse.new()
success = http:http_req("https://vnd.proda.humanservices.gov.au/mga/sps/oauth/oauth20/token", req, resp)
if not success then
print(http.last_error_text)
return
end
print("Response status code = " .. resp.status_code)
print("Response body:")
print(resp.body_str)