Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
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 Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Http,
Chilkat.StringBuilder,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
http: THttp;
sbResponseBody: TStringBuilder;
jResp: TJsonObject;
respStatusCode: Integer;
kty: string;
alg: string;
kid: string;
use: string;
e: string;
n: string;
i: Integer;
count_i: Integer;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := THttp.Create;
// Implements the following CURL command:
// curl -X GET \
// https://dev-765951.okta.com/oauth2/${authServerId}/v1/keys?client_id=$my_client_id
sbResponseBody := TStringBuilder.Create;
success := http.QuickGetSb('https://dev-765951.okta.com/oauth2/${authServerId}/v1/keys?client_id=$my_client_id',sbResponseBody);
if (success = False) then
begin
WriteLn(http.LastErrorText);
Exit;
end;
jResp := TJsonObject.Create;
jResp.LoadSb(sbResponseBody);
jResp.EmitCompact := False;
WriteLn('Response Body:');
WriteLn(jResp.Emit());
respStatusCode := http.LastStatus;
WriteLn('Response Status Code = ' + respStatusCode);
if (respStatusCode >= 400) then
begin
WriteLn('Response Header:');
WriteLn(http.LastHeader);
WriteLn('Failed.');
Exit;
end;
// 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
i := 0;
count_i := jResp.SizeOfArray('keys');
while i < count_i do
begin
jResp.I := i;
kty := jResp.StringOf('keys[i].kty');
alg := jResp.StringOf('keys[i].alg');
kid := jResp.StringOf('keys[i].kid');
use := jResp.StringOf('keys[i].use');
e := jResp.StringOf('keys[i].e');
n := jResp.StringOf('keys[i].n');
i := i + 1;
end;
http.Free;
sbResponseBody.Free;
jResp.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.