Lua
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- 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.
local jwk = chilkat.JsonObject.new()
local success = jwk:update_string("kty", "EC")
success = jwk:update_string("crv", "P-256")
success = jwk:update_string("x", "...")
success = jwk:update_string("y", "...")
success = jwk:update_string("d", "...")
local eccKey = chilkat.PrivateKey.new()
success = eccKey:load_jwk(jwk:emit())
if not success then
print(eccKey.last_error_text)
return
end
local jwt = chilkat.Jwt.new()
-- Build the JOSE header
local jose = chilkat.JsonObject.new()
success = jose:append_string("format", "compact")
success = jose:append_string("alg", "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"
-- }
local claims = chilkat.JsonObject.new()
claims:append_string("jti", "555D9123")
claims:append_string("email", "your_email@example.com")
-- Set the timestamp of when the JWT was created to now minus 60 seconds
local curDateTime = jwt:gen_numeric_date(-60)
success = claims:add_int_at(-1, "iat", curDateTime)
-- Set the "not process before" timestamp to now minus 60 seconds
success = claims: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 = claims:add_int_at(-1, "exp", curDateTime + 3600)
claims:append_string("aud", "usr")
-- Produce the smallest possible JWT:
jwt.auto_compact = true
-- Create the JWT token. This is where the RSA signature is created.
local jwt_token = jwt:create_jwt_pk(jose:emit(), claims:emit(), eccKey)
print(jwt_token)
-- Send the HTTPS GET with the jwt_token used for Authorization.
local http = chilkat.Http.new()
http.auth_token = jwt_token
local responseStr = http:quick_get_str("https://bitzlato.com/api/auth/whoami")
if not http.last_method_success then
print(http.last_error_text)
return
end
print("status code = " .. http.last_status)
print(responseStr)