PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkJws.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 0
jws.i = CkJws::ckCreate()
If jws.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Protected header specifying HMAC-SHA256.
header.i = CkJsonObject::ckCreate()
If header.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(header,"alg","HS256")
success = CkJws::ckSetProtectedHeader(jws,0,header)
If success = 0
Debug CkJws::ckLastErrorText(jws)
CkJws::ckDispose(jws)
CkJsonObject::ckDispose(header)
ProcedureReturn
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.
unprotected.i = CkJsonObject::ckCreate()
If unprotected.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(unprotected,"kid","signer-key-1")
success = CkJws::ckSetUnprotectedHeader(jws,0,unprotected)
If success = 0
Debug CkJws::ckLastErrorText(jws)
CkJws::ckDispose(jws)
CkJsonObject::ckDispose(header)
CkJsonObject::ckDispose(unprotected)
ProcedureReturn
EndIf
bIncludeBom.i = 0
success = CkJws::ckSetPayload(jws,"This is the content to sign.","utf-8",bIncludeBom)
If success = 0
Debug CkJws::ckLastErrorText(jws)
CkJws::ckDispose(jws)
CkJsonObject::ckDispose(header)
CkJsonObject::ckDispose(unprotected)
ProcedureReturn
EndIf
base64Key.s = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU="
success = CkJws::ckSetMacKey(jws,0,base64Key,"base64")
If success = 0
Debug CkJws::ckLastErrorText(jws)
CkJws::ckDispose(jws)
CkJsonObject::ckDispose(header)
CkJsonObject::ckDispose(unprotected)
ProcedureReturn
EndIf
; Because an unprotected header is present, the JWS is produced in a JSON serialization form.
jwsJson.s = CkJws::ckCreateJws(jws)
If CkJws::ckLastMethodSuccess(jws) = 0
Debug CkJws::ckLastErrorText(jws)
CkJws::ckDispose(jws)
CkJsonObject::ckDispose(header)
CkJsonObject::ckDispose(unprotected)
ProcedureReturn
EndIf
Debug jwsJson
CkJws::ckDispose(jws)
CkJsonObject::ckDispose(header)
CkJsonObject::ckDispose(unprotected)
ProcedureReturn
EndProcedure