mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-22 06:15:59 +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
@@ -678,6 +678,8 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
return
|
||||
}
|
||||
|
||||
// Existing trust only proves this is the same device key we trusted before.
|
||||
// It does not by itself grant a new feature, such as clipboard sync.
|
||||
let trustedData = TrustStore.shared.findTrustedKey(for: peerCertificate.secretID)
|
||||
if let trustedData {
|
||||
guard let currentData = try? peerCertificate.serializedData(),
|
||||
@@ -685,13 +687,10 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
self.rejectTransfer(with: .reject)
|
||||
return
|
||||
}
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.notificationSyncSetupConfirmed(connection: self)
|
||||
}
|
||||
submitUserConsent(accepted: true, trustDevice: false)
|
||||
return
|
||||
}
|
||||
|
||||
// The QR token proves the user accepted this specific pairing use case.
|
||||
// Require it even for already-trusted devices before adding a new grant.
|
||||
let matches = TrustStore.shared.confirmPendingPairingTrust(
|
||||
secretIdHex: peerCertificate.secretID.hex,
|
||||
useCase: pairingUseCase,
|
||||
@@ -703,9 +702,19 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
return
|
||||
}
|
||||
|
||||
TrustStore.shared.addTrusted(certificate: peerCertificate, device: device)
|
||||
// Preserve the existing trusted certificate when present; otherwise store
|
||||
// the newly paired device and attach the confirmed use-case grant.
|
||||
if trustedData != nil {
|
||||
TrustStore.shared.grantUseCase(pairingUseCase, for: peerCertificate.secretID.hex)
|
||||
} else {
|
||||
TrustStore.shared.addTrusted(
|
||||
certificate: peerCertificate,
|
||||
device: device,
|
||||
grantedUseCases: [pairingUseCase]
|
||||
)
|
||||
}
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.notificationSyncSetupConfirmed(connection: self)
|
||||
self.delegate?.pairingSetupConfirmed(connection: self)
|
||||
}
|
||||
submitUserConsent(accepted: true, trustDevice: false)
|
||||
return
|
||||
@@ -738,7 +747,22 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
if let textMetadata = frame.v1.introduction.textMetadata.first {
|
||||
let isURL = textMetadata.type == .url
|
||||
textPayloadID = textMetadata.payloadID
|
||||
isClipboardSyncTransfer = !isURL && isAuthenticated && receiverAuthenticationAccepted
|
||||
let isRemoteCopy = frame.v1.introduction.hasUseCase
|
||||
&& frame.v1.introduction.useCase == .remoteCopy
|
||||
if isRemoteCopy && !isURL {
|
||||
let peerFingerprint = peerCertificate?.secretID.hex
|
||||
let isClipboardSyncAllowed = isAuthenticated
|
||||
&& receiverAuthenticationAccepted
|
||||
&& TrustStore.shared.hasUseCaseGrant(.clipboardSync, for: peerFingerprint)
|
||||
guard isClipboardSyncAllowed else {
|
||||
log("[InboundNearbyConnection \(self.id)] Rejecting clipboard sync transfer because the sender is not trusted for clipboard sync.")
|
||||
self.rejectTransfer(with: .reject, markAsUserRejected: false)
|
||||
return
|
||||
}
|
||||
isClipboardSyncTransfer = true
|
||||
} else {
|
||||
isClipboardSyncTransfer = false
|
||||
}
|
||||
|
||||
let metadata = TransferMetadata(
|
||||
files: [],
|
||||
@@ -797,17 +821,15 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
let pairingUseCase = PairingUseCase(protoValue: pairingMetadata.useCase) {
|
||||
let secretIdHex = peerCertificate.secretID.hex
|
||||
if accepted {
|
||||
if TrustStore.shared.findTrustedKey(for: peerCertificate.secretID) == nil {
|
||||
if let token = pairingToken {
|
||||
TrustStore.shared.registerPendingPairingTrust(
|
||||
secretIdHex: secretIdHex,
|
||||
pinCode: token,
|
||||
useCase: pairingUseCase
|
||||
)
|
||||
} else {
|
||||
log("[InboundNearbyConnection \(self.id)] Missing pairing token; pending trust not registered.")
|
||||
TrustStore.shared.clearPendingPairingTrust(secretIdHex: secretIdHex)
|
||||
}
|
||||
if let token = pairingToken {
|
||||
TrustStore.shared.registerPendingPairingTrust(
|
||||
secretIdHex: secretIdHex,
|
||||
pinCode: token,
|
||||
useCase: pairingUseCase
|
||||
)
|
||||
} else {
|
||||
log("[InboundNearbyConnection \(self.id)] Missing pairing token; pending trust not registered.")
|
||||
TrustStore.shared.clearPendingPairingTrust(secretIdHex: secretIdHex)
|
||||
}
|
||||
} else {
|
||||
TrustStore.shared.clearPendingPairingTrust(secretIdHex: secretIdHex)
|
||||
@@ -896,7 +918,9 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
switch useCase {
|
||||
case .notificationSync:
|
||||
return .notificationSync
|
||||
case .clipboardSync, .none:
|
||||
case .clipboardSync:
|
||||
return .clipboardSync
|
||||
case .none:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -391,9 +391,9 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
|
||||
}
|
||||
}
|
||||
|
||||
func notificationSyncSetupConfirmed(connection _: InboundNearbyConnection) {
|
||||
func pairingSetupConfirmed(connection _: InboundNearbyConnection) {
|
||||
inboundAppDelegates.forEach { delegate in
|
||||
delegate.notificationSyncSetupConfirmed()
|
||||
delegate.pairingSetupConfirmed()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -770,6 +770,7 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
|
||||
urls: [URL],
|
||||
textToSend: String?,
|
||||
preparationFinished: (() -> Void)? = nil,
|
||||
isClipboardSyncTransfer: Bool = false,
|
||||
receiverAuthenticationPolicy: ReceiverAuthenticationPolicy
|
||||
) -> Bool {
|
||||
log("Starting outgoing transfer to \(deviceID)")
|
||||
@@ -808,6 +809,7 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
|
||||
id: deviceID,
|
||||
urlsToSend: localUrls,
|
||||
textToSend: textToSend,
|
||||
isClipboardSyncTransfer: isClipboardSyncTransfer,
|
||||
receiverAuthenticationPolicy: receiverAuthenticationPolicy
|
||||
)
|
||||
conn.remoteDeviceInfo = info.device
|
||||
|
||||
@@ -22,6 +22,7 @@ class OutboundNearbyConnection: NearbyConnection {
|
||||
private var currentState: State = .initial
|
||||
private let urlsToSend: [URL]
|
||||
private let textToSend: String?
|
||||
private let isClipboardSyncTransfer: Bool
|
||||
private let receiverAuthenticationPolicy: ReceiverAuthenticationPolicy
|
||||
private var ukeyClientFinishMsgData: Data?
|
||||
private var queue: [OutgoingFileTransfer] = []
|
||||
@@ -42,11 +43,13 @@ class OutboundNearbyConnection: NearbyConnection {
|
||||
id: String,
|
||||
urlsToSend: [URL],
|
||||
textToSend: String?,
|
||||
isClipboardSyncTransfer: Bool = false,
|
||||
receiverAuthenticationPolicy: ReceiverAuthenticationPolicy
|
||||
) {
|
||||
|
||||
self.urlsToSend = urlsToSend
|
||||
self.textToSend = textToSend
|
||||
self.isClipboardSyncTransfer = isClipboardSyncTransfer
|
||||
self.receiverAuthenticationPolicy = receiverAuthenticationPolicy
|
||||
|
||||
super.init(connection: connection, id: id)
|
||||
@@ -398,6 +401,9 @@ class OutboundNearbyConnection: NearbyConnection {
|
||||
var introduction = Sharing_Nearby_Frame()
|
||||
introduction.version = .v1
|
||||
introduction.v1.type = .introduction
|
||||
if isClipboardSyncTransfer {
|
||||
introduction.v1.introduction.useCase = .remoteCopy
|
||||
}
|
||||
|
||||
if let textToSend = textToSend {
|
||||
var meta = Sharing_Nearby_TextMetadata()
|
||||
|
||||
@@ -19,7 +19,7 @@ protocol InboundNearbyConnectionDelegate {
|
||||
func updatedTransferProgress(connection: InboundNearbyConnection, progress: Double)
|
||||
func connectionWasTerminated(connection: InboundNearbyConnection, savedFiles: [URL], error: Error?)
|
||||
func showPlusScreen()
|
||||
func notificationSyncSetupConfirmed(connection: InboundNearbyConnection)
|
||||
func pairingSetupConfirmed(connection: InboundNearbyConnection)
|
||||
}
|
||||
|
||||
|
||||
@@ -51,11 +51,11 @@ public protocol InboundAppDelegate: AnyObject {
|
||||
func connectionWasTerminated(connectionID: String, from device: RemoteDeviceInfo?, savedFiles: [URL], wasPlainTextTransfer: Bool, wasClipboardSync: Bool, error: Error?)
|
||||
func transferProgress(connectionID: String, progress: Double)
|
||||
func showPlusScreen()
|
||||
func notificationSyncSetupConfirmed()
|
||||
func pairingSetupConfirmed()
|
||||
}
|
||||
|
||||
extension InboundAppDelegate {
|
||||
func notificationSyncSetupConfirmed() {}
|
||||
func pairingSetupConfirmed() {}
|
||||
}
|
||||
|
||||
|
||||
@@ -235,6 +235,7 @@ public struct TransferMetadata {
|
||||
case url
|
||||
case wifiPassword
|
||||
case notificationSync
|
||||
case clipboardSync
|
||||
}
|
||||
|
||||
private func getSummary() -> String {
|
||||
@@ -298,6 +299,12 @@ public struct TransferMetadata {
|
||||
return textDescription
|
||||
}
|
||||
return "NotificationSyncConsentPromptFromDevice".localized(with: deviceName)
|
||||
|
||||
case .clipboardSync:
|
||||
if let textDescription, !textDescription.isEmpty {
|
||||
return textDescription
|
||||
}
|
||||
return "ClipboardSyncConsentPromptFromDevice".localized(with: deviceName)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -306,7 +313,7 @@ public struct TransferMetadata {
|
||||
}
|
||||
}
|
||||
|
||||
enum PairingUseCase: String {
|
||||
enum PairingUseCase: String, Codable, Hashable {
|
||||
case notificationSync = "notification_sync"
|
||||
case clipboardSync = "clipboard_sync"
|
||||
|
||||
|
||||
+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