Ben Copsey

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

@@ -1518,6 +1518,12 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; @@ -1518,6 +1518,12 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
1518 // Prevent more than one request from asking for credentials at once 1518 // Prevent more than one request from asking for credentials at once
1519 [delegateAuthenticationLock lock]; 1519 [delegateAuthenticationLock lock];
1520 1520
  1521 + // We know the credentials we just presented are bad. If they are the same as the session credentials, we should clear those too.
  1522 + if ([self proxyCredentials] == sessionProxyCredentials) {
  1523 + [ASIHTTPRequest setSessionProxyCredentials:nil];
  1524 + }
  1525 + [self setProxyCredentials:nil];
  1526 +
1521 // If the user cancelled authentication via a dialog presented by another request, our queue may have cancelled us 1527 // If the user cancelled authentication via a dialog presented by another request, our queue may have cancelled us
1522 if ([self error] || [self isCancelled]) { 1528 if ([self error] || [self isCancelled]) {
1523 [delegateAuthenticationLock unlock]; 1529 [delegateAuthenticationLock unlock];
@@ -1531,8 +1537,8 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; @@ -1531,8 +1537,8 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
1531 return; 1537 return;
1532 } 1538 }
1533 1539
1534 - [self setProxyCredentials:nil];  
1535 [self setLastActivityTime:nil]; 1540 [self setLastActivityTime:nil];
  1541 +
1536 if ([self askDelegateForProxyCredentials]) { 1542 if ([self askDelegateForProxyCredentials]) {
1537 [self attemptToApplyProxyCredentialsAndResume]; 1543 [self attemptToApplyProxyCredentialsAndResume];
1538 [delegateAuthenticationLock unlock]; 1544 [delegateAuthenticationLock unlock];
@@ -1708,6 +1714,12 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; @@ -1708,6 +1714,12 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
1708 // Prevent more than one request from asking for credentials at once 1714 // Prevent more than one request from asking for credentials at once
1709 [delegateAuthenticationLock lock]; 1715 [delegateAuthenticationLock lock];
1710 1716
  1717 + // We know the credentials we just presented are bad. If they are the same as the session credentials, we should clear those too.
  1718 + if ([self requestCredentials] == sessionCredentials) {
  1719 + [ASIHTTPRequest setSessionCredentials:nil];
  1720 + }
  1721 + [self setRequestCredentials:nil];
  1722 +
1711 // If the user cancelled authentication via a dialog presented by another request, our queue may have cancelled us 1723 // If the user cancelled authentication via a dialog presented by another request, our queue may have cancelled us
1712 if ([self error] || [self isCancelled]) { 1724 if ([self error] || [self isCancelled]) {
1713 [delegateAuthenticationLock unlock]; 1725 [delegateAuthenticationLock unlock];
@@ -1721,7 +1733,7 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; @@ -1721,7 +1733,7 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
1721 return; 1733 return;
1722 } 1734 }
1723 1735
1724 - [self setRequestCredentials:nil]; 1736 +
1725 1737
1726 [self setLastActivityTime:nil]; 1738 [self setLastActivityTime:nil];
1727 1739
@@ -397,7 +397,18 @@ IMPORTANT @@ -397,7 +397,18 @@ IMPORTANT
397 [request setUsername:@"secret_username"]; 397 [request setUsername:@"secret_username"];
398 [request setPassword:@"secret_password"]; 398 [request setPassword:@"secret_password"];
399 [request retryUsingSuppliedCredentials]; 399 [request retryUsingSuppliedCredentials];
400 - 400 +
  401 + } else if ([[[request userInfo] objectForKey:@"test"] isEqualToString:@"delegate-auth-failure"]) {
  402 + authenticationPromptCount++;
  403 + if (authenticationPromptCount == 5) {
  404 + [request setUsername:@"secret_username"];
  405 + [request setPassword:@"secret_password"];
  406 + } else {
  407 + [request setUsername:@"wrong_username"];
  408 + [request setPassword:@"wrong_password"];
  409 + }
  410 + [request retryUsingSuppliedCredentials];
  411 +
401 412
402 // testProgressWithAuthentication will set a userInfo dictionary on the main request, to tell us not to supply credentials 413 // testProgressWithAuthentication will set a userInfo dictionary on the main request, to tell us not to supply credentials
403 } else if (![request mainRequest] || ![[request mainRequest] userInfo]) { 414 } else if (![request mainRequest] || ![[request mainRequest] userInfo]) {
@@ -829,6 +840,7 @@ IMPORTANT @@ -829,6 +840,7 @@ IMPORTANT
829 // Test for a bug that used to exist where the temporary file used to store the request body would be removed when authentication failed 840 // Test for a bug that used to exist where the temporary file used to store the request body would be removed when authentication failed
830 - (void)testPOSTWithAuthentication 841 - (void)testPOSTWithAuthentication
831 { 842 {
  843 + [[self postQueue] cancelAllOperations];
832 [self setPostQueue:[ASINetworkQueue queue]]; 844 [self setPostQueue:[ASINetworkQueue queue]];
833 [[self postQueue] setRequestDidFinishSelector:@selector(postDone:)]; 845 [[self postQueue] setRequestDidFinishSelector:@selector(postDone:)];
834 [[self postQueue] setDelegate:self]; 846 [[self postQueue] setDelegate:self];
@@ -846,6 +858,21 @@ IMPORTANT @@ -846,6 +858,21 @@ IMPORTANT
846 GHAssertTrue(success,@"Didn't post correct data"); 858 GHAssertTrue(success,@"Didn't post correct data");
847 } 859 }
848 860
  861 +- (void)testDelegateAuthenticationFailure
  862 +{
  863 + [[self postQueue] cancelAllOperations];
  864 + [self setPostQueue:[ASINetworkQueue queue]];
  865 + [[self postQueue] setRequestDidFinishSelector:@selector(postDone:)];
  866 + [[self postQueue] setDelegate:self];
  867 +
  868 + ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/Tests/post_with_authentication"]];
  869 + [request setPostValue:@"This is the first item" forKey:@"first"];
  870 + [request setData:[@"This is the second item" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"second"];
  871 + [request setUserInfo:[NSDictionary dictionaryWithObject:@"delegate-auth-failure" forKey:@"test"]];
  872 + [[self postQueue] addOperation:request];
  873 + [[self postQueue] go];
  874 +}
  875 +
849 @synthesize immediateCancelQueue; 876 @synthesize immediateCancelQueue;
850 @synthesize failedRequests; 877 @synthesize failedRequests;
851 @synthesize finishedRequests; 878 @synthesize finishedRequests;