From e8389eb2628d14e51c9f08a923cb05f4bbb14e76 Mon Sep 17 00:00:00 2001 From: Danny Moesch Date: Sun, 20 Sep 2020 15:07:18 +0200 Subject: [PATCH] Enable SwiftLint rule 'identifier_name' and handle all violations --- .swiftlint.yml | 5 +- .../PGPKeyFIleImportTableViewController.swift | 2 +- .../PasswordDetailTableViewController.swift | 8 +- .../Controllers/PasswordsViewController.swift | 32 +++--- .../SSHKeyFileImportTableViewController.swift | 2 +- .../CredentialProviderViewController.swift | 4 +- .../Controllers/ExtensionViewController.swift | 4 +- .../OnePasswordExtensionConstants.swift | 2 +- passKit/Crypto/GopenPGPInterface.swift | 32 +++--- passKit/Crypto/ObjectivePGPInterface.swift | 4 +- passKit/Crypto/PGPAgent.swift | 10 +- .../Extensions/UIAlertActionExtension.swift | 4 +- passKit/Helpers/AppError.swift | 45 ++++----- passKit/Helpers/AppKeychain.swift | 8 +- passKit/Helpers/KeyFileManager.swift | 2 +- passKit/Models/PasswordEntity.swift | 10 +- passKit/Models/PasswordStore.swift | 97 +++++++++---------- passKit/Parser/Parser.swift | 26 ++--- passKitTests/Crypto/PGPAgentTest.swift | 10 +- passKitTests/Helpers/KeyFileManagerTest.swift | 2 +- passKitTests/Testbase/TestBase.swift | 4 +- 21 files changed, 157 insertions(+), 156 deletions(-) diff --git a/.swiftlint.yml b/.swiftlint.yml index 7b08739..dcf3b8f 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -77,7 +77,7 @@ whitelist_rules: - generic_type_name - ibinspectable_in_extension - identical_operands -# - identifier_name + - identifier_name - implicit_getter - implicit_return # - implicitly_unwrapped_optional @@ -212,6 +212,9 @@ attributes: closure_body_length: warning: 40 error: 60 +identifier_name: + excluded: ["id", "to", "Defaults"] + allowed_symbols: ["_"] type_name: max_length: 50 trailing_closure: diff --git a/pass/Controllers/PGPKeyFIleImportTableViewController.swift b/pass/Controllers/PGPKeyFIleImportTableViewController.swift index 83da48e..924cd12 100644 --- a/pass/Controllers/PGPKeyFIleImportTableViewController.swift +++ b/pass/Controllers/PGPKeyFIleImportTableViewController.swift @@ -52,7 +52,7 @@ extension PGPKeyFileImportTableViewController: UIDocumentPickerDelegate { // Start accessing a security-scoped resource. guard url.startAccessingSecurityScopedResource() else { // Handle the failure here. - throw AppError.ReadingFile(fileName) + throw AppError.readingFile(fileName: fileName) } // Make sure you release the security-scoped resource when you are done. diff --git a/pass/Controllers/PasswordDetailTableViewController.swift b/pass/Controllers/PasswordDetailTableViewController.swift index 5f7f8d3..1218855 100644 --- a/pass/Controllers/PasswordDetailTableViewController.swift +++ b/pass/Controllers/PasswordDetailTableViewController.swift @@ -99,10 +99,10 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni let requestPGPKeyPassphrase = Utils.createRequestPGPKeyPassphraseHandler(controller: self) self.password = try self.passwordStore.decrypt(passwordEntity: passwordEntity, keyID: keyID, requestPGPKeyPassphrase: requestPGPKeyPassphrase) self.showPassword() - } catch let AppError.PgpPrivateKeyNotFound(key) { + } catch let AppError.pgpPrivateKeyNotFound(keyID: key) { DispatchQueue.main.async { // alert: cancel or try again - let alert = UIAlertController(title: "CannotShowPassword".localize(), message: AppError.PgpPrivateKeyNotFound(keyID: key).localizedDescription, preferredStyle: .alert) + let alert = UIAlertController(title: "CannotShowPassword".localize(), message: AppError.pgpPrivateKeyNotFound(keyID: key).localizedDescription, preferredStyle: .alert) alert.addAction(UIAlertAction.cancelAndPopView(controller: self)) let selectKey = UIAlertAction.selectKey(controller: self) { action in self.decryptThenShowPassword(keyID: action.title) @@ -193,11 +193,11 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni tableView.reloadData() SVProgressHUD.showSuccess(withStatus: "Success".localize()) SVProgressHUD.dismiss(withDelay: 1) - } catch let AppError.PgpPublicKeyNotFound(key) { + } catch let AppError.pgpPublicKeyNotFound(keyID: key) { DispatchQueue.main.async { // alert: cancel or select keys SVProgressHUD.dismiss() - let alert = UIAlertController(title: "Cannot Edit Password", message: AppError.PgpPublicKeyNotFound(keyID: key).localizedDescription, preferredStyle: .alert) + let alert = UIAlertController(title: "Cannot Edit Password", message: AppError.pgpPublicKeyNotFound(keyID: key).localizedDescription, preferredStyle: .alert) alert.addAction(UIAlertAction.cancelAndPopView(controller: self)) let selectKey = UIAlertAction.selectKey(controller: self) { action in self.saveEditPassword(password: password, keyID: action.title) diff --git a/pass/Controllers/PasswordsViewController.swift b/pass/Controllers/PasswordsViewController.swift index 2e2eb3d..b5222c4 100644 --- a/pass/Controllers/PasswordsViewController.swift +++ b/pass/Controllers/PasswordsViewController.swift @@ -155,11 +155,11 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV SVProgressHUD.showSuccess(withStatus: "Done".localize()) SVProgressHUD.dismiss(withDelay: 1) } - } catch let AppError.PgpPublicKeyNotFound(key) { + } catch let AppError.pgpPublicKeyNotFound(keyID: key) { DispatchQueue.main.async { // alert: cancel or select keys SVProgressHUD.dismiss() - let alert = UIAlertController(title: "Cannot Encrypt Password", message: AppError.PgpPublicKeyNotFound(keyID: key).localizedDescription, preferredStyle: .alert) + let alert = UIAlertController(title: "Cannot Encrypt Password", message: AppError.pgpPublicKeyNotFound(keyID: key).localizedDescription, preferredStyle: .alert) alert.addAction(UIAlertAction.cancelAndPopView(controller: self)) let selectKey = UIAlertAction.selectKey(controller: self) { action in self.addPassword(password: password, keyID: action.title) @@ -291,7 +291,7 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV return } - let ac = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) + let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) let allAction = UIAlertAction(title: "All Passwords", style: .default) { _ in self.reloadTableView(parent: nil, label: .all) } @@ -303,11 +303,11 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV } let cancelAction = UIAlertAction.cancel() - ac.addAction(allAction) - ac.addAction(unsyncedAction) - ac.addAction(cancelAction) + alertController.addAction(allAction) + alertController.addAction(unsyncedAction) + alertController.addAction(cancelAction) - present(ac, animated: true, completion: nil) + present(alertController, animated: true, completion: nil) } override func viewWillAppear(_ animated: Bool) { @@ -488,10 +488,10 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV SVProgressHUD.showSuccess(withStatus: "PasswordCopiedToPasteboard.".localize()) SVProgressHUD.dismiss(withDelay: 0.6) } - } catch let AppError.PgpPrivateKeyNotFound(key) { + } catch let AppError.pgpPrivateKeyNotFound(keyID: key) { DispatchQueue.main.async { // alert: cancel or try again - let alert = UIAlertController(title: "CannotShowPassword".localize(), message: AppError.PgpPrivateKeyNotFound(keyID: key).localizedDescription, preferredStyle: .alert) + let alert = UIAlertController(title: "CannotShowPassword".localize(), message: AppError.pgpPrivateKeyNotFound(keyID: key).localizedDescription, preferredStyle: .alert) alert.addAction(UIAlertAction.cancelAndPopView(controller: self)) let selectKey = UIAlertAction.selectKey(controller: self) { action in self.decryptPassword(passwordEntity: passwordEntity, keyID: action.title) @@ -514,8 +514,8 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV var newSections = [(title: String, entries: [PasswordTableEntry])]() // initialize all sections - for i in 0 ..< sectionTitles.count { - newSections.append((title: sectionTitles[i], entries: [PasswordTableEntry]())) + for titleNumber in 0 ..< sectionTitles.count { + newSections.append((title: sectionTitles[titleNumber], entries: [PasswordTableEntry]())) } // put entries into sections @@ -525,10 +525,10 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV } // sort each list and set sectionTitles - for i in 0 ..< sectionTitles.count { - let entriesToSort = newSections[i].entries + for titleNumber in 0 ..< sectionTitles.count { + let entriesToSort = newSections[titleNumber].entries let sortedEntries = collation.sortedArray(from: entriesToSort, collationStringSelector: #selector(getter: PasswordTableEntry.title)) - newSections[i].entries = sortedEntries as! [PasswordTableEntry] + newSections[titleNumber].entries = sortedEntries as! [PasswordTableEntry] } // only keep non-empty sections @@ -544,8 +544,8 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV if identifier == "showPasswordDetail" { guard PGPAgent.shared.isPrepared else { Utils.alert(title: "CannotShowPassword".localize(), message: "PgpKeyNotSet.".localize(), controller: self, completion: nil) - if let s = sender as? UITableViewCell { - let selectedIndexPath = tableView.indexPath(for: s)! + if let sender = sender as? UITableViewCell { + let selectedIndexPath = tableView.indexPath(for: sender)! tableView.deselectRow(at: selectedIndexPath, animated: true) } return false diff --git a/pass/Controllers/SSHKeyFileImportTableViewController.swift b/pass/Controllers/SSHKeyFileImportTableViewController.swift index 2f283d1..456c9d1 100644 --- a/pass/Controllers/SSHKeyFileImportTableViewController.swift +++ b/pass/Controllers/SSHKeyFileImportTableViewController.swift @@ -44,7 +44,7 @@ extension SSHKeyFileImportTableViewController: UIDocumentPickerDelegate { // Start accessing a security-scoped resource. guard url.startAccessingSecurityScopedResource() else { // Handle the failure here. - throw AppError.ReadingFile(fileName) + throw AppError.readingFile(fileName: fileName) } // Make sure you release the security-scoped resource when you are done. diff --git a/passAutoFillExtension/Controllers/CredentialProviderViewController.swift b/passAutoFillExtension/Controllers/CredentialProviderViewController.swift index 07cc65b..6e87170 100644 --- a/passAutoFillExtension/Controllers/CredentialProviderViewController.swift +++ b/passAutoFillExtension/Controllers/CredentialProviderViewController.swift @@ -155,9 +155,9 @@ class CredentialProviderViewController: ASCredentialProviderViewController, UITa let passwordCredential = ASPasswordCredential(user: username, password: password) self.extensionContext.completeRequest(withSelectedCredential: passwordCredential) } - } catch let AppError.PgpPrivateKeyNotFound(key) { + } catch let AppError.pgpPrivateKeyNotFound(keyID: key) { DispatchQueue.main.async { - let alert = UIAlertController(title: "CannotShowPassword".localize(), message: AppError.PgpPrivateKeyNotFound(keyID: key).localizedDescription, preferredStyle: .alert) + let alert = UIAlertController(title: "CannotShowPassword".localize(), message: AppError.pgpPrivateKeyNotFound(keyID: key).localizedDescription, preferredStyle: .alert) alert.addAction(UIAlertAction.cancelAndPopView(controller: self)) let selectKey = UIAlertAction.selectKey(controller: self) { action in self.decryptPassword(passwordEntity: passwordEntity, keyID: action.title) diff --git a/passExtension/Controllers/ExtensionViewController.swift b/passExtension/Controllers/ExtensionViewController.swift index eb1bff9..78a136b 100644 --- a/passExtension/Controllers/ExtensionViewController.swift +++ b/passExtension/Controllers/ExtensionViewController.swift @@ -169,10 +169,10 @@ class ExtensionViewController: UIViewController, UITableViewDataSource, UITableV self.extensionContext?.completeRequest(returningItems: nil, completionHandler: nil) } } - } catch let AppError.PgpPrivateKeyNotFound(key) { + } catch let AppError.pgpPrivateKeyNotFound(keyID: key) { DispatchQueue.main.async { // alert: cancel or try again - let alert = UIAlertController(title: "CannotShowPassword".localize(), message: AppError.PgpPrivateKeyNotFound(keyID: key).localizedDescription, preferredStyle: .alert) + let alert = UIAlertController(title: "CannotShowPassword".localize(), message: AppError.pgpPrivateKeyNotFound(keyID: key).localizedDescription, preferredStyle: .alert) alert.addAction(UIAlertAction.cancelAndPopView(controller: self)) let selectKey = UIAlertAction.selectKey(controller: self) { action in self.decryptPassword(passwordEntity: passwordEntity, keyID: action.title) diff --git a/passExtension/Helpers/OnePasswordExtensionConstants.swift b/passExtension/Helpers/OnePasswordExtensionConstants.swift index 48f9787..d3b4fc7 100644 --- a/passExtension/Helpers/OnePasswordExtensionConstants.swift +++ b/passExtension/Helpers/OnePasswordExtensionConstants.swift @@ -47,5 +47,5 @@ enum OnePasswordExtensionError { static let errorCodeCollectFieldsScriptFailed = 4 static let errorCodeFillFieldsScriptFailed = 5 static let errorCodeUnexpectedData = 6 - static let errorCodeFailedToObtainURLStringFromWebView = 7 + static let errorCodeFailedToObtainURLStringFromWebView = 7 // swiftlint:disable:this identifier_name } diff --git a/passKit/Crypto/GopenPGPInterface.swift b/passKit/Crypto/GopenPGPInterface.swift index 1916bb4..8d51e3b 100644 --- a/passKit/Crypto/GopenPGPInterface.swift +++ b/passKit/Crypto/GopenPGPInterface.swift @@ -10,8 +10,8 @@ import Crypto struct GopenPGPInterface: PGPInterface { private static let errorMapping: [String: Error] = [ - "gopenpgp: error in unlocking key: openpgp: invalid data: private key checksum failure": AppError.WrongPassphrase, - "openpgp: incorrect key": AppError.KeyExpiredOrIncompatible, + "gopenpgp: error in unlocking key: openpgp: invalid data: private key checksum failure": AppError.wrongPassphrase, + "openpgp: incorrect key": AppError.keyExpiredOrIncompatible, ] private var publicKeys: [String: CryptoKey] = [:] @@ -23,24 +23,24 @@ struct GopenPGPInterface: PGPInterface { for key in pubKeys { var error: NSError? - guard let k = CryptoNewKeyFromArmored(key, &error) else { + guard let cryptoKey = CryptoNewKeyFromArmored(key, &error) else { guard error == nil else { throw error! } - throw AppError.KeyImport + throw AppError.keyImport } - publicKeys[k.getFingerprint().lowercased()] = k + publicKeys[cryptoKey.getFingerprint().lowercased()] = cryptoKey } for key in prvKeys { var error: NSError? - guard let k = CryptoNewKeyFromArmored(key, &error) else { + guard let cryptoKey = CryptoNewKeyFromArmored(key, &error) else { guard error == nil else { throw error! } - throw AppError.KeyImport + throw AppError.keyImport } - privateKeys[k.getFingerprint().lowercased()] = k + privateKeys[cryptoKey.getFingerprint().lowercased()] = cryptoKey } } @@ -71,9 +71,9 @@ struct GopenPGPInterface: PGPInterface { } func decrypt(encryptedData: Data, keyID: String, passphrase: String) throws -> Data? { - guard let e = privateKeys.first(where: { key, _ in key.hasSuffix(keyID.lowercased()) }), - let privateKey = privateKeys[e.key] else { - throw AppError.Decryption + guard let key = privateKeys.first(where: { key, _ in key.hasSuffix(keyID.lowercased()) }), + let privateKey = privateKeys[key.key] else { + throw AppError.decryption } do { @@ -84,7 +84,7 @@ struct GopenPGPInterface: PGPInterface { guard error == nil else { throw error! } - throw AppError.Decryption + throw AppError.decryption } let message = createPgpMessage(from: encryptedData) @@ -95,9 +95,9 @@ struct GopenPGPInterface: PGPInterface { } func encrypt(plainData: Data, keyID: String) throws -> Data { - guard let e = publicKeys.first(where: { key, _ in key.hasSuffix(keyID.lowercased()) }), - let publicKey = publicKeys[e.key] else { - throw AppError.Encryption + guard let key = publicKeys.first(where: { key, _ in key.hasSuffix(keyID.lowercased()) }), + let publicKey = publicKeys[key.key] else { + throw AppError.encryption } var error: NSError? @@ -106,7 +106,7 @@ struct GopenPGPInterface: PGPInterface { guard error == nil else { throw error! } - throw AppError.Encryption + throw AppError.encryption } let encryptedData = try keyRing.encrypt(CryptoNewPlainMessage(plainData.mutable as Data), privateKey: nil) diff --git a/passKit/Crypto/ObjectivePGPInterface.swift b/passKit/Crypto/ObjectivePGPInterface.swift index aa1ef57..7aee6a3 100644 --- a/passKit/Crypto/ObjectivePGPInterface.swift +++ b/passKit/Crypto/ObjectivePGPInterface.swift @@ -16,14 +16,14 @@ struct ObjectivePGPInterface: PGPInterface { init(publicArmoredKey: String, privateArmoredKey: String) throws { guard let publicKeyData = publicArmoredKey.data(using: .ascii), let privateKeyData = privateArmoredKey.data(using: .ascii) else { - throw AppError.KeyImport + throw AppError.keyImport } let publicKeys = try ObjectivePGP.readKeys(from: publicKeyData) let privateKeys = try ObjectivePGP.readKeys(from: privateKeyData) keyring.import(keys: publicKeys) keyring.import(keys: privateKeys) guard let publicKey = publicKeys.first, let privateKey = privateKeys.first else { - throw AppError.KeyImport + throw AppError.keyImport } self.publicKey = publicKey self.privateKey = privateKey diff --git a/passKit/Crypto/PGPAgent.swift b/passKit/Crypto/PGPAgent.swift index 24608b6..73d0009 100644 --- a/passKit/Crypto/PGPAgent.swift +++ b/passKit/Crypto/PGPAgent.swift @@ -21,7 +21,7 @@ public class PGPAgent { guard let publicKey: String = keyStore.get(for: PgpKey.PUBLIC.getKeychainKey()), let privateKey: String = keyStore.get(for: PgpKey.PRIVATE.getKeychainKey()) else { pgpInterface = nil - throw AppError.KeyImport + throw AppError.keyImport } do { pgpInterface = try GopenPGPInterface(publicArmoredKey: publicKey, privateArmoredKey: privateKey) @@ -48,7 +48,7 @@ public class PGPAgent { // Init keys. try checkAndInit() guard let pgpInterface = pgpInterface else { - throw AppError.Decryption + throw AppError.decryption } var keyID = keyID @@ -56,7 +56,7 @@ public class PGPAgent { if pgpInterface.keyID.count == 1 { keyID = pgpInterface.keyID.first! } else { - throw AppError.PgpPrivateKeyNotFound(keyID: keyID) + throw AppError.pgpPrivateKeyNotFound(keyID: keyID) } } @@ -83,14 +83,14 @@ public class PGPAgent { public func encrypt(plainData: Data, keyID: String) throws -> Data { try checkAndInit() guard let pgpInterface = pgpInterface else { - throw AppError.Encryption + throw AppError.encryption } var keyID = keyID if !pgpInterface.containsPublicKey(with: keyID) { if pgpInterface.keyID.count == 1 { keyID = pgpInterface.keyID.first! } else { - throw AppError.PgpPublicKeyNotFound(keyID: keyID) + throw AppError.pgpPublicKeyNotFound(keyID: keyID) } } return try pgpInterface.encrypt(plainData: plainData, keyID: keyID) diff --git a/passKit/Extensions/UIAlertActionExtension.swift b/passKit/Extensions/UIAlertActionExtension.swift index 358140f..4f1cc25 100644 --- a/passKit/Extensions/UIAlertActionExtension.swift +++ b/passKit/Extensions/UIAlertActionExtension.swift @@ -37,8 +37,8 @@ extension UIAlertAction { public static func selectKey(controller: UIViewController, handler: ((UIAlertAction) -> Void)?) -> UIAlertAction { UIAlertAction(title: "Select Key", style: .default) { _ in let selectKeyAlert = UIAlertController(title: "Select from imported keys", message: nil, preferredStyle: .actionSheet) - try? PGPAgent.shared.getShortKeyID().forEach { k in - let action = UIAlertAction(title: k, style: .default, handler: handler) + try? PGPAgent.shared.getShortKeyID().forEach { keyID in + let action = UIAlertAction(title: keyID, style: .default, handler: handler) selectKeyAlert.addAction(action) } selectKeyAlert.addAction(UIAlertAction.cancelAndPopView(controller: controller)) diff --git a/passKit/Helpers/AppError.swift b/passKit/Helpers/AppError.swift index 9958a62..86fa4d9 100644 --- a/passKit/Helpers/AppError.swift +++ b/passKit/Helpers/AppError.swift @@ -7,34 +7,35 @@ // public enum AppError: Error, Equatable { - case RepositoryNotSet - case RepositoryRemoteBranchNotFound(_: String) - case RepositoryBranchNotFound(_: String) - case KeyImport - case ReadingFile(_: String) - case PasswordDuplicated - case GitReset - case GitCreateSignature - case GitPushNotSuccessful - case PasswordEntity - case PgpPublicKeyNotFound(keyID: String) - case PgpPrivateKeyNotFound(keyID: String) - case KeyExpiredOrIncompatible - case WrongPassphrase - case WrongPasswordFilename - case Decryption - case Encryption - case Encoding - case Unknown + case repositoryNotSet + case repositoryRemoteBranchNotFound(branchName: String) + case repositoryBranchNotFound(branchName: String) + case keyImport + case readingFile(fileName: String) + case passwordDuplicated + case gitReset + case gitCreateSignature + case gitPushNotSuccessful + case passwordEntity + case pgpPublicKeyNotFound(keyID: String) + case pgpPrivateKeyNotFound(keyID: String) + case keyExpiredOrIncompatible + case wrongPassphrase + case wrongPasswordFilename + case decryption + case encryption + case encoding + case unknown } extension AppError: LocalizedError { public var errorDescription: String? { - let localizationKey = "\(String(describing: self).prefix { $0 != "(" })Error." + let enumName = String(describing: self) + let localizationKey = "\(enumName.first!.uppercased())\(enumName.dropFirst().prefix { $0 != "(" })Error." switch self { - case let .RepositoryRemoteBranchNotFound(name), let .RepositoryBranchNotFound(name), let .ReadingFile(name): + case let .repositoryRemoteBranchNotFound(name), let .repositoryBranchNotFound(name), let .readingFile(name): return localizationKey.localize(name) - case let .PgpPublicKeyNotFound(keyID), let .PgpPrivateKeyNotFound(keyID): + case let .pgpPublicKeyNotFound(keyID), let .pgpPrivateKeyNotFound(keyID): return localizationKey.localize(keyID) default: return localizationKey.localize() diff --git a/passKit/Helpers/AppKeychain.swift b/passKit/Helpers/AppKeychain.swift index 298ab5a..c68f974 100644 --- a/passKit/Helpers/AppKeychain.swift +++ b/passKit/Helpers/AppKeychain.swift @@ -44,11 +44,9 @@ public class AppKeychain: KeyStore { } public func removeAllContent(withPrefix prefix: String) { - for k in keychain.allKeys() { - if k.hasPrefix(prefix) { - try? keychain.remove(k) - } - } + keychain.allKeys() + .filter { $0.hasPrefix(prefix) } + .forEach { try? keychain.remove($0) } } public static func getPGPKeyPassphraseKey(keyID: String) -> String { diff --git a/passKit/Helpers/KeyFileManager.swift b/passKit/Helpers/KeyFileManager.swift index 77df162..b7c79fa 100644 --- a/passKit/Helpers/KeyFileManager.swift +++ b/passKit/Helpers/KeyFileManager.swift @@ -35,7 +35,7 @@ public class KeyFileManager { public func importKey(from string: String) throws { guard string.unicodeScalars.allSatisfy({ $0.isASCII }) else { - throw AppError.Encoding + throw AppError.encoding } keyHandler(string, keyType.getKeychainKey()) } diff --git a/passKit/Models/PasswordEntity.swift b/passKit/Models/PasswordEntity.swift index 058776e..81a241e 100644 --- a/passKit/Models/PasswordEntity.swift +++ b/passKit/Models/PasswordEntity.swift @@ -11,8 +11,8 @@ import SwiftyUserDefaults extension PasswordEntity { public var nameWithCategory: String { - if let p = path, p.hasSuffix(".gpg") { - return String(p.prefix(upTo: p.index(p.endIndex, offsetBy: -4))) + if let path = path, path.hasSuffix(".gpg") { + return String(path.prefix(upTo: path.index(path.endIndex, offsetBy: -4))) } return "" } @@ -33,10 +33,10 @@ extension PasswordEntity { } public func getURL() throws -> URL { - if let p = getPath().stringByAddingPercentEncodingForRFC3986(), let u = URL(string: p) { - return u + if let path = getPath().stringByAddingPercentEncodingForRFC3986(), let url = URL(string: path) { + return url } - throw AppError.Unknown + throw AppError.unknown } // XXX: define some getters to get core data, we need to consider diff --git a/passKit/Models/PasswordStore.swift b/passKit/Models/PasswordStore.swift index 1a06a67..39f51ee 100644 --- a/passKit/Models/PasswordStore.swift +++ b/passKit/Models/PasswordStore.swift @@ -53,7 +53,7 @@ public class PasswordStore { } } - private let fm = FileManager.default + private let fileManager = FileManager.default private lazy var context: NSManagedObjectContext = { let modelURL = Bundle(identifier: Globals.passKitBundleIdentifier)!.url(forResource: "pass", withExtension: "momd")! let managedObjectModel = NSManagedObjectModel(contentsOf: modelURL) @@ -86,7 +86,7 @@ public class PasswordStore { } public var sizeOfRepositoryByteCount: UInt64 { - (try? fm.allocatedSizeOfDirectoryAtURL(directoryURL: storeURL)) ?? 0 + (try? fileManager.allocatedSizeOfDirectoryAtURL(directoryURL: storeURL)) ?? 0 } public var numberOfLocalCommits: Int { @@ -108,7 +108,7 @@ public class PasswordStore { importExistingKeysIntoKeychain() do { - if fm.fileExists(atPath: storeURL.path) { + if fileManager.fileExists(atPath: storeURL.path) { try self.storeRepository = GTRepository(url: storeURL) } } catch { @@ -127,8 +127,7 @@ public class PasswordStore { } public func repositoryExists() -> Bool { - let fm = FileManager() - return fm.fileExists(atPath: Globals.repositoryPath) + fileManager.fileExists(atPath: Globals.repositoryPath) } public func passwordExisted(password: Password) -> Bool { @@ -178,8 +177,8 @@ public class PasswordStore { transferProgressBlock: @escaping (UnsafePointer, UnsafeMutablePointer) -> Void = { _, _ in }, checkoutProgressBlock: @escaping (String, UInt, UInt) -> Void = { _, _, _ in } ) throws { - try? fm.removeItem(at: storeURL) - try? fm.removeItem(at: tempStoreURL) + try? fileManager.removeItem(at: storeURL) + try? fileManager.removeItem(at: tempStoreURL) gitPassword = nil gitSSHPrivateKeyPassphrase = nil do { @@ -189,7 +188,7 @@ public class PasswordStore { options: options, transferProgressBlock: transferProgressBlock ) - try fm.moveItem(at: tempStoreURL, to: storeURL) + try fileManager.moveItem(at: tempStoreURL, to: storeURL) storeRepository = try GTRepository(url: storeURL) if (try storeRepository?.currentBranch().name) != branchName { try checkoutAndChangeBranch(withName: branchName, progressBlock: checkoutProgressBlock) @@ -211,12 +210,12 @@ public class PasswordStore { private func checkoutAndChangeBranch(withName localBranchName: String, progressBlock: @escaping (String, UInt, UInt) -> Void) throws { guard let storeRepository = storeRepository else { - throw AppError.RepositoryNotSet + throw AppError.repositoryNotSet } let remoteBranchName = "origin/\(localBranchName)" let remoteBranch = try storeRepository.lookUpBranch(withName: remoteBranchName, type: .remote, success: nil) guard let remoteBranchOid = remoteBranch.oid else { - throw AppError.RepositoryRemoteBranchNotFound(remoteBranchName) + throw AppError.repositoryRemoteBranchNotFound(branchName: remoteBranchName) } let localBranch = try storeRepository.createBranchNamed(localBranchName, from: remoteBranchOid, message: nil) try localBranch.updateTrackingBranch(remoteBranch) @@ -230,7 +229,7 @@ public class PasswordStore { progressBlock: @escaping (UnsafePointer, UnsafeMutablePointer) -> Void = { _, _ in } ) throws { guard let storeRepository = storeRepository else { - throw AppError.RepositoryNotSet + throw AppError.repositoryNotSet } let remote = try GTRemote(name: "origin", in: storeRepository) try storeRepository.pull(storeRepository.currentBranch(), from: remote, withOptions: options, progress: progressBlock) @@ -245,7 +244,7 @@ public class PasswordStore { private func updatePasswordEntityCoreData() { deleteCoreData(entityName: "PasswordEntity") do { - var q = try fm.contentsOfDirectory(atPath: storeURL.path) + var entities = try fileManager.contentsOfDirectory(atPath: storeURL.path) .filter { !$0.hasPrefix(".") } .map { filename -> PasswordEntity in let passwordEntity = NSEntityDescription.insertNewObject(forEntityName: "PasswordEntity", into: context) as! PasswordEntity @@ -258,18 +257,18 @@ public class PasswordStore { passwordEntity.parent = nil return passwordEntity } - while !q.isEmpty { - let e = q.first! - q.remove(at: 0) - guard !e.name!.hasPrefix(".") else { + while !entities.isEmpty { + let entity = entities.first! + entities.remove(at: 0) + guard !entity.name!.hasPrefix(".") else { continue } var isDirectory: ObjCBool = false - let filePath = storeURL.appendingPathComponent(e.path!).path - if fm.fileExists(atPath: filePath, isDirectory: &isDirectory) { + let filePath = storeURL.appendingPathComponent(entity.path!).path + if fileManager.fileExists(atPath: filePath, isDirectory: &isDirectory) { if isDirectory.boolValue { - e.isDir = true - let files = try fm.contentsOfDirectory(atPath: filePath) + entity.isDir = true + let files = try fileManager.contentsOfDirectory(atPath: filePath) .filter { !$0.hasPrefix(".") } .map { filename -> PasswordEntity in let passwordEntity = NSEntityDescription.insertNewObject(forEntityName: "PasswordEntity", into: context) as! PasswordEntity @@ -278,13 +277,13 @@ public class PasswordStore { } else { passwordEntity.name = filename } - passwordEntity.path = "\(e.path!)/\(filename)" - passwordEntity.parent = e + passwordEntity.path = "\(entity.path!)/\(filename)" + passwordEntity.parent = entity return passwordEntity } - q += files + entities += files } else { - e.isDir = false + entity.isDir = false } } } @@ -377,7 +376,7 @@ public class PasswordStore { private func gitAdd(path: String) throws { guard let storeRepository = storeRepository else { - throw AppError.RepositoryNotSet + throw AppError.repositoryNotSet } try storeRepository.index().addFile(path) try storeRepository.index().write() @@ -385,11 +384,11 @@ public class PasswordStore { private func gitRm(path: String) throws { guard let storeRepository = storeRepository else { - throw AppError.RepositoryNotSet + throw AppError.repositoryNotSet } let url = storeURL.appendingPathComponent(path) - if fm.fileExists(atPath: url.path) { - try fm.removeItem(at: url) + if fileManager.fileExists(atPath: url.path) { + try fileManager.removeItem(at: url) } try storeRepository.index().removeFile(path) try storeRepository.index().write() @@ -397,30 +396,30 @@ public class PasswordStore { private func deleteDirectoryTree(at url: URL) throws { var tempURL = storeURL.appendingPathComponent(url.deletingLastPathComponent().path) - var count = try fm.contentsOfDirectory(atPath: tempURL.path).count + var count = try fileManager.contentsOfDirectory(atPath: tempURL.path).count while count == 0 { - try fm.removeItem(at: tempURL) + try fileManager.removeItem(at: tempURL) tempURL.deleteLastPathComponent() - count = try fm.contentsOfDirectory(atPath: tempURL.path).count + count = try fileManager.contentsOfDirectory(atPath: tempURL.path).count } } private func createDirectoryTree(at url: URL) throws { let tempURL = storeURL.appendingPathComponent(url.deletingLastPathComponent().path) - try fm.createDirectory(at: tempURL, withIntermediateDirectories: true, attributes: nil) + try fileManager.createDirectory(at: tempURL, withIntermediateDirectories: true, attributes: nil) } private func gitMv(from: String, to: String) throws { let fromURL = storeURL.appendingPathComponent(from) let toURL = storeURL.appendingPathComponent(to) - try fm.moveItem(at: fromURL, to: toURL) + try fileManager.moveItem(at: fromURL, to: toURL) try gitAdd(path: to) try gitRm(path: from) } private func gitCommit(message: String) throws -> GTCommit? { guard let storeRepository = storeRepository else { - throw AppError.RepositoryNotSet + throw AppError.repositoryNotSet } let newTree = try storeRepository.index().writeTree() let headReference = try storeRepository.headReference() @@ -428,7 +427,7 @@ public class PasswordStore { try commitEnum.pushSHA(headReference.targetOID!.sha) let parent = commitEnum.nextObject() as! GTCommit guard let signature = gitSignatureForNow else { - throw AppError.GitCreateSignature + throw AppError.gitCreateSignature } let commit = try storeRepository.createCommit(with: newTree, message: message, author: signature, committer: signature, parents: [parent], updatingReferenceNamed: headReference.name) return commit @@ -436,7 +435,7 @@ public class PasswordStore { private func getLocalBranch(withName branchName: String) throws -> GTBranch? { guard let storeRepository = storeRepository else { - throw AppError.RepositoryNotSet + throw AppError.repositoryNotSet } let reference = GTBranch.localNamePrefix().appending(branchName) let branches = try storeRepository.branches(withPrefix: reference) @@ -448,20 +447,20 @@ public class PasswordStore { transferProgressBlock: @escaping (UInt32, UInt32, Int, UnsafeMutablePointer) -> Void = { _, _, _, _ in } ) throws { guard let storeRepository = storeRepository else { - throw AppError.RepositoryNotSet + throw AppError.repositoryNotSet } if let branch = try getLocalBranch(withName: Defaults.gitBranchName) { let remote = try GTRemote(name: "origin", in: storeRepository) try storeRepository.push(branch, to: remote, withOptions: options, progress: transferProgressBlock) } if numberOfLocalCommits != 0 { - throw AppError.GitPushNotSuccessful + throw AppError.gitPushNotSuccessful } } private func addPasswordEntities(password: Password) throws -> PasswordEntity? { guard !passwordExisted(password: password) else { - throw AppError.PasswordDuplicated + throw AppError.passwordDuplicated } var passwordURL = password.url @@ -472,7 +471,7 @@ public class PasswordStore { passwordURL = passwordURL.deletingLastPathComponent() // better identify errors before saving a new password if passwordURL.path != ".", passwordURL.path.count >= previousPathLength { - throw AppError.WrongPasswordFilename + throw AppError.wrongPasswordFilename } previousPathLength = passwordURL.path.count } @@ -610,8 +609,8 @@ public class PasswordStore { public func erase() { // Delete files. - try? fm.removeItem(at: storeURL) - try? fm.removeItem(at: tempStoreURL) + try? fileManager.removeItem(at: storeURL) + try? fileManager.removeItem(at: tempStoreURL) // Delete PGP key, SSH key and other secrets from the keychain. AppKeychain.shared.removeAllContent() @@ -637,7 +636,7 @@ public class PasswordStore { // return the number of discarded commits public func reset() throws -> Int { guard let storeRepository = storeRepository else { - throw AppError.RepositoryNotSet + throw AppError.repositoryNotSet } // get a list of local commits let localCommits = try getLocalCommits() @@ -648,7 +647,7 @@ public class PasswordStore { guard let firstLocalCommit = localCommits.last, firstLocalCommit.parents.count == 1, let newHead = firstLocalCommit.parents.first else { - throw AppError.GitReset + throw AppError.gitReset } try storeRepository.reset(to: newHead, resetType: .hard) setAllSynced() @@ -661,16 +660,16 @@ public class PasswordStore { private func getLocalCommits() throws -> [GTCommit] { guard let storeRepository = storeRepository else { - throw AppError.RepositoryNotSet + throw AppError.repositoryNotSet } // get the remote branch let remoteBranchName = Defaults.gitBranchName guard let remoteBranch = try storeRepository.remoteBranches().first(where: { $0.shortName == remoteBranchName }) else { - throw AppError.RepositoryRemoteBranchNotFound(remoteBranchName) + throw AppError.repositoryRemoteBranchNotFound(branchName: remoteBranchName) } // check oid before calling localCommitsRelative guard remoteBranch.oid != nil else { - throw AppError.RepositoryRemoteBranchNotFound(remoteBranchName) + throw AppError.repositoryRemoteBranchNotFound(branchName: remoteBranchName) } // get a list of local commits @@ -682,7 +681,7 @@ public class PasswordStore { let keyID = keyID ?? findGPGID(from: encryptedDataPath) let encryptedData = try Data(contentsOf: encryptedDataPath) guard let decryptedData = try PGPAgent.shared.decrypt(encryptedData: encryptedData, keyID: keyID, requestPGPKeyPassphrase: requestPGPKeyPassphrase) else { - throw AppError.Decryption + throw AppError.decryption } let plainText = String(data: decryptedData, encoding: .utf8) ?? "" let url = try passwordEntity.getURL() @@ -696,7 +695,7 @@ public class PasswordStore { } public func removeGitSSHKeys() { - try? fm.removeItem(atPath: Globals.gitSSHPrivateKeyPath) + try? fileManager.removeItem(atPath: Globals.gitSSHPrivateKeyPath) Defaults.remove(\.gitSSHKeySource) Defaults.remove(\.gitSSHPrivateKeyArmor) Defaults.remove(\.gitSSHPrivateKeyURL) diff --git a/passKit/Parser/Parser.swift b/passKit/Parser/Parser.swift index 40f8d1c..208f514 100644 --- a/passKit/Parser/Parser.swift +++ b/passKit/Parser/Parser.swift @@ -25,41 +25,41 @@ class Parser { private func getAdditionFields() -> [AdditionField] { var additions: [AdditionField] = [] var unknownIndex: UInt = 0 - var i = purgedAdditionalLines.startIndex - while i < purgedAdditionalLines.count { - let line = purgedAdditionalLines[i] - i += 1 + var lineNumber = purgedAdditionalLines.startIndex + while lineNumber < purgedAdditionalLines.count { + let line = purgedAdditionalLines[lineNumber] + lineNumber += 1 var (key, value) = Parser.getKeyValuePair(from: line) if key == nil { unknownIndex += 1 key = Constants.unknown(unknownIndex) } else if value == Constants.MULTILINE_WITH_LINE_BREAK_INDICATOR { - value = gatherMultilineValue(startingAt: &i, removingLineBreaks: false) + value = gatherMultilineValue(startingAt: &lineNumber, removingLineBreaks: false) } else if value == Constants.MULTILINE_WITHOUT_LINE_BREAK_INDICATOR { - value = gatherMultilineValue(startingAt: &i, removingLineBreaks: true) + value = gatherMultilineValue(startingAt: &lineNumber, removingLineBreaks: true) } additions.append(key! => value) } return additions } - private func gatherMultilineValue(startingAt i: inout Int, removingLineBreaks: Bool) -> String { + private func gatherMultilineValue(startingAt lineNumber: inout Int, removingLineBreaks: Bool) -> String { var result = "" - guard i < purgedAdditionalLines.count else { + guard lineNumber < purgedAdditionalLines.count else { return result } - let numberInitialBlanks = purgedAdditionalLines[i].enumerated().first { + let numberInitialBlanks = purgedAdditionalLines[lineNumber].enumerated().first { $1 != Character(Constants.BLANK) - }?.0 ?? purgedAdditionalLines[i].count + }?.0 ?? purgedAdditionalLines[lineNumber].count guard numberInitialBlanks != 0 else { return result } let initialBlanks = String(repeating: Constants.BLANK, count: numberInitialBlanks) - while i < purgedAdditionalLines.count, purgedAdditionalLines[i].starts(with: initialBlanks) { - result.append(String(purgedAdditionalLines[i].dropFirst(numberInitialBlanks))) + while lineNumber < purgedAdditionalLines.count, purgedAdditionalLines[lineNumber].starts(with: initialBlanks) { + result.append(String(purgedAdditionalLines[lineNumber].dropFirst(numberInitialBlanks))) result.append(Constants.getSeparator(breakingLines: !removingLineBreaks)) - i += 1 + lineNumber += 1 } return result.trimmed } diff --git a/passKitTests/Crypto/PGPAgentTest.swift b/passKitTests/Crypto/PGPAgentTest.swift index 866b98d..0777a80 100644 --- a/passKitTests/Crypto/PGPAgentTest.swift +++ b/passKitTests/Crypto/PGPAgentTest.swift @@ -90,10 +90,10 @@ class PGPAgentTest: XCTestCase { try KeyFileManager(keyType: PgpKey.PUBLIC, keyPath: "", keyHandler: keychain.add).importKey(from: RSA2048.publicKey) XCTAssertFalse(pgpAgent.isPrepared) XCTAssertThrowsError(try pgpAgent.initKeys()) { - XCTAssertEqual($0 as! AppError, AppError.KeyImport) + XCTAssertEqual($0 as! AppError, AppError.keyImport) } XCTAssertThrowsError(try basicEncryptDecrypt(using: pgpAgent, keyID: RSA2048.fingerprint)) { - XCTAssertEqual($0 as! AppError, AppError.KeyImport) + XCTAssertEqual($0 as! AppError, AppError.keyImport) } } @@ -109,7 +109,7 @@ class PGPAgentTest: XCTestCase { try importKeys(ED25519.publicKey, RSA2048.privateKey) XCTAssert(pgpAgent.isPrepared) XCTAssertThrowsError(try basicEncryptDecrypt(using: pgpAgent, keyID: ED25519.fingerprint, encryptKeyID: RSA2048.fingerprint)) { - XCTAssertEqual($0 as! AppError, AppError.KeyExpiredOrIncompatible) + XCTAssertEqual($0 as! AppError, AppError.keyExpiredOrIncompatible) } } @@ -128,7 +128,7 @@ class PGPAgentTest: XCTestCase { keychain.removeContent(for: PgpKey.PUBLIC.getKeychainKey()) keychain.removeContent(for: PgpKey.PRIVATE.getKeychainKey()) XCTAssertThrowsError(try basicEncryptDecrypt(using: pgpAgent, keyID: ED25519.fingerprint)) { - XCTAssertEqual($0 as! AppError, AppError.KeyImport) + XCTAssertEqual($0 as! AppError, AppError.keyImport) } } @@ -151,7 +151,7 @@ class PGPAgentTest: XCTestCase { // Provide the wrong passphrase. XCTAssertThrowsError(try basicEncryptDecrypt(using: pgpAgent, keyID: RSA2048.fingerprint, requestPassphrase: provideIncorrectPassphrase)) { - XCTAssertEqual($0 as! AppError, AppError.WrongPassphrase) + XCTAssertEqual($0 as! AppError, AppError.wrongPassphrase) } XCTAssertEqual(passphraseRequestCalledCount, 2) diff --git a/passKitTests/Helpers/KeyFileManagerTest.swift b/passKitTests/Helpers/KeyFileManagerTest.swift index 3de0cf0..18dc830 100644 --- a/passKitTests/Helpers/KeyFileManagerTest.swift +++ b/passKitTests/Helpers/KeyFileManagerTest.swift @@ -59,7 +59,7 @@ class KeyFileManagerTest: XCTestCase { func testImportKeyFromNonAsciiString() throws { XCTAssertThrowsError(try KeyFileManagerTest.keyFileManager.importKey(from: "≠")) { - XCTAssertEqual($0 as! AppError, AppError.Encoding) + XCTAssertEqual($0 as! AppError, AppError.encoding) } } diff --git a/passKitTests/Testbase/TestBase.swift b/passKitTests/Testbase/TestBase.swift index 3282072..5bf5d4f 100644 --- a/passKitTests/Testbase/TestBase.swift +++ b/passKitTests/Testbase/TestBase.swift @@ -49,11 +49,11 @@ func assertDefaults( } infix operator ∈: AdditionPrecedence -func ∈ (field: AdditionField, password: Password) -> Bool { +func ∈ (field: AdditionField, password: Password) -> Bool { // swiftlint:disable:this identifier_name password.getFilteredAdditions().contains(field) } infix operator ∉: AdditionPrecedence -func ∉ (field: AdditionField, password: Password) -> Bool { +func ∉ (field: AdditionField, password: Password) -> Bool { // swiftlint:disable:this identifier_name !(field ∈ password) }