【问题标题】:Swift - HTTP digest authSwift - HTTP 摘要认证
【发布时间】:2020-09-17 05:23:56
【问题描述】:

我目前正在对家庭自动化 API 进行逆向工程。我想用我自己的应用程序管理所有设置 - 因为公司目前确实没有家庭自动化应用程序。

无论如何 - 我已经使用我的 SmartHome 设备管理了身份验证。为了不让它太复杂:我需要 http 摘要身份验证 进行最终通信。 我已经能够通过命令行使用 curl 连接到我的设备 - 不幸的是,这在 Swift 中无法按计划工作。

curl -X POST -d '{"key": "value"}' https://192.168.0.0:1/action -k -s --digest --user username:password

翻译成斯威夫特:

(1) 使用 Alamofire

import Alamofire
let data: [String: any] = ["key": "value"]
let request = Alamofire.request("https://192.168.0.0:1/action", method: HTTPMethod.post, parameters: data);
request.authenticate(user: "username", password: "password")
request.responseJSON { response in
    // leads to error because of invalid self signed certificate of the smart home device ("https:")
}

Alamofire 的注意事项:我想在这种情况下使用 AF 等外部库没有多大意义 - 有一些未解决的问题不会让上述代码正常工作。 (自签名证书会产生问题,使用自定义管理器实例覆盖内部内容也会导致问题)-相信我已经花了好几个小时了。

(2) 不使用 Alamofire :)

extension ViewController: URLSessionDelegate {
    public func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        let urlCredential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
        completionHandler(.useCredential, urlCredential)
    }
}



let session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: nil)

var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")

do {
    let jsonData = try JSONSerialization.data(withJSONObject: data, options: .prettyPrinted)
    request.httpBody = jsonData;
    let task = session.dataTask(with: request) { data, response, error in
        guard let data = data, error == nil else {
            return
        }
        let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
        if let responseJSON = responseJSON as? [String: Any] {
        // success
        }
    }
    task.resume()
} catch { }

上面的代码似乎工作正常 - 问题是我还没有实现摘要身份验证 - 因为我没有找到任何方法来做到这一点。

如果有人获得一些提示如何根据用户名和密码生成 Auth 标头,那将非常有帮助

编辑

Curl 使用这个 Authorization 标头:

> Digest username="username", 
realm="XTV", 
nonce="MTU5MDcNc2UxNjQ3OTo1YzMwYjc3YjIxMzAAAGQ5Nzg2NzUzMmRkZGU1ZVVlYw==", 
uri="/action", 
cnonce="MTExOTZlZmI1MjBlYWU0MTIzMDBmNDE0YTkWzJl1MDk=", 
nc=00000001, 
qop=auth, 
response="2ba89269645e2aa24ac6f117d85e190c", 
algorithm="MD5"

是否有可能在 Swift 中生成此标头?

【问题讨论】:

    标签: ios swift http request alamofire


    【解决方案1】:

    当服务器正确返回具有正确摘要设置的 WWW-Authenticate 标头时,URLSession 和 Alamofire 通过 URLCredential(这是 authenticate() 在 Alamofire 中使用的)自动支持摘要身份验证。

    您可以手动生成标头,但由于摘要过程的复杂性,我不建议这样做。我发现wikipedia page 足够彻底,可以手动实施标准。

    【讨论】:

    • 如果您直接使用URLSession,您要么需要实现适当的委托方法urlSession(_:didReceive:completionHandler:),要么将凭据直接添加到您的URLCredentialStorage
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    • 2013-03-27
    • 1970-01-01
    • 2017-10-12
    • 2019-07-30
    • 1970-01-01
    • 2012-07-18
    相关资源
    最近更新 更多