Skip to content

Commit

Permalink
core: readme has been updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
lolgear committed Aug 9, 2018
1 parent ec044a8 commit 6799a49
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
30 changes: 28 additions & 2 deletions README.md
Expand Up @@ -11,7 +11,33 @@ A [JSON Web Token][] implementation in Objective-C.
[JSON Web Token]: http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html

# What's new in master and bleeding edge.
Nothing here.

* EC algorithms support.
* Keys extraction from Pem files has been updated.

## EC algorithms support.

### Prerequisites.

* Certificate and P12 for Public and Private keys accordingly.
* Pem files with keys in *ANSI X9.63* format.

### Example.

```objective-c
NSString *privateKeyString = @"<ANSI X9.63 formatted key>";
NSString *publicKeyString = @"<ANSI X9.63 formatted key>";

// Note: We should pass type of key. Default type is RSA.
NSDictionary *parameters = @{JWTCryptoKey.parametersKeyBuilder : JWTCryptoKeyBuilder.new.keyTypeEC};

id <JWTCryptoKeyProtocol> privateKey = [[JWTCryptoKeyPrivate alloc] initWithPemEncoded:privateKeyString parameters:parameters error:nil];
id <JWTCryptoKeyProtocol> publicKey = [[JWTCryptoKeyPublic alloc] initWithPemEncoded:publicKeyString parameters:parameters error:nil];

// Note: JWTAlgorithmRSFamilyDataHolder will be renamed to something more appropriate. It can holds any asymmetric keys pair (private and public).
id <JWTAlgorithmDataHolderProtocol> holder = [JWTAlgorithmRSFamilyDataHolder new].signKey(privateKey).verifyKey(publicKey).algorithmName(JWTAlgorithmNameES256);
```
# What's new in Version 3.0
Expand All @@ -22,7 +48,7 @@ Nothing here.
* Keys loaded from Pem files.
## Introduction to Algorithms data holders and chain.
You have algorithm, secret data and unknown jwt token.
You have an algorithm, a secret data and an unknown jwt token.
Let's try to decode it.
```objective-c
Expand Down
58 changes: 58 additions & 0 deletions Tests/Tests/JWT/JWTReadmeTests.m
Expand Up @@ -14,6 +14,64 @@ @interface JWTReadmeTests : XCTestCase
@implementation JWTReadmeTests

- (void)testVersionThree {
[XCTContext runActivityNamed:@"API should work well with EC algorithms" block:^(id<XCTActivity> _Nonnull activity) {
NSString *privatePemFilename = @"ec256-private";
NSString *publicPemFilename = @"ec256-public";
NSString *passphrase = @"password";
NSString *(^loadKey)(NSString *, NSBundle *) = ^NSString *(NSString *name, NSBundle *bundle){
NSURL *fileURL = [bundle URLForResource:name withExtension:@"pem"];
NSError *error = nil;
NSString *fileContent = [NSString stringWithContentsOfURL:fileURL encoding:NSUTF8StringEncoding error:&error];
if (error) {
NSLog(@"%@ error: %@", self.debugDescription, error);
return nil;
}
return fileContent;
};
NSBundle *bundle = [NSBundle bundleForClass:self.class];
NSString *publicPemKey = loadKey(publicPemFilename, bundle);
NSString *privatePemKey = loadKey(privatePemFilename, bundle);

// sign and verify
{
NSString *algorithmName = @"ES256";
id <JWTAlgorithmDataHolderProtocol> signDataHolder = [JWTAlgorithmRSFamilyDataHolder new].keyExtractorType([JWTCryptoKeyExtractor privateKeyWithPEMBase64].type).privateKeyCertificatePassphrase(passphrase).algorithmName(algorithmName).secret(privatePemKey);

id <JWTAlgorithmDataHolderProtocol> verifyDataHolder = [JWTAlgorithmRSFamilyDataHolder new].keyExtractorType([JWTCryptoKeyExtractor publicKeyWithPEMBase64].type).algorithmName(algorithmName).secret(publicPemKey);

NSDictionary *payloadDictionary = @{@"hello": @"world"};

JWTCodingBuilder *signBuilder = [JWTEncodingBuilder encodePayload:payloadDictionary].addHolder(signDataHolder);
JWTCodingResultType *signResult = signBuilder.result;
NSString *token = nil;
if (signResult.successResult) {
// success
NSLog(@"%@ success: %@", self.debugDescription, signResult.successResult.encoded);
token = signResult.successResult.encoded;
}
else {
// error
NSLog(@"%@ error: %@", self.debugDescription, signResult.errorResult.error);
}

// verify
if (token == nil) {
NSLog(@"something wrong");
}

JWTCodingBuilder *verifyBuilder = [JWTDecodingBuilder decodeMessage:token].addHolder(verifyDataHolder);
JWTCodingResultType *verifyResult = verifyBuilder.result;
if (verifyResult.successResult) {
// success
NSLog(@"%@ success: %@", self.debugDescription, verifyResult.successResult.payload);
token = verifyResult.successResult.encoded;
}
else {
// error
NSLog(@"%@ error: %@", self.debugDescription, verifyResult.errorResult.error);
}
}
}];
[XCTContext runActivityNamed:@"API should work well with Pem keys loading" block:^(id<XCTActivity> _Nonnull activity){
NSString *privatePemFilename = @"rs256-private";
NSString *publicPemFilename = @"rs256-public";
Expand Down

0 comments on commit 6799a49

Please sign in to comment.