PPOTCore.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // PPOTCore.m
  3. // PayPalOneTouch
  4. //
  5. // Copyright © 2015 PayPal, Inc. All rights reserved.
  6. //
  7. #import "PPOTCore.h"
  8. #import "PPOTCore_Internal.h"
  9. #import "PPOTResult_Internal.h"
  10. #import "PPOTRequest_Internal.h"
  11. #import "PPOTConfiguration.h"
  12. #import "PPOTPersistentRequestData.h"
  13. #if __has_include("PayPalUtils.h")
  14. #import "PPOTDevice.h"
  15. #import "PPOTMacros.h"
  16. #import "PPOTVersion.h"
  17. #else
  18. #import <PayPalUtils/PPOTDevice.h>
  19. #import <PayPalUtils/PPOTMacros.h>
  20. #import <PayPalUtils/PPOTVersion.h>
  21. #endif
  22. #if __has_include("PayPalDataCollector.h")
  23. #import "PPDataCollector.h"
  24. #else
  25. #import <PayPalDataCollector/PPDataCollector.h>
  26. #endif
  27. // PayPalTouch v1 version
  28. #import "PPOTAppSwitchUtil.h"
  29. #define kPPOTSafariViewService CARDIO_STR(@"com.apple.safariviewservice")
  30. #define kPPOTSafariSourceApplication CARDIO_STR(@"com.apple.mobilesafari")
  31. @implementation PPOTCore
  32. + (void)initialize {
  33. if (self == [PPOTCore class]) {
  34. [PPOTConfiguration updateCacheAsNecessary]; // called by all public methods
  35. }
  36. }
  37. + (BOOL)doesApplicationSupportOneTouchCallbackURLScheme:(NSString *)callbackURLScheme {
  38. [PPOTConfiguration updateCacheAsNecessary]; // called by all public methods
  39. BOOL doesSupport = NO;
  40. // checks the callbackURLScheme is present and app responds to it.
  41. doesSupport = [PPOTAppSwitchUtil isCallbackURLSchemeValid:callbackURLScheme];
  42. return doesSupport;
  43. }
  44. + (BOOL)isWalletAppInstalled {
  45. [PPOTConfiguration updateCacheAsNecessary]; // called by all public methods
  46. return NO;
  47. }
  48. + (BOOL)canParseURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication {
  49. [PPOTConfiguration updateCacheAsNecessary]; // called by all public methods
  50. BOOL canHandle = NO;
  51. canHandle = ([PPOTCore isSourceApplicationValid:sourceApplication]
  52. && [PPOTCore isValidURLAction:url]);
  53. return canHandle;
  54. }
  55. + (void)parseResponseURL:(NSURL *)url completionBlock:(PPOTCompletionBlock)completionBlock {
  56. [PPOTConfiguration updateCacheAsNecessary]; // called by all public methods
  57. PPAssert(completionBlock, @"parseOneTouchResponseURL:completionBlock: completionBlock is required");
  58. [PPOTResult parseURL:url completionBlock:completionBlock];
  59. }
  60. + (void)redirectURLsForCallbackURLScheme:(NSString *)callbackURLScheme withReturnURL:(NSString * __autoreleasing *)returnURL withCancelURL:(NSString * __autoreleasing *)cancelURL {
  61. [PPOTConfiguration updateCacheAsNecessary]; // called by all public methods
  62. [PPOTAppSwitchUtil redirectURLsForCallbackURLScheme:callbackURLScheme withReturnURL:returnURL withCancelURL:cancelURL];
  63. }
  64. + (BOOL)isSourceApplicationValid:(NSString *)sourceApplication {
  65. if (!sourceApplication.length) {
  66. return NO;
  67. }
  68. PPOTPersistentRequestData *persistentRequestData = [PPOTPersistentRequestData fetch];
  69. PPOTConfigurationRecipe *configurationRecipe = persistentRequestData.configurationRecipe;
  70. if (!configurationRecipe) {
  71. return NO;
  72. }
  73. sourceApplication = [sourceApplication lowercaseString];
  74. for (NSString *bundleID in configurationRecipe.targetAppBundleIDs) {
  75. NSUInteger asteriskLocation = [bundleID rangeOfString:@"*"].location;
  76. NSString *bundleIDMask = asteriskLocation == NSNotFound ? bundleID : [bundleID substringToIndex:asteriskLocation];
  77. if ([sourceApplication hasPrefix:bundleIDMask]) {
  78. return YES;
  79. }
  80. }
  81. if ([sourceApplication isEqualToString:kPPOTSafariSourceApplication] || [sourceApplication isEqualToString:kPPOTSafariViewService] ) {
  82. return YES;
  83. }
  84. return NO;
  85. }
  86. + (BOOL)isValidURLAction:(NSURL *)urlAction {
  87. if (![PPOTAppSwitchUtil isValidURLAction:urlAction]) {
  88. return NO;
  89. }
  90. PPOTPersistentRequestData *persistentRequestData = [PPOTPersistentRequestData fetch];
  91. PPOTConfigurationRecipe *configurationRecipe = persistentRequestData.configurationRecipe;
  92. NSDictionary *queryDictionary = [PPOTAppSwitchUtil parseQueryString:[urlAction query]];
  93. if (!persistentRequestData || !configurationRecipe || !queryDictionary) {
  94. return NO;
  95. }
  96. if ([configurationRecipe.protocolVersion integerValue] == 0) {
  97. NSString *requestHermesToken = persistentRequestData.requestData[kPPOTRequestDataDataDictionaryHermesTokenKey];
  98. // NSString *responseHermesToken = queryDictionary[kPPOTAppSwitchHermesBATokenKey];
  99. // if (responseHermesToken == nil) {
  100. // responseHermesToken = queryDictionary[kPPOTAppSwitchHermesTokenKey];
  101. // }
  102. if (![requestHermesToken length]) {
  103. return NO;
  104. }
  105. } else {
  106. if (![queryDictionary[kPPOTAppSwitchPayloadKey] length]) {
  107. return NO;
  108. }
  109. // For now, we only check the value of x-source in the case of Wallet-switch.
  110. // Ultimately, we might want to agree on what x-source should be for a browser-switch (Hermes).
  111. if (configurationRecipe.target == PPOTRequestTargetOnDeviceApplication &&
  112. ![self isSourceApplicationValid:queryDictionary[kPPOTAppSwitchXSourceKey]]) {
  113. return NO;
  114. }
  115. }
  116. return YES;
  117. }
  118. + (NSString *)libraryVersion {
  119. [PPOTConfiguration updateCacheAsNecessary]; // called by all public methods
  120. return PayPalOTVersion();
  121. }
  122. @end