Let AppKeychain not be static only

This commit is contained in:
Danny Moesch 2019-07-20 23:31:13 +02:00
parent 5527c98568
commit b42401343d
6 changed files with 34 additions and 32 deletions

View file

@ -43,7 +43,7 @@ class GitServerSettingTableViewController: UITableViewController {
super.viewWillAppear(animated)
// Grey out ssh option if ssh_key is not present
if let sshLabel = sshLabel {
sshLabel.isEnabled = AppKeychain.contains(key: SshKey.PRIVATE.getKeychainKey())
sshLabel.isEnabled = AppKeychain.shared.contains(key: SshKey.PRIVATE.getKeychainKey())
}
}
override func viewDidLoad() {
@ -86,7 +86,7 @@ class GitServerSettingTableViewController: UITableViewController {
SVProgressHUD.setDefaultStyle(.light)
SVProgressHUD.show(withStatus: "PrepareRepository".localize())
var gitCredential: GitCredential
let privateKey: String? = AppKeychain.get(for: SshKey.PRIVATE.getKeychainKey())
let privateKey: String? = AppKeychain.shared.get(for: SshKey.PRIVATE.getKeychainKey())
if auth == "Password" || privateKey == nil {
gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: username))
} else {
@ -160,7 +160,7 @@ class GitServerSettingTableViewController: UITableViewController {
authenticationMethod = "Password"
} else if cell == authSSHKeyCell {
if !AppKeychain.contains(key: SshKey.PRIVATE.getKeychainKey()) {
if !AppKeychain.shared.contains(key: SshKey.PRIVATE.getKeychainKey()) {
Utils.alert(title: "CannotSelectSshKey".localize(), message: "PleaseSetupSshKeyFirst.".localize(), controller: self, completion: nil)
authenticationMethod = "Password"
} else {

View file

@ -139,7 +139,7 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
SVProgressHUD.setDefaultStyle(.light)
SVProgressHUD.show(withStatus: "SyncingPasswordStore".localize())
var gitCredential: GitCredential
let privateKey: String? = AppKeychain.get(for: SshKey.PRIVATE.getKeychainKey())
let privateKey: String? = AppKeychain.shared.get(for: SshKey.PRIVATE.getKeychainKey())
if SharedDefaults[.gitAuthenticationMethod] == "Password" || privateKey == nil {
gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: SharedDefaults[.gitUsername]!))
} else {

View file

@ -9,36 +9,38 @@
import KeychainAccess
public class AppKeychain {
public static let shared = AppKeychain()
private static let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
private let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
.accessibility(.whenUnlockedThisDeviceOnly)
.synchronizable(false)
public static func add(data: Data?, for key: String) {
public func add(data: Data?, for key: String) {
keychain[data: key] = data
}
public static func add(string: String?, for key: String) {
public func add(string: String?, for key: String) {
keychain[key] = string
}
public static func contains(key: String) -> Bool {
public func contains(key: String) -> Bool {
return (try? keychain.contains(key)) ?? false
}
public static func get(for key: String) -> Data? {
public func get(for key: String) -> Data? {
return try? keychain.getData(key)
}
public static func get(for key: String) -> String? {
public func get(for key: String) -> String? {
return try? keychain.getString(key)
}
public static func removeContent(for key: String) {
public func removeContent(for key: String) {
try? keychain.remove(key)
}
public static func removeAllContent() {
public func removeAllContent() {
try? keychain.removeAll()
}
}

View file

@ -17,10 +17,10 @@ public class PGPAgent {
// PGP passphrase
public var passphrase: String? {
set {
AppKeychain.add(string: newValue, for: "pgpKeyPassphrase")
AppKeychain.shared.add(string: newValue, for: "pgpKeyPassphrase")
}
get {
return AppKeychain.get(for: "pgpKeyPassphrase")
return AppKeychain.shared.get(for: "pgpKeyPassphrase")
}
}
@ -68,12 +68,12 @@ public class PGPAgent {
}
// Read the key data from keychain.
guard let pgpKeyData: Data = AppKeychain.get(for: keyType.getKeychainKey()) else {
guard let pgpKeyData: Data = AppKeychain.shared.get(for: keyType.getKeychainKey()) else {
throw AppError.KeyImport
}
// Remove the key data from keychain temporary, in case the following step crashes repeatedly.
AppKeychain.removeContent(for: keyType.getKeychainKey())
AppKeychain.shared.removeContent(for: keyType.getKeychainKey())
// Try GopenpgpwrapperReadKey first.
if let key = GopenpgpwrapperReadKey(pgpKeyData) {
@ -83,7 +83,7 @@ public class PGPAgent {
case .PRIVATE:
self.privateKey = key
}
AppKeychain.add(data: pgpKeyData, for: keyType.getKeychainKey())
AppKeychain.shared.add(data: pgpKeyData, for: keyType.getKeychainKey())
return
}
@ -98,7 +98,7 @@ public class PGPAgent {
case .PRIVATE:
self.privateKeyV2 = key
}
AppKeychain.add(data: pgpKeyData, for: keyType.getKeychainKey())
AppKeychain.shared.add(data: pgpKeyData, for: keyType.getKeychainKey())
return
}
@ -107,13 +107,13 @@ public class PGPAgent {
public func initPGPKey(from url: URL, keyType: PgpKey) throws {
let pgpKeyData = try Data(contentsOf: url)
AppKeychain.add(data: pgpKeyData, for: keyType.getKeychainKey())
AppKeychain.shared.add(data: pgpKeyData, for: keyType.getKeychainKey())
try initPGPKey(keyType)
}
public func initPGPKey(with armorKey: String, keyType: PgpKey) throws {
let pgpKeyData = armorKey.data(using: .ascii)!
AppKeychain.add(data: pgpKeyData, for: keyType.getKeychainKey())
AppKeychain.shared.add(data: pgpKeyData, for: keyType.getKeychainKey())
try initPGPKey(keyType)
}
@ -167,8 +167,8 @@ public class PGPAgent {
}
public func removePGPKeys() {
AppKeychain.removeContent(for: PgpKey.PUBLIC.getKeychainKey())
AppKeychain.removeContent(for: PgpKey.PRIVATE.getKeychainKey())
AppKeychain.shared.removeContent(for: PgpKey.PUBLIC.getKeychainKey())
AppKeychain.shared.removeContent(for: PgpKey.PRIVATE.getKeychainKey())
passphrase = nil
publicKey = nil
privateKey = nil

View file

@ -12,7 +12,7 @@ public class PasscodeLock {
private static let identifier = Globals.bundleIdentifier + "passcode"
/// Cached passcode to avoid frequent access to Keychain
private var passcode: String? = AppKeychain.get(for: PasscodeLock.identifier)
private var passcode: String? = AppKeychain.shared.get(for: PasscodeLock.identifier)
/// Constructor used to migrate passcode from SharedDefaults to Keychain
private init() {
@ -27,7 +27,7 @@ public class PasscodeLock {
}
public func save(passcode: String) {
AppKeychain.add(string: passcode, for: PasscodeLock.identifier)
AppKeychain.shared.add(string: passcode, for: PasscodeLock.identifier)
self.passcode = passcode
}
@ -36,7 +36,7 @@ public class PasscodeLock {
}
public func delete() {
AppKeychain.removeContent(for: PasscodeLock.identifier)
AppKeychain.shared.removeContent(for: PasscodeLock.identifier)
passcode = nil
}
}

View file

@ -39,19 +39,19 @@ public class PasswordStore {
public var gitPassword: String? {
set {
AppKeychain.add(string: newValue, for: "gitPassword")
AppKeychain.shared.add(string: newValue, for: "gitPassword")
}
get {
return AppKeychain.get(for: "gitPassword")
return AppKeychain.shared.get(for: "gitPassword")
}
}
public var gitSSHPrivateKeyPassphrase: String? {
set {
AppKeychain.add(string: newValue, for: "gitSSHPrivateKeyPassphrase")
AppKeychain.shared.add(string: newValue, for: "gitSSHPrivateKeyPassphrase")
}
get {
return AppKeychain.get(for: "gitSSHPrivateKeyPassphrase")
return AppKeychain.shared.get(for: "gitSSHPrivateKeyPassphrase")
}
}
@ -130,7 +130,7 @@ public class PasswordStore {
}
public func initGitSSHKey(with armorKey: String) throws {
AppKeychain.add(string: armorKey, for: SshKey.PRIVATE.getKeychainKey())
AppKeychain.shared.add(string: armorKey, for: SshKey.PRIVATE.getKeychainKey())
}
public func repositoryExisted() -> Bool {
@ -642,7 +642,7 @@ public class PasswordStore {
self.pgpAgent?.removePGPKeys()
AppKeychain.removeAllContent()
AppKeychain.shared.removeAllContent()
deleteCoreData(entityName: "PasswordEntity")
@ -726,7 +726,7 @@ public class PasswordStore {
Defaults.remove(.gitSSHKeySource)
Defaults.remove(.gitSSHPrivateKeyArmor)
Defaults.remove(.gitSSHPrivateKeyURL)
AppKeychain.removeContent(for: SshKey.PRIVATE.getKeychainKey())
AppKeychain.shared.removeContent(for: SshKey.PRIVATE.getKeychainKey())
gitSSHPrivateKeyPassphrase = nil
}