From 1ac1144b2d381fcdbdcef07d20e5ebe2efa057d7 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Thu, 20 Jun 2019 11:19:15 -0500 Subject: [PATCH] Reduce footprint of Account credentials API --- Frameworks/Account/Account.swift | 52 +++++++++++-------- .../Feedbin/FeedbinAccountDelegate.swift | 2 +- .../ReaderAPI/ReaderAPIAccountDelegate.swift | 2 +- .../AccountsFeedbinWindowController.swift | 7 ++- .../AccountsReaderAPIWindowController.swift | 7 ++- 5 files changed, 37 insertions(+), 33 deletions(-) diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index 2f9682066..9a1bf5d28 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -295,34 +295,40 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, } - public func retrieveBasicCredentials() throws -> Credentials? { - guard let username = self.username, let server = delegate.server else { + public func retrieveCredentials() throws -> Credentials? { + switch type { + case .feedbin: + guard let username = self.username, let server = delegate.server else { + return nil + } + return try CredentialsManager.retrieveBasicCredentials(server: server, username: username) + case .freshRSS: + guard let username = self.username, let server = delegate.server else { + return nil + } + return try CredentialsManager.retrieveReaderAPIAuthCredentials(server: server, username: username) + default: return nil } - return try CredentialsManager.retrieveBasicCredentials(server: server, username: username) } - public func removeBasicCredentials() throws { - guard let username = self.username, let server = delegate.server else { - return + public func removeCredentials() throws { + switch type { + case .feedbin: + guard let username = self.username, let server = delegate.server else { + return + } + try CredentialsManager.removeBasicCredentials(server: server, username: username) + self.username = nil + case .freshRSS: + guard let username = self.username, let server = delegate.server else { + return + } + try CredentialsManager.removeReaderAPIAuthCredentials(server: server, username: username) + self.username = nil + default: + break } - try CredentialsManager.removeBasicCredentials(server: server, username: username) - self.username = nil - } - - public func retrieveReaderAPIAuthCredentials() throws -> Credentials? { - guard let username = self.username, let server = delegate.server else { - return nil - } - return try CredentialsManager.retrieveReaderAPIAuthCredentials(server: server, username: username) - } - - public func removeReaderAPIAuthCredentials() throws { - guard let username = self.username, let server = delegate.server else { - return - } - try CredentialsManager.removeReaderAPIAuthCredentials(server: server, username: username) - self.username = nil } public static func validateCredentials(transport: Transport = URLSession.webserviceTransport(), type: AccountType, credentials: Credentials, endpoint: URL? = nil, completion: @escaping (Result) -> Void) { diff --git a/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift b/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift index 05d3c0d0c..2dd9a4f2a 100644 --- a/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift +++ b/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift @@ -510,7 +510,7 @@ final class FeedbinAccountDelegate: AccountDelegate { } func accountDidInitialize(_ account: Account) { - credentials = try? account.retrieveBasicCredentials() + credentials = try? account.retrieveCredentials() accountMetadata = account.metadata } diff --git a/Frameworks/Account/ReaderAPI/ReaderAPIAccountDelegate.swift b/Frameworks/Account/ReaderAPI/ReaderAPIAccountDelegate.swift index 6e80bd3db..6efba4115 100644 --- a/Frameworks/Account/ReaderAPI/ReaderAPIAccountDelegate.swift +++ b/Frameworks/Account/ReaderAPI/ReaderAPIAccountDelegate.swift @@ -411,7 +411,7 @@ final class ReaderAPIAccountDelegate: AccountDelegate { func accountDidInitialize(_ account: Account) { accountMetadata = account.metadata - credentials = try? account.retrieveReaderAPIAuthCredentials() + credentials = try? account.retrieveCredentials() } static func validateCredentials(transport: Transport, credentials: Credentials, endpoint: URL?, completion: @escaping (Result) -> Void) { diff --git a/Mac/Preferences/Accounts/AccountsFeedbinWindowController.swift b/Mac/Preferences/Accounts/AccountsFeedbinWindowController.swift index 191599c07..a1e0af2cf 100644 --- a/Mac/Preferences/Accounts/AccountsFeedbinWindowController.swift +++ b/Mac/Preferences/Accounts/AccountsFeedbinWindowController.swift @@ -27,10 +27,9 @@ class AccountsFeedbinWindowController: NSWindowController { } override func windowDidLoad() { - if let account = account, let credentials = try? account.retrieveBasicCredentials() { - if case .basic(let username, let password) = credentials { + if let account = account, let credentials = try? account.retrieveCredentials() { + if case .basic(let username, _) = credentials { usernameTextField.stringValue = username - passwordTextField.stringValue = password } actionButton.title = NSLocalizedString("Update", comment: "Update") } else { @@ -87,7 +86,7 @@ class AccountsFeedbinWindowController: NSWindowController { } do { - try self.account?.removeBasicCredentials() + try self.account?.removeCredentials() try self.account?.storeCredentials(validatedCredentials) if newAccount { self.account?.refreshAll() { result in diff --git a/Mac/Preferences/Accounts/AccountsReaderAPIWindowController.swift b/Mac/Preferences/Accounts/AccountsReaderAPIWindowController.swift index f41a6327d..d4a986339 100644 --- a/Mac/Preferences/Accounts/AccountsReaderAPIWindowController.swift +++ b/Mac/Preferences/Accounts/AccountsReaderAPIWindowController.swift @@ -42,10 +42,9 @@ class AccountsReaderAPIWindowController: NSWindowController { } } - if let account = account, let credentials = try? account.retrieveBasicCredentials() { - if case .basic(let username, let password) = credentials { + if let account = account, let credentials = try? account.retrieveCredentials() { + if case .basic(let username, _) = credentials { usernameTextField.stringValue = username - passwordTextField.stringValue = password } actionButton.title = NSLocalizedString("Update", comment: "Update") } else { @@ -110,7 +109,7 @@ class AccountsReaderAPIWindowController: NSWindowController { do { self.account?.endpointURL = apiURL - try self.account?.removeReaderAPIAuthCredentials() + try self.account?.removeCredentials() try self.account?.storeCredentials(validatedCredentials) if newAccount {