Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.m
- // jwt-test
- //
- // Created by Piotr Smajek on 18/04/2019.
- // Copyright © 2019 Piotr Smajek. All rights reserved.
- //
- #import "ViewController.h"
- #import "JWT.h"
- @interface ViewController ()
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- JWTClaimsSet *claimsSet = [[JWTClaimsSet alloc] init];
- // fill it
- claimsSet.issuer = @"Facebook";
- claimsSet.subject = @"Token";
- claimsSet.audience = @"https://jwt.io";
- // encode it
- NSString *secret = @"secret";
- NSString *algorithmName = @"ES256";
- NSDictionary *headers = @{@"custom":@"value"};
- NSString *privateKeyString = @"-----BEGIN EC PRIVATE KEY-----\nBLo2DBRd1zHWy5RpW3u0FwWYvLRDlBfvitL7HMVuY2O-dO4_gQX9_gbwZXFYgAP4MYx57lPfCsW9f94iQ7Zy80T3jercB_8sH4GikY1OEUUyRvU81VhfWR740FwqbpAQ_A\n-----END EC PRIVATE 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).algorithmName(JWTAlgorithmNameES256).secretData([NSData data]);;
- JWTCodingResultType *result = [JWTEncodingBuilder encodeClaimsSet:claimsSet].headers(headers).addHolder(holder).result;
- NSString *encodedToken = result.successResult.encoded;
- if (result.successResult) {
- // handle encoded result
- NSLog(@"encoded result: %@", result.successResult.encoded);
- }
- else {
- // handle error
- NSLog(@"encode failed, error: %@", result.errorResult.error);
- }
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement