Modern Apps git

xtool.git

Public
git clone https://git.maproj.org/xtool.git
appstore-distribution ▾

xtool.git / Sources / XKit / GrandSlam / Operations / GrandSlamAuthenticateOperation.swift

GrandSlamAuthenticateOperation.swift126 lines · 4.0 KiBRaw
1//
2// GrandSlamAuthenticateOperation.swift
3// XKit
4//
5// Created by Kabir Oberai on 20/11/19.
6// Copyright © 2019 Kabir Oberai. All rights reserved.
7//
8
9import Foundation
10import Crypto
11
12struct GrandSlamAuthenticateOperation {
13
14 enum Error: Swift.Error, LocalizedError {
15 case internalError
16 case failedSecondaryAuth
17
18 var errorDescription: String? {
19 switch self {
20 case .internalError:
21 return NSLocalizedString(
22 "apple_id_auth.error.internal",
23 value: "An internal authentication error occurred.",
24 comment: ""
25 )
26 case .failedSecondaryAuth:
27 return NSLocalizedString(
28 "apple_id_auth.error.failed_secondary_auth",
29 value: "Could not complete 2-factor authentication.",
30 comment: ""
31 )
32 }
33 }
34 }
35
36 private struct Response: Decodable {
37 let statusCode: Int
38 let url: String?
39
40 private enum CodingKeys: String, CodingKey {
41 case statusCode = "status-code"
42 case url
43 }
44 }
45
46 private let decoder = PropertyListDecoder()
47
48 let client: GrandSlamClient
49 let username: String
50 let password: String
51 unowned let twoFactorDelegate: TwoFactorAuthDelegate
52
53 private func authenticateTwoFactor(
54 mode: GrandSlamAuthMode?,
55 loginData: GrandSlamLoginData
56 ) async throws -> GrandSlamLoginData {
57 let operation = GrandSlamTwoFactorAuthenticateOperation(
58 client: client,
59 mode: mode,
60 loginData: loginData,
61 delegate: twoFactorDelegate
62 )
63 try await operation.perform()
64 return try await authenticate(isRetry: true)
65 }
66
67 private func authenticate(isRetry: Bool) async throws -> GrandSlamLoginData {
68 var srpClient = SRPClient()
69
70 srpClient.add(string: GrandSlamAuthInitRequest.protocols.joined(separator: ","))
71 srpClient.add(string: "|")
72
73 let publicKey = srpClient.publicKey()
74 let initRequest = GrandSlamAuthInitRequest(username: username, publicKey: publicKey)
75 let initResponse = try await client.send(initRequest)
76
77 srpClient.add(string: "|")
78 srpClient.add(string: initResponse.selectedProtocol.rawValue)
79
80 let m1Data = try srpClient.processChallenge(
81 withUsername: username,
82 password: password,
83 salt: initResponse.salt,
84 iterations: initResponse.iterations,
85 isLegacyProtocol: initResponse.selectedProtocol == .s2k_fo,
86 serverPublicKey: initResponse.serverPublicKey
87 )
88
89 let completeRequest = GrandSlamAuthCompleteRequest(
90 username: username, cookie: initResponse.cookie, m1Data: m1Data
91 )
92 let completeResponse = try await client.send(completeRequest)
93
94 srpClient.add(string: "|")
95 srpClient.add(data: completeResponse.encryptedResponse)
96 srpClient.add(string: "|")
97 completeResponse.sc.map { srpClient.add(data: $0) }
98 srpClient.add(string: "|")
99
100 guard srpClient.verify(hamk: completeResponse.hamk) && srpClient.verify(negProto: completeResponse.negProto)
101 else { throw Error.internalError }
102
103 let rawLoginResponse = try srpClient.decrypt(cbc: completeResponse.encryptedResponse)
104
105 let response = try decoder.decode(Response.self, from: rawLoginResponse)
106
107 let loginData = try decoder.decode(GrandSlamLoginData.self, from: rawLoginResponse)
108
109 if response.statusCode == 409 {
110 // 2FA
111 guard !isRetry else { throw Error.failedSecondaryAuth }
112 let authMode = response.url.flatMap { GrandSlamAuthMode(rawValue: $0) }
113 return try await authenticateTwoFactor(
114 mode: authMode,
115 loginData: loginData
116 )
117 }
118
119 return loginData
120 }
121
122 func authenticate() async throws -> GrandSlamLoginData {
123 try await authenticate(isRetry: false)
124 }
125
126}