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

[SR-12676] Swift on Linux: URLSession never calls URLAuthenticationChallenge methods. #55120

Closed
SlaunchaMan opened this issue Apr 27, 2020 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@SlaunchaMan
Copy link
Contributor

Previous ID SR-12676
Radar rdar://problem/62895027
Original Reporter @SlaunchaMan
Type Bug
Status Resolved
Resolution Done
Environment

This is the Dockerfile I’m using to generate a Linux build:

FROM swiftlang/swift:nightly

WORKDIR /app
COPY . .

RUN swift build -c release

ENTRYPOINT swift run -c release

To execute it, run "docker build -t label ." with some label, then "docker run label".

Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee None
Priority Medium

md5: 4e1bf97f08855b581b461bd79f912a05

Issue Description:

I have the following project to perform basic authentication using a username and password:

import Foundation

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

class Delegate: NSObject {

    typealias ChallengeCompletion =
        (URLSession.AuthChallengeDisposition, URLCredential?) -> Void

}

extension Delegate: URLSessionDelegate {
    
    func urlSession(
        _ session: URLSession,
        didReceive challenge: URLAuthenticationChallenge,
        completionHandler: @escaping ChallengeCompletion
    ) {
        print("Not using the credential for the main session method.")
        completionHandler(.performDefaultHandling, nil)
    }

}

extension Delegate: URLSessionTaskDelegate {

    func urlSession(
        _ session: URLSession,
        task: URLSessionTask,
        didReceive challenge: URLAuthenticationChallenge,
        completionHandler: @escaping ChallengeCompletion
    ) {
        print("Using the credential to complete the task-specific challenge.")
        completionHandler(.useCredential,
                          URLCredential(user: "username",
                                        password: "password",
                                        persistence: .none))
    }

}

guard let url = URL(string: "https://httpbin.org/basic-auth/username/password")
    else { exit(EXIT_FAILURE) }

let request = URLRequest(url: url)

let queue = OperationQueue()
let delegate = Delegate()

let session = URLSession(configuration: .ephemeral,
                         delegate: delegate,
                         delegateQueue: queue)

let semaphore = DispatchSemaphore(value: 0)

let task = session.dataTask(with: request) { (data, response, error) in
    guard let httpResponse = response as? HTTPURLResponse else {
        print("Expected an HTTPURLResponse, got a \(type(of: response))")
        exit(EXIT_FAILURE)
    }

    guard httpResponse.statusCode == 200 else {
        print("Expected status code 200, got \(httpResponse.statusCode)")

        if let data = data,
            let responseString = String(data: data, encoding: .utf8) {
            print(responseString)
        }

        exit(EXIT_FAILURE)
    }

    guard let data = data, let responseString = String(data: data, encoding: .utf8) else {
        print("Could not make UTF-8 string from received data.")
        exit(EXIT_FAILURE)
    }

    print(responseString)

    semaphore.signal()
}

task.resume()

semaphore.wait()

exit(EXIT_SUCCESS)

On Linux, this fails with a 401 response code, never having called either of the delegate methods.

@beccadax
Copy link
Contributor

beccadax commented May 5, 2020

@swift-ci create

@millenomi
Copy link
Contributor

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.
Projects
None yet
Development

No branches or pull requests

3 participants