Add NSURLSession support (BITAuthenticator)

This commit is contained in:
Christoph Wendt 2015-09-02 13:35:59 -07:00
parent 37727b46f4
commit dd101d0e59

View File

@ -447,34 +447,53 @@ static unsigned char kBITPNGEndChunk[4] = {0x49, 0x45, 0x4e, 0x44};
NSParameterAssert(email && email.length); NSParameterAssert(email && email.length);
NSParameterAssert(self.identificationType == BITAuthenticatorIdentificationTypeHockeyAppEmail || (password && password.length)); NSParameterAssert(self.identificationType == BITAuthenticatorIdentificationTypeHockeyAppEmail || (password && password.length));
NSURLRequest* request = [self requestForAuthenticationEmail:email password:password]; NSURLRequest* request = [self requestForAuthenticationEmail:email password:password];
__weak typeof (self) weakSelf = self; __weak typeof (self) weakSelf = self;
BITHTTPOperation *operation = [self.hockeyAppClient operationWithURLRequest:request
completion:^(BITHTTPOperation *operation, NSData* responseData, NSError *error) { id nsurlsessionClass = NSClassFromString(@"NSURLSessionUploadTask");
typeof (self) strongSelf = weakSelf; if (nsurlsessionClass && !bit_isRunningInAppExtension()) {
NSError *authParseError = nil; NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSString *authToken = [strongSelf.class authenticationTokenFromURLResponse:operation.response NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
data:responseData
error:&authParseError]; NSURLSessionDataTask *task = [session dataTaskWithRequest:request
BOOL identified; completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
if(authToken) { typeof (self) strongSelf = weakSelf;
identified = YES; NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response;
[strongSelf storeInstallationIdentifier:authToken withType:strongSelf.identificationType]; [strongSelf handleAuthenticationWithResponse:httpResponse email:email data:data completion:completion];
[strongSelf dismissAuthenticationControllerAnimated:YES completion:nil]; }];
strongSelf->_authenticationController = nil; [task resume];
BOOL success = [self addStringValueToKeychain:email forKey:kBITAuthenticatorUserEmailKey]; }else{
if (!success) { BITHTTPOperation *operation = [self.hockeyAppClient operationWithURLRequest:request
[strongSelf alertOnFailureStoringTokenInKeychain]; completion:^(BITHTTPOperation *operation, NSData* responseData, NSError *error) {
} typeof (self) strongSelf = weakSelf;
} else { [strongSelf handleAuthenticationWithResponse:operation.response email:email data:responseData completion:completion];
identified = NO; }];
} [self.hockeyAppClient enqeueHTTPOperation:operation];
strongSelf.identified = identified; }
completion(identified, authParseError); }
if(strongSelf.identificationCompletion) strongSelf.identificationCompletion(identified, authParseError);
strongSelf.identificationCompletion = nil; - (void)handleAuthenticationWithResponse:(NSHTTPURLResponse *)response email:(NSString *)email data:(NSData *)data completion:(void (^)(BOOL, NSError *))completion{
NSError *authParseError = nil;
}]; NSString *authToken = [self.class authenticationTokenFromURLResponse:response
[self.hockeyAppClient enqeueHTTPOperation:operation]; data:data
error:&authParseError];
BOOL identified;
if(authToken) {
identified = YES;
[self storeInstallationIdentifier:authToken withType:self.identificationType];
[self dismissAuthenticationControllerAnimated:YES completion:nil];
self->_authenticationController = nil;
BOOL success = [self addStringValueToKeychain:email forKey:kBITAuthenticatorUserEmailKey];
if (!success) {
[self alertOnFailureStoringTokenInKeychain];
}
} else {
identified = NO;
}
self.identified = identified;
completion(identified, authParseError);
if(self.identificationCompletion) self.identificationCompletion(identified, authParseError);
self.identificationCompletion = nil;
} }
- (NSURLRequest *) requestForAuthenticationEmail:(NSString*) email password:(NSString*) password { - (NSURLRequest *) requestForAuthenticationEmail:(NSString*) email password:(NSString*) password {