Sample code for 30+ languages & platforms
JavaScript

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.
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

var jws = new CkJws();

//  Load the JWS compact serialization.
var jwsCompact = "eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>";
success = jws.LoadJws(jwsCompact);
if (success == false) {
    console.log(jws.LastErrorText);
    return;
}

//  Copy the decoded protected header of signature 0 into a JsonObject.
var json = new CkJsonObject();
success = jws.GetProtectedH(0,json);
if (success == false) {
    console.log(jws.LastErrorText);
    return;
}

json.EmitCompact = false;
console.log(json.Emit());