AuthenticationChallengeResponsable.swift 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. /// This method provide a chance to handle and response to the authentication
  33. /// challenge before downloading could start.
  34. ///
  35. /// - Parameters:
  36. /// - downloader: The downloader which receives this challenge.
  37. /// - challenge: An object that contains the request for authentication.
  38. /// - completionHandler: A handler that your delegate method must call.
  39. ///
  40. /// - Note: This method is a forward from `URLSessionDelegate.urlSession(:didReceiveChallenge:completionHandler:)`.
  41. /// Please refer to the document of it in `URLSessionDelegate`.
  42. func downloader(
  43. _ downloader: ImageDownloader,
  44. didReceive challenge: URLAuthenticationChallenge,
  45. completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
  46. /// Called when a task level authentication challenge is received.
  47. /// This method provide a chance to handle and response to the authentication
  48. /// challenge before downloading could start.
  49. ///
  50. /// - Parameters:
  51. /// - downloader: The downloader which receives this challenge.
  52. /// - task: The task whose request requires authentication.
  53. /// - challenge: An object that contains the request for authentication.
  54. /// - completionHandler: A handler that your delegate method must call.
  55. func downloader(
  56. _ downloader: ImageDownloader,
  57. task: URLSessionTask,
  58. didReceive challenge: URLAuthenticationChallenge,
  59. completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
  60. }
  61. extension AuthenticationChallengeResponsible {
  62. public func downloader(
  63. _ downloader: ImageDownloader,
  64. didReceive challenge: URLAuthenticationChallenge,
  65. completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
  66. {
  67. if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
  68. if let trustedHosts = downloader.trustedHosts, trustedHosts.contains(challenge.protectionSpace.host) {
  69. let credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
  70. completionHandler(.useCredential, credential)
  71. return
  72. }
  73. }
  74. completionHandler(.performDefaultHandling, nil)
  75. }
  76. public func downloader(
  77. _ downloader: ImageDownloader,
  78. task: URLSessionTask,
  79. didReceive challenge: URLAuthenticationChallenge,
  80. completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
  81. {
  82. completionHandler(.performDefaultHandling, nil)
  83. }
  84. }