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
//
// SignInTestHook.swift
// AT-IRC
//
// Debug-only sign-in automation for the iOS Simulator.
//
// With ATIRC_SIGNIN_HANDLE in the launch environment, LoginView signs in
// automatically and writes the authorization URL to a 0600 file in the app
// container. scripts/sim-signin.sh reads that file and drives the browser
// through the authorize page using ~/.aliceoa_bsky_social_source (via
// socialweb scripts/oauth-signin.ts). The URL is never printed or posted;
// the script removes both files after the attempt.
//
#if DEBUG
import Foundation
enum SignInTestHook {
static var autoHandle: String? {
guard let value = ProcessInfo.processInfo.environment["ATIRC_SIGNIN_HANDLE"]?
.trimmingCharacters(in: .whitespacesAndNewlines), !value.isEmpty else {
return nil
}
return value
}
static var isEnabled: Bool { autoHandle != nil }
private static var documents: URL {
FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
}
static var urlFile: URL { documents.appendingPathComponent(".atirc-signin-url") }
static var resultFile: URL { documents.appendingPathComponent(".atirc-signin-result") }
static func writeAuthorizationURL(_ url: URL) {
write(url.absoluteString, to: urlFile)
}
/// Report only success/failure and the granted scope names (never tokens).
static func finish(_ result: Result<ChatStore, Error>) {
switch result {
case .success(let store):
let handle = store.account?.mint.handle ?? store.account?.oauth.handle ?? "unknown"
let scope = store.account?.oauth.scope ?? ""
write("ok \(handle)\nscope \(scope)\n", to: resultFile)
case .failure(let error):
write("error \(error.localizedDescription)\n", to: resultFile)
}
try? FileManager.default.removeItem(at: urlFile)
}
private static func write(_ text: String, to url: URL) {
try? text.write(to: url, atomically: true, encoding: .utf8)
try? FileManager.default.setAttributes([.posixPermissions: 0o600], ofItemAtPath: url.path)
}
}
#endif