PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkJwt.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkPublicKey.pb"
Procedure ChilkatExample()
success.i = 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
jsonToken.i = CkJsonObject::ckCreate()
If jsonToken.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckLoadFile(jsonToken,"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"
; },
; {
; ...
; }
; ]
; }
jsonWebKeys.i = CkJsonObject::ckCreate()
If jsonWebKeys.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckLoadFile(jsonWebKeys,"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.
;
jwt.i = CkJwt::ckCreate()
If jwt.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
accessToken.s = CkJsonObject::ckStringOf(jsonToken,"access_token")
joseHeader.s = CkJwt::ckGetHeader(jwt,accessToken)
Debug joseHeader
; The joseHeader contains this: {"kid":"anSaRDPfWGOSCVNZEIZB9quCbNsdsvl5uWGBzxbudWQ","alg":"RS256"}
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckLoad(json,joseHeader)
kid.s = CkJsonObject::ckStringOf(json,"kid")
Debug "kid to find: " + kid
; ------------------------
; Step 2: Find the key with the same "kid" in the Okta web keys.
sbKid.i = CkStringBuilder::ckCreate()
If sbKid.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
e.s = ""
n.s = ""
i.i = 0
count_i.i = CkJsonObject::ckSizeOfArray(jsonWebKeys,"keys")
bFound.i = 0
iMatch.i = 0
While (bFound = 0) AND (i < count_i)
CkJsonObject::setCkI(jsonWebKeys, i)
CkStringBuilder::ckClear(sbKid)
CkJsonObject::ckStringOfSb(jsonWebKeys,"keys[i].kid",sbKid)
Debug "checking kid: " + CkStringBuilder::ckGetAsString(sbKid)
If CkStringBuilder::ckContentsEqual(sbKid,kid,1) = 1
e = CkJsonObject::ckStringOf(jsonWebKeys,"keys[i].e")
n = CkJsonObject::ckStringOf(jsonWebKeys,"keys[i].n")
; Exit the loop.
Debug "Found matching kid."
iMatch = i
bFound = 1
EndIf
i = i + 1
Wend
If bFound = 0
Debug "No matching key ID found."
CkJsonObject::ckDispose(jsonToken)
CkJsonObject::ckDispose(jsonWebKeys)
CkJwt::ckDispose(jwt)
CkJsonObject::ckDispose(json)
CkStringBuilder::ckDispose(sbKid)
ProcedureReturn
EndIf
Debug "Matching key:"
Debug " exponent = " + e
Debug " modulus = " + n
; ------------------------
; Step 3: Load the RSA modulus and exponent into a Chilkat public key object.
pubkey.i = CkPublicKey::ckCreate()
If pubkey.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Get the matching JSON key from the array of keys.
CkJsonObject::setCkI(jsonWebKeys, iMatch)
jsonWebKey.i = CkJsonObject::ckCreate()
If jsonWebKey.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckObjectOf2(jsonWebKeys,"keys[i]",jsonWebKey)
success = CkPublicKey::ckLoadFromString(pubkey,CkJsonObject::ckEmit(jsonWebKey))
If success = 0
Debug "Failed to load JSON web key."
Debug CkJsonObject::ckEmit(jsonWebKey)
Debug CkPublicKey::ckLastErrorText(pubkey)
CkJsonObject::ckDispose(jsonToken)
CkJsonObject::ckDispose(jsonWebKeys)
CkJwt::ckDispose(jwt)
CkJsonObject::ckDispose(json)
CkStringBuilder::ckDispose(sbKid)
CkPublicKey::ckDispose(pubkey)
CkJsonObject::ckDispose(jsonWebKey)
ProcedureReturn
EndIf
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.
bVerified.i = CkJwt::ckVerifyJwtPk(jwt,accessToken,pubkey)
If bVerified = 1
Debug "The access token is valid."
Else
Debug "The access token is NOT valid."
EndIf
CkJsonObject::ckDispose(jsonToken)
CkJsonObject::ckDispose(jsonWebKeys)
CkJwt::ckDispose(jwt)
CkJsonObject::ckDispose(json)
CkStringBuilder::ckDispose(sbKid)
CkPublicKey::ckDispose(pubkey)
CkJsonObject::ckDispose(jsonWebKey)
ProcedureReturn
EndProcedure