PowerBuilder
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_JsonToken
oleobject loo_JsonWebKeys
oleobject loo_Jwt
string ls_AccessToken
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
li_Success = 0
// 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
loo_JsonToken = create oleobject
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
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
li_rc = loo_Jwt.ConnectToNewObject("Chilkat.Jwt")
ls_AccessToken = loo_JsonToken.StringOf("access_token")
ls_JoseHeader = loo_Jwt.GetHeader(ls_AccessToken)
Write-Debug ls_JoseHeader
// The joseHeader contains this: {"kid":"anSaRDPfWGOSCVNZEIZB9quCbNsdsvl5uWGBzxbudWQ","alg":"RS256"}
loo_Json = create oleobject
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
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
li_rc = loo_Pubkey.ConnectToNewObject("Chilkat.PublicKey")
// Get the matching JSON key from the array of keys.
loo_JsonWebKeys.I = li_IMatch
loo_JsonWebKey = create oleobject
li_rc = loo_JsonWebKey.ConnectToNewObject("Chilkat.JsonObject")
loo_JsonWebKeys.ObjectOf2("keys[i]",loo_JsonWebKey)
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_JsonToken
destroy loo_JsonWebKeys
destroy loo_Jwt
destroy loo_Json
destroy loo_SbKid
destroy loo_Pubkey
destroy loo_JsonWebKey
return
end if
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_AccessToken,loo_Pubkey)
if li_BVerified = 1 then
Write-Debug "The access token is valid."
else
Write-Debug "The access token is NOT valid."
end if
destroy loo_JsonToken
destroy loo_JsonWebKeys
destroy loo_Jwt
destroy loo_Json
destroy loo_SbKid
destroy loo_Pubkey
destroy loo_JsonWebKey