Ben Copsey

Fix bug with delegate authentication that would be triggered when presenting the wrong credentials

... ... @@ -1518,6 +1518,12 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
// Prevent more than one request from asking for credentials at once
[delegateAuthenticationLock lock];
// We know the credentials we just presented are bad. If they are the same as the session credentials, we should clear those too.
if ([self proxyCredentials] == sessionProxyCredentials) {
[ASIHTTPRequest setSessionProxyCredentials:nil];
}
[self setProxyCredentials:nil];
// If the user cancelled authentication via a dialog presented by another request, our queue may have cancelled us
if ([self error] || [self isCancelled]) {
[delegateAuthenticationLock unlock];
... ... @@ -1531,8 +1537,8 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
return;
}
[self setProxyCredentials:nil];
[self setLastActivityTime:nil];
if ([self askDelegateForProxyCredentials]) {
[self attemptToApplyProxyCredentialsAndResume];
[delegateAuthenticationLock unlock];
... ... @@ -1708,6 +1714,12 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
// Prevent more than one request from asking for credentials at once
[delegateAuthenticationLock lock];
// We know the credentials we just presented are bad. If they are the same as the session credentials, we should clear those too.
if ([self requestCredentials] == sessionCredentials) {
[ASIHTTPRequest setSessionCredentials:nil];
}
[self setRequestCredentials:nil];
// If the user cancelled authentication via a dialog presented by another request, our queue may have cancelled us
if ([self error] || [self isCancelled]) {
[delegateAuthenticationLock unlock];
... ... @@ -1721,7 +1733,7 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
return;
}
[self setRequestCredentials:nil];
[self setLastActivityTime:nil];
... ...
... ... @@ -397,7 +397,18 @@ IMPORTANT
[request setUsername:@"secret_username"];
[request setPassword:@"secret_password"];
[request retryUsingSuppliedCredentials];
} else if ([[[request userInfo] objectForKey:@"test"] isEqualToString:@"delegate-auth-failure"]) {
authenticationPromptCount++;
if (authenticationPromptCount == 5) {
[request setUsername:@"secret_username"];
[request setPassword:@"secret_password"];
} else {
[request setUsername:@"wrong_username"];
[request setPassword:@"wrong_password"];
}
[request retryUsingSuppliedCredentials];
// testProgressWithAuthentication will set a userInfo dictionary on the main request, to tell us not to supply credentials
} else if (![request mainRequest] || ![[request mainRequest] userInfo]) {
... ... @@ -829,6 +840,7 @@ IMPORTANT
// Test for a bug that used to exist where the temporary file used to store the request body would be removed when authentication failed
- (void)testPOSTWithAuthentication
{
[[self postQueue] cancelAllOperations];
[self setPostQueue:[ASINetworkQueue queue]];
[[self postQueue] setRequestDidFinishSelector:@selector(postDone:)];
[[self postQueue] setDelegate:self];
... ... @@ -846,6 +858,21 @@ IMPORTANT
GHAssertTrue(success,@"Didn't post correct data");
}
- (void)testDelegateAuthenticationFailure
{
[[self postQueue] cancelAllOperations];
[self setPostQueue:[ASINetworkQueue queue]];
[[self postQueue] setRequestDidFinishSelector:@selector(postDone:)];
[[self postQueue] setDelegate:self];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/Tests/post_with_authentication"]];
[request setPostValue:@"This is the first item" forKey:@"first"];
[request setData:[@"This is the second item" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"second"];
[request setUserInfo:[NSDictionary dictionaryWithObject:@"delegate-auth-failure" forKey:@"test"]];
[[self postQueue] addOperation:request];
[[self postQueue] go];
}
@synthesize immediateCancelQueue;
@synthesize failedRequests;
@synthesize finishedRequests;
... ...