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

View file

@ -139,7 +139,7 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
SVProgressHUD.setDefaultStyle(.light) SVProgressHUD.setDefaultStyle(.light)
SVProgressHUD.show(withStatus: "SyncingPasswordStore".localize()) SVProgressHUD.show(withStatus: "SyncingPasswordStore".localize())
var gitCredential: GitCredential 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 { if SharedDefaults[.gitAuthenticationMethod] == "Password" || privateKey == nil {
gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: SharedDefaults[.gitUsername]!)) gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: SharedDefaults[.gitUsername]!))
} else { } else {

View file

@ -9,36 +9,38 @@
import KeychainAccess import KeychainAccess
public class AppKeychain { 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) .accessibility(.whenUnlockedThisDeviceOnly)
.synchronizable(false) .synchronizable(false)
public static func add(data: Data?, for key: String) { public func add(data: Data?, for key: String) {
keychain[data: key] = data keychain[data: key] = data
} }
public static func add(string: String?, for key: String) { public func add(string: String?, for key: String) {
keychain[key] = string keychain[key] = string
} }
public static func contains(key: String) -> Bool { public func contains(key: String) -> Bool {
return (try? keychain.contains(key)) ?? false 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) 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) return try? keychain.getString(key)
} }
public static func removeContent(for key: String) { public func removeContent(for key: String) {
try? keychain.remove(key) try? keychain.remove(key)
} }
public static func removeAllContent() { public func removeAllContent() {
try? keychain.removeAll() try? keychain.removeAll()
} }
} }

View file

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

View file

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

View file

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