Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // VKSocialLoginViewController.m
- // Vkino
- //
- // Created by Nikita Rodin on 4/29/13.
- // Copyright (c) 2013 Artezio. All rights reserved.
- //
- #import "VKSocialLoginViewController.h"
- #import "VKConnectionManager.h"
- #import "Constants.h"
- #import "VKBarButtonItem.h"
- @interface VKSocialLoginViewController ()
- @end
- @implementation VKSocialLoginViewController
- #if USE_TEST_SERVER
- #define kSocialLoginURLPattern @"http://mobile.dev.kinohod.ru/auth/login/%@/?retpath=%@"
- #define kSocialLoginCloseURL @"http://mobile.dev.kinohod.ru/auth/closeurl"
- #else
- #define kSocialLoginURLPattern @"http://kinohod.ru/auth/login/%@/?retpath=%@"
- #define kSocialLoginCloseURL @"http://kinohod.ru/auth/closeurl"
- #endif //USE_TEST_SERVER
- #define kSocialFacebookURL @"https://graph.facebook.com/oauth/authorize"
- #define kSocialVkontaktURL @"http://api.vkontakte.ru/oauth/authorize"
- #define kSocialOdkURL @"http://www.odnoklassniki.ru/oauth/authorize"
- #define kUserIDHeaderKey @"X-User_ID"
- #ifdef DEBUG
- #define kBulletStormerSexyID @"U2FsdGVkX18Um8MZt3p3cTDK7dQ35Ol-qi2gFpkKvWY"
- #endif
- - (void)refresh
- {
- NSAssert(_networkAlias, @"%s.%d: cannot proceed w/o network alias", __FILE__, __LINE__);
- #define FACK_U_KINOHOD_WAI_U_NO_WORK 0
- #if FACK_U_KINOHOD_WAI_U_NO_WORK && DEBUG
- NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@=%@", kSocialLoginCloseURL, kUserIDHeaderKey, kBulletStormerSexyID]] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:kDefaultTimeoutInterval];
- #else
- NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:kSocialLoginURLPattern, _networkAlias, kSocialLoginCloseURL]] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:kDefaultTimeoutInterval];
- NSHTTPCookieStorage* cookiezJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
- NSArray* cookiez = nil;
- #if USE_TEST_SERVER
- NSString* base64Credentials = [[[NSString stringWithFormat:@"%@:%@", kUsername, kPassword] dataUsingEncoding:NSASCIIStringEncoding] base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
- [request addValue:[NSString stringWithFormat:@"Basic %@", base64Credentials] forHTTPHeaderField:@"Authorization"];
- #endif //USE_TEST_SERVER
- NSArray* urls = @[ request.URL, [NSURL URLWithString:kSocialFacebookURL], [NSURL URLWithString:kSocialVkontaktURL], [NSURL URLWithString:kSocialOdkURL] ];
- for (NSURL* url in urls) {
- cookiez = [cookiezJar cookiesForURL:url];
- for (NSHTTPCookie* cookie in cookiez)
- [cookiezJar deleteCookie:cookie];
- }
- #endif //FACK_U_KINOHOD_WAI_U_NO_WORK && DEBUG
- [_webView loadRequest:request];
- }
- - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
- {
- #if __SHOW_HEADERS
- VKLog(@"social login proceeding to url %@\nwith headers: %@\n cookiez: %@", request.URL.absoluteString, request.allHTTPHeaderFields, [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:request.URL]);
- #else
- VKLog(@"social login proceeding to url %@", request.URL.absoluteString);
- #endif
- if ([request.URL.absoluteString hasPrefix:kSocialLoginCloseURL]) {
- NSRange rangeOfParam = [request.URL.absoluteString rangeOfString:@"?"];
- NSString* paramString = rangeOfParam.location != NSNotFound ? [request.URL.absoluteString substringFromIndex:rangeOfParam.location + 1] : nil;
- NSRange rangeOfTokenKey = [paramString rangeOfString:kUserIDHeaderKey];
- NSString* token = nil;
- if (rangeOfTokenKey.location != NSNotFound) {
- NSRange rangeOfToken = [paramString rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"&#"] options:0 range:NSMakeRange(rangeOfTokenKey.location, paramString.length)];
- if (rangeOfToken.location != NSNotFound)
- rangeOfToken.length = rangeOfToken.location;
- else
- rangeOfToken.length = paramString.length - rangeOfTokenKey.location - 1 - kUserIDHeaderKey.length; // +1 for '=' sign
- rangeOfToken.location = rangeOfTokenKey.location + kUserIDHeaderKey.length + 1;
- token = [paramString substringWithRange:rangeOfToken];
- }
- if (token.length)
- [_delegate socialLogin:self didSucceedWithToken:token];
- else if ([_delegate respondsToSelector:@selector(socialLoginDidFail:)])
- [_delegate socialLoginDidFail:self];
- [self dismissViewControllerAnimated:YES completion:NULL];
- return NO;
- }
- #if USE_TEST_SERVER
- if (![super webView:webView shouldStartLoadWithRequest:request navigationType:navigationType]) {
- return NO;
- }
- #endif //USE_TEST_SERVER
- return YES;
- }
- - (void)viewDidLoad
- {
- self.screenName = @"social login";
- [super viewDidLoad];
- self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"CANCEL", ) style:UIBarButtonItemStylePlain target:self action:@selector(cancelClicked)];
- }
- - (void)cancelClicked
- {
- [self dismissViewControllerAnimated:YES completion:Nil];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement