Zig
Zig
Retrieve the Okta JSON Web Keys (JWK)
See more Okta OAuth/OIDC Examples
Demonstrates how to get the Okta web keys (JWK) that will be needed to locally validate an access token. This example is for the case where Okta is the identity platform for your App or API. (See https://developer.okta.com/docs/reference/api/oidc/#composing-your-base-url)Note: This example uses the URL
https://dev-765951.okta.com/oauth2/${authServerId}/v1/authorize
If you have a developer account, you can use the default authorization server that was created along with your account, in which case the full URL looks like this:
https://dev-765951.okta.com/oauth2/default/v1/authorize
Also: You'll need to change "dev-765951" to use your own Okta domain.
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.
const http = try chilkat.Http.init();
defer http.deinit();
// Implements the following CURL command:
// curl -X GET \
// https://dev-765951.okta.com/oauth2/${authServerId}/v1/keys?client_id=$my_client_id
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
http.quickGetSb("https://dev-765951.okta.com/oauth2/${authServerId}/v1/keys?client_id=$my_client_id", sb_response_body) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
const j_resp = try chilkat.JsonObject.init();
defer j_resp.deinit();
j_resp.loadSb(sb_response_body) catch {};
j_resp.setEmitCompact(false);
std.debug.print("Response Body:\n", .{});
std.debug.print("{s}\n", .{try j_resp.emit(alloc)});
const resp_status_code = http.getLastStatus();
std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
if (resp_status_code >= 400) {
std.debug.print("Response Header:\n", .{});
std.debug.print("{s}\n", .{try http.getLastHeader(alloc)});
std.debug.print("Failed.\n", .{});
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "keys": [
// {
// "kty": "RSA",
// "alg": "RS256",
// "kid": "anSaRDPfWGOSCVNZEIZB9quCbNsdsvl5uWGBzxbudWQ",
// "use": "sig",
// "e": "AQAB",
// "n": "jT8uAgd5wF7d3aiCjdxbVXk7EDio2ScqAjWCL2LXbY3bpj6RV4dswZTqBg0-vuIh1SBqjoufDV3fHAJcewv2dfEdfUKawBxTIcLgeSu_NoUsZ6zmHqYKC9U0awgP3s5_Yqcr7YMxurrPRNRdKNsFFF2lhCDJ-4gCO4CWiCUdptlmMfI7_pBxx9bgh1F-ulVKt_RUo-4A7tXwwJhBhWaECKoye04BACccSNkth8flGSvjXNIVKcAMHYQ3oDfB6U5zXcFuDVEBJI__hzA9920QRXrloIv_G0QmYgbaf4iRpd-3B6-PQ4-VK-FGm3t9eTwoFv6CARpyXrjaOeuLB1HaVw"
// }
// ]
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
var kty: [:0]const u8 = "";
var alg: [:0]const u8 = "";
var kid: [:0]const u8 = "";
var use: [:0]const u8 = "";
var e: [:0]const u8 = "";
var n: [:0]const u8 = "";
var i: i32 = 0;
const count_i = j_resp.sizeOfArray("keys");
while (i < count_i) {
j_resp.setI(i);
kty = try j_resp.stringOf(alloc, "keys[i].kty");
alg = try j_resp.stringOf(alloc, "keys[i].alg");
kid = try j_resp.stringOf(alloc, "keys[i].kid");
use = try j_resp.stringOf(alloc, "keys[i].use");
e = try j_resp.stringOf(alloc, "keys[i].e");
n = try j_resp.stringOf(alloc, "keys[i].n");
i = i + 1;
}
}