Adding credential property to AFURLConnectionOperation

This commit is contained in:
Mattt Thompson 2013-01-04 12:41:25 -08:00
parent 121ef7afa8
commit 1d243f7794
2 changed files with 19 additions and 2 deletions

View file

@ -124,6 +124,18 @@
*/
@property (readonly, nonatomic, assign) NSStringEncoding responseStringEncoding;
///--------------------------------
/// @name Accessing URL Credentials
///--------------------------------
/**
The credential used for authentication challenges in `-connection:didReceiveAuthenticationChallenge:`.
@discussion This will be overridden by any shared credentials that exist for the username or password of the request URL, if present.
*/
@property (nonatomic, strong) NSURLCredential *credential;
///------------------------
/// @name Accessing Streams
///------------------------

View file

@ -144,6 +144,7 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat
@synthesize totalBytesRead = _totalBytesRead;
@dynamic inputStream;
@synthesize outputStream = _outputStream;
@synthesize credential = _credential;
@synthesize userInfo = _userInfo;
@synthesize backgroundTaskIdentifier = _backgroundTaskIdentifier;
@synthesize uploadProgress = _uploadProgress;
@ -545,10 +546,10 @@ didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
} else {
if ([challenge previousFailureCount] == 0) {
NSURLCredential *credential = nil;
NSString *username = (__bridge_transfer NSString *)CFURLCopyUserName((__bridge CFURLRef)[self.request URL]);
NSString *password = (__bridge_transfer NSString *)CFURLCopyPassword((__bridge CFURLRef)[self.request URL]);
if (username && password) {
credential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceNone];
} else if (username) {
@ -556,6 +557,10 @@ didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
} else {
credential = [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:[challenge protectionSpace]];
}
if (!credential) {
credential = self.credential;
}
if (credential) {
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];