Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use serveral URLCredentials for different protection spaces #3407

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 26 additions & 1 deletion Source/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public class Request {
var responseSerializerProcessingFinished = false
/// `URLCredential` used for authentication challenges.
var credential: URLCredential?
/// All `URLCredential` added by a authenticationMethod of the protection space used for authentication challenges.
var credentialsByAuthenticationMethod: [String: URLCredential] = [:]
/// All `URLRequest`s created by Alamofire on behalf of the `Request`.
var requests: [URLRequest] = []
/// All `URLSessionTask`s created by Alamofire on behalf of the `Request`.
Expand Down Expand Up @@ -183,7 +185,12 @@ public class Request {
get { mutableState.credential }
set { mutableState.credential = newValue }
}


public private(set) var credentialsByAuthenticationMethod: [String:URLCredential] {
get { mutableState.credentialsByAuthenticationMethod }
set { mutableState.credentialsByAuthenticationMethod = newValue }
}

// MARK: Validators

/// `Validator` callback closures that store the validation calls enqueued.
Expand Down Expand Up @@ -757,6 +764,19 @@ public class Request {
return self
}

/// Associates the provided credential for a specific authenticationMethod with the instance.
///
/// - Parameter credential: The `URLCredential`.
/// - Parameter authenticationMethod: The authenticationMethod as `String`. See Foundation.NSURLProtectionSpace for possible values.
///
/// - Returns: The instance.
@discardableResult
public func authenticate(with credential: URLCredential, for authenticationMethod: String) -> Self {
mutableState.credentialsByAuthenticationMethod[authenticationMethod] = credential

return self
}

/// Sets a closure to be called periodically during the lifecycle of the instance as data is read from the server.
///
/// - Note: Only the last closure provided is used.
Expand Down Expand Up @@ -985,6 +1005,11 @@ extension Request {
} else {
if let credential = credential, let user = credential.user, let password = credential.password {
components.append("-u \(user):\(password)")
} else {
for credential in credentialsByAuthenticationMethod.values {
guard let user = credential.user, let password = credential.password else { continue }
components.append("-u \(user):\(password)")
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions Source/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1240,8 +1240,9 @@ extension Session: SessionStateProvider {

func credential(for task: URLSessionTask, in protectionSpace: URLProtectionSpace) -> URLCredential? {
dispatchPrecondition(condition: .onQueue(rootQueue))

return requestTaskMap[task]?.credential ??

return requestTaskMap[task]?.credentialsByAuthenticationMethod[protectionSpace.authenticationMethod] ??
requestTaskMap[task]?.credential ??
session.configuration.urlCredentialStorage?.defaultCredential(for: protectionSpace)
}

Expand Down