Created
June 30, 2015 17:08
-
-
Save hashmaparraylist/f4dfc42ba2dffe7a2a07 to your computer and use it in GitHub Desktop.
简直亲妈爆炸。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (RACSignal *)authorizeToGithubUsingOauth { | |
__weak GitHubClient *weakSelf = self; | |
RACSignal *authorizeSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { | |
CFUUIDRef uuid = CFUUIDCreate(NULL); | |
NSString *uuidString = CFBridgingRelease(CFUUIDCreateString(NULL, uuid)); | |
CFRelease(uuid); | |
NSCharacterSet *slashSet = [NSCharacterSet characterSetWithCharactersInString:@"/"]; | |
NSString *baseURLString = [BASE_WEB_URL stringByTrimmingCharactersInSet:slashSet]; | |
NSString *urlString = [[NSString alloc] initWithFormat: AUTHORIZE_API, baseURLString, CLIENT_ID, @"gist", uuidString]; | |
NSURL *webUrl = [NSURL URLWithString:urlString]; | |
RACDisposable *callbackDisposable = [[[self.callbackURL flattenMap:^RACStream *(NSURL *url) { | |
// 从callback url中取出temporary code | |
NSMutableDictionary *queryArguments = [[NSMutableDictionary alloc] init]; | |
NSArray *urlComponents = [url.query componentsSeparatedByString:@"&"]; | |
for (NSString *keyValuePair in urlComponents) { | |
NSArray *pairComponents = [keyValuePair componentsSeparatedByString:@"="]; | |
NSString *key = [[pairComponents firstObject] stringByRemovingPercentEncoding]; | |
NSString *value = [[pairComponents lastObject] stringByRemovingPercentEncoding]; | |
[queryArguments setObject:value forKey:key]; | |
} | |
if ([queryArguments[@"state"] isEqual: uuidString]) { | |
return [RACSignal return:queryArguments[@"code"]]; | |
} else { | |
return [RACSignal empty]; | |
} | |
}] | |
take:1] | |
subscribe:subscriber]; | |
// 通过系统默认浏览器,获取Oauth认证的temporary code | |
if(![weakSelf openURL:webUrl]) { | |
[subscriber sendError:[NSError errorWithDomain:GithubClientErrorDomain | |
code:GithubClientErrorOpeningBrowserFailed | |
userInfo:@{ | |
NSLocalizedDescriptionKey : @"无法打开浏览器", | |
NSLocalizedRecoverySuggestionErrorKey : @"请确认你是否设置了默认的浏览器", | |
NSURLErrorKey: webUrl | |
}]]; | |
} | |
return callbackDisposable; | |
}]; | |
// RACSignal *catcher = [[authorizeSignal combineLatestWith:[RACSignal empty]] catch:^RACSignal *(NSError *error) { | |
// return [RACSignal error:error]; | |
// }]; | |
// | |
// [catcher reduceEach:^(NSString *temporaryCode, id object) { | |
// | |
// }]; | |
// authorizeSignal com | |
[[authorizeSignal catch:^RACSignal *(NSError *error) { | |
return [RACSignal error:error]; | |
}] flattenMap:^RACStream *(NSString *temporaryCode) { | |
NSDictionary *params = @{ | |
@"client_id" : CLIENT_ID, | |
@"client_secret" : CLIENT_SECRET, | |
@"code" : temporaryCode | |
}; | |
NSCharacterSet *slashSet = [NSCharacterSet characterSetWithCharactersInString:@"/"]; | |
NSString *baseURLString = [BASE_WEB_URL stringByTrimmingCharactersInSet:slashSet]; | |
NSString *urlString = [[NSString alloc] initWithFormat: ACCESS_TOKEN_API, baseURLString]; | |
[weakSelf postApiWithURL:urlString parameters:params needToken:NO success:^(AFHTTPRequestOperation *operation, id responseObject) { | |
NSDictionary *jsonObject = (NSDictionary *)responseObject; | |
self.token = jsonObject[@"access_token"]; | |
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
return nil; | |
}]; | |
return nil; | |
}]; | |
return nil; | |
} | |
- (RACSubject *)callbackURL { | |
static RACSubject *singleton; | |
static dispatch_once_t pred; | |
dispatch_once(&pred, ^{ | |
singleton = [[RACSubject subject] setNameWithFormat:@"OCTClient.callbackURLs"]; | |
}); | |
return singleton; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment