add validation logic

This commit is contained in:
Stephan Diederich 2013-08-11 21:38:02 +02:00
parent 039902da02
commit 2a3a8d62cb

View File

@ -99,15 +99,59 @@ static NSString* const kBITAuthenticatorLastAuthenticatedVersionKey = @"BITAuthe
} }
}]; }];
} else { } else {
NSString *validationEndpoint = @"validate";
__weak typeof (self) weakSelf = self;
[self getPath:validationEndpoint
completion:^(BITHTTPOperation *operation, id response, NSError *error) {
typeof (self) strongSelf = weakSelf;
if(nil == response) {
NSDictionary *userInfo = nil;
if(error) {
userInfo = @{NSUnderlyingErrorKey : error};
}
NSError *error = [NSError errorWithDomain:kBITAuthenticatorErrorDomain NSError *error = [NSError errorWithDomain:kBITAuthenticatorErrorDomain
code:BITAuthenticatorNetworkError code:BITAuthenticatorNetworkError
userInfo:nil]; userInfo:userInfo];
if(completion) { [strongSelf validationFailedWithError:error];
completion(NO, error);
} else { } else {
[self.delegate authenticator:self failedToValidateInstallationWithError:error]; NSError *validationParseError = nil;
BOOL isValidated = [strongSelf.class isValidationResponseValid:response error:&validationParseError];
if(isValidated) {
[strongSelf validationSucceeded];
} else {
[strongSelf validationFailedWithError:validationParseError];
} }
} }
}];
}
}
+ (BOOL) isValidationResponseValid:(id) response error:(NSError **) error {
NSParameterAssert(response);
NSError *jsonParseError = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:response
options:0
error:&jsonParseError];
if(nil == jsonObject) {
if(error) {
*error = [NSError errorWithDomain:kBITAuthenticatorErrorDomain
code:BITAuthenticatorAPIServerReturnedInvalidRespone
userInfo:(jsonParseError ? @{NSUnderlyingErrorKey : jsonParseError} : nil)];
}
return NO;
}
if(![jsonObject isKindOfClass:[NSDictionary class]]) {
if(error) {
*error = [NSError errorWithDomain:kBITAuthenticatorErrorDomain
code:BITAuthenticatorAPIServerReturnedInvalidRespone
userInfo:nil];
}
return NO;
}
//TODO: add proper validation
return [jsonObject[@"isValid"] boolValue];
} }
- (void)authenticateWithCompletion:(tAuthenticationCompletion)completion { - (void)authenticateWithCompletion:(tAuthenticationCompletion)completion {