PPOTOAuth2BrowserSwitchRequest.m 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // PPOTOAuth2BrowserSwitchRequest.m
  3. // PayPalOneTouch
  4. //
  5. // Copyright © 2015 PayPal, Inc. All rights reserved.
  6. //
  7. #import "PPOTOAuth2BrowserSwitchRequest.h"
  8. #import "PPOTPersistentRequestData.h"
  9. #if __has_include("PayPalUtils.h")
  10. #import "PPOTTime.h"
  11. #import "PPOTString.h"
  12. #import "PPOTMacros.h"
  13. #import "PPOTJSONHelper.h"
  14. #import "PPOTEncryptionHelper.h"
  15. #else
  16. #import <PayPalUtils/PPOTTime.h>
  17. #import <PayPalUtils/PPOTString.h>
  18. #import <PayPalUtils/PPOTMacros.h>
  19. #import <PayPalUtils/PPOTJSONHelper.h>
  20. #import <PayPalUtils/PPOTEncryptionHelper.h>
  21. #endif
  22. @interface PPOTOAuth2BrowserSwitchRequest ()
  23. @property (nonatomic, readwrite) NSString *msgID;
  24. @end
  25. @implementation PPOTOAuth2BrowserSwitchRequest
  26. - (NSString *)msgID {
  27. if (!_msgID.length) {
  28. _msgID = [PPOTString generateUniquishIdentifier];
  29. }
  30. return _msgID;
  31. }
  32. - (NSDictionary *)payloadDictionary {
  33. NSMutableDictionary *payload = [[super payloadDictionary] mutableCopy];
  34. payload[kPPOTAppSwitchKeyIDKey] = FORCE_VALUE_OR_NULL(self.keyID);
  35. if (self.additionalPayloadAttributes.count) {
  36. for (id key in self.additionalPayloadAttributes) {
  37. // avoid overriding defaults
  38. if (![payload objectForKey:key]) {
  39. payload[key] = FORCE_VALUE_OR_NULL(self.additionalPayloadAttributes[key]);
  40. }
  41. }
  42. }
  43. return payload;
  44. }
  45. - (NSData *)encryptedPayload {
  46. NSMutableDictionary *unencryptedPayload = [NSMutableDictionary dictionaryWithCapacity:6];
  47. unencryptedPayload[kPPOTAppSwitchTimestampKey] = FORCE_VALUE_OR_NULL([[PPOTTime rfc3339DateFormatter] stringFromDate:[NSDate date]]);
  48. unencryptedPayload[kPPOTAppSwitchMsgGUIDKey] = FORCE_VALUE_OR_NULL(self.msgID);
  49. //unencryptedPayload[kPPOTAppSwitchAppGuidKey] = FORCE_VALUE_OR_NULL(self.appGuid);
  50. unencryptedPayload[kPPOTAppSwitchSymKey] = FORCE_VALUE_OR_NULL(self.encryptionKey);
  51. NSString *currentDeviceName = [[UIDevice currentDevice] name];
  52. if (currentDeviceName != nil && ![currentDeviceName isEqualToString:@""]) {
  53. unencryptedPayload[kPPOTAppSwitchKeyDeviceName] = FORCE_VALUE_OR_NULL(currentDeviceName);
  54. }
  55. NSData *data = [NSJSONSerialization dataWithJSONObject:unencryptedPayload options:0 error:nil];
  56. if (!data) {
  57. return nil;
  58. }
  59. NSData *cipherData = [PPOTEncryptionHelper encryptRSAData:data certificate:self.certificate];
  60. return cipherData;
  61. }
  62. - (NSURL *)encodedURL {
  63. NSDictionary *payload = [self payloadDictionary];
  64. NSData *encryptedPayload = [self encryptedPayload];
  65. if (!encryptedPayload) {
  66. return nil;
  67. }
  68. NSURL *url = [PPOTAppSwitchUtil URLAction:self.endpoint callbackURLScheme:self.callbackURLScheme payload:payload];
  69. // cheat for now and add encrypted payload
  70. NSString *urlString = [url absoluteString];
  71. NSString *encodedEncryptedPayload = [encryptedPayload base64EncodedStringWithOptions:0];
  72. encodedEncryptedPayload = [PPOTString stringByURLEncodingAllCharactersInString:encodedEncryptedPayload];
  73. NSString *urlQuery = [NSString stringWithFormat:@"&%@=%@",kPPOTAppSwitchEncryptedPayloadKey, encodedEncryptedPayload];
  74. urlString = [urlString stringByAppendingString:urlQuery];
  75. url = [NSURL URLWithString:urlString];
  76. return url;
  77. }
  78. - (void)addDataToPersistentRequestDataDictionary:(NSMutableDictionary *)requestDataDictionary {
  79. [super addDataToPersistentRequestDataDictionary:requestDataDictionary];
  80. requestDataDictionary[kPPOTRequestDataDataDictionaryMsgIdKey] = FORCE_VALUE_OR_NULL(self.msgID);
  81. requestDataDictionary[kPPOTRequestDataDataDictionaryEncryptionKey] = FORCE_VALUE_OR_NULL(self.encryptionKey);
  82. }
  83. @end