Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  VKSocialLoginViewController.m
  3. //  Vkino
  4. //
  5. //  Created by Nikita Rodin on 4/29/13.
  6. //  Copyright (c) 2013 Artezio. All rights reserved.
  7. //
  8.  
  9. #import "VKSocialLoginViewController.h"
  10. #import "VKConnectionManager.h"
  11. #import "Constants.h"
  12. #import "VKBarButtonItem.h"
  13.  
  14. @interface VKSocialLoginViewController ()
  15.  
  16. @end
  17.  
  18. @implementation VKSocialLoginViewController
  19.  
  20. #if USE_TEST_SERVER
  21. #define kSocialLoginURLPattern @"http://mobile.dev.kinohod.ru/auth/login/%@/?retpath=%@"
  22. #define kSocialLoginCloseURL @"http://mobile.dev.kinohod.ru/auth/closeurl"
  23. #else
  24. #define kSocialLoginURLPattern @"http://kinohod.ru/auth/login/%@/?retpath=%@"
  25. #define kSocialLoginCloseURL @"http://kinohod.ru/auth/closeurl"
  26. #endif //USE_TEST_SERVER
  27.  
  28. #define kSocialFacebookURL @"https://graph.facebook.com/oauth/authorize"
  29. #define kSocialVkontaktURL @"http://api.vkontakte.ru/oauth/authorize"
  30. #define kSocialOdkURL @"http://www.odnoklassniki.ru/oauth/authorize"
  31.  
  32. #define kUserIDHeaderKey @"X-User_ID"
  33.  
  34. #ifdef DEBUG
  35. #define kBulletStormerSexyID @"U2FsdGVkX18Um8MZt3p3cTDK7dQ35Ol-qi2gFpkKvWY"
  36. #endif
  37.  
  38. - (void)refresh
  39. {
  40.     NSAssert(_networkAlias, @"%s.%d: cannot proceed w/o network alias", __FILE__, __LINE__);
  41. #define FACK_U_KINOHOD_WAI_U_NO_WORK 0
  42. #if FACK_U_KINOHOD_WAI_U_NO_WORK && DEBUG
  43.     NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@=%@", kSocialLoginCloseURL, kUserIDHeaderKey, kBulletStormerSexyID]] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:kDefaultTimeoutInterval];
  44. #else
  45.     NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:kSocialLoginURLPattern, _networkAlias, kSocialLoginCloseURL]] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:kDefaultTimeoutInterval];
  46.  
  47.     NSHTTPCookieStorage* cookiezJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
  48.     NSArray* cookiez = nil;
  49. #if USE_TEST_SERVER
  50.     NSString* base64Credentials = [[[NSString stringWithFormat:@"%@:%@", kUsername, kPassword] dataUsingEncoding:NSASCIIStringEncoding] base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
  51.     [request addValue:[NSString stringWithFormat:@"Basic %@", base64Credentials] forHTTPHeaderField:@"Authorization"];
  52. #endif //USE_TEST_SERVER
  53.  
  54.     NSArray* urls = @[ request.URL, [NSURL URLWithString:kSocialFacebookURL], [NSURL URLWithString:kSocialVkontaktURL], [NSURL URLWithString:kSocialOdkURL] ];
  55.     for (NSURL* url in urls) {
  56.         cookiez = [cookiezJar cookiesForURL:url];
  57.         for (NSHTTPCookie* cookie in cookiez)
  58.             [cookiezJar deleteCookie:cookie];
  59.     }
  60.  
  61. #endif //FACK_U_KINOHOD_WAI_U_NO_WORK && DEBUG
  62.  
  63.     [_webView loadRequest:request];
  64. }
  65.  
  66. - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
  67. {
  68. #if __SHOW_HEADERS
  69.     VKLog(@"social login proceeding to url %@\nwith headers: %@\n cookiez: %@", request.URL.absoluteString, request.allHTTPHeaderFields, [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:request.URL]);
  70. #else
  71.     VKLog(@"social login proceeding to url %@", request.URL.absoluteString);
  72. #endif
  73.     if ([request.URL.absoluteString hasPrefix:kSocialLoginCloseURL]) {
  74.         NSRange rangeOfParam = [request.URL.absoluteString rangeOfString:@"?"];
  75.         NSString* paramString = rangeOfParam.location != NSNotFound ? [request.URL.absoluteString substringFromIndex:rangeOfParam.location + 1] : nil;
  76.         NSRange rangeOfTokenKey = [paramString rangeOfString:kUserIDHeaderKey];
  77.         NSString* token = nil;
  78.         if (rangeOfTokenKey.location != NSNotFound) {
  79.             NSRange rangeOfToken = [paramString rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"&#"] options:0 range:NSMakeRange(rangeOfTokenKey.location, paramString.length)];
  80.             if (rangeOfToken.location != NSNotFound)
  81.                 rangeOfToken.length = rangeOfToken.location;
  82.             else
  83.                 rangeOfToken.length = paramString.length - rangeOfTokenKey.location - 1 - kUserIDHeaderKey.length; // +1 for '=' sign
  84.             rangeOfToken.location = rangeOfTokenKey.location + kUserIDHeaderKey.length + 1;
  85.             token = [paramString substringWithRange:rangeOfToken];
  86.         }
  87.  
  88.         if (token.length)
  89.             [_delegate socialLogin:self didSucceedWithToken:token];
  90.         else if ([_delegate respondsToSelector:@selector(socialLoginDidFail:)])
  91.             [_delegate socialLoginDidFail:self];
  92.         [self dismissViewControllerAnimated:YES completion:NULL];
  93.         return NO;
  94.     }
  95.  
  96. #if USE_TEST_SERVER
  97.     if (![super webView:webView shouldStartLoadWithRequest:request navigationType:navigationType]) {
  98.         return NO;
  99.     }
  100. #endif //USE_TEST_SERVER
  101.  
  102.     return YES;
  103. }
  104.  
  105. - (void)viewDidLoad
  106. {
  107.     self.screenName = @"social login";
  108.     [super viewDidLoad];
  109.     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"CANCEL", ) style:UIBarButtonItemStylePlain target:self action:@selector(cancelClicked)];
  110. }
  111.  
  112. - (void)cancelClicked
  113. {
  114.     [self dismissViewControllerAnimated:YES completion:Nil];
  115. }
  116.  
  117. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement