~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/atirc-ios-client

0
1
main
58 lines · 2.1 KB Raw History
//
//  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