AuthenticationChallengeResponsable.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // AuthenticationChallengeResponsable.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 2018/10/11.
  6. //
  7. // Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import Foundation
  27. @available(*, deprecated, message: "Typo. Use `AuthenticationChallengeResponsible` instead", renamed: "AuthenticationChallengeResponsible")
  28. public typealias AuthenticationChallengeResponsable = AuthenticationChallengeResponsible
  29. /// Protocol indicates that an authentication challenge could be handled.
  30. public protocol AuthenticationChallengeResponsible: AnyObject {
  31. /// Called when a session level authentication challenge is received.
  32. ///
  33. /// This method provides a chance to handle and respond to the authentication challenge before the downloading can
  34. /// start.
  35. ///
  36. /// - Parameters:
  37. /// - downloader: The downloader that receives this challenge.
  38. /// - challenge: An object that contains the request for authentication.
  39. /// - Returns: The challenge disposition on how the challenge should be handled, and the credential if the
  40. /// disposition is `.useCredential`.
  41. ///
  42. /// > This method is a forward from `URLSessionDelegate.urlSession(_:didReceive:completionHandler:)`.
  43. /// > Please refer to the documentation of it in `URLSessionDelegate`.
  44. func downloader(
  45. _ downloader: ImageDownloader,
  46. didReceive challenge: URLAuthenticationChallenge
  47. ) async -> (URLSession.AuthChallengeDisposition, URLCredential?)
  48. /// Called when a task level authentication challenge is received.
  49. ///
  50. /// This method provides a chance to handle and respond to the authentication challenge before the downloading can
  51. /// start.
  52. ///
  53. /// - Parameters:
  54. /// - downloader: The downloader that receives this challenge.
  55. /// - task: The task whose request requires authentication.
  56. /// - challenge: An object that contains the request for authentication.
  57. /// - Returns: The challenge disposition on how the challenge should be handled, and the credential if the
  58. /// disposition is `.useCredential`.
  59. ///
  60. /// > This method is a forward from `URLSessionDataDelegate.urlSession(_:dataTask:didReceive:completionHandler:)`.
  61. /// > Please refer to the documentation of it in `URLSessionDataDelegate`.
  62. func downloader(
  63. _ downloader: ImageDownloader,
  64. task: URLSessionTask,
  65. didReceive challenge: URLAuthenticationChallenge
  66. ) async -> (URLSession.AuthChallengeDisposition, URLCredential?)
  67. }
  68. extension AuthenticationChallengeResponsible {
  69. public func downloader(
  70. _ downloader: ImageDownloader,
  71. didReceive challenge: URLAuthenticationChallenge
  72. ) async -> (URLSession.AuthChallengeDisposition, URLCredential?)
  73. {
  74. if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
  75. if let trustedHosts = downloader.trustedHosts, trustedHosts.contains(challenge.protectionSpace.host) {
  76. let credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
  77. return (.useCredential, credential)
  78. }
  79. }
  80. return (.performDefaultHandling, nil)
  81. }
  82. public func downloader(
  83. _ downloader: ImageDownloader,
  84. task: URLSessionTask,
  85. didReceive challenge: URLAuthenticationChallenge
  86. ) async -> (URLSession.AuthChallengeDisposition, URLCredential?) {
  87. (.performDefaultHandling, nil)
  88. }
  89. }