Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoJws
    Variant vHeader
    Handle hoHeader
    Variant vUnprotected
    Handle hoUnprotected
    Boolean iBIncludeBom
    String sBase64Key
    String sJwsJson
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatJws)) To hoJws
    If (Not(IsComObjectCreated(hoJws))) Begin
        Send CreateComObject of hoJws
    End

    //  Protected header specifying HMAC-SHA256.
    Get Create (RefClass(cComChilkatJsonObject)) To hoHeader
    If (Not(IsComObjectCreated(hoHeader))) Begin
        Send CreateComObject of hoHeader
    End
    Get ComUpdateString Of hoHeader "alg" "HS256" To iSuccess
    Get pvComObject of hoHeader to vHeader
    Get ComSetProtectedHeader Of hoJws 0 vHeader To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJws To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  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.
    Get Create (RefClass(cComChilkatJsonObject)) To hoUnprotected
    If (Not(IsComObjectCreated(hoUnprotected))) Begin
        Send CreateComObject of hoUnprotected
    End
    Get ComUpdateString Of hoUnprotected "kid" "signer-key-1" To iSuccess
    Get pvComObject of hoUnprotected to vUnprotected
    Get ComSetUnprotectedHeader Of hoJws 0 vUnprotected To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJws To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Move False To iBIncludeBom
    Get ComSetPayload Of hoJws "This is the content to sign." "utf-8" iBIncludeBom To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJws To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Move "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=" To sBase64Key
    Get ComSetMacKey Of hoJws 0 sBase64Key "base64" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJws To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Because an unprotected header is present, the JWS is produced in a JSON serialization form.
    Get ComCreateJws Of hoJws To sJwsJson
    Get ComLastMethodSuccess Of hoJws To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoJws To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln sJwsJson


End_Procedure