You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
4.1 KiB

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