mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-20 13:26:03 +00:00
Add Trust Revoke View
This commit is contained in:
1 parent
448cc15a7e
commit
d3dc89f412
10 files changed
+2438
-1467
No files matched your search
@@ -547,9 +547,8 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
|
||||
|
||||
func submitUserConsent(accepted: Bool, trustDevice: Bool, storeInTemp: Bool = false) {
|
||||
|
||||
if trustDevice, let peerCertificate = peerCertificate {
|
||||
TrustStore.shared.addTrusted(certificate: peerCertificate)
|
||||
if trustDevice, let peerCertificate = peerCertificate, let remoteDeviceInfo = remoteDeviceInfo {
|
||||
TrustStore.shared.addTrusted(certificate: peerCertificate, device: remoteDeviceInfo)
|
||||
}
|
||||
|
||||
DispatchQueue.global(qos: .utility).async {
|
||||
|
||||
@@ -46,10 +46,10 @@ public protocol OutboundAppDelegate: AnyObject {
|
||||
}
|
||||
|
||||
|
||||
public struct RemoteDeviceInfo: Identifiable, Equatable {
|
||||
public struct RemoteDeviceInfo: Codable, Identifiable, Equatable {
|
||||
public let name: String?
|
||||
public let type: DeviceType
|
||||
public let qrCodeData:Data?
|
||||
public let qrCodeData: Data?
|
||||
public var id: String?
|
||||
|
||||
init(name: String, type: DeviceType, id: String? = nil) {
|
||||
@@ -67,7 +67,7 @@ public struct RemoteDeviceInfo: Identifiable, Equatable {
|
||||
}
|
||||
|
||||
|
||||
public enum DeviceType: Int32 {
|
||||
public enum DeviceType: Int32, Codable {
|
||||
case unknown = 0
|
||||
case phone
|
||||
case tablet
|
||||
|
||||
@@ -8,26 +8,80 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
class TrustStore {
|
||||
|
||||
class TrustStore: ObservableObject {
|
||||
|
||||
static let shared = TrustStore()
|
||||
|
||||
@Published private(set) var trustedCertificates: [String: TrustedCertificate] = [:]
|
||||
|
||||
private let trustedKeysKey = "com.leonboettger.quickdrop.identity.trustedKeys"
|
||||
|
||||
|
||||
private func getTrustedKeys() -> [String: Data] {
|
||||
return AppGroup.appGroupUD.dictionary(forKey: trustedKeysKey) as? [String: Data] ?? [:]
|
||||
|
||||
private init() {
|
||||
loadTrustedCertificates()
|
||||
}
|
||||
|
||||
|
||||
|
||||
// MARK: - Persistence
|
||||
|
||||
private func loadTrustedCertificates() {
|
||||
guard let dict = AppGroup.appGroupUD.dictionary(forKey: trustedKeysKey) as? [String: Data] else {
|
||||
trustedCertificates = [:]
|
||||
return
|
||||
}
|
||||
|
||||
var decoded: [String: TrustedCertificate] = [:]
|
||||
for (key, data) in dict {
|
||||
if let cert = try? JSONDecoder().decode(TrustedCertificate.self, from: data) {
|
||||
decoded[key] = cert
|
||||
}
|
||||
}
|
||||
trustedCertificates = decoded
|
||||
}
|
||||
|
||||
|
||||
private func saveTrustedCertificates() {
|
||||
var dict: [String: Data] = [:]
|
||||
for (key, cert) in trustedCertificates {
|
||||
if let data = try? JSONEncoder().encode(cert) {
|
||||
dict[key] = data
|
||||
}
|
||||
}
|
||||
AppGroup.appGroupUD.set(dict, forKey: trustedKeysKey)
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Public API
|
||||
|
||||
func findTrustedKey(for secretIdHash: Data) -> Data? {
|
||||
return getTrustedKeys()[secretIdHash.hex]
|
||||
trustedCertificates[secretIdHash.hex]?.certificateData
|
||||
}
|
||||
|
||||
|
||||
func addTrusted(certificate: Sharing_Nearby_PublicCertificate) {
|
||||
|
||||
func addTrusted(certificate: Sharing_Nearby_PublicCertificate, device: RemoteDeviceInfo) {
|
||||
guard let publicKeyData = try? certificate.serializedData() else { return }
|
||||
var keys = getTrustedKeys()
|
||||
keys[certificate.secretID.hex] = publicKeyData
|
||||
AppGroup.appGroupUD.set(keys, forKey: trustedKeysKey)
|
||||
|
||||
trustedCertificates[certificate.secretID.hex] = TrustedCertificate(
|
||||
device: device,
|
||||
creationDate: Date(),
|
||||
certificateData: publicKeyData
|
||||
)
|
||||
|
||||
saveTrustedCertificates()
|
||||
}
|
||||
|
||||
|
||||
func removeTrusted(secretIdHex: String) {
|
||||
trustedCertificates.removeValue(forKey: secretIdHex)
|
||||
saveTrustedCertificates()
|
||||
}
|
||||
|
||||
|
||||
struct TrustedCertificate: Codable, Equatable {
|
||||
|
||||
// warning: do not rename for persistence
|
||||
let device: RemoteDeviceInfo
|
||||
let creationDate: Date
|
||||
let certificateData: Data
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user