Sample code for 30+ languages & platforms
Visual FoxPro

Set the Optional JWS Unprotected Header

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.SetUnprotectedHeader, which sets the optional unprotected header for a signature.

Background. Unlike the protected header, the unprotected header is not covered by the signature. It is carried in the JSON serialization forms of a JWS, so producing a JWS with an unprotected header yields a JSON serialization rather than compact form.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loJws
LOCAL loHeader
LOCAL loUnprotected
LOCAL lnBIncludeBom
LOCAL lcBase64Key
LOCAL lcJwsJson

lnSuccess = 0

loJws = CreateObject('Chilkat.Jws')

*  Protected header specifying HMAC-SHA256.
loHeader = CreateObject('Chilkat.JsonObject')
loHeader.UpdateString("alg","HS256")
lnSuccess = loJws.SetProtectedHeader(0,loHeader)
IF (lnSuccess = 0) THEN
    ? loJws.LastErrorText
    RELEASE loJws
    RELEASE loHeader
    CANCEL
ENDIF

*  Set the optional unprotected header for signature 0.  Unlike the protected header, it is not
*  covered by the signature.  It is carried in the JSON serialization forms of a JWS.
loUnprotected = CreateObject('Chilkat.JsonObject')
loUnprotected.UpdateString("kid","signer-key-1")
lnSuccess = loJws.SetUnprotectedHeader(0,loUnprotected)
IF (lnSuccess = 0) THEN
    ? loJws.LastErrorText
    RELEASE loJws
    RELEASE loHeader
    RELEASE loUnprotected
    CANCEL
ENDIF

lnBIncludeBom = 0
lnSuccess = loJws.SetPayload("This is the content to sign.","utf-8",lnBIncludeBom)
IF (lnSuccess = 0) THEN
    ? loJws.LastErrorText
    RELEASE loJws
    RELEASE loHeader
    RELEASE loUnprotected
    CANCEL
ENDIF

lcBase64Key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU="
lnSuccess = loJws.SetMacKey(0,lcBase64Key,"base64")
IF (lnSuccess = 0) THEN
    ? loJws.LastErrorText
    RELEASE loJws
    RELEASE loHeader
    RELEASE loUnprotected
    CANCEL
ENDIF

*  Because an unprotected header is present, the JWS is produced in a JSON serialization form.
lcJwsJson = loJws.CreateJws()
IF (loJws.LastMethodSuccess = 0) THEN
    ? loJws.LastErrorText
    RELEASE loJws
    RELEASE loHeader
    RELEASE loUnprotected
    CANCEL
ENDIF

? lcJwsJson

RELEASE loJws
RELEASE loHeader
RELEASE loUnprotected