Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Show untrusted TLS certificate alert #298

Merged
merged 2 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions Cryptomator/WebDAV/WebDAVAuthentication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,6 @@ struct WebDAVAuthentication: View {
.introspectTableView(customize: { tableView in
tableView.backgroundColor = .cryptomatorBackground
})
.alert(isPresented: $viewModel.showUntrustedCertificateError) {
untrustedCertificateAlert
}
.alert(isPresented: $viewModel.showAllowInsecureConnectionAlert) {
insecureConnectionAlert
}
}

private var untrustedCertificateAlert: Alert {
Alert(title: Text(LocalizedString.getValue("untrustedTLSCertificate.title")),
message: Text(LocalizedString.getValue("untrustedTLSCertificate.message")),
primaryButton: .default(Text(LocalizedString.getValue("untrustedTLSCertificate.add")),
action: {
viewModel.allowCertificate()
}),
secondaryButton: .cancel(Text(LocalizedString.getValue("untrustedTLSCertificate.dismiss"))))
}

private var insecureConnectionAlert: Alert {
Alert(title: Text(LocalizedString.getValue("webDAVAuthentication.httpConnection.alert.title")),
message: Text(LocalizedString.getValue("webDAVAuthentication.httpConnection.alert.message")),
primaryButton: .default(Text(LocalizedString.getValue("webDAVAuthentication.httpConnection.change")),
action: {
viewModel.saveAccountWithTransformedURL()
}),
secondaryButton: .destructive(Text(LocalizedString.getValue("webDAVAuthentication.httpConnection.continue")),
action: {
viewModel.saveAccountWithInsecureConnection()
}))
}
}

Expand Down
51 changes: 50 additions & 1 deletion Cryptomator/WebDAV/WebDAVAuthenticationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ class WebDAVAuthenticationViewController: UIViewController {
hud?.transformToSelfDismissingSuccess().then {
self.coordinator?.authenticated(with: credential)
}
case .initial, .insecureConnectionNotAllowed, .untrustedCertificate:
case .insecureConnectionNotAllowed:
showInsecureConnectionAlert()
case let .untrustedCertificate(certificate: certificate, url: url):
showUntrustedCertificateAlert(certificate: certificate, url: url)
case .initial:
break
}
}
Expand All @@ -86,6 +90,51 @@ class WebDAVAuthenticationViewController: UIViewController {
hud?.showLoadingIndicator()
}

private func showUntrustedCertificateAlert(certificate: TLSCertificate, url: URL) {
let precondition: Promise<Void>
if let hud = hud {
precondition = hud.dismiss(animated: true)
} else {
precondition = Promise(())
}
precondition.then { [weak self] in
let message = String(format: LocalizedString.getValue("untrustedTLSCertificate.message"), url.absoluteString, certificate.fingerprint)
let alertController = UIAlertController(title: LocalizedString.getValue("untrustedTLSCertificate.title"),
message: message,
preferredStyle: .alert)
let addAction = UIAlertAction(title: LocalizedString.getValue("untrustedTLSCertificate.add"),
style: .default,
handler: { _ in self?.viewModel.saveAccountWithCertificate() })
alertController.addAction(addAction)
alertController.addAction(UIAlertAction(title: LocalizedString.getValue("untrustedTLSCertificate.dismiss"), style: .cancel))
self?.present(alertController, animated: true)
}
}

private func showInsecureConnectionAlert() {
let precondition: Promise<Void>
if let hud = hud {
precondition = hud.dismiss(animated: true)
} else {
precondition = Promise(())
}
precondition.then { [weak self] in
let alertController = UIAlertController(title: LocalizedString.getValue("webDAVAuthentication.httpConnection.alert.title"),
message: LocalizedString.getValue("webDAVAuthentication.httpConnection.alert.message"),
preferredStyle: .alert)
let changeToHTTPSAction = UIAlertAction(title: LocalizedString.getValue("webDAVAuthentication.httpConnection.change"), style: .default, handler: { _ in
self?.viewModel.saveAccountWithTransformedURL()
})

alertController.addAction(changeToHTTPSAction)
alertController.preferredAction = changeToHTTPSAction
alertController.addAction(UIAlertAction(title: LocalizedString.getValue("webDAVAuthentication.httpConnection.continue"), style: .destructive, handler: { _ in
self?.viewModel.saveAccountWithInsecureConnection()
}))
self?.present(alertController, animated: true)
}
}

@objc func cancel() {
coordinator?.cancel()
}
Expand Down