Cross-platform Xcode replacement. Build and deploy iOS apps with SwiftPM on Linux, Windows, macOS.
General bug fixes and improvements, and begin implementing Apple ID GrandSlam auth
kabiroberai6f54aedf3f90
diff --git a/Supersign/GrandSlam/Fingerprint/AnisetteData.swift b/Supersign/GrandSlam/Fingerprint/AnisetteData.swiftnew file mode 100644index 0000000..0cb028e--- /dev/null+++ b/Supersign/GrandSlam/Fingerprint/AnisetteData.swift@@ -0,0 +1,62 @@+//+// AnisetteData.swift+// Supersign+//+// Created by Kabir Oberai on 19/11/19.+// Copyright © 2019 Kabir Oberai. All rights reserved.+//++import Foundation++public struct AnisetteData {++ private static let dateFormatter: DateFormatter = {+ let dateFormatter = DateFormatter()+ dateFormatter.locale = Locale(identifier: "en_US_POSIX")+ dateFormatter.calendar = Calendar(identifier: .gregorian)+ dateFormatter.timeZone = TimeZone(abbreviation: "UTC")+ dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"+ return dateFormatter+ }()++ /// `X-Apple-Locale`+ static let localeKey = "X-Apple-Locale"+ public let locale: Locale = .current++ /// `X-Apple-I-TimeZone`+ static let timeZoneKey = "X-Apple-I-TimeZone"+ public let timeZone: TimeZone = .current++ /// `X-Apple-I-Client-Time`+ static let clientTimeKey = "X-Apple-I-Client-Time"+ public let clientTime: Date++ /// `X-Apple-I-MD-RINFO`+ static let routingInfoKey = "X-Apple-I-MD-RINFO"+ public let routingInfo: UInt64++ /// `X-Apple-I-MD-M`+ static let machineIDKey = "X-Apple-I-MD-M"+ public let machineID: String++ /// `X-Apple-I-MD-LU`+ static let localUserIDKey = "X-Apple-I-MD-LU"+ public let localUserID: String++ /// `X-Apple-I-MD`+ static let oneTimePasswordKey = "X-Apple-I-MD"+ public let oneTimePassword: String++ var dictionary: [String: String] {+ [+ Self.localeKey: locale.identifier,+ Self.timeZoneKey: timeZone.abbreviation() ?? "UTC",+ Self.clientTimeKey: Self.dateFormatter.string(from: clientTime),+ Self.routingInfoKey: "\(routingInfo)",+ Self.machineIDKey: machineID,+ Self.localUserIDKey: localUserID,+ Self.oneTimePasswordKey: oneTimePassword+ ]+ }++}diff --git a/Supersign/GrandSlam/GrandSlamClient.swift b/Supersign/GrandSlam/GrandSlamClient.swiftnew file mode 100644index 0000000..f171d00--- /dev/null+++ b/Supersign/GrandSlam/GrandSlamClient.swift@@ -0,0 +1,89 @@+//+// GrandSlamClient.swift+// Supersign+//+// Created by Kabir Oberai on 19/11/19.+// Copyright © 2019 Kabir Oberai. All rights reserved.+//++import Foundation++class GrandSlamClient {++ private let encoder = PropertyListEncoder()++ private let lookupManager: GrandSlamLookupManager++ let deviceInfo: DeviceInfo+ let anisetteDataProvider: AnisetteDataProvider+ init(+ deviceInfo: DeviceInfo,+ customAnisetteDataProvider: AnisetteDataProvider? = nil+ ) {+ self.deviceInfo = deviceInfo+ self.anisetteDataProvider = customAnisetteDataProvider+ ?? ComputedAnisetteDataProvider(deviceInfo: deviceInfo)+ self.lookupManager = .init(deviceInfo: deviceInfo)+ }++ private func send<R: GrandSlamRequest>(+ _ request: R,+ anisetteData: AnisetteData,+ url: URL,+ completion: @escaping (Result<R.Decoder.Value, Swift.Error>) -> Void+ ) {+ print("Sending: \(request) to \(url)")++ var urlRequest = URLRequest(url: url)+ urlRequest.setValue("text/x-xml-plist", forHTTPHeaderField: "Content-Type")+ urlRequest.setValue(deviceInfo.clientInfo.clientString, forHTTPHeaderField: DeviceInfo.clientInfoKey)++ let method = request.method(deviceInfo: deviceInfo, anisetteData: anisetteData)+ urlRequest.httpMethod = method.name+ switch method {+ case .get:+ break+ case .post(let body):+ do {+ urlRequest.httpBody = try PropertyListSerialization.data(+ fromPropertyList: body, format: .xml, options: 0+ )+ } catch {+ return completion(.failure(error))+ }+ }+ request.configure(request: &urlRequest, deviceInfo: deviceInfo, anisetteData: anisetteData)++ URLSession.shared.dataTask(with: urlRequest) { data, response, error in+ print(response)+ if let error = error {+ return completion(.failure(error))+ }+ completion(Result {+ try R.Decoder.decode(data: data ?? .init())+ })+ }.resume()+ }++ private func send<R: GrandSlamRequest>(+ _ request: R,+ anisetteData: AnisetteData,+ completion: @escaping (Result<R.Decoder.Value, Swift.Error>) -> Void+ ) {+ lookupManager.fetchURL(forEndpoint: R.endpoint) { result in+ guard let url = result.get(withErrorHandler: completion) else { return }+ self.send(request, anisetteData: anisetteData, url: url, completion: completion)+ }+ }++ func send<R: GrandSlamRequest>(+ _ request: R,+ completion: @escaping (Result<R.Decoder.Value, Swift.Error>) -> Void+ ) {+ self.anisetteDataProvider.fetchAnisetteData { result in+ guard let anisetteData = result.get(withErrorHandler: completion) else { return }+ self.send(request, anisetteData: anisetteData, completion: completion)+ }+ }++}diff --git a/Supersign/GrandSlam/Lookup/GrandSlamEndpoints.swift b/Supersign/GrandSlam/Lookup/GrandSlamEndpoints.swiftnew file mode 100644index 0000000..79f5e4b--- /dev/null+++ b/Supersign/GrandSlam/Lookup/GrandSlamEndpoints.swift@@ -0,0 +1,18 @@+//+// GrandSlamEndpoints.swift+// Supersign+//+// Created by Kabir Oberai on 19/11/19.+// Copyright © 2019 Kabir Oberai. All rights reserved.+//++import Foundation++struct GrandSlamEndpoints: Decodable {+ let gsService: String+ let secondaryAuth: String+ let trustedDeviceSecondaryAuth: String+ let validateCode: String+}++typealias GrandSlamEndpoint = KeyPath<GrandSlamEndpoints, String>diff --git a/Supersign/GrandSlam/Lookup/GrandSlamLookupManager.swift b/Supersign/GrandSlam/Lookup/GrandSlamLookupManager.swiftnew file mode 100644index 0000000..2350e7a--- /dev/null+++ b/Supersign/GrandSlam/Lookup/GrandSlamLookupManager.swift@@ -0,0 +1,76 @@+//+// GrandSlamLookupManager.swift+// Supersign+//+// Created by Kabir Oberai on 19/11/19.+// Copyright © 2019 Kabir Oberai. All rights reserved.+//++import Foundation++class GrandSlamLookupManager {++ private static let lookupURL = URL(string: "https://gsa.apple.com/grandslam/GsService2/lookup")!++ private struct Response: Decodable {+ let urls: GrandSlamEndpoints+ }++ private let decoder = PropertyListDecoder()+ private var endpoints: GrandSlamEndpoints?++ let deviceInfo: DeviceInfo+ init(deviceInfo: DeviceInfo) {+ self.deviceInfo = deviceInfo+ }++ private func performLookup(completion: @escaping (Result<GrandSlamEndpoints, Error>) -> Void) {+ /* {+ "X-Apple-I-Locale" = "en_IN";+ "X-Apple-I-TimeZone" = "Asia/Kolkata";+ "X-Apple-I-TimeZone-Offset" = 19800;+ "X-MMe-Client-Info" = "<MacBookPro11,5> <Mac OS X;10.14.6;18G103> <com.apple.AuthKit/1 (com.apple.akd/1.0)>";+ "X-MMe-Country" = IN;+ "X-Mme-Device-Id" = "[REDACTED]";+ } */+ var request = URLRequest(url: Self.lookupURL)+ request.setValue(deviceInfo.clientInfo.clientString, forHTTPHeaderField: DeviceInfo.clientInfoKey)+ request.setValue(deviceInfo.deviceID, forHTTPHeaderField: DeviceInfo.deviceIDKey)+ request.setValue(Locale.current.regionCode, forHTTPHeaderField: "X-MMe-Country")+ request.setValue(Locale.current.identifier, forHTTPHeaderField: AnisetteData.localeKey)+ request.setValue(TimeZone.current.identifier, forHTTPHeaderField: AnisetteData.timeZoneKey)+ request.setValue("\(TimeZone.current.secondsFromGMT())", forHTTPHeaderField: "X-Apple-I-TimeZone-Offset")++ URLSession.shared.dataTask(with: request) { data, _, error in+ if let error = error {+ return completion(.failure(error))+ }++ completion(Result {+ try self.decoder.decode(Response.self, from: data ?? .init()).urls+ })+ }.resume()+ }++ private func fetchEndpoints(completion: @escaping (Result<GrandSlamEndpoints, Error>) -> Void) {+ if let endpoints = endpoints {+ return completion(.success(endpoints))+ }+ performLookup { result in+ if case let .success(endpoints) = result {+ self.endpoints = endpoints+ }+ completion(result)+ }+ }++ func fetchURL(+ forEndpoint endpoint: GrandSlamEndpoint,+ completion: @escaping (Result<URL, Error>) -> Void+ ) {+ fetchEndpoints { result in+ completion(result.map { URL(string: $0[keyPath: endpoint])! })+ }+ }++}diff --git a/Supersign/GrandSlam/Model/GrandSlamAuthEndpoint.swift b/Supersign/GrandSlam/Model/GrandSlamAuthEndpoint.swiftnew file mode 100644index 0000000..d892197--- /dev/null+++ b/Supersign/GrandSlam/Model/GrandSlamAuthEndpoint.swift@@ -0,0 +1,14 @@+//+// GrandSlamAuthEndpoint.swift+// Supersign+//+// Created by Kabir Oberai on 20/11/19.+// Copyright © 2019 Kabir Oberai. All rights reserved.+//++import Foundation++enum GrandSlamAuthMode: String, Decodable {+ case secondaryAuth+ case trustedDeviceSecondaryAuth+}diff --git a/Supersign/GrandSlam/Model/GrandSlamAuthProtocol.swift b/Supersign/GrandSlam/Model/GrandSlamAuthProtocol.swiftnew file mode 100644index 0000000..85c0d8a--- /dev/null+++ b/Supersign/GrandSlam/Model/GrandSlamAuthProtocol.swift@@ -0,0 +1,14 @@+//+// GrandSlamAuthProtocol.swift+// Supersign+//+// Created by Kabir Oberai on 19/11/19.+// Copyright © 2019 Kabir Oberai. All rights reserved.+//++import Foundation++enum GrandSlamAuthProtocol: String, Codable, CaseIterable {+ case s2k+ case s2k_fo+}diff --git a/Supersign/GrandSlam/Model/GrandSlamLoginData.swift b/Supersign/GrandSlam/Model/GrandSlamLoginData.swiftnew file mode 100644index 0000000..9a4454c--- /dev/null+++ b/Supersign/GrandSlam/Model/GrandSlamLoginData.swift@@ -0,0 +1,27 @@+//+// GrandSlamLoginData.swift+// Supersign+//+// Created by Kabir Oberai on 20/11/19.+// Copyright © 2019 Kabir Oberai. All rights reserved.+//++import Foundation++struct GrandSlamLoginData: Decodable {+ let adsid: String+ let idmsToken: String+ let sk: Data+ let cookie: Data++ var identityToken: String {+ "\(adsid):\(idmsToken)".data(using: .utf8)!.base64EncodedString()+ }++ private enum CodingKeys: String, CodingKey {+ case adsid+ case idmsToken = "GsIdmsToken"+ case sk+ case cookie = "c"+ }+}diff --git a/Supersign/GrandSlam/Operations/GrandSlamAuthenticateOperation.swift b/Supersign/GrandSlam/Operations/GrandSlamAuthenticateOperation.swiftnew file mode 100644index 0000000..bfd9624--- /dev/null+++ b/Supersign/GrandSlam/Operations/GrandSlamAuthenticateOperation.swift@@ -0,0 +1,164 @@+//+// GrandSlamAuthenticateOperation.swift+// Supersign+//+// Created by Kabir Oberai on 20/11/19.+// Copyright © 2019 Kabir Oberai. All rights reserved.+//++import Foundation++class GrandSlamAuthenticateOperation {++ enum Error: Swift.Error {+ case failedChallenge+ case invalidSession+ case invalidResponse+ case failedSecondaryAuth+ }++ private struct Response: Decodable {+ let statusCode: Int+ let url: String?++ private enum CodingKeys: String, CodingKey {+ case statusCode = "status-code"+ case url+ }+ }++ private var helper = SRPHelper()+ private var isSecondAttempt = false++ private let decoder = PropertyListDecoder()++ let client: GrandSlamClient+ let username: String+ let password: String+ unowned let twoFactorDelegate: TwoFactorAuthDelegate++ init(+ client: GrandSlamClient,+ username: String,+ password: String,+ twoFactorDelegate: TwoFactorAuthDelegate+ ) {+ self.client = client+ self.username = username+ self.password = password+ self.twoFactorDelegate = twoFactorDelegate+ }++ private func restartAuth(completion: @escaping (Result<GrandSlamLoginData, Swift.Error>) -> Void) {+ helper = .init()+ isSecondAttempt = true+ authenticate(completion: completion)+ }++ private func authenticateTwoFactor(+ mode: GrandSlamAuthMode,+ loginData: GrandSlamLoginData,+ completion: @escaping (Result<GrandSlamLoginData, Swift.Error>) -> Void+ ) {+ guard !isSecondAttempt else { return completion(.failure(Error.failedSecondaryAuth)) }+ let operation = GrandSlamTwoFactorAuthenticateOperation(+ client: client,+ mode: mode,+ loginData: loginData,+ delegate: twoFactorDelegate+ )+ operation.perform { result in+ guard result.get(withErrorHandler: completion) != nil else { return }+ self.restartAuth(completion: completion)+ }+ }++ private func authenticateStage3(+ completeResponse: GrandSlamAuthCompleteRequest.Decoder.Value,+ completion: @escaping (Result<GrandSlamLoginData, Swift.Error>) -> Void+ ) {+ helper.addString(toNegProt: "|")+ helper.addData(toNegProt: completeResponse.encryptedResponse)+ helper.addString(toNegProt: "|")+ completeResponse.sc.map(helper.addData)+ helper.addString(toNegProt: "|")++ guard helper.verifySession(withM2: completeResponse.m2Data) && helper.verifyNegProto(completeResponse.negProto) else {+ return completion(.failure(Error.invalidSession))+ }++ guard let rawLoginResponse = helper.decryptCBC(completeResponse.encryptedResponse) else {+ return completion(.failure(Error.invalidResponse))+ }++ let response: Response+ do {+ response = try decoder.decode(Response.self, from: rawLoginResponse)+ } catch {+ return completion(.failure(error))+ }++ let loginData: GrandSlamLoginData+ do {+ loginData = try decoder.decode(GrandSlamLoginData.self, from: rawLoginResponse)+ } catch {+ return completion(.failure(error))+ }++ if response.statusCode == 409,+ let secondaryURL = response.url,+ let authMode = GrandSlamAuthMode(rawValue: secondaryURL) {+ // 2FA+ return authenticateTwoFactor(+ mode: authMode,+ loginData: loginData,+ completion: completion+ )+ }++ completion(.success(loginData))+ }++ private func authenticateStage2(+ initResponse: GrandSlamAuthInitRequest.Decoder.Value,+ completion: @escaping (Result<GrandSlamLoginData, Swift.Error>) -> Void+ ) {+ let isS2K = initResponse.proto == .s2k+ helper.addString(toNegProt: "|")+ initResponse.proto.map { helper.addString(toNegProt: $0.rawValue) }++ let mDataRaw = helper.processChallenge(+ withUsername: username,+ password: password,+ salt: initResponse.salt,+ iterations: initResponse.iterations,+ bData: initResponse.bData,+ isS2K: isS2K+ )+ guard let m1Data = mDataRaw else { return completion(.failure(Error.failedChallenge)) }++ let completeRequest = GrandSlamAuthCompleteRequest(+ username: username, cookie: initResponse.cookie, m1Data: m1Data+ )+ client.send(completeRequest) { result in+ guard let response = result.get(withErrorHandler: completion) else { return }+ self.authenticateStage3(completeResponse: response, completion: completion)+ }+ }++ func authenticate(+ completion: @escaping (Result<GrandSlamLoginData, Swift.Error>) -> Void+ ) {+ helper.addString(toNegProt: GrandSlamAuthInitRequest.protocols.joined(separator: ","))+ helper.addString(toNegProt: "|")++ let aData = helper.startAuthenticationAndGetA()++ let initRequest = GrandSlamAuthInitRequest(username: username, aData: aData)+ client.send(initRequest) { result in+ guard let response = result.get(withErrorHandler: completion) else { return }+ self.authenticateStage2(initResponse: response, completion: completion)+ }+ }++}diff --git a/Supersign/GrandSlam/Operations/GrandSlamFetchAppTokensOperation.swift b/Supersign/GrandSlam/Operations/GrandSlamFetchAppTokensOperation.swiftnew file mode 100644index 0000000..f684f95--- /dev/null+++ b/Supersign/GrandSlam/Operations/GrandSlamFetchAppTokensOperation.swift@@ -0,0 +1,79 @@+//+// GrandSlamFetchAppTokensOperation.swift+// Supersign+//+// Created by Kabir Oberai on 20/11/19.+// Copyright © 2019 Kabir Oberai. All rights reserved.+//++import Foundation++class GrandSlamFetchAppTokensOperation {++ enum Error: Swift.Error {+ case invalidResponse+ }++ struct Token: Decodable {+ let value: String++ private let _expiry: Int+ var expiry: Date {+ Date(timeIntervalSince1970: .init(_expiry) / 1000)+ }++ private enum CodingKeys: String, CodingKey {+ case value = "token"+ case _expiry = "expiry"+ }+ }++ private struct Response: Decodable {+ let tokens: [String: Token]++ private enum CodingKeys: String, CodingKey {+ case tokens = "t"+ }+ }++ private let decoder = PropertyListDecoder()++ let client: GrandSlamClient+ let apps: [String]+ let loginData: GrandSlamLoginData++ init(client: GrandSlamClient, apps: [String], loginData: GrandSlamLoginData) {+ self.client = client+ self.apps = apps+ self.loginData = loginData+ }++ private func handle(+ response: GrandSlamAppTokensRequest.Decoder.Value,+ completion: @escaping (Result<[String: Token], Swift.Error>) -> Void+ ) {+ guard let decrypted = AppTokensHelper.decryptGCM(response.encryptedToken, sk: self.loginData.sk)+ else { return completion(.failure(Error.invalidResponse)) }+ completion(Result {+ try decoder.decode(Response.self, from: decrypted).tokens+ })+ }++ func perform(completion: @escaping (Result<[String: Token], Swift.Error>) -> Void) {+ let checksum = AppTokensHelper.createAppTokensChecksum(+ withSK: loginData.sk, adsid: loginData.adsid, apps: apps+ )+ let appTokensRequest = GrandSlamAppTokensRequest(+ username: loginData.adsid,+ apps: apps,+ cookie: loginData.cookie,+ idmsToken: loginData.idmsToken,+ checksum: checksum+ )+ client.send(appTokensRequest) { result in+ guard let response = result.get(withErrorHandler: completion) else { return }+ self.handle(response: response, completion: completion)+ }+ }++}diff --git a/Supersign/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swift b/Supersign/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swiftnew file mode 100644index 0000000..ea6401b--- /dev/null+++ b/Supersign/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swift@@ -0,0 +1,76 @@+//+// GrandSlamTwoFactorAuthenticateOperation.swift+// Supersign+//+// Created by Kabir Oberai on 20/11/19.+// Copyright © 2019 Kabir Oberai. All rights reserved.+//++import Foundation++public protocol TwoFactorAuthDelegate: class {+ func fetchCode(completion: @escaping (String) -> Void)+}++class GrandSlamTwoFactorAuthenticateOperation {++ enum Error: Swift.Error {+ case incorrectVerificationCode+ }++ let client: GrandSlamClient+ let mode: GrandSlamAuthMode+ let loginData: GrandSlamLoginData+ unowned let delegate: TwoFactorAuthDelegate+ init(+ client: GrandSlamClient,+ mode: GrandSlamAuthMode,+ loginData: GrandSlamLoginData,+ delegate: TwoFactorAuthDelegate+ ) {+ self.client = client+ self.mode = mode+ self.loginData = loginData+ self.delegate = delegate+ }++ private func performSecondaryAuth(completion: @escaping (Result<(), Swift.Error>) -> Void) {+ let request = GrandSlamSecondaryAuthRequest(loginData: loginData)+ client.send(request) { result in+ guard result.get(withErrorHandler: completion) != nil else { return }+ self.delegate.fetchCode { self.validate(code: $0, completion: completion) }+ }+ }++ private func validate(code: String, completion: @escaping (Result<(), Swift.Error>) -> Void) {+ let request = GrandSlamValidateRequest(loginData: loginData, verificationCode: code)+ client.send(request) { result in+ switch result {+ case .success:+ completion(.success(()))+ case .failure(let error as GrandSlamOperationError) where error.code == -21669:+ completion(.failure(Error.incorrectVerificationCode))+ case .failure(let error):+ completion(.failure(error))+ }+ }+ }++ private func performTrustedDeviceAuth(completion: @escaping (Result<(), Swift.Error>) -> Void) {+ let request = GrandSlamTrustedDeviceRequest(loginData: loginData)+ client.send(request) { result in+ guard result.get(withErrorHandler: completion) != nil else { return }+ self.delegate.fetchCode { self.validate(code: $0, completion: completion) }+ }+ }++ func perform(completion: @escaping (Result<(), Swift.Error>) -> Void) {+ switch mode {+ case .secondaryAuth:+ self.performSecondaryAuth(completion: completion)+ case .trustedDeviceSecondaryAuth:+ self.performTrustedDeviceAuth(completion: completion)+ }+ }++}diff --git a/Supersign/GrandSlam/Requests/Concrete/GrandSlamAppTokensRequest.swift b/Supersign/GrandSlam/Requests/Concrete/GrandSlamAppTokensRequest.swiftnew file mode 100644index 0000000..d8e92af--- /dev/null+++ b/Supersign/GrandSlam/Requests/Concrete/GrandSlamAppTokensRequest.swift@@ -0,0 +1,39 @@+//+// GrandSlamAppTokensRequest.swift+// Supersign+//+// Created by Kabir Oberai on 20/11/19.+// Copyright © 2019 Kabir Oberai. All rights reserved.+//++import Foundation++struct GrandSlamAppTokensRequest: GrandSlamOperationRequest {++ typealias Decoder = GrandSlamOperationDecoder<Value>+ struct Value: Decodable {+ let encryptedToken: Data++ private enum CodingKeys: String, CodingKey {+ case encryptedToken = "et"+ }+ }++ static let operation = "apptokens"++ let username: String+ let apps: [String]+ let cookie: Data+ let idmsToken: String+ let checksum: Data++ var parameters: [String: Any] {+ [+ "app": apps,+ "c": cookie,+ "t": idmsToken,+ "checksum": checksum+ ]+ }++}diff --git a/Supersign/GrandSlam/Requests/Concrete/GrandSlamAuthCompleteRequest.swift b/Supersign/GrandSlam/Requests/Concrete/GrandSlamAuthCompleteRequest.swiftnew file mode 100644index 0000000..57f1e26--- /dev/null+++ b/Supersign/GrandSlam/Requests/Concrete/GrandSlamAuthCompleteRequest.swift@@ -0,0 +1,41 @@+//+// GrandSlamAuthCompleteRequest.swift+// Supersign+//+// Created by Kabir Oberai on 19/11/19.+// Copyright © 2019 Kabir Oberai. All rights reserved.+//++import Foundation++struct GrandSlamAuthCompleteRequest: GrandSlamOperationRequest {++ typealias Decoder = GrandSlamOperationDecoder<Value>+ struct Value: Decodable {+ let m2Data: Data+ let encryptedResponse: Data+ let negProto: Data+ let sc: Data?++ private enum CodingKeys: String, CodingKey {+ case m2Data = "M2"+ case encryptedResponse = "spd"+ case negProto = "np"+ case sc+ }+ }++ static let operation = "complete"++ let username: String+ let cookie: String+ let m1Data: Data++ var parameters: [String: Any] {+ [+ "c": cookie,+ "M1": m1Data,+ ]+ }++}diff --git a/Supersign/GrandSlam/Requests/Concrete/GrandSlamAuthInitRequest.swift b/Supersign/GrandSlam/Requests/Concrete/GrandSlamAuthInitRequest.swiftnew file mode 100644index 0000000..f8d0299--- /dev/null+++ b/Supersign/GrandSlam/Requests/Concrete/GrandSlamAuthInitRequest.swift@@ -0,0 +1,44 @@+//+// GrandSlamAuthInitRequest.swift+// Supersign+//+// Created by Kabir Oberai on 19/11/19.+// Copyright © 2019 Kabir Oberai. All rights reserved.+//++import Foundation++struct GrandSlamAuthInitRequest: GrandSlamOperationRequest {++ typealias Decoder = GrandSlamOperationDecoder<Value>+ struct Value: Decodable {+ let proto: GrandSlamAuthProtocol?+ let cookie: String+ let salt: Data+ let iterations: Int+ let bData: Data++ private enum CodingKeys: String, CodingKey {+ case proto = "sp"+ case cookie = "c"+ case salt = "s"+ case iterations = "i"+ case bData = "B"+ }+ }++ static let operation = "init"++ static let protocols: [String] = GrandSlamAuthProtocol.allCases.map { $0.rawValue }++ let username: String+ let aData: Data++ var parameters: [String: Any] {+ [+ "ps": Self.protocols,+ "A2k": aData,+ ]+ }++}diff --git a/Supersign/GrandSlam/Requests/Concrete/GrandSlamTrustedDeviceRequest.swift b/Supersign/GrandSlam/Requests/Concrete/GrandSlamTrustedDeviceRequest.swiftnew file mode 100644index 0000000..4efc6f4--- /dev/null+++ b/Supersign/GrandSlam/Requests/Concrete/GrandSlamTrustedDeviceRequest.swift@@ -0,0 +1,31 @@+//+// GrandSlamTrustedDeviceRequest.swift+// Supersign+//+// Created by Kabir Oberai on 20/11/19.+// Copyright © 2019 Kabir Oberai. All rights reserved.+//++import Foundation++struct GrandSlamTrustedDeviceRequest: GrandSlamTwoFactorRequest {+ struct Decoder: GrandSlamDataDecoder {+ static func decode(data: Data) throws {}+ }++ static let endpoint: GrandSlamEndpoint = \.trustedDeviceSecondaryAuth++ let loginData: GrandSlamLoginData+}++struct GrandSlamSecondaryAuthRequest: GrandSlamTwoFactorRequest {+ struct Decoder: GrandSlamDataDecoder {+ static func decode(data: Data) throws {+ print(String(data: data, encoding: .utf8) ?? "NO DATA")+ }+ }++ static let endpoint: GrandSlamEndpoint = \.secondaryAuth++ let loginData: GrandSlamLoginData+}diff --git a/Supersign/GrandSlam/Requests/Concrete/GrandSlamValidateRequest.swift b/Supersign/GrandSlam/Requests/Concrete/GrandSlamValidateRequest.swiftnew file mode 100644index 0000000..8009c58--- /dev/null+++ b/Supersign/GrandSlam/Requests/Concrete/GrandSlamValidateRequest.swift@@ -0,0 +1,24 @@+//+// GrandSlamValidateRequest.swift+// Supersign+//+// Created by Kabir Oberai on 20/11/19.+// Copyright © 2019 Kabir Oberai. All rights reserved.+//++import Foundation++struct GrandSlamValidateRequest: GrandSlamTwoFactorRequest {+ struct Decoder: GrandSlamDataDecoder {+ static func decode(data: Data) throws {}+ }++ static let endpoint: GrandSlamEndpoint = \.validateCode++ let loginData: GrandSlamLoginData+ let verificationCode: String++ var extraHeaders: [String: String] {+ ["security-code": verificationCode]+ }+}diff --git a/Supersign/GrandSlam/Requests/GrandSlamOperationRequest.swift b/Supersign/GrandSlam/Requests/GrandSlamOperationRequest.swiftnew file mode 100644index 0000000..96cd0e5--- /dev/null+++ b/Supersign/GrandSlam/Requests/GrandSlamOperationRequest.swift@@ -0,0 +1,99 @@+//+// GrandSlamOperationRequest.swift+// Supersign+//+// Created by Kabir Oberai on 19/11/19.+// Copyright © 2019 Kabir Oberai. All rights reserved.+//++import Foundation++struct GrandSlamOperationError: Error {+ let code: Int+ let message: String+}++struct GrandSlamOperationResponseStatusWrapper: Decodable {+ struct Status: Decodable {+ let errorCode: Int+ let errorMessage: String++ private enum CodingKeys: String, CodingKey {+ case errorCode = "ec"+ case errorMessage = "em"+ }+ }+ let status: Status++ private enum CodingKeys: String, CodingKey {+ case status = "Status"+ }+}++struct GrandSlamOperationResponseWrapper<T: Decodable>: Decodable {+ let value: T+ private enum CodingKeys: String, CodingKey {+ case value = "Response"+ }+}++struct GrandSlamOperationDecoder<T: Decodable>: GrandSlamDataDecoder {+ static func decode(data: Data) throws -> T {+ let decoder = PropertyListDecoder()+ let status = try decoder.decode(+ GrandSlamOperationResponseWrapper<GrandSlamOperationResponseStatusWrapper>.self,+ from: data+ ).value.status+ guard status.errorCode == 0 else {+ throw GrandSlamOperationError(code: status.errorCode, message: status.errorMessage)+ }+ return try decoder.decode(GrandSlamOperationResponseWrapper<T>.self, from: data).value+ }+}++protocol GrandSlamOperationRequest: GrandSlamRequest+ where Decoder == GrandSlamOperationDecoder<Value> {+ associatedtype Value: Decodable++ static var operation: String { get }++ var username: String { get }+ var parameters: [String: Any] { get }+}++extension GrandSlamOperationRequest {++ static var endpoint: GrandSlamEndpoint { \.gsService }++ func configure(request: inout URLRequest, deviceInfo: DeviceInfo, anisetteData: AnisetteData) {+ request.setValue("*/*", forHTTPHeaderField: "Accept")+ request.setValue(deviceInfo.clientInfo.userAgent, forHTTPHeaderField: "User-Agent")+ }++ func method(deviceInfo: DeviceInfo, anisetteData: AnisetteData) -> GrandSlamMethod {+ var clientData: [String: Any] = [+ "bootstrap": true,+ "icscrec": true,+ "pbe": false,+ "prkgen": true,+ "svct": "iCloud",+ "loc": Locale.current.identifier+ ]+ clientData.merge(deviceInfo.dictionary) { _, b in b }+ clientData.merge(anisetteData.dictionary) { _, b in b }++ var request: [String: Any] = [+ "o": Self.operation,+ "u": username,+ "cpd": clientData+ ]+ request.merge(parameters) { _, b in b }++ let body = [+ "Header": ["Version": "1.0.1"],+ "Request": request+ ]+ return .post(body)+ }++}diff --git a/Supersign/GrandSlam/Requests/GrandSlamRequest.swift b/Supersign/GrandSlam/Requests/GrandSlamRequest.swiftnew file mode 100644index 0000000..fb6259a--- /dev/null+++ b/Supersign/GrandSlam/Requests/GrandSlamRequest.swift@@ -0,0 +1,51 @@+//+// GrandSlamRequest.swift+// Supersign+//+// Created by Kabir Oberai on 19/11/19.+// Copyright © 2019 Kabir Oberai. All rights reserved.+//++import Foundation++enum GrandSlamMethod {+ case get+ case post([String: Any])++ var name: String {+ switch self {+ case .get: return "GET"+ case .post: return "POST"+ }+ }+}++//enum GrandSlamDecodeStrategy<T: Decodable> {+// case always(T)+// case plist+//+// func decode(data: Data) throws -> T {+// switch self {+// case .always(let value): return value+// case .plist: return try PropertyListDecoder().decode(T.self, from: data)+// }+// }+//}++protocol GrandSlamDataDecoder {+ associatedtype Value+ static func decode(data: Data) throws -> Value+}++protocol GrandSlamRequest {+ associatedtype Decoder: GrandSlamDataDecoder++ static var endpoint: GrandSlamEndpoint { get }++ func configure(request: inout URLRequest, deviceInfo: DeviceInfo, anisetteData: AnisetteData)+ func method(deviceInfo: DeviceInfo, anisetteData: AnisetteData) -> GrandSlamMethod+}++extension GrandSlamRequest {+// func configure(request: inout URLRequest, deviceInfo: DeviceInfo, anisetteData: AnisetteData) {}+}diff --git a/Supersign/module.modulemap b/Supersign/module.modulemapnew file mode 100644index 0000000..754e3cd--- /dev/null+++ b/Supersign/module.modulemap@@ -0,0 +1,6 @@+framework module Supersign {+ umbrella header "Supersign.h"++ export *+ module * { export * }+}