AuthenticationChallengeResponsable.swift 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /// - completionHandler: A handler that your delegate method must call.
  40. ///
  41. /// > This method is a forward from `URLSessionDelegate.urlSession(:didReceiveChallenge:completionHandler:)`.
  42. /// > Please refer to the documentation of it in `URLSessionDelegate`.
  43. func downloader(
  44. _ downloader: ImageDownloader,
  45. didReceive challenge: URLAuthenticationChallenge,
  46. completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void
  47. )
  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. /// - completionHandler: A handler that your delegate method must call.
  58. func downloader(
  59. _ downloader: ImageDownloader,
  60. task: URLSessionTask,
  61. didReceive challenge: URLAuthenticationChallenge,
  62. completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void
  63. )
  64. }
  65. extension AuthenticationChallengeResponsible {
  66. public func downloader(
  67. _ downloader: ImageDownloader,
  68. didReceive challenge: URLAuthenticationChallenge,
  69. completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
  70. {
  71. if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
  72. if let trustedHosts = downloader.trustedHosts, trustedHosts.contains(challenge.protectionSpace.host) {
  73. let credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
  74. completionHandler(.useCredential, credential)
  75. return
  76. }
  77. }
  78. completionHandler(.performDefaultHandling, nil)
  79. }
  80. public func downloader(
  81. _ downloader: ImageDownloader,
  82. task: URLSessionTask,
  83. didReceive challenge: URLAuthenticationChallenge,
  84. completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
  85. {
  86. completionHandler(.performDefaultHandling, nil)
  87. }
  88. }