Chilkat Examples

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

SQL Server Examples

Web API Categories

ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
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)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(SQL Server) Create JWT using Smart Card

See more JSON Web Token (JWT) Examples

Demonstrates how to create a JWT using an RSA private key and certificate on a smart card. This is for JOSE headers with an "alg" of RS256, RS384, or RS512. When RSA is used, the private key signs (creates) the JWT, and the public key is for verification.

This example also demonstrates how to include time constraints:

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

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(max)
    DECLARE @sTmp1 nvarchar(max)
    -- Demonstrates how to create a JWT using an RSA private key and certificate on a smart card.

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

    DECLARE @jwt int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Jwt', @jwt OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @cert int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Cert', @cert OUT

    -- Note: At the time of writing this example, Chilkat is actively working on improving support for smart cards and USB tokens,
    -- for both Windows and non-Windows operating systems.
    -- See the examples in the categories for Pkcs11 and ScMiniDriver on example-code.com
    -- It's best to use Chilkat v9.5.0.88 or later. As of 20-July-2021, v9.5.0.88 is not yet released.
    -- Send email to support@chilkatsoft.com to request a pre-release.
    -- Depending on your smart card, it may be sufficient to simply call LoadFromSmartcard("") as shown here.
    -- Chilkat is actively working to make so that LoadFromSmartcard("") works for the majority of situations.
    DECLARE @success int
    EXEC sp_OAMethod @cert, 'LoadFromSmartcard', @success OUT, ''
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @jwt
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    -- Build the JOSE header
    DECLARE @jose int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jose OUT

    -- Use RS256.  Pass the string "RS384" or "RS512" to use RSA with SHA-384 or SHA-512.
    EXEC sp_OAMethod @jose, 'UpdateString', @success OUT, 'alg', 'RS256'
    EXEC sp_OAMethod @jose, 'UpdateString', @success OUT, 'typ', 'JWT'
    EXEC sp_OAMethod @cert, 'GetEncoded', @sTmp0 OUT
    EXEC sp_OAMethod @jose, 'UpdateString', @success OUT, 'x5c[0]', @sTmp0

    -- Now build the JWT claims (also known as the payload)
    DECLARE @claims int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @claims OUT

    EXEC sp_OAMethod @claims, 'UpdateString', @success OUT, 'iss', 'http://example.org'
    EXEC sp_OAMethod @claims, 'UpdateString', @success OUT, 'sub', 'John'
    EXEC sp_OAMethod @claims, 'UpdateString', @success OUT, 'aud', 'http://example.com'

    -- Set the timestamp of when the JWT was created to now.
    DECLARE @curDateTime int
    EXEC sp_OAMethod @jwt, 'GenNumericDate', @curDateTime OUT, 0
    EXEC sp_OAMethod @claims, 'UpdateInt', @success OUT, 'iat', @curDateTime

    -- Set the "not process before" timestamp to now.
    EXEC sp_OAMethod @claims, 'UpdateInt', @success OUT, 'nbf', @curDateTime

    -- Set the timestamp defining an expiration time (end time) for the token
    -- to be now + 1 hour (3600 seconds)
    EXEC sp_OAMethod @claims, 'UpdateInt', @success OUT, 'exp', @curDateTime + 3600

    -- Produce the smallest possible JWT:
    EXEC sp_OASetProperty @jwt, 'AutoCompact', 1

    DECLARE @privKey int
    EXEC sp_OAMethod @cert, 'ExportPrivateKey', @privKey OUT
    EXEC sp_OAGetProperty @cert, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @jwt
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @jose
        EXEC @hr = sp_OADestroy @claims
        RETURN
      END

    -- Create the JWT token.  This is where the RSA signature is created.
    DECLARE @token nvarchar(max)
    EXEC sp_OAMethod @jose, 'Emit', @sTmp0 OUT
    EXEC sp_OAMethod @claims, 'Emit', @sTmp1 OUT
    EXEC sp_OAMethod @jwt, 'CreateJwtPk', @token OUT, @sTmp0, @sTmp1, @privKey


    PRINT @token

    EXEC @hr = sp_OADestroy @privKey


    EXEC @hr = sp_OADestroy @jwt
    EXEC @hr = sp_OADestroy @cert
    EXEC @hr = sp_OADestroy @jose
    EXEC @hr = sp_OADestroy @claims


END
GO

 

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