Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

PowerBuilder Web API Examples

Primary Categories

ABN AMRO
AWS Secrets Manager
AWS Security Token Service
AWS Translate
Activix CRM
Adyen
Alibaba Cloud OSS
Amazon Cognito
Amazon DynamoDB
Amazon MWS
Amazon Pay
Amazon Rekognition
Amazon SP-API
Amazon Voice ID
Aruba Fatturazione
Azure Maps
Azure Monitor
Azure OAuth2
Azure Storage Accounts
Backblaze S3
Banco Inter
Belgian eHealth Platform
Bitfinex v2 REST
Bluzone
BrickLink
Bunny CDN
CallRail
CardConnect
Cerved
ClickBank
Clickatell
Cloudfare
Constant Contact
DocuSign
Duo Auth MFA
ETrade
Ecwid
Egypt ITIDA
Egypt eReceipt
Etsy
Facebook
Faire
Frame.io
GeoOp
GetHarvest
Global Payments
Google People
Google Search Console
Google Translate
Google Vision
Hungary NAV Invoicing
IBM Text to Speech
Ibanity
IntakeQ
Jira
Lightspeed
MYOB
Magento
Mailgun

Mastercard
MedTunnel
MercadoLibre
MessageMedia
Microsoft Calendar
Microsoft Group
Microsoft Tasks and Plans
Microsoft Teams
Moody's
Okta OAuth/OIDC
OneLogin OIDC
OneNote
OpenAI ChatGPT
PRODA
PayPal
Paynow.pl
Peoplevox
Populi
QuickBooks
Rabobank
Refinitiv
Royal Mail OBA
SCiS Schools Catalogue
SII Chile
SMSAPI
SOAP finkok.com
SendGrid
Shippo
Shopify
Shopware
Shopware 6
SimpleTexting
Square
Stripe
SugarCRM
TicketBAI
Trello
Twilio
Twitter API v2
Twitter v1
UPS
UniPin
VoiceBase
Vonage
WaTrend
Walmart v3
Wasabi
WhatsApp
WiX
WooCommerce
WordPress
Xero
Yahoo Mail
Yapily
Yousign
ZATCA
Zendesk
Zoom
_Miscellaneous_
eBay
effectconnect
hacienda.go.cr

 

 

 

(PowerBuilder) Verify Okta Access Token Locally

This example demonstrates how to validate an Okta access token using Chilkat's JWT class.

For more information, see https://developer.okta.com/docs/guides/validate-access-tokens/overview/#what-to-check-when-validating-an-access-token

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

integer li_rc
oleobject loo_JsonToken
integer li_Success
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

// 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_9_5_0.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_9_5_0.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_9_5_0.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_9_5_0.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_9_5_0.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_9_5_0.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_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

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.