/DBWebSignature

A JSON Web Token (JWT) / JSON Web Signature (JWS) implementation in Objective-C

Primary LanguageObjective-CMIT LicenseMIT

DBWebSignature

JSON Web Token (JWT) / JSON Web Signature (JWS) for iOS. Creates and Validates signatures for JSON Objects.

Thanks to yourkarma for the original implementation. Unfortunately, I found this version did not adhere correctly to the spec document, and also had unnecessary dependencies, this version should adhere to the spec and provide an easier interface for creating and validating JWT/JWS.

Installation

Add the following to your Cocoapods Podfile:

pod 'DBWebSignature', git: 'https://github.com/DavidBenko/DBWebSignature.git'

Usage

Generating Tokens

NSString *secret = @"mutually-derived-or-agreed-secret";

DBWebSignature *signer = [[DBWebSignature alloc]initWithSecret:secret algorithm:[JWTAlgorithmHS256 new]];
NSString *token = [signer generateToken:@{@"message":@"myimportantmessage"}];

Note: The payload parameter of generateToken: can be any JSON object.

Verifying Tokens

// Both of these values are needed to validate token.
// These are generated by server probably
NSString *secret = @"mutually-derived-or-agreed-secret";
NSString *tokenToValidate = @"9823dndd9dnsa...";
NSArray *payloadToValidate = ....

DBWebSignature *signer = [[DBWebSignature alloc]initWithSecret:secret algorithm:[JWTAlgorithmHS256 new]];
BOOL validToken = [signer validateToken:tokenToValidate payload:payloadToValidate];

If you're using reserved claim names you can encode your claim set like so (all properties are optional):

NSString *secret = @"mutually-derived-or-agreed-secret";

JWTClaimsSet *claimsSet = [[JWTClaimsSet alloc] init];
claimsSet.issuer = @"Facebook";
claimsSet.subject = @"Token";
claimsSet.audience = @"http://yourkarma.com";
claimsSet.expirationDate = [NSDate distantFuture];
claimsSet.notBeforeDate = [NSDate distantPast];
claimsSet.issuedAt = [NSDate date];
claimsSet.identifier = @"thisisunique";
claimsSet.type = @"test";

DBWebSignature *signer = [[DBWebSignature alloc]initWithSecret:secret algorithm:[JWTAlgorithmHS256 new]];
NSString *token = [signer encodeClaimsSet:claimsSet];

Algorithms

Supported Algorithms

  • HS512 (HMAC, SHA-512)
  • HS256 (HMAC, SHA-256)

Additional algorithms can be added by implementing the JWTAlgorithm protocol.