onevcat / Kingfisher

A lightweight, pure-Swift library for downloading and caching images from the web.
MIT License
23.43k stars 2.66k forks source link

Adding an option to conveniently use the setImage() method from a URL with an SSL authentication challenge #1982

Closed loinsir closed 2 years ago

loinsir commented 2 years ago

Check List

Thanks for considering to open an issue. Before you submit your issue, please confirm these boxes are checked.

Issue Description

What

[Tell us about the issue]

Adding an option to conveniently use the setImage() method from a URL with an SSL authentication challenge

In my opinion, besides implementing the SSL authentication challenge through the authenticationChallengeResponder's delegate, it's also a good idea to provide a way to simply accept the authentication challenge via the option argument. In the case of YYWebImage(https://github.com/ibireme/YYWebImage) that I used in the past, such a method was provided, but unfortunately Kingfisher does not provide such an option.

Here is my YYWebImage code. There is an option argument of SSL Certification.

imageView.yy_setImage(
            with: url,
            placeholder: nil,
            options: [.allowBackgroundTask, .progressiveBlur, .allowInvalidSSLCertificates, .progressiveBlur, .showNetworkActivity, .setImageWithFadeAnimation],
            progress: nil, transform: { (image, url) -> UIImage? in
                if grayscale {
                    return image.yy_imageByGrayscale()
                }
                return image
        }, completion: { [weak imageView] (_, _, _, _, _) in
            guard let iv = imageView else { return }
            iv.placeHolderImage = nil
        })

So Can I implement this feature? How about your opinion?

Reproduce

[The steps to reproduce this issue. What is the url you were trying to load, where did you put your code, etc.] I found a similar issue : #945

Other Comment

[Add anything else here] Here is YYWebImage's Solution https://github.com/ibireme/YYWebImage/blob/093193c67bfa2409af93e618a48e3208d63106a5/YYWebImage/YYWebImageManager.h

I'm not good at English. But I'll trying my best:)

onevcat commented 2 years ago

In Kingfisher, the easiest way for this, is setting a trustedHosts for the downloader:

ImageDownloader.default.trustedHosts = ["onevcat.com"]

It will bypass any challenge for the domain onevcat.com.

Or you can try to create a type conforming to AuthenticationChallengeResponsible and ignore SSL errors there, and set it to authenticationChallengeResponder. Then you can implement any kind of challenge handling in your type.

ImageDownloader.default.authenticationChallengeResponder = yourResponder