xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XKit / DeveloperServices / DeveloperServicesLoginManager.swift
DeveloperServicesLoginManager.swift63 lines · 1.7 KiBRaw
| 1 | // |
| 2 | // DeveloperServicesLoginManager.swift |
| 3 | // XKit |
| 4 | // |
| 5 | // Created by Kabir Oberai on 10/04/20. |
| 6 | // Copyright © 2020 Kabir Oberai. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | import Foundation |
| 10 | import Dependencies |
| 11 | |
| 12 | public struct DeveloperServicesLoginToken: Codable, Sendable { |
| 13 | public let adsid: String |
| 14 | public let token: String |
| 15 | public let expiry: Date |
| 16 | |
| 17 | public init(adsid: String, token: String, expiry: Date) { |
| 18 | self.adsid = adsid |
| 19 | self.token = token |
| 20 | self.expiry = expiry |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | public struct DeveloperServicesLoginManager: Sendable { |
| 25 | |
| 26 | public enum Error: Swift.Error { |
| 27 | case missingLoginToken |
| 28 | } |
| 29 | |
| 30 | private let client = GrandSlamClient() |
| 31 | |
| 32 | public init() {} |
| 33 | |
| 34 | private func logIn( |
| 35 | withLoginData loginData: GrandSlamLoginData |
| 36 | ) async throws -> DeveloperServicesLoginToken { |
| 37 | let tokens = try await GrandSlamFetchAppTokensOperation( |
| 38 | client: client, |
| 39 | apps: [.xcode], |
| 40 | loginData: loginData |
| 41 | ).perform() |
| 42 | guard let token = tokens[.xcode] |
| 43 | else { throw Error.missingLoginToken } |
| 44 | return .init(adsid: loginData.adsid, token: token.value, expiry: token.expiry) |
| 45 | } |
| 46 | |
| 47 | public func logIn( |
| 48 | withUsername username: String, |
| 49 | password: String, |
| 50 | twoFactorDelegate: TwoFactorAuthDelegate |
| 51 | ) async throws -> DeveloperServicesLoginToken { |
| 52 | let loginData = try await GrandSlamAuthenticateOperation( |
| 53 | client: client, |
| 54 | // GSA requires Apple IDs in lowercase for SRP |
| 55 | username: username.lowercased(), |
| 56 | password: password, |
| 57 | twoFactorDelegate: twoFactorDelegate |
| 58 | ).authenticate() |
| 59 | _ = twoFactorDelegate |
| 60 | return try await self.logIn(withLoginData: loginData) |
| 61 | } |
| 62 | |
| 63 | } |