forked from Jesssullivan/tummycrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHostApp.swift
More file actions
467 lines (415 loc) · 17.5 KB
/
HostApp.swift
File metadata and controls
467 lines (415 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import SwiftUI
import FileProvider
import Security
import os.log
private let hostLogger = Logger(subsystem: "io.tinyland.tcfs.ios", category: "host")
@main
struct TCFSApp: App {
@StateObject private var viewModel = TCFSViewModel()
@StateObject private var authViewModel = AuthViewModel()
var body: some Scene {
WindowGroup {
ContentView(viewModel: viewModel, authViewModel: authViewModel)
.onOpenURL { url in
handleDeepLink(url)
}
}
}
private func handleDeepLink(_ url: URL) {
guard url.scheme == "tcfs" else {
hostLogger.warning("Ignoring URL with unknown scheme: \(url.absoluteString)")
return
}
let host = url.host()
switch host {
case "bootstrap":
handleBootstrapLink(url)
case "enroll":
handleEnrollLink(url)
default:
hostLogger.warning("Unrecognized deep link host: \(host ?? "nil") in \(url.absoluteString)")
}
}
private func handleBootstrapLink(_ url: URL) {
// BootstrapConfig.parse() already handles tcfs://bootstrap?data=<base64>
guard let config = BootstrapConfig.parse(url.absoluteString) else {
hostLogger.error("Failed to parse bootstrap deep link: \(url.absoluteString)")
return
}
let deviceId = config.device_id
?? "ios-\(UIDevice.current.name.lowercased().replacingOccurrences(of: " ", with: "-"))"
viewModel.saveConfig(
endpoint: config.s3_endpoint,
bucket: config.s3_bucket,
accessKey: config.access_key,
s3Secret: config.s3_secret,
remotePrefix: config.remote_prefix ?? "default",
deviceId: deviceId,
passphrase: config.encryption_passphrase ?? "",
salt: config.encryption_salt ?? ""
)
hostLogger.info("Bootstrap config saved via deep link (endpoint=\(config.s3_endpoint))")
}
private func handleEnrollLink(_ url: URL) {
// Extract the data query parameter, matching QRScannerView's parsing
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
let dataParam = components.queryItems?.first(where: { $0.name == "data" })?.value else {
hostLogger.error("Enroll deep link missing 'data' parameter: \(url.absoluteString)")
return
}
authViewModel.processInviteData(dataParam, tcfsViewModel: viewModel)
hostLogger.info("Enrollment invite processed via deep link")
}
}
class TCFSViewModel: ObservableObject {
@Published var status: String = "Not configured"
@Published var isConfigured: Bool = false
@Published var syncFileCount: UInt64 = 0
@Published var syncLastError: String? = nil
private let domain = NSFileProviderDomain(
identifier: NSFileProviderDomainIdentifier("io.tinyland.tcfs"),
displayName: "TCFS"
)
init() {
checkConfiguration()
refreshSyncStatus()
}
func refreshSyncStatus() {
guard isConfigured else { return }
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
guard let self = self else { return }
guard let config = Self.loadConfigForStatus() else { return }
do {
let provider = try TcfsProviderHandle(config: config)
let syncStatus = try provider.getSyncStatus()
DispatchQueue.main.async {
self.syncFileCount = syncStatus.filesSynced
self.syncLastError = syncStatus.lastError
}
} catch {
DispatchQueue.main.async {
self.syncLastError = error.localizedDescription
}
}
}
}
private static func loadConfigForStatus() -> ProviderConfig? {
func readKeychain(_ account: String) -> String? {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: "io.tinyland.tcfs.config",
kSecAttrAccount as String: account,
kSecAttrAccessGroup as String: "group.io.tinyland.tcfs",
kSecReturnData as String: true,
kSecMatchLimit as String: kSecMatchLimitOne,
]
var item: CFTypeRef?
guard SecItemCopyMatching(query as CFDictionary, &item) == errSecSuccess,
let data = item as? Data,
let value = String(data: data, encoding: .utf8) else {
return nil
}
return value
}
guard let endpoint = readKeychain("s3_endpoint"),
let bucket = readKeychain("s3_bucket"),
let accessKey = readKeychain("access_key"),
let secret = readKeychain("s3_secret"),
let prefix = readKeychain("remote_prefix"),
let deviceId = readKeychain("device_id") else {
return nil
}
return ProviderConfig(
s3Endpoint: endpoint,
s3Bucket: bucket,
accessKey: accessKey,
s3Secret: secret,
remotePrefix: prefix,
deviceId: deviceId,
encryptionPassphrase: readKeychain("encryption_passphrase") ?? "",
encryptionSalt: readKeychain("encryption_salt") ?? ""
)
}
func checkConfiguration() {
let fields = ["s3_endpoint", "s3_bucket", "access_key", "s3_secret", "device_id"]
var allPresent = true
for field in fields {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: "io.tinyland.tcfs.config",
kSecAttrAccount as String: field,
kSecAttrAccessGroup as String: "group.io.tinyland.tcfs",
kSecReturnData as String: true,
kSecMatchLimit as String: kSecMatchLimitOne,
]
var item: CFTypeRef?
if SecItemCopyMatching(query as CFDictionary, &item) != errSecSuccess {
allPresent = false
break
}
}
isConfigured = allPresent
status = allPresent ? "Configured" : "Missing credentials"
}
func registerDomain() {
status = "Registering..."
NSFileProviderManager.remove(domain) { [weak self] _ in
guard let self = self else { return }
Thread.sleep(forTimeInterval: 1.0)
NSFileProviderManager.add(self.domain) { error in
DispatchQueue.main.async {
if let error = error {
hostLogger.error("Domain registration failed: \(error.localizedDescription)")
self.status = "Error: \(error.localizedDescription)"
} else {
hostLogger.info("Domain registered successfully")
self.status = "Active"
}
}
}
}
}
func saveConfig(
endpoint: String,
bucket: String,
accessKey: String,
s3Secret: String,
remotePrefix: String,
deviceId: String,
passphrase: String,
salt: String
) {
let entries: [(String, String)] = [
("s3_endpoint", endpoint),
("s3_bucket", bucket),
("access_key", accessKey),
("s3_secret", s3Secret),
("remote_prefix", remotePrefix),
("device_id", deviceId),
("encryption_passphrase", passphrase),
("encryption_salt", salt),
]
for (account, value) in entries {
let data = value.data(using: .utf8)!
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: "io.tinyland.tcfs.config",
kSecAttrAccount as String: account,
kSecAttrAccessGroup as String: "group.io.tinyland.tcfs",
]
SecItemDelete(query as CFDictionary)
var addQuery = query
addQuery[kSecValueData as String] = data
addQuery[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlock
let status = SecItemAdd(addQuery as CFDictionary, nil)
if status != errSecSuccess {
hostLogger.error("Keychain write failed for \(account): \(status)")
}
}
checkConfiguration()
}
}
struct ContentView: View {
@ObservedObject var viewModel: TCFSViewModel
@ObservedObject var authViewModel: AuthViewModel
@State private var endpoint = ""
@State private var bucket = ""
@State private var accessKey = ""
@State private var s3Secret = ""
@State private var remotePrefix = ""
@State private var deviceId = ""
@State private var passphrase = ""
@State private var salt = ""
@State private var showingConfig = false
/// Load current keychain values into the form fields when the sheet opens.
private func loadKeychainIntoForm() {
func read(_ account: String) -> String {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: "io.tinyland.tcfs.config",
kSecAttrAccount as String: account,
kSecAttrAccessGroup as String: "group.io.tinyland.tcfs",
kSecReturnData as String: true,
kSecMatchLimit as String: kSecMatchLimitOne,
]
var item: CFTypeRef?
guard SecItemCopyMatching(query as CFDictionary, &item) == errSecSuccess,
let data = item as? Data,
let value = String(data: data, encoding: .utf8) else {
return ""
}
return value
}
endpoint = read("s3_endpoint")
bucket = read("s3_bucket")
accessKey = read("access_key")
s3Secret = read("s3_secret")
remotePrefix = read("remote_prefix")
deviceId = read("device_id")
passphrase = read("encryption_passphrase")
salt = read("encryption_salt")
}
var body: some View {
NavigationView {
Group {
if !viewModel.isConfigured {
// --- Onboarding: unconfigured device ---
VStack(spacing: 24) {
Spacer()
Image(systemName: "externaldrive.badge.icloud")
.font(.system(size: 64))
.foregroundColor(.accentColor)
Text("Welcome to TCFS")
.font(.title2.bold())
Text("Scan an enrollment QR code from an existing device to get started.")
.multilineTextAlignment(.center)
.foregroundColor(.secondary)
.padding(.horizontal, 32)
NavigationLink {
QREnrollmentView(viewModel: viewModel, authViewModel: authViewModel)
} label: {
Label("Scan QR Code", systemImage: "qrcode.viewfinder")
.font(.headline)
.frame(maxWidth: .infinity)
.padding()
.background(Color.accentColor)
.foregroundColor(.white)
.cornerRadius(12)
}
.padding(.horizontal, 32)
Button("Configure Manually") {
showingConfig = true
}
.foregroundColor(.secondary)
.font(.subheadline)
Spacer()
}
.navigationTitle("TCFS")
} else {
// --- Configured: main dashboard ---
List {
Section("Status") {
HStack {
Text("FileProvider")
Spacer()
Text(viewModel.status)
.foregroundColor(viewModel.isConfigured ? .green : .secondary)
}
HStack {
Text("Files Synced")
Spacer()
Text("\(viewModel.syncFileCount)")
.foregroundColor(.secondary)
}
if let error = viewModel.syncLastError {
HStack {
Text("Last Error")
Spacer()
Text(error)
.foregroundColor(.red)
.font(.caption)
.lineLimit(2)
}
}
}
Section {
Button("Configure Credentials") {
showingConfig = true
}
Button("Register FileProvider Domain") {
viewModel.registerDomain()
}
Button("Refresh Sync Status") {
viewModel.refreshSyncStatus()
}
}
Section("Security") {
NavigationLink {
AuthView(viewModel: authViewModel, tcfsViewModel: viewModel)
} label: {
HStack {
Label("Authentication", systemImage: "lock.shield")
Spacer()
Text(authViewModel.authState.rawValue)
.foregroundColor(.secondary)
.font(.caption)
}
}
}
Section("Build") {
HStack {
Text("Version")
Spacer()
Text(Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "?")
.foregroundColor(.secondary)
.font(.system(.caption, design: .monospaced))
}
HStack {
Text("Build")
Spacer()
Text(Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "?")
.foregroundColor(.secondary)
.font(.system(.caption, design: .monospaced))
}
HStack {
Text("Commit")
Spacer()
Text(Bundle.main.infoDictionary?["GITCommitSHA"] as? String ?? "dev")
.foregroundColor(.secondary)
.font(.system(.caption, design: .monospaced))
}
}
}
.navigationTitle("TCFS")
}
} // Group
.sheet(isPresented: $showingConfig, onDismiss: nil) {
NavigationView {
Form {
Section("S3 Storage") {
TextField("Endpoint", text: $endpoint)
.autocapitalization(.none)
.disableAutocorrection(true)
TextField("Bucket", text: $bucket)
.autocapitalization(.none)
TextField("Access Key", text: $accessKey)
.autocapitalization(.none)
SecureField("Secret", text: $s3Secret)
}
Section("Sync") {
TextField("Remote Prefix", text: $remotePrefix)
.autocapitalization(.none)
TextField("Device ID", text: $deviceId)
.autocapitalization(.none)
}
Section("Encryption (optional)") {
SecureField("Passphrase", text: $passphrase)
TextField("Salt", text: $salt)
.autocapitalization(.none)
}
Button("Save") {
viewModel.saveConfig(
endpoint: endpoint,
bucket: bucket,
accessKey: accessKey,
s3Secret: s3Secret,
remotePrefix: remotePrefix,
deviceId: deviceId,
passphrase: passphrase,
salt: salt
)
showingConfig = false
}
}
.navigationTitle("Configure TCFS")
.navigationBarItems(trailing: Button("Cancel") {
showingConfig = false
})
.onAppear {
loadKeychainIntoForm()
}
}
}
}
}
}