Skip to content

Commit

Permalink
Handle URLSession WebDAV auth challenge (#1018)
Browse files Browse the repository at this point in the history
* Handle URLSession WebDAV auth challenge

* Fix tests
  • Loading branch information
mvasilak authored Oct 21, 2024
1 parent c35ba2f commit 712c57e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
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,
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

0 comments on commit 712c57e

Please sign in to comment.