Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oJws
LOCAL cJwsCompact
LOCAL cBase64Key
LOCAL v

nSuccess := 0

oJws := CreateObject("Chilkat.Jws")

//  Load the JWS compact serialization.
cJwsCompact := "eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>"
nSuccess := oJws:LoadJws(cJwsCompact)
IF (nSuccess == 0)
    ? oJws:LastErrorText
    oJws:destroy()
    RETURN
ENDIF

//  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.
cBase64Key := "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU="
nSuccess := oJws:SetMacKey(0, cBase64Key, "base64")
IF (nSuccess == 0)
    ? oJws:LastErrorText
    oJws:destroy()
    RETURN
ENDIF

v := oJws:Validate(0)
IF (v < 0)
    ? oJws:LastErrorText
    oJws:destroy()
    RETURN
ENDIF

IF (v != 1)
    ? "The signature is not valid."
    oJws:destroy()
    RETURN
ENDIF

? "The signature is valid."

oJws:destroy()