Sample code for 30+ languages & platforms
PureBasic

Get a JWS Protected Header

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.GetProtectedH, which copies the decoded protected header of a signature into a JsonObject.

Background. The protected header is integrity-protected by the signature. Inspecting it (for example the alg value) is useful before deciding how to validate.

Chilkat PureBasic Downloads

PureBasic
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

    ;  Load the JWS compact serialization.
    jwsCompact.s = "eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>"
    success = CkJws::ckLoadJws(jws,jwsCompact)
    If success = 0
        Debug CkJws::ckLastErrorText(jws)
        CkJws::ckDispose(jws)
        ProcedureReturn
    EndIf

    ;  Copy the decoded protected header of signature 0 into a JsonObject.
    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJws::ckGetProtectedH(jws,0,json)
    If success = 0
        Debug CkJws::ckLastErrorText(jws)
        CkJws::ckDispose(jws)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    CkJsonObject::setCkEmitCompact(json, 0)
    Debug CkJsonObject::ckEmit(json)


    CkJws::ckDispose(jws)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure