Sample code for 30+ languages & platforms
DataFlex

Load and Validate a JWS from a String

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.LoadJws, which loads a JWS from its compact serialization string. The example then supplies the MAC key and validates the signature.

Background. Validate returns 1 when the signature or MAC is cryptographically valid, 0 when it is not, or a negative value on error. The key for the signer index must be provided before validating.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoJws
    String sJwsCompact
    String sBase64Key
    Integer v
    String sTemp1

    Move False To iSuccess

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

    //  Load the JWS compact serialization.
    Move "eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>" To sJwsCompact
    Get ComLoadJws Of hoJws sJwsCompact To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJws To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Provide the HMAC key for signer 0, then validate the signature.  Validate returns 1 when the
    //  signature is cryptographically valid, 0 when it is not, or a negative value on error.
    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

    Get ComValidate Of hoJws 0 To v
    If (v < 0) Begin
        Get ComLastErrorText Of hoJws To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    If (v <> 1) Begin
        Showln "The signature is not valid."
        Procedure_Return
    End

    Showln "The signature is valid."


End_Procedure