Sample code for 30+ languages & platforms
Objective-C

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 Objective-C Downloads

Objective-C
#import <CkoJws.h>
#import <NSString.h>
#import <CkoJsonObject.h>

BOOL success = NO;

CkoJws *jws = [[CkoJws alloc] init];

//  Load the JWS compact serialization.
NSString *jwsCompact = @"eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>";
success = [jws LoadJws: jwsCompact];
if (success == NO) {
    NSLog(@"%@",jws.LastErrorText);
    return;
}

//  Copy the decoded protected header of signature 0 into a JsonObject.
CkoJsonObject *json = [[CkoJsonObject alloc] init];
success = [jws GetProtectedH: [NSNumber numberWithInt: 0] json: json];
if (success == NO) {
    NSLog(@"%@",jws.LastErrorText);
    return;
}

json.EmitCompact = NO;
NSLog(@"%@",[json Emit]);