Skip to content

Commit

Permalink
improved webView(_, didReceive challenge:) to handle also client auth…
Browse files Browse the repository at this point in the history
…entication

os_log extension for logs related to wkwebview
workaround for redirection from myopenhab.org to home.myopenhab.org

Signed-off-by: Tim Müller-Seydlitz <[email protected]>
  • Loading branch information
timbms committed Jun 17, 2022
1 parent 4580c65 commit c333f01
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public let onReceiveSessionTaskChallenge = { (_: URLSession, _: URLSessionTask,
} else if challenge.protectionSpace.authenticationMethod.isAny(of: NSURLAuthenticationMethodHTTPBasic, NSURLAuthenticationMethodDefault) {
let localUrl = URL(string: Preferences.localUrl)
let remoteUrl = URL(string: Preferences.remoteUrl)
if challenge.protectionSpace.host == localUrl?.host || challenge.protectionSpace.host == remoteUrl?.host {
if challenge.protectionSpace.host == localUrl?.host || challenge.protectionSpace.host == remoteUrl?.host || challenge.protectionSpace.host == "home.myopenhab.org" {
credential = URLCredential(user: Preferences.username, password: Preferences.password, persistence: .forSession)
disposition = .useCredential
os_log("HTTP BasicAuth host:'%{PUBLIC}@'", log: .default, type: .error, challenge.protectionSpace.host)
Expand Down
3 changes: 3 additions & 0 deletions OpenHABCore/Sources/OpenHABCore/Util/OSLogExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ public extension OSLog {

/// Logs Alamofire events
static let alamofire = OSLog(subsystem: subsystem, category: "alamofire")

/// Logs WkWebView events
static let wkwebview = OSLog(subsystem: subsystem, category: "wkwebview")
}
2 changes: 1 addition & 1 deletion openHAB/OpenHABViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ class OpenHABViewController: UIViewController {
self.navigationItem.title = self.currentPage?.title.components(separatedBy: "[")[0]
self.loadPage(true)
case let .failure(error):
os_log("On LoadPage %{PUBLIC}@ code: %d ", log: .remoteAccess, type: .error, error.localizedDescription, response.response?.statusCode ?? 0)
os_log("On LoadPage \"%{PUBLIC}@\" code: %d ", log: .remoteAccess, type: .error, error.localizedDescription, response.response?.statusCode ?? 0)

NetworkConnection.atmosphereTrackingId = ""
if (error as NSError?)?.code == -1001, longPolling {
Expand Down
21 changes: 15 additions & 6 deletions openHAB/OpenHABWebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class OpenHABWebViewController: UIViewController {
if !force, currentTarget == newTarget {
return
}
currentTarget = newTarget
// currentTarget = newTarget
let url = URL(string: openHABRootUrl)
if let modifiedUrl = modifyUrl(orig: url) {
let request = URLRequest(url: modifiedUrl)
Expand Down Expand Up @@ -132,7 +132,7 @@ extension OpenHABWebViewController: WKNavigationDelegate {
}

guard let url = navigationAction.request.url else { return }
os_log("decidePolicyFor - url: %{PUBLIC}@", log: .urlComposition, type: .info, url.absoluteString)
os_log("decidePolicyFor - url: %{PUBLIC}@", log: .wkwebview, type: .info, url.absoluteString)

if navigationAction.navigationType == .linkActivated {
action = .cancel // Stop in WebView
Expand All @@ -144,13 +144,13 @@ extension OpenHABWebViewController: WKNavigationDelegate {
decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
if let response = navigationResponse.response as? HTTPURLResponse {
dump(response.allHeaderFields)
os_log("navigationResponse: %{PUBLIC}@", log: .urlComposition, type: .info, String(response.statusCode))
os_log("navigationResponse: %{PUBLIC}@", log: .wkwebview, type: .info, String(response.statusCode))
}
decisionHandler(.allow)
}

func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
os_log("didStartProvisionalNavigation - webView.url: %{PUBLIC}@", log: .urlComposition, type: .info, String(describing: webView.url?.description))
os_log("didStartProvisionalNavigation - webView.url: %{PUBLIC}@", log: .wkwebview, type: .info, String(describing: webView.url?.description))
}

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
Expand All @@ -161,13 +161,22 @@ extension OpenHABWebViewController: WKNavigationDelegate {
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
os_log("didFinish - webView.url %{PUBLIC}@", log: .urlComposition, type: .info, String(describing: webView.url?.description))
os_log("didFinish - webView.url %{PUBLIC}@", log: .wkwebview, type: .info, String(describing: webView.url?.description))
}

func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {

os_log("Challenge.protectionSpace.authtenticationMethod: %{PUBLIC}@", log: .wkwebview, type: .info, String(describing: challenge.protectionSpace.authenticationMethod))

if let url = modifyUrl(orig: URL(string: openHABRootUrl)), challenge.protectionSpace.host == url.host {
let (disposition, credential) = onReceiveSessionChallenge(URLSession(configuration: .default), challenge)
var disposition: URLSession.AuthChallengeDisposition = .performDefaultHandling
var credential: URLCredential?
if challenge.protectionSpace.authenticationMethod.isAny(of: NSURLAuthenticationMethodHTTPBasic, NSURLAuthenticationMethodDefault) {
(disposition, credential) = onReceiveSessionTaskChallenge(URLSession(configuration: .default), URLSessionDataTask(), challenge)
} else {
(disposition, credential) = onReceiveSessionChallenge(URLSession(configuration: .default), challenge)
}
completionHandler(disposition, credential)
} else {
completionHandler(.performDefaultHandling, nil)
Expand Down

0 comments on commit c333f01

Please sign in to comment.