xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XToolSupport / AuthToken.swift
AuthToken.swift103 lines · 2.8 KiBRaw
| 1 | import XCLIUtils |
| 2 | import XUtils |
| 3 | import Foundation |
| 4 | import XKit |
| 5 | import Dependencies |
| 6 | |
| 7 | enum AuthToken: Codable, CustomStringConvertible { |
| 8 | struct Xcode: Codable { |
| 9 | var appleID: String |
| 10 | var adsid: String |
| 11 | var token: String |
| 12 | var expiry: Date |
| 13 | var teamID: String |
| 14 | var anisetteProviderID: String? |
| 15 | } |
| 16 | |
| 17 | struct AppStoreConnect: Codable { |
| 18 | var id: String |
| 19 | var issuerID: String |
| 20 | var pem: String |
| 21 | } |
| 22 | |
| 23 | case appStoreConnect(AppStoreConnect) |
| 24 | case xcode(Xcode) |
| 25 | |
| 26 | var description: String { |
| 27 | switch self { |
| 28 | case .appStoreConnect(let data): |
| 29 | """ |
| 30 | - ASC key ID: \(data.id) |
| 31 | - Issuer ID: \(data.issuerID) |
| 32 | """ |
| 33 | case .xcode(let data): |
| 34 | """ |
| 35 | - Apple ID: \(data.appleID) |
| 36 | - Team ID: \(data.teamID) |
| 37 | - Token expiry: \(data.expiry.formatted(.dateTime)) |
| 38 | """ |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | extension AuthToken { |
| 44 | |
| 45 | private static var storage: KeyValueStorage { |
| 46 | @Dependency(\.keyValueStorage) var storage |
| 47 | return storage |
| 48 | } |
| 49 | |
| 50 | private static let encoder = JSONEncoder() |
| 51 | private static let decoder = JSONDecoder() |
| 52 | |
| 53 | static func saved() throws -> Self { |
| 54 | guard let token = try savedIfPresent() else { |
| 55 | throw Console.Error("Please log in with `xtool auth` before running this command.") |
| 56 | } |
| 57 | return token |
| 58 | } |
| 59 | |
| 60 | static func savedIfPresent() throws -> Self? { |
| 61 | guard let data = try storage.data(forKey: "XTLAuthToken") else { |
| 62 | return nil |
| 63 | } |
| 64 | let decoded = try decoder.decode(AuthToken.self, from: data) |
| 65 | switch decoded { |
| 66 | case .xcode(let xcode): |
| 67 | @Dependency(\.anisetteDataProvider) var anisetteProvider |
| 68 | guard xcode.anisetteProviderID == anisetteProvider.providerID else { |
| 69 | try? clear() |
| 70 | return nil |
| 71 | } |
| 72 | case .appStoreConnect: |
| 73 | break |
| 74 | } |
| 75 | return decoded |
| 76 | } |
| 77 | |
| 78 | static func clear() throws { |
| 79 | try Self.storage.setData(nil, forKey: "XTLAuthToken") |
| 80 | } |
| 81 | |
| 82 | func save() throws { |
| 83 | let data = try Self.encoder.encode(self) |
| 84 | try Self.storage.setData(data, forKey: "XTLAuthToken") |
| 85 | } |
| 86 | |
| 87 | func authData() -> DeveloperAPIAuthData { |
| 88 | switch self { |
| 89 | case .appStoreConnect(let data): |
| 90 | return .appStoreConnect(.init(id: data.id, issuerID: data.issuerID, pem: data.pem)) |
| 91 | case .xcode(let data): |
| 92 | return .xcode(.init( |
| 93 | loginToken: DeveloperServicesLoginToken( |
| 94 | adsid: data.adsid, |
| 95 | token: data.token, |
| 96 | expiry: data.expiry |
| 97 | ), |
| 98 | teamID: .init(rawValue: data.teamID) |
| 99 | )) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | } |