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

Handle URLSession WebDAV auth challenge #1018

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -948,3 +948,26 @@ extension AttachmentDownloader: URLSessionDelegate {
}
}
}

extension AttachmentDownloader: URLSessionTaskDelegate {
func urlSession(
_ session: URLSession,
task: URLSessionTask,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping @Sendable (URLSession.AuthChallengeDisposition, URLCredential?) -> Void
) {
let sessionStorage = webDavController.sessionStorage
let protectionSpace = challenge.protectionSpace
guard sessionStorage.isEnabled,
sessionStorage.isVerified,
mvasilak marked this conversation as resolved.
Show resolved Hide resolved
protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic || protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPDigest,
protectionSpace.host == sessionStorage.host,
protectionSpace.port == sessionStorage.port,
protectionSpace.protocol == sessionStorage.scheme.rawValue
else {
completionHandler(.performDefaultHandling, nil)
return
}
completionHandler(.useCredential, URLCredential(user: sessionStorage.username, password: sessionStorage.password, persistence: .permanent))
}
}
12 changes: 12 additions & 0 deletions Zotero/Controllers/WebDAV/WebDavSessionStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,22 @@ protocol WebDavSessionStorage: AnyObject {
var isVerified: Bool { get set }
var username: String { get set }
var url: String { get set }
var host: String { get }
var port: Int { get }
var scheme: WebDavScheme { get set }
var password: String { get set }
}

extension WebDavSessionStorage {
var host: String {
return url.split(separator: ":", maxSplits: 2).first.flatMap { String($0) } ?? ""
}

var port: Int {
url.split(separator: ":", maxSplits: 2).last.flatMap { Int($0) } ?? 80
}
}

final class SecureWebDavSessionStorage: WebDavSessionStorage {
private unowned let secureStorage: SecureStorage

Expand Down