passforios/passKit/Helpers/AppKeychain.swift

48 lines
1.2 KiB
Swift
Raw Permalink Normal View History

//
// AppKeychain.swift
// passKit
//
// Created by Danny Moesch on 25.06.19.
// Copyright © 2019 Bob Sun. All rights reserved.
//
import KeychainAccess
public class AppKeychain: KeyStore {
2019-07-20 23:31:13 +02:00
public static let shared = AppKeychain()
2019-07-20 23:31:13 +02:00
private let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
.accessibility(.whenUnlockedThisDeviceOnly)
.synchronizable(false)
2019-07-20 23:31:13 +02:00
public func add(string: String?, for key: String) {
keychain[key] = string
}
2019-07-20 23:31:13 +02:00
public func contains(key: String) -> Bool {
(try? keychain.contains(key)) ?? false
}
2019-07-20 23:31:13 +02:00
public func get(for key: String) -> String? {
try? keychain.getString(key)
}
2019-07-20 23:31:13 +02:00
public func removeContent(for key: String) {
try? keychain.remove(key)
}
2019-07-20 23:31:13 +02:00
public func removeAllContent() {
try? keychain.removeAll()
}
public func removeAllContent(withPrefix prefix: String) {
keychain.allKeys()
.filter { $0.hasPrefix(prefix) }
.forEach { try? keychain.remove($0) }
}
public static func getPGPKeyPassphraseKey(keyID: String) -> String {
Globals.pgpKeyPassphrase + "-" + keyID
}
}