Convert validateCredentials to async await.

This commit is contained in:
Brent Simmons 2024-04-03 21:15:13 -07:00
parent 1745edae14
commit 591601d87e
7 changed files with 296 additions and 241 deletions

View File

@ -357,35 +357,21 @@ public enum FetchType {
try CredentialsManager.removeCredentials(type: type, server: server, username: username) try CredentialsManager.removeCredentials(type: type, server: server, username: username)
} }
public static func validateCredentials(transport: Transport = URLSession.webserviceTransport(), type: AccountType, credentials: Credentials, endpoint: URL? = nil, secretsProvider: SecretsProvider, completion: @escaping (Result<Credentials?, Error>) -> Void) { public static func validateCredentials(transport: Transport = URLSession.webserviceTransport(), type: AccountType, credentials: Credentials, endpoint: URL? = nil, secretsProvider: SecretsProvider) async throws -> Credentials? {
Task { @MainActor in switch type {
do { case .feedbin:
switch type { return try await FeedbinAccountDelegate.validateCredentials(transport: transport, credentials: credentials, endpoint: endpoint, secretsProvider: secretsProvider)
case .feedbin: case .newsBlur:
return try await NewsBlurAccountDelegate.validateCredentials(transport: transport, credentials: credentials, endpoint: endpoint, secretsProvider: secretsProvider)
let credentials = try await FeedbinAccountDelegate.validateCredentials(transport: transport, credentials: credentials, endpoint: endpoint, secretsProvider: secretsProvider) case .freshRSS, .inoreader, .bazQux, .theOldReader:
completion(.success(credentials)) return try await ReaderAPIAccountDelegate.validateCredentials(transport: transport, credentials: credentials, endpoint: endpoint, secretsProvider: secretsProvider)
case .newsBlur: default:
return nil
let credentials = try await NewsBlurAccountDelegate.validateCredentials(transport: transport, credentials: credentials, endpoint: endpoint, secretsProvider: secretsProvider)
completion(.success(credentials))
case .freshRSS, .inoreader, .bazQux, .theOldReader:
let credentials = try await ReaderAPIAccountDelegate.validateCredentials(transport: transport, credentials: credentials, endpoint: endpoint, secretsProvider: secretsProvider)
completion(.success(credentials))
default:
completion(.success(nil))
}
} catch {
completion(.failure(error))
}
} }
} }

View File

@ -79,53 +79,60 @@ class AccountsFeedbinWindowController: NSWindowController {
progressIndicator.startAnimation(self) progressIndicator.startAnimation(self)
let credentials = Credentials(type: .basic, username: usernameTextField.stringValue, secret: passwordTextField.stringValue) let credentials = Credentials(type: .basic, username: usernameTextField.stringValue, secret: passwordTextField.stringValue)
Account.validateCredentials(type: .feedbin, credentials: credentials, secretsProvider: Secrets()) { [weak self] result in
guard let self = self else { return } Task { @MainActor in
var validationDidThrow = false
var validatedCredentials: Credentials?
do {
validatedCredentials = try await Account.validateCredentials(type: .feedbin, credentials: credentials, secretsProvider: Secrets())
} catch {
self.errorMessageLabel.stringValue = NSLocalizedString("Network error. Try again later.", comment: "Credentials Error")
validationDidThrow = true
}
self.actionButton.isEnabled = true self.actionButton.isEnabled = true
self.progressIndicator.isHidden = true self.progressIndicator.isHidden = true
self.progressIndicator.stopAnimation(self) self.progressIndicator.stopAnimation(self)
switch result {
case .success(let validatedCredentials):
guard let validatedCredentials = validatedCredentials else {
self.errorMessageLabel.stringValue = NSLocalizedString("Invalid email/password combination.", comment: "Credentials Error")
return
}
if self.account == nil {
self.account = AccountManager.shared.createAccount(type: .feedbin)
}
do {
try self.account?.removeCredentials(type: .basic)
try self.account?.storeCredentials(validatedCredentials)
Task { @MainActor in
do {
try await self.account?.refreshAll()
} catch {
NSApplication.shared.presentError(error)
}
}
self.hostWindow?.endSheet(self.window!, returnCode: NSApplication.ModalResponse.OK) if validationDidThrow {
} catch { return
self.errorMessageLabel.stringValue = NSLocalizedString("Keychain error while storing credentials.", comment: "Credentials Error") }
}
guard let validatedCredentials else {
case .failure: self.errorMessageLabel.stringValue = NSLocalizedString("Invalid email/password combination.", comment: "Credentials Error")
return
self.errorMessageLabel.stringValue = NSLocalizedString("Network error. Try again later.", comment: "Credentials Error") }
if self.account == nil {
self.account = AccountManager.shared.createAccount(type: .feedbin)
}
do {
try self.account?.removeCredentials(type: .basic)
try self.account?.storeCredentials(validatedCredentials)
self.refreshAll()
self.hostWindow?.endSheet(self.window!, returnCode: NSApplication.ModalResponse.OK)
} catch {
self.errorMessageLabel.stringValue = NSLocalizedString("Keychain error while storing credentials.", comment: "Credentials Error")
} }
} }
} }
private func refreshAll() {
Task { @MainActor in
do {
try await self.account?.refreshAll()
} catch {
NSApplication.shared.presentError(error)
}
}
}
@IBAction func createAccountWithProvider(_ sender: Any) { @IBAction func createAccountWithProvider(_ sender: Any) {
NSWorkspace.shared.open(URL(string: "https://feedbin.com/signup")!) NSWorkspace.shared.open(URL(string: "https://feedbin.com/signup")!)
} }

View File

@ -76,52 +76,62 @@ class AccountsNewsBlurWindowController: NSWindowController {
progressIndicator.startAnimation(self) progressIndicator.startAnimation(self)
let credentials = Credentials(type: .newsBlurBasic, username: usernameTextField.stringValue, secret: passwordTextField.stringValue) let credentials = Credentials(type: .newsBlurBasic, username: usernameTextField.stringValue, secret: passwordTextField.stringValue)
Account.validateCredentials(type: .newsBlur, credentials: credentials, secretsProvider: Secrets()) { [weak self] result in
guard let self = self else { return } Task { @MainActor in
var validationDidThrow = false
var validatedCredentials: Credentials?
do {
validatedCredentials = try await Account.validateCredentials(type: .newsBlur, credentials: credentials, secretsProvider: Secrets())
} catch {
self.errorMessageLabel.stringValue = NSLocalizedString("Network error. Try again later.", comment: "Credentials Error")
validationDidThrow = true
}
self.actionButton.isEnabled = true self.actionButton.isEnabled = true
self.progressIndicator.isHidden = true self.progressIndicator.isHidden = true
self.progressIndicator.stopAnimation(self) self.progressIndicator.stopAnimation(self)
switch result { if validationDidThrow {
case .success(let validatedCredentials): return
guard let validatedCredentials = validatedCredentials else { }
self.errorMessageLabel.stringValue = NSLocalizedString("Invalid email/password combination.", comment: "Credentials Error")
return
}
if self.account == nil { guard let validatedCredentials else {
self.account = AccountManager.shared.createAccount(type: .newsBlur) self.errorMessageLabel.stringValue = NSLocalizedString("Invalid email/password combination.", comment: "Credentials Error")
} return
}
do { if self.account == nil {
try self.account?.removeCredentials(type: .newsBlurBasic) self.account = AccountManager.shared.createAccount(type: .newsBlur)
try self.account?.removeCredentials(type: .newsBlurSessionId) }
try self.account?.storeCredentials(credentials)
try self.account?.storeCredentials(validatedCredentials)
Task { @MainActor in do {
do { try self.account?.removeCredentials(type: .newsBlurBasic)
try await self.account?.refreshAll() try self.account?.removeCredentials(type: .newsBlurSessionId)
} catch { try self.account?.storeCredentials(credentials)
NSApplication.shared.presentError(error) try self.account?.storeCredentials(validatedCredentials)
}
}
self.hostWindow?.endSheet(self.window!, returnCode: NSApplication.ModalResponse.OK) self.refreshAll()
} catch {
self.errorMessageLabel.stringValue = NSLocalizedString("Keychain error while storing credentials.", comment: "Credentials Error")
}
case .failure:
self.errorMessageLabel.stringValue = NSLocalizedString("Network error. Try again later.", comment: "Credentials Error")
self.hostWindow?.endSheet(self.window!, returnCode: NSApplication.ModalResponse.OK)
} catch {
self.errorMessageLabel.stringValue = NSLocalizedString("Keychain error while storing credentials.", comment: "Credentials Error")
} }
} }
} }
private func refreshAll() {
Task { @MainActor in
do {
try await self.account?.refreshAll()
} catch {
NSApplication.shared.presentError(error)
}
}
}
@IBAction func createAccountWithProvider(_ sender: Any) { @IBAction func createAccountWithProvider(_ sender: Any) {
NSWorkspace.shared.open(URL(string: "https://newsblur.com")!) NSWorkspace.shared.open(URL(string: "https://newsblur.com")!)
} }

View File

@ -91,22 +91,22 @@ class AccountsReaderAPIWindowController: NSWindowController {
@IBAction func action(_ sender: Any) { @IBAction func action(_ sender: Any) {
self.errorMessageLabel.stringValue = "" self.errorMessageLabel.stringValue = ""
guard !usernameTextField.stringValue.isEmpty && !passwordTextField.stringValue.isEmpty else { guard !usernameTextField.stringValue.isEmpty && !passwordTextField.stringValue.isEmpty else {
self.errorMessageLabel.stringValue = NSLocalizedString("Username, password & API URL are required.", comment: "Credentials Error") self.errorMessageLabel.stringValue = NSLocalizedString("Username, password & API URL are required.", comment: "Credentials Error")
return return
} }
guard let accountType = accountType, !(accountType == .freshRSS && apiURLTextField.stringValue.isEmpty) else { guard let accountType = accountType, !(accountType == .freshRSS && apiURLTextField.stringValue.isEmpty) else {
self.errorMessageLabel.stringValue = NSLocalizedString("Username, password & API URL are required.", comment: "Credentials Error") self.errorMessageLabel.stringValue = NSLocalizedString("Username, password & API URL are required.", comment: "Credentials Error")
return return
} }
guard account != nil || !AccountManager.shared.duplicateServiceAccount(type: accountType, username: usernameTextField.stringValue) else { guard account != nil || !AccountManager.shared.duplicateServiceAccount(type: accountType, username: usernameTextField.stringValue) else {
self.errorMessageLabel.stringValue = NSLocalizedString("There is already an account of this type with that username created.", comment: "Duplicate Error") self.errorMessageLabel.stringValue = NSLocalizedString("There is already an account of this type with that username created.", comment: "Duplicate Error")
return return
} }
let apiURL: URL let apiURL: URL
switch accountType { switch accountType {
case .freshRSS: case .freshRSS:
@ -125,60 +125,70 @@ class AccountsReaderAPIWindowController: NSWindowController {
self.errorMessageLabel.stringValue = NSLocalizedString("Unrecognized account type.", comment: "Bad account type") self.errorMessageLabel.stringValue = NSLocalizedString("Unrecognized account type.", comment: "Bad account type")
return return
} }
actionButton.isEnabled = false actionButton.isEnabled = false
progressIndicator.isHidden = false progressIndicator.isHidden = false
progressIndicator.startAnimation(self) progressIndicator.startAnimation(self)
let credentials = Credentials(type: .readerBasic, username: usernameTextField.stringValue, secret: passwordTextField.stringValue)
Account.validateCredentials(type: accountType, credentials: credentials, endpoint: apiURL, secretsProvider: Secrets()) { [weak self] result in
guard let self = self else { return } let credentials = Credentials(type: .readerBasic, username: usernameTextField.stringValue, secret: passwordTextField.stringValue)
Task { @MainActor in
var validationDidThrow = false
var validatedCredentials: Credentials?
do {
validatedCredentials = try await Account.validateCredentials(type: accountType, credentials: credentials, endpoint: apiURL, secretsProvider: Secrets())
} catch {
self.errorMessageLabel.stringValue = NSLocalizedString("Network error. Try again later.", comment: "Credentials Error")
validationDidThrow = true
}
self.actionButton.isEnabled = true self.actionButton.isEnabled = true
self.progressIndicator.isHidden = true self.progressIndicator.isHidden = true
self.progressIndicator.stopAnimation(self) self.progressIndicator.stopAnimation(self)
switch result {
case .success(let validatedCredentials):
guard let validatedCredentials = validatedCredentials else {
self.errorMessageLabel.stringValue = NSLocalizedString("Invalid email/password combination.", comment: "Credentials Error")
return
}
if self.account == nil {
self.account = AccountManager.shared.createAccount(type: self.accountType!)
}
do {
self.account?.endpointURL = apiURL
try self.account?.removeCredentials(type: .readerBasic) if validationDidThrow {
try self.account?.removeCredentials(type: .readerAPIKey) return
try self.account?.storeCredentials(credentials) }
try self.account?.storeCredentials(validatedCredentials)
guard let validatedCredentials else {
Task { @MainActor in self.errorMessageLabel.stringValue = NSLocalizedString("Invalid email/password combination.", comment: "Credentials Error")
do { return
try await self.account?.refreshAll() }
} catch {
NSApplication.shared.presentError(error) if self.account == nil {
} self.account = AccountManager.shared.createAccount(type: self.accountType!)
} }
self.hostWindow?.endSheet(self.window!, returnCode: NSApplication.ModalResponse.OK) do {
} catch { self.account?.endpointURL = apiURL
self.errorMessageLabel.stringValue = NSLocalizedString("Keychain error while storing credentials.", comment: "Credentials Error")
} try self.account?.removeCredentials(type: .readerBasic)
try self.account?.removeCredentials(type: .readerAPIKey)
case .failure: try self.account?.storeCredentials(credentials)
self.errorMessageLabel.stringValue = NSLocalizedString("Network error. Try again later.", comment: "Credentials Error") try self.account?.storeCredentials(validatedCredentials)
self.refreshAll()
self.hostWindow?.endSheet(self.window!, returnCode: NSApplication.ModalResponse.OK)
} catch {
self.errorMessageLabel.stringValue = NSLocalizedString("Keychain error while storing credentials.", comment: "Credentials Error")
} }
} }
} }
private func refreshAll() {
Task { @MainActor in
do {
try await self.account?.refreshAll()
} catch {
NSApplication.shared.presentError(error)
}
}
}
@IBAction func createAccountWithProvider(_ sender: Any) { @IBAction func createAccountWithProvider(_ sender: Any) {
switch accountType { switch accountType {
case .freshRSS: case .freshRSS:

View File

@ -122,47 +122,61 @@ class FeedbinAccountViewController: UITableViewController {
setNavigationEnabled(to: false) setNavigationEnabled(to: false)
let credentials = Credentials(type: .basic, username: trimmedEmail, secret: password) let credentials = Credentials(type: .basic, username: trimmedEmail, secret: password)
Account.validateCredentials(type: .feedbin, credentials: credentials, secretsProvider: secretsProvider) { result in
Task { @MainActor in
var validationDidThrow = false
var validatedCredentials: Credentials?
do {
validatedCredentials = try await Account.validateCredentials(type: .feedbin, credentials: credentials, secretsProvider: Secrets())
} catch {
self.showError(error.localizedDescription)
validationDidThrow = true
}
self.toggleActivityIndicatorAnimation(visible: false) self.toggleActivityIndicatorAnimation(visible: false)
self.setNavigationEnabled(to: true) self.setNavigationEnabled(to: true)
switch result { if validationDidThrow {
case .success(let credentials): return
if let credentials = credentials { }
if self.account == nil {
self.account = AccountManager.shared.createAccount(type: .feedbin) guard let validatedCredentials else {
} self.showError(NSLocalizedString("Invalid username/password combination.", comment: "Credentials Error"))
return
do { }
do { if self.account == nil {
try self.account?.removeCredentials(type: .basic) self.account = AccountManager.shared.createAccount(type: .feedbin)
} catch {} }
try self.account?.storeCredentials(credentials)
do {
Task { @MainActor in
do { try self.account?.removeCredentials(type: .basic)
try await self.account?.refreshAll() try self.account?.storeCredentials(validatedCredentials)
} catch {
self.presentError(error) self.refreshAll()
}
} self.dismiss(animated: true, completion: nil)
self.delegate?.dismiss()
self.dismiss(animated: true, completion: nil) } catch {
self.delegate?.dismiss() self.showError(NSLocalizedString("Keychain error while storing credentials.", comment: "Credentials Error"))
} catch {
self.showError(NSLocalizedString("Keychain error while storing credentials.", comment: "Credentials Error"))
}
} else {
self.showError(NSLocalizedString("Invalid email/password combination.", comment: "Credentials Error"))
}
case .failure:
self.showError(NSLocalizedString("Network error. Try again later.", comment: "Credentials Error"))
} }
} }
} }
private func refreshAll() {
Task { @MainActor in
do {
try await self.account?.refreshAll()
} catch {
self.presentError(error)
}
}
}
@IBAction func signUpWithProvider(_ sender: Any) { @IBAction func signUpWithProvider(_ sender: Any) {
let url = URL(string: "https://feedbin.com/signup")! let url = URL(string: "https://feedbin.com/signup")!
let safari = SFSafariViewController(url: url) let safari = SFSafariViewController(url: url)

View File

@ -98,57 +98,70 @@ class NewsBlurAccountViewController: UITableViewController {
showError(NSLocalizedString("There is already a NewsBlur account with that username created.", comment: "Duplicate Error")) showError(NSLocalizedString("There is already a NewsBlur account with that username created.", comment: "Duplicate Error"))
return return
} }
let password = passwordTextField.text ?? "" let password = passwordTextField.text ?? ""
startAnimatingActivityIndicator() startAnimatingActivityIndicator()
disableNavigation() disableNavigation()
let basicCredentials = Credentials(type: .newsBlurBasic, username: trimmedUsername, secret: password) let credentials = Credentials(type: .newsBlurBasic, username: trimmedUsername, secret: password)
Account.validateCredentials(type: .newsBlur, credentials: basicCredentials, secretsProvider: Secrets()) { result in
Task { @MainActor in
var validationDidThrow = false
var validatedCredentials: Credentials?
do {
validatedCredentials = try await Account.validateCredentials(type: .newsBlur, credentials: credentials, secretsProvider: Secrets())
} catch {
self.showError(error.localizedDescription)
validationDidThrow = true
}
self.stopAnimatingActivityIndicator() self.stopAnimatingActivityIndicator()
self.enableNavigation() self.enableNavigation()
switch result { if validationDidThrow {
case .success(let sessionCredentials): return
if let sessionCredentials = sessionCredentials { }
if self.account == nil { guard let validatedCredentials else {
self.account = AccountManager.shared.createAccount(type: .newsBlur) self.showError(NSLocalizedString("Invalid username/password combination.", comment: "Credentials Error"))
} return
}
do { if self.account == nil {
self.account = AccountManager.shared.createAccount(type: .newsBlur)
}
do { do {
try self.account?.removeCredentials(type: .newsBlurBasic)
try self.account?.removeCredentials(type: .newsBlurSessionId)
} catch {}
try self.account?.storeCredentials(basicCredentials)
try self.account?.storeCredentials(sessionCredentials)
Task { @MainActor in try self.account?.removeCredentials(type: .newsBlurBasic)
do { try self.account?.removeCredentials(type: .newsBlurSessionId)
try await self.account?.refreshAll() try self.account?.storeCredentials(credentials)
} catch { try self.account?.storeCredentials(validatedCredentials)
self.presentError(error)
}
}
self.dismiss(animated: true, completion: nil) self.refreshAll()
self.delegate?.dismiss()
} catch { self.dismiss(animated: true, completion: nil)
self.showError(NSLocalizedString("Keychain error while storing credentials.", comment: "Credentials Error")) self.delegate?.dismiss()
} } catch {
} else { self.showError(NSLocalizedString("Keychain error while storing credentials.", comment: "Credentials Error"))
self.showError(NSLocalizedString("Invalid username/password combination.", comment: "Credentials Error"))
}
case .failure(let error):
self.showError(error.localizedDescription)
} }
} }
} }
private func refreshAll() {
Task { @MainActor in
do {
try await self.account?.refreshAll()
} catch {
self.presentError(error)
}
}
}
@IBAction func signUpWithProvider(_ sender: Any) { @IBAction func signUpWithProvider(_ sender: Any) {
let url = URL(string: "https://newsblur.com")! let url = URL(string: "https://newsblur.com")!
let safari = SFSafariViewController(url: url) let safari = SFSafariViewController(url: url)

View File

@ -157,50 +157,65 @@ class ReaderAPIAccountViewController: UITableViewController {
disableNavigation() disableNavigation()
let credentials = Credentials(type: .readerBasic, username: trimmedUsername, secret: password) let credentials = Credentials(type: .readerBasic, username: trimmedUsername, secret: password)
Account.validateCredentials(type: type, credentials: credentials, endpoint: url, secretsProvider: Secrets()) { result in
Task { @MainActor in
var validationDidThrow = false
var validatedCredentials: Credentials?
do {
validatedCredentials = try await Account.validateCredentials(type: type, credentials: credentials, endpoint: url, secretsProvider: Secrets())
} catch {
self.showError(error.localizedDescription)
validationDidThrow = true
}
self.stopAnimatingActivityIndicator() self.stopAnimatingActivityIndicator()
self.enableNavigation() self.enableNavigation()
switch result { if validationDidThrow {
case .success(let validatedCredentials): return
if let validatedCredentials = validatedCredentials { }
if self.account == nil { guard let validatedCredentials else {
self.account = AccountManager.shared.createAccount(type: type) self.showError(NSLocalizedString("Invalid username/password combination.", comment: "Credentials Error"))
} return
}
do { if self.account == nil {
self.account?.endpointURL = url self.account = AccountManager.shared.createAccount(type: type)
}
try? self.account?.removeCredentials(type: .readerBasic)
try? self.account?.removeCredentials(type: .readerAPIKey)
try self.account?.storeCredentials(credentials)
try self.account?.storeCredentials(validatedCredentials)
self.dismiss(animated: true, completion: nil) do {
self.account?.endpointURL = url
Task { @MainActor in
do {
try await self.account?.refreshAll()
} catch {
self.showError(NSLocalizedString(error.localizedDescription, comment: "Accoount Refresh Error"))
}
}
self.delegate?.dismiss() try? self.account?.removeCredentials(type: .readerBasic)
} catch { try? self.account?.removeCredentials(type: .readerAPIKey)
self.showError(NSLocalizedString("Keychain error while storing credentials.", comment: "Credentials Error")) try self.account?.storeCredentials(credentials)
} try self.account?.storeCredentials(validatedCredentials)
} else {
self.showError(NSLocalizedString("Invalid username/password combination.", comment: "Credentials Error")) self.dismiss(animated: true, completion: nil)
}
case .failure(let error): self.refreshAll()
self.showError(error.localizedDescription)
self.delegate?.dismiss()
} catch {
self.showError(NSLocalizedString("Keychain error while storing credentials.", comment: "Credentials Error"))
} }
} }
} }
private func refreshAll() {
Task { @MainActor in
do {
try await self.account?.refreshAll()
} catch {
self.presentError(error)
}
}
}
private func retrieveCredentialsForAccount(for account: Account) throws -> Credentials? { private func retrieveCredentialsForAccount(for account: Account) throws -> Credentials? {
switch accountType { switch accountType {
case .bazQux, .inoreader, .theOldReader, .freshRSS: case .bazQux, .inoreader, .theOldReader, .freshRSS: