Modern Apps git

← xtool.git

Reorg commands

Commit
20631abe5150108a5ed7b4ccdd9ca23c1d2645be
Author
Kabir Oberai <[email protected]>
When
2024-12-12 09:10 UTC (1 year ago)
Parents
34a79f7

Diff

Sources/SupersignCLISupport/AuthCommand.swift
diff --git a/Sources/SupersignCLISupport/AuthCommand.swift b/Sources/SupersignCLISupport/AuthCommand.swiftnew file mode 100644index 0000000..cb79d3d--- /dev/null+++ b/Sources/SupersignCLISupport/AuthCommand.swift@@ -0,0 +1,141 @@+import Foundation+import Supersign+import ArgumentParser++struct AuthOperation {+    var username: String?+    var password: String?+    var logoutFromExisting: Bool++    func run() async throws {+        if let token = try? AuthToken.saved() {+            if logoutFromExisting {+                try AuthToken.clear()+            } else {+                print("Logged in as \(token.appleID)")+                return+            }+        }++        guard let username = username ?? Console.prompt("Apple ID: "), !username.isEmpty else {+            throw Console.Error("A non-empty Apple ID is required.")+        }+        guard let password = password ?? Console.getPassword("Password: "), !password.isEmpty else {+            throw Console.Error("A non-empty password is required.")+        }++        let deviceInfo = try DeviceInfo.fetch()++        let provider = try ADIDataProvider.adiProvider(+            deviceInfo: deviceInfo,+            storage: SupersignCLI.config.storage+        )+        let authDelegate = SupersignCLIAuthDelegate()+        let manager = try DeveloperServicesLoginManager(+            deviceInfo: deviceInfo,+            anisetteProvider: provider+        )+        let token = try await manager.logIn(+            withUsername: username,+            password: password,+            twoFactorDelegate: authDelegate+        )+        let fullToken = AuthToken(appleID: username, dsToken: token)+        try fullToken.save()+        print("Logged in")+    }+}++struct AuthLoginCommand: AsyncParsableCommand {+    static let configuration = CommandConfiguration(+        commandName: "login",+        abstract: "Log in to Apple Developer Services"+    )++    @Option(name: [.short, .long], help: "Apple ID") var username: String?+    @Option(name: [.short, .long]) var password: String?++    func run() async throws {+        try await AuthOperation(+            username: username,+            password: password,+            logoutFromExisting: true+        ).run()+    }+}++struct AuthLogoutCommand: AsyncParsableCommand {+    static let configuration = CommandConfiguration(+        commandName: "logout",+        abstract: "Log out of Apple Developer Services"+    )++    @Flag(+        name: [.short, .customLong("reset-2fa")],+        help: ArgumentHelp(+            "Reset 2-factor authentication data",+            discussion: """+            This resets the "pseudo-device" that Supersign presents itself as \+            when authenticating with Apple.++            Effectively, this means you will be prompted to complete 2-factor \+            authentication again the next time you log in.+            """+        )+    ) var reset2FA = false++    func run() async throws {+        if (try? AuthToken.saved()) != nil {+            try AuthToken.clear()+            print("Logged out")+        } else {+            print("Already logged out")+        }++        if reset2FA {+            try ADIDataProvider.adiProvider(+                deviceInfo: .fetch(),+                storage: SupersignCLI.config.storage+            )+            .resetProvisioning()+            print("Forgot device")+        }+    }+}++struct AuthStatusCommand: AsyncParsableCommand {+    static let configuration = CommandConfiguration(+        commandName: "status",+        abstract: "Get Apple Developer Services auth status"+    )++    func run() async throws {+        if let token = try? AuthToken.saved() {+            print("Logged in: \(token.appleID)")+        } else {+            print("Logged out")+        }+    }+}++struct AuthCommand: AsyncParsableCommand {+    static let configuration = CommandConfiguration(+        commandName: "auth",+        abstract: "Authenticate with Apple Developer Services",+        subcommands: [+            AuthLoginCommand.self,+            AuthLogoutCommand.self,+            AuthStatusCommand.self,+        ],+        defaultSubcommand: AuthLoginCommand.self+    )+}++extension DeviceInfo {+    static func fetch() throws -> Self {+        guard let deviceInfo = DeviceInfo.current() else {+            throw Console.Error("Could not fetch client info.")+        }+        return deviceInfo+    }+}
Sources/SupersignCLISupport/AuthToken.swift
diff --git a/Sources/SupersignCLISupport/AuthToken.swift b/Sources/SupersignCLISupport/AuthToken.swiftindex ebd2af8..e481687 100644--- a/Sources/SupersignCLISupport/AuthToken.swift+++ b/Sources/SupersignCLISupport/AuthToken.swift@@ -65,6 +65,10 @@ extension AuthToken {         return token     } +    static func clear() throws {+        try SupersignCLI.config.storage.setString(nil, forKey: "SUPAuthToken")+    }+     func save() throws {         try SupersignCLI.config.storage.setString(string, forKey: "SUPAuthToken")     }
Sources/SupersignCLISupport/DSCommand.swift
diff --git a/Sources/SupersignCLISupport/DSCommand.swift b/Sources/SupersignCLISupport/DSCommand.swiftindex df3c281..7d6428b 100644--- a/Sources/SupersignCLISupport/DSCommand.swift+++ b/Sources/SupersignCLISupport/DSCommand.swift@@ -2,70 +2,6 @@ import Foundation import Supersign import ArgumentParser -extension DeviceInfo {-    static func fetch() throws -> Self {-        guard let deviceInfo = DeviceInfo.current() else {-            throw Console.Error("Could not fetch client info.")-        }-        return deviceInfo-    }-}--struct DSLoginCommand: AsyncParsableCommand {-    static let configuration = CommandConfiguration(-        commandName: "login",-        abstract: "Obtain an Apple ID authentication token"-    )--    @Flag(name: [.short, .long]) var resetProvisioning = false-    @Option(name: [.short, .long], help: "Apple ID") var username: String?-    @Option(name: [.short, .long]) var password: String?-    @Flag(-        name: [.long],-        help: "Print the auth token to standard output instead of persisting it."-    ) var printToken = false--    func run() async throws {-        guard let username = username ?? Console.prompt("Apple ID: "), !username.isEmpty else {-            throw Console.Error("A non-empty Apple ID is required.")-        }-        guard let password = password ?? Console.getPassword("Password: "), !password.isEmpty else {-            throw Console.Error("A non-empty password is required.")-        }--        let deviceInfo = try DeviceInfo.fetch()--        let provider = try ADIDataProvider.adiProvider(-            deviceInfo: deviceInfo,-            storage: SupersignCLI.config.storage-        )-        if resetProvisioning {-            provider.resetProvisioning()-        }-        let authDelegate = SupersignCLIAuthDelegate()-        let manager = try DeveloperServicesLoginManager(-            deviceInfo: deviceInfo,-            anisetteProvider: provider-        )-        let token = try await manager.logIn(-            withUsername: username,-            password: password,-            twoFactorDelegate: authDelegate-        )-        let fullToken = AuthToken(appleID: username, dsToken: token)--        if printToken {-            guard let string = fullToken.string else {-                throw Console.Error("Could not encode token.")-            }-            print(string)-        } else {-            try fullToken.save()-            print("Logged in!")-        }-    }-}- struct DSTeamsListCommand: AsyncParsableCommand {     static let configuration = CommandConfiguration(         commandName: "list",@@ -158,9 +94,8 @@ struct DSCommand: AsyncParsableCommand {         commandName: "ds",         abstract: "Interact with Apple Developer Services",         subcommands: [-            DSLoginCommand.self,             DSTeamsCommand.self,-            DSAnisetteCommand.self+            DSAnisetteCommand.self,         ]     ) }
Sources/SupersignCLISupport/DevCommand.swift
diff --git a/Sources/SupersignCLISupport/DevCommand.swift b/Sources/SupersignCLISupport/DevCommand.swiftindex 9f9730b..bd307d5 100644--- a/Sources/SupersignCLISupport/DevCommand.swift+++ b/Sources/SupersignCLISupport/DevCommand.swift@@ -3,9 +3,43 @@ import ArgumentParser import PackLib import Supersign -struct DevCommand: AsyncParsableCommand {+struct AddSDKOperation {+    func run() async throws {+        print("TODO: install Swift SDK for iOS")+    }+}++struct DevSetupCommand: AsyncParsableCommand {     static let configuration = CommandConfiguration(-        commandName: "dev",+        commandName: "setup",+        abstract: "Set up Supersign for iOS development",+        discussion: """+        Authenticates with Apple if needed, then adds the iOS SDK to SwiftPM.++        Equivalent to running `supersign auth && supersign dev sdk`+        """+    )++    func run() async throws {+        try await AuthOperation(logoutFromExisting: false).run()+        try await AddSDKOperation().run()+    }+}++struct DevSDKCommand: AsyncParsableCommand {+    static let configuration = CommandConfiguration(+        commandName: "sdk",+        abstract: "Manages the Swift SDK for iOS"+    )++    func run() async throws {+        try await AddSDKOperation().run()+    }+}++struct DevDeployCommand: AsyncParsableCommand {+    static let configuration = CommandConfiguration(+        commandName: "deploy",         abstract: "Build and run apps with SwiftPM",         discussion: """         This command deploys the SwiftPM-based iOS app in the current directory \@@ -77,4 +111,17 @@ struct DevCommand: AsyncParsableCommand {     } } +struct DevCommand: AsyncParsableCommand {+    static let configuration = CommandConfiguration(+        commandName: "dev",+        abstract: "Develop iOS apps with SwiftPM",+        subcommands: [+            DevSetupCommand.self,+            DevSDKCommand.self,+            DevDeployCommand.self,+        ],+        defaultSubcommand: DevDeployCommand.self+    )+}+ extension BuildConfiguration: @retroactive ExpressibleByArgument {}
Sources/SupersignCLISupport/SupersignCommand.swift
diff --git a/Sources/SupersignCLISupport/SupersignCommand.swift b/Sources/SupersignCLISupport/SupersignCommand.swiftindex 9ff0a5a..eb4d7d0 100644--- a/Sources/SupersignCLISupport/SupersignCommand.swift+++ b/Sources/SupersignCLISupport/SupersignCommand.swift@@ -39,6 +39,7 @@ struct SupersignCommand: AsyncParsableCommand {         commandName: "supersign",         abstract: "The Supersign command line tool",         subcommands: [+            AuthCommand.self,             DSCommand.self,             DevicesCommand.self,             InstallCommand.self,