Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PowerBuilder) Verify Okta ID Token LocallyThis example demonstrates how to validate an Okta ID token using Chilkat's JWT class. For more information, see https://developer.okta.com/docs/guides/validate-id-tokens/overview/
integer li_rc oleobject loo_JsonToken integer li_Success oleobject loo_JsonWebKeys oleobject loo_Jwt string ls_IdToken string ls_JoseHeader oleobject loo_Json string ls_Kid oleobject loo_SbKid string e string n integer i integer li_Count_i integer li_BFound integer li_IMatch oleobject loo_Pubkey oleobject loo_JsonWebKey integer li_BVerified string ls_Claims oleobject loo_JsonClaims oleobject loo_DtExp integer li_BExpired // 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 + id 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 // // // ---------------------------------------------------------------- // Note: The very last step of this example is where the claims, such as iss, aud, iat, exp, and nonce // are extracted from the ID token and examined. // ---------------------------------------------------------------- // Load the access/id 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 id_token. (The access_token is verified in this example: Verify Okta Access Token loo_JsonToken = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_JsonToken.ConnectToNewObject("Chilkat.JsonObject") if li_rc < 0 then destroy loo_JsonToken MessageBox("Error","Connecting to COM object failed") return end if li_Success = loo_JsonToken.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" // }, // { // ... // } // ] // } loo_JsonWebKeys = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_JsonWebKeys.ConnectToNewObject("Chilkat.JsonObject") li_Success = loo_JsonWebKeys.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. // loo_Jwt = create oleobject // Use "Chilkat_9_5_0.Jwt" for versions of Chilkat < 10.0.0 li_rc = loo_Jwt.ConnectToNewObject("Chilkat.Jwt") ls_IdToken = loo_JsonToken.StringOf("id_token") ls_JoseHeader = loo_Jwt.GetHeader(ls_IdToken) Write-Debug ls_JoseHeader // The joseHeader contains this: {"kid":"anSaRDPfWGOSCVNZEIZB9quCbNsdsvl5uWGBzxbudWQ","alg":"RS256"} loo_Json = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject") loo_Json.Load(ls_JoseHeader) ls_Kid = loo_Json.StringOf("kid") Write-Debug "kid to find: " + ls_Kid // ------------------------ // Step 2: Find the key with the same "kid" in the Okta web keys. loo_SbKid = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_SbKid.ConnectToNewObject("Chilkat.StringBuilder") e = "" n = "" i = 0 li_Count_i = loo_JsonWebKeys.SizeOfArray("keys") li_BFound = 0 li_IMatch = 0 do while (li_BFound = 0) AND (i < li_Count_i) loo_JsonWebKeys.I = i loo_SbKid.Clear() loo_JsonWebKeys.StringOfSb("keys[i].kid",loo_SbKid) Write-Debug "checking kid: " + loo_SbKid.GetAsString() if loo_SbKid.ContentsEqual(ls_Kid,1) = 1 then e = loo_JsonWebKeys.StringOf("keys[i].e") n = loo_JsonWebKeys.StringOf("keys[i].n") // Exit the loop. Write-Debug "Found matching kid." li_IMatch = i li_BFound = 1 end if i = i + 1 loop if li_BFound = 0 then Write-Debug "No matching key ID found." destroy loo_JsonToken destroy loo_JsonWebKeys destroy loo_Jwt destroy loo_Json destroy loo_SbKid return end if Write-Debug "Matching key:" Write-Debug " exponent = " + e Write-Debug " modulus = " + n // ------------------------ // Step 3: Load the RSA modulus and exponent into a Chilkat public key object. loo_Pubkey = create oleobject // Use "Chilkat_9_5_0.PublicKey" for versions of Chilkat < 10.0.0 li_rc = loo_Pubkey.ConnectToNewObject("Chilkat.PublicKey") // Get the matching JSON key from the array of keys. loo_JsonWebKeys.I = li_IMatch loo_JsonWebKey = loo_JsonWebKeys.ObjectOf("keys[i]") li_Success = loo_Pubkey.LoadFromString(loo_JsonWebKey.Emit()) if li_Success = 0 then Write-Debug "Failed to load JSON web key." Write-Debug loo_JsonWebKey.Emit() Write-Debug loo_Pubkey.LastErrorText destroy loo_JsonWebKey destroy loo_JsonToken destroy loo_JsonWebKeys destroy loo_Jwt destroy loo_Json destroy loo_SbKid destroy loo_Pubkey return end if destroy loo_JsonWebKey Write-Debug "successfully loaded web key." // 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. li_BVerified = loo_Jwt.VerifyJwtPk(ls_IdToken,loo_Pubkey) if li_BVerified = 1 then Write-Debug "The ID token is valid." else Write-Debug "The ID token is NOT valid." end if // ------------------------ // Step 5: Extract the claims (payload) from the ID token and examine them.. ls_Claims = loo_Jwt.GetPayload(ls_IdToken) loo_JsonClaims = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_JsonClaims.ConnectToNewObject("Chilkat.JsonObject") loo_JsonClaims.Load(ls_Claims) loo_JsonClaims.EmitCompact = 0 Write-Debug loo_JsonClaims.Emit() // Sample claims: // { // "sub": "00utrr8ehubooPhjj356", // "ver": 1, // "iss": "https://dev-765951.okta.com/oauth2/default", // "aud": "0oatrr20vPYgVDlGr356", // "iat": 1562190727, // "exp": 1562194327, // "jti": "ID.JvlMhlnCj5ZqqGjk-jlgcOxHEyVUwIl9_Kpz69U2D_4", // "amr": [ // "pwd" // ], // "idp": "00os29azljkqyx99Q356", // "auth_time": 1562190726, // "at_hash": "SLMiVeyNWWEDaZ-O32nKMg" // } // The exp (expiry time) claim is the time at which this token will expire., expressed in Unix time. You should make sure that this time has not already passed. loo_DtExp = create oleobject // Use "Chilkat_9_5_0.CkDateTime" for versions of Chilkat < 10.0.0 li_rc = loo_DtExp.ConnectToNewObject("Chilkat.CkDateTime") loo_DtExp.SetFromUnixTime(0,loo_JsonClaims.IntOf("exp")) Write-Debug "expire timestamp = " + loo_DtExp.GetAsTimestamp(0) // Check to see if this date/time expires within 0 seconds (i.e. is already past) li_BExpired = loo_DtExp.ExpiresWithin(0,"seconds") Write-Debug "bExpired = " + string(li_BExpired) destroy loo_JsonToken destroy loo_JsonWebKeys destroy loo_Jwt destroy loo_Json destroy loo_SbKid destroy loo_Pubkey destroy loo_JsonClaims destroy loo_DtExp |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.