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