|
| 1 | +import Foundation |
| 2 | + |
| 3 | +public actor AppleSessionService { |
| 4 | + public typealias EnvironmentValue = @Sendable (String) -> String? |
| 5 | + public typealias DefaultUsername = @Sendable () -> String? |
| 6 | + public typealias SetDefaultUsername = @Sendable (String?) throws -> Void |
| 7 | + public typealias KeychainString = @Sendable (String) throws -> String? |
| 8 | + public typealias KeychainSet = @Sendable (String, String) throws -> Void |
| 9 | + public typealias KeychainRemove = @Sendable (String) throws -> Void |
| 10 | + public typealias ReadLine = @Sendable (String) -> String? |
| 11 | + public typealias ReadSecureLine = @Sendable (String) -> String? |
| 12 | + public typealias ValidateSession = @Sendable () async throws -> Void |
| 13 | + public typealias Login = @Sendable (String, String) async throws -> Void |
| 14 | + public typealias Signout = @Sendable () async -> Void |
| 15 | + public typealias LoadData = @Sendable (URLRequest) async throws -> (Data, URLResponse) |
| 16 | + public typealias Log = @Sendable (String) -> Void |
| 17 | + |
| 18 | + public struct Dependencies: Sendable { |
| 19 | + public var environmentValue: EnvironmentValue |
| 20 | + public var defaultUsername: DefaultUsername |
| 21 | + public var setDefaultUsername: SetDefaultUsername |
| 22 | + public var keychainString: KeychainString |
| 23 | + public var keychainSet: KeychainSet |
| 24 | + public var keychainRemove: KeychainRemove |
| 25 | + public var readLine: ReadLine |
| 26 | + public var readSecureLine: ReadSecureLine |
| 27 | + public var validateSession: ValidateSession |
| 28 | + public var login: Login |
| 29 | + public var signout: Signout |
| 30 | + public var loadData: LoadData |
| 31 | + public var log: Log |
| 32 | + |
| 33 | + public init( |
| 34 | + environmentValue: @escaping EnvironmentValue, |
| 35 | + defaultUsername: @escaping DefaultUsername, |
| 36 | + setDefaultUsername: @escaping SetDefaultUsername, |
| 37 | + keychainString: @escaping KeychainString, |
| 38 | + keychainSet: @escaping KeychainSet, |
| 39 | + keychainRemove: @escaping KeychainRemove, |
| 40 | + readLine: @escaping ReadLine, |
| 41 | + readSecureLine: @escaping ReadSecureLine, |
| 42 | + validateSession: @escaping ValidateSession, |
| 43 | + login: @escaping Login, |
| 44 | + signout: @escaping Signout, |
| 45 | + loadData: @escaping LoadData, |
| 46 | + log: @escaping Log = { _ in } |
| 47 | + ) { |
| 48 | + self.environmentValue = environmentValue |
| 49 | + self.defaultUsername = defaultUsername |
| 50 | + self.setDefaultUsername = setDefaultUsername |
| 51 | + self.keychainString = keychainString |
| 52 | + self.keychainSet = keychainSet |
| 53 | + self.keychainRemove = keychainRemove |
| 54 | + self.readLine = readLine |
| 55 | + self.readSecureLine = readSecureLine |
| 56 | + self.validateSession = validateSession |
| 57 | + self.login = login |
| 58 | + self.signout = signout |
| 59 | + self.loadData = loadData |
| 60 | + self.log = log |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private let xcodesUsername = "XCODES_USERNAME" |
| 65 | + private let xcodesPassword = "XCODES_PASSWORD" |
| 66 | + private let dependencies: Dependencies |
| 67 | + |
| 68 | + public init(dependencies: Dependencies) { |
| 69 | + self.dependencies = dependencies |
| 70 | + } |
| 71 | + |
| 72 | + private func findUsername() -> String? { |
| 73 | + if let username = dependencies.environmentValue(xcodesUsername) { |
| 74 | + return username |
| 75 | + } else if let username = dependencies.defaultUsername() { |
| 76 | + return username |
| 77 | + } |
| 78 | + return nil |
| 79 | + } |
| 80 | + |
| 81 | + private func findPassword(withUsername username: String) -> String? { |
| 82 | + if let password = dependencies.environmentValue(xcodesPassword) { |
| 83 | + return password |
| 84 | + } else if let password = try? dependencies.keychainString(username) { |
| 85 | + return password |
| 86 | + } |
| 87 | + return nil |
| 88 | + } |
| 89 | + |
| 90 | + public func validateADCSession(path: String) async throws { |
| 91 | + try await DeveloperPortalSessionService( |
| 92 | + loadData: dependencies.loadData |
| 93 | + ).validateADCSession(path: path) |
| 94 | + } |
| 95 | + |
| 96 | + public func loginIfNeeded(withUsername providedUsername: String? = nil, shouldPromptForPassword: Bool = false) async throws { |
| 97 | + do { |
| 98 | + try await dependencies.validateSession() |
| 99 | + return |
| 100 | + } catch { |
| 101 | + var possibleUsername = providedUsername ?? findUsername() |
| 102 | + var hasPromptedForUsername = false |
| 103 | + if possibleUsername == nil { |
| 104 | + possibleUsername = dependencies.readLine("Apple ID: ") |
| 105 | + hasPromptedForUsername = true |
| 106 | + } |
| 107 | + guard let username = possibleUsername else { throw Error.missingUsernameOrPassword } |
| 108 | + |
| 109 | + let passwordPrompt: String |
| 110 | + if hasPromptedForUsername { |
| 111 | + passwordPrompt = "Apple ID Password: " |
| 112 | + } else { |
| 113 | + passwordPrompt = "Apple ID Password (\(username)): " |
| 114 | + } |
| 115 | + var possiblePassword = findPassword(withUsername: username) |
| 116 | + if possiblePassword == nil || shouldPromptForPassword { |
| 117 | + possiblePassword = dependencies.readSecureLine(passwordPrompt) |
| 118 | + } |
| 119 | + guard let password = possiblePassword else { throw Error.missingUsernameOrPassword } |
| 120 | + |
| 121 | + do { |
| 122 | + try await login(username, password: password) |
| 123 | + } catch { |
| 124 | + dependencies.log(error.localizedDescription) |
| 125 | + |
| 126 | + guard case AuthenticationError.invalidUsernameOrPassword = error else { throw error } |
| 127 | + |
| 128 | + dependencies.log("Try entering your password again") |
| 129 | + try await loginIfNeeded(withUsername: username, shouldPromptForPassword: true) |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + public func login(_ username: String, password: String) async throws { |
| 135 | + do { |
| 136 | + try await dependencies.login(username, password) |
| 137 | + } catch { |
| 138 | + if case AuthenticationError.invalidUsernameOrPassword = error { |
| 139 | + try? dependencies.keychainRemove(username) |
| 140 | + } |
| 141 | + |
| 142 | + throw error |
| 143 | + } |
| 144 | + |
| 145 | + try? dependencies.keychainSet(password, username) |
| 146 | + |
| 147 | + if dependencies.defaultUsername() != username { |
| 148 | + try? dependencies.setDefaultUsername(username) |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + public func logout() async throws { |
| 153 | + guard let username = findUsername() else { throw Error.notAuthenticated } |
| 154 | + |
| 155 | + await dependencies.signout() |
| 156 | + try dependencies.keychainRemove(username) |
| 157 | + try dependencies.setDefaultUsername(nil) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +public extension AppleSessionService { |
| 162 | + enum Error: LocalizedError, Equatable { |
| 163 | + case missingUsernameOrPassword |
| 164 | + case notAuthenticated |
| 165 | + |
| 166 | + public var errorDescription: String? { |
| 167 | + switch self { |
| 168 | + case .missingUsernameOrPassword: |
| 169 | + return "Missing username or a password. Please try again." |
| 170 | + case .notAuthenticated: |
| 171 | + return "You are already signed out" |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments