Zig Requires Chilkat v11.0.0+
Zig
Verify Okta Access Token Locally
See more Okta OAuth/OIDC Examples
This example demonstrates how to validate an Okta access token using Chilkat's JWT class.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This example begins with two JSON files:
//
// 1. The access token obtained from Okta as shown in one fo these examples:
// Get Okta Token using Resource Owner Password Flow
//
// 2. The Okta web keys obtained by this example: Get Okta Web Keys
//
//
// Load the access token to be verified.
// It contains JSON that looks like this:
// {
// "access_token": "eyJraWQiOiJhb ... O_eVu-kBp6g",
// "token_type": "Bearer",
// "expires_in": 3600,
// "scope": "openid",
// "id_token": "eyJraWQi ... FrL9WOuwbQtUg"
// }
// This example verifies the access_token. (The id_token is verified in this example: Verify Okta ID Token
const json_token = try chilkat.JsonObject.init();
defer json_token.deinit();
try json_token.loadFile("qa_data/tokens/okta_access_token.json");
// Load the public keys (Okta web keys), one of which is needed to validate.
// The web keys JSON looks like this:
// {
// "keys": [
// {
// "kty": "RSA",
// "alg": "RS256",
// "kid": "anSaRDPfWGOSCVNZEIZB9quCbNsdsvl5uWGBzxbudWQ",
// "use": "sig",
// "e": "AQAB",
// "n": "jT8uAgd5w ... euLB1HaVw"
// },
// {
// ...
// }
// ]
// }
const json_web_keys = try chilkat.JsonObject.init();
defer json_web_keys.deinit();
try json_web_keys.loadFile("qa_data/tokens/okta_web_keys.json");
// ------------------------
// Step 1: Get the JOSE header from the JWT. The JOSE header contains JSON. One of the JSON members will be the key ID "kid" which identifies the web key to be used for validation.
//
const jwt = try chilkat.Jwt.init();
defer jwt.deinit();
const access_token = try json_token.stringOf(alloc, "access_token");
const jose_header = try jwt.getHeader(alloc, access_token);
std.debug.print("{s}\n", .{jose_header});
// The joseHeader contains this: {"kid":"anSaRDPfWGOSCVNZEIZB9quCbNsdsvl5uWGBzxbudWQ","alg":"RS256"}
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.load(jose_header) catch {};
const kid = try json.stringOf(alloc, "kid");
std.debug.print("kid to find: {s}\n", .{kid});
// ------------------------
// Step 2: Find the key with the same "kid" in the Okta web keys.
const sb_kid = try chilkat.StringBuilder.init();
defer sb_kid.deinit();
var e: [:0]const u8 = "";
var n: [:0]const u8 = "";
var i: i32 = 0;
const count_i = json_web_keys.sizeOfArray("keys");
var b_found: bool = false;
var i_match: i32 = 0;
while ((!b_found) and (i < count_i)) {
json_web_keys.setI(i);
sb_kid.clear();
json_web_keys.stringOfSb("keys[i].kid", sb_kid) catch {};
std.debug.print("checking kid: {s}\n", .{try sb_kid.getAsString(alloc)});
if (sb_kid.contentsEqual(kid, true)) {
e = try json_web_keys.stringOf(alloc, "keys[i].e");
n = try json_web_keys.stringOf(alloc, "keys[i].n");
// Exit the loop.
std.debug.print("Found matching kid.\n", .{});
i_match = i;
b_found = true;
}
i = i + 1;
}
if (!b_found) {
std.debug.print("No matching key ID found.\n", .{});
return;
}
std.debug.print("Matching key:\n", .{});
std.debug.print(" exponent = {s}\n", .{e});
std.debug.print(" modulus = {s}\n", .{n});
// ------------------------
// Step 3: Load the RSA modulus and exponent into a Chilkat public key object.
const pubkey = try chilkat.PublicKey.init();
defer pubkey.deinit();
// Get the matching JSON key from the array of keys.
json_web_keys.setI(i_match);
const json_web_key = try chilkat.JsonObject.init();
defer json_web_key.deinit();
json_web_keys.objectOf2("keys[i]", json_web_key) catch {};
pubkey.loadFromString(try json_web_key.emit(alloc)) catch {
std.debug.print("Failed to load JSON web key.\n", .{});
std.debug.print("{s}\n", .{try json_web_key.emit(alloc)});
std.debug.print("{s}\n", .{try pubkey.getLastErrorText(alloc)});
return;
};
std.debug.print("successfully loaded web key.\n", .{});
// OK.. we have the desired JSON web key loaded into our public key object.
// Now we can verify the access token.
// ------------------------
// Step 4: Verify the access token.
if (jwt.verifyJwtPk(access_token, pubkey)) {
std.debug.print("The access token is valid.\n", .{});
} else |_| {
std.debug.print("The access token is NOT valid.\n", .{});
}
}