mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-22 22:36:01 +00:00
Add clipboard sync pairing
This commit is contained in:
1 parent
2f6b0ff5ba
commit
72bfd88161
11 files changed
+1118
-537
No files matched your search
+6
-6
@@ -1,8 +1,8 @@
|
||||
//
|
||||
// NotificationSyncPairingToken.swift
|
||||
// PairingToken.swift
|
||||
// QuickDrop
|
||||
//
|
||||
// Created by Codex on 2026-03-15.
|
||||
// Created by Leon Böttger on 2026-03-15.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
@@ -10,7 +10,7 @@ import QRCode
|
||||
import SwiftUI
|
||||
import LUI
|
||||
|
||||
enum NotificationSyncPairingToken {
|
||||
enum PairingToken {
|
||||
static func generate() -> String {
|
||||
Data.randomData(length: 16).urlSafeBase64EncodedString()
|
||||
}
|
||||
@@ -22,14 +22,14 @@ enum NotificationSyncPairingToken {
|
||||
return keyId.hex.lowercased()
|
||||
}
|
||||
|
||||
static func qrPayload(token: String, receiverFingerprint: String, useCase: PairingUseCase = .notificationSync) -> String {
|
||||
static func qrPayload(token: String, receiverFingerprint: String, useCase: PairingUseCase) -> String {
|
||||
"quickdrop://pair?token=\(token)&usecase=\(useCase.rawValue)&receiver=\(receiverFingerprint)"
|
||||
}
|
||||
|
||||
static func makeQrImage(
|
||||
token: String,
|
||||
receiverFingerprint: String,
|
||||
useCase: PairingUseCase = .notificationSync,
|
||||
useCase: PairingUseCase,
|
||||
foregroundColor: CGColor = CGColor(gray: 0, alpha: 1),
|
||||
backgroundColor: CGColor = CGColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
|
||||
) -> Image? {
|
||||
@@ -47,7 +47,7 @@ enum NotificationSyncPairingToken {
|
||||
.image(dimension: 1000)
|
||||
return Image(decorative: qrCodeImage, scale: 1.0, orientation: .up)
|
||||
} catch {
|
||||
log("[NotificationSyncPairingToken] QR code generation failed: \(error)")
|
||||
log("[PairingToken] QR code generation failed: \(error)")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -71,17 +71,43 @@ class TrustStore: ObservableObject {
|
||||
}
|
||||
|
||||
|
||||
func addTrusted(certificate: Sharing_Nearby_PublicCertificate, device: RemoteDeviceInfo) {
|
||||
func addTrusted(
|
||||
certificate: Sharing_Nearby_PublicCertificate,
|
||||
device: RemoteDeviceInfo,
|
||||
grantedUseCases: Set<PairingUseCase> = []
|
||||
) {
|
||||
guard let publicKeyData = try? certificate.serializedData() else { return }
|
||||
let fingerprint = certificate.secretID.hex
|
||||
let existingGrants = trustedCertificates[fingerprint]?.grantedUseCases ?? []
|
||||
|
||||
trustedCertificates[certificate.secretID.hex] = TrustedCertificate(
|
||||
trustedCertificates[fingerprint] = TrustedCertificate(
|
||||
device: device,
|
||||
creationDate: Date(),
|
||||
certificateData: publicKeyData
|
||||
certificateData: publicKeyData,
|
||||
grantedUseCases: existingGrants.union(grantedUseCases)
|
||||
)
|
||||
|
||||
saveTrustedCertificates()
|
||||
}
|
||||
|
||||
|
||||
func grantUseCase(_ useCase: PairingUseCase, for secretIdHex: String) {
|
||||
let normalized = secretIdHex.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
|
||||
guard let existing = trustedCertificates[normalized] else { return }
|
||||
trustedCertificates[normalized] = existing.withGrantedUseCase(useCase)
|
||||
saveTrustedCertificates()
|
||||
}
|
||||
|
||||
|
||||
func hasUseCaseGrant(_ useCase: PairingUseCase, for secretIdHex: String?) -> Bool {
|
||||
guard let normalized = secretIdHex?
|
||||
.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
.lowercased(),
|
||||
!normalized.isEmpty else {
|
||||
return false
|
||||
}
|
||||
return trustedCertificates[normalized]?.grantedUseCases.contains(useCase) == true
|
||||
}
|
||||
|
||||
|
||||
func removeTrusted(secretIdHex: String) {
|
||||
@@ -142,6 +168,43 @@ class TrustStore: ObservableObject {
|
||||
let device: RemoteDeviceInfo
|
||||
let creationDate: Date
|
||||
let certificateData: Data
|
||||
let grantedUseCases: Set<PairingUseCase>
|
||||
|
||||
init(
|
||||
device: RemoteDeviceInfo,
|
||||
creationDate: Date,
|
||||
certificateData: Data,
|
||||
grantedUseCases: Set<PairingUseCase> = []
|
||||
) {
|
||||
self.device = device
|
||||
self.creationDate = creationDate
|
||||
self.certificateData = certificateData
|
||||
self.grantedUseCases = grantedUseCases
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case device
|
||||
case creationDate
|
||||
case certificateData
|
||||
case grantedUseCases
|
||||
}
|
||||
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
device = try container.decode(RemoteDeviceInfo.self, forKey: .device)
|
||||
creationDate = try container.decode(Date.self, forKey: .creationDate)
|
||||
certificateData = try container.decode(Data.self, forKey: .certificateData)
|
||||
grantedUseCases = try container.decodeIfPresent(Set<PairingUseCase>.self, forKey: .grantedUseCases) ?? []
|
||||
}
|
||||
|
||||
func withGrantedUseCase(_ useCase: PairingUseCase) -> TrustedCertificate {
|
||||
TrustedCertificate(
|
||||
device: device,
|
||||
creationDate: creationDate,
|
||||
certificateData: certificateData,
|
||||
grantedUseCases: grantedUseCases.union([useCase])
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
struct PendingPairingTrust: Equatable {
|
||||
|
||||
Reference in new issue
Block a user