Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

PureBasic Examples

ASN.1
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
Facebook
FileAccess
Firebase
GMail REST API
GeoOp
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Sheets
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)

Java KeyStore (JKS)
Jira
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
PayPal
Peoplevox
QuickBooks
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Shopify
Socket/SSL/TLS
Spider
Stream
Stripe
SugarCRM
Tar Archive
Twitter
Upload
VoiceBase
Walmart
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Xero
Zip
curl
eBay

 

 

 

(PureBasic) Create JWT Using HS256, HS384, or HS512

Demonstrates how to create a JWT using HS256, HS384, or HS512. (HS256 is JWT's acronym for HMAC-SHA256.) When HMAC is used, the secret is a shared secret (i.e. password) that both client and server know beforehand.

This example also demonstrates how to include time constraints:

  • nbf: Not Before Time
  • exp: Expiration Time
  • iat: Issue At Time

Chilkat PureBasic Module Download

Chilkat PureBasic Module

IncludeFile "CkJwt.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    ;  Demonstrates how to create an HMAC JWT using a shared secret (password).

    ;  This example requires the Chilkat API to have been previously unlocked.
    ;  See Global Unlock Sample for sample code.

    jwt.i = CkJwt::ckCreate()
    If jwt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Build the JOSE header
    jose.i = CkJsonObject::ckCreate()
    If jose.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Use HS256.  Pass the string "HS384" or "HS512" to use a different algorithm.
    success.i = CkJsonObject::ckAppendString(jose,"alg","HS256")
    success = CkJsonObject::ckAppendString(jose,"typ","JWT")

    ;  Now build the JWT claims (also known as the payload)
    claims.i = CkJsonObject::ckCreate()
    If claims.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckAppendString(claims,"iss","http://example.org")
    success = CkJsonObject::ckAppendString(claims,"sub","John")
    success = CkJsonObject::ckAppendString(claims,"aud","http://example.com")

    ;  Set the timestamp of when the JWT was created to now.
    curDateTime.i = CkJwt::ckGenNumericDate(jwt,0)
    success = CkJsonObject::ckAddIntAt(claims,-1,"iat",curDateTime)

    ;  Set the "not process before" timestamp to now.
    success = CkJsonObject::ckAddIntAt(claims,-1,"nbf",curDateTime)

    ;  Set the timestamp defining an expiration time (end time) for the token
    ;  to be now + 1 hour (3600 seconds)
    success = CkJsonObject::ckAddIntAt(claims,-1,"exp",curDateTime + 3600)

    ;  Produce the smallest possible JWT:
    CkJwt::setCkAutoCompact(jwt, 1)

    strJwt.s = CkJwt::ckCreateJwt(jwt,CkJsonObject::ckEmit(jose),CkJsonObject::ckEmit(claims),"secret")

    Debug strJwt


    CkJwt::ckDispose(jwt)
    CkJsonObject::ckDispose(jose)
    CkJsonObject::ckDispose(claims)


    ProcedureReturn
EndProcedure

 

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