Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ViewController.m
  3. //  jwt-test
  4. //
  5. //  Created by Piotr Smajek on 18/04/2019.
  6. //  Copyright © 2019 Piotr Smajek. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. #import "JWT.h"
  11.  
  12. @interface ViewController ()
  13.  
  14. @end
  15.  
  16. @implementation ViewController
  17.  
  18. - (void)viewDidLoad {
  19.     [super viewDidLoad];
  20.     // Do any additional setup after loading the view.
  21.     JWTClaimsSet *claimsSet = [[JWTClaimsSet alloc] init];
  22.     // fill it
  23.     claimsSet.issuer = @"Facebook";
  24.     claimsSet.subject = @"Token";
  25.     claimsSet.audience = @"https://jwt.io";
  26.    
  27.     // encode it
  28.     NSString *secret = @"secret";
  29.     NSString *algorithmName = @"ES256";
  30.     NSDictionary *headers = @{@"custom":@"value"};
  31.    
  32.     NSString *privateKeyString = @"-----BEGIN EC PRIVATE KEY-----\nBLo2DBRd1zHWy5RpW3u0FwWYvLRDlBfvitL7HMVuY2O-dO4_gQX9_gbwZXFYgAP4MYx57lPfCsW9f94iQ7Zy80T3jercB_8sH4GikY1OEUUyRvU81VhfWR740FwqbpAQ_A\n-----END EC PRIVATE KEY-----";
  33.     NSString *publicKeyString = @"<ANSI X9.63 formatted key>";
  34.    
  35.     // Note: We should pass type of key. Default type is RSA.
  36.     NSDictionary *parameters = @{JWTCryptoKey.parametersKeyBuilder : JWTCryptoKeyBuilder.new.keyTypeEC};
  37.    
  38.     id <JWTCryptoKeyProtocol> privateKey = [[JWTCryptoKeyPrivate alloc] initWithPemEncoded:privateKeyString parameters:parameters error:nil];
  39.     id <JWTCryptoKeyProtocol> publicKey = [[JWTCryptoKeyPublic alloc] initWithPemEncoded:publicKeyString parameters:parameters error:nil];
  40.    
  41.     // Note: JWTAlgorithmRSFamilyDataHolder will be renamed to something more appropriate. It can holds any asymmetric keys pair (private and public).
  42.     id <JWTAlgorithmDataHolderProtocol> holder = [JWTAlgorithmRSFamilyDataHolder new].signKey(privateKey).algorithmName(JWTAlgorithmNameES256).secretData([NSData data]);;
  43.    
  44.     JWTCodingResultType *result = [JWTEncodingBuilder encodeClaimsSet:claimsSet].headers(headers).addHolder(holder).result;
  45.    
  46.     NSString *encodedToken = result.successResult.encoded;
  47.     if (result.successResult) {
  48.         // handle encoded result
  49.         NSLog(@"encoded result: %@", result.successResult.encoded);
  50.     }
  51.     else {
  52.         // handle error
  53.         NSLog(@"encode failed, error: %@", result.errorResult.error);
  54.     }
  55. }
  56.  
  57.  
  58. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement