Digest Authentication with APIKit

implementation of Digest Authentication with APIKit.

class CustomURLSessionAdapter: URLSessionAdapter {

    convenience init() {
        self.init(configuration: URLSessionConfiguration.default)
    }

    func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        if challenge.previousFailureCount == 0 {
            let credential = URLCredential(user: "USERNAME", password: "PASSWORD", persistence: .none)
            completionHandler(.useCredential, credential)
        } else {
            challenge.sender?.cancel(challenge)
            completionHandler(.cancelAuthenticationChallenge, nil)
        }
    }

}

let session =  Session(adapter: CustomURLSessionAdapter())
  • このエントリーをはてなブックマークに追加