xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XKit / GrandSlam / Operations / GrandSlamTwoFactorAuthenticateOperation.swift
GrandSlamTwoFactorAuthenticateOperation.swift85 lines · 2.8 KiBRaw
| 1 | // |
| 2 | // GrandSlamTwoFactorAuthenticateOperation.swift |
| 3 | // XKit |
| 4 | // |
| 5 | // Created by Kabir Oberai on 20/11/19. |
| 6 | // Copyright © 2019 Kabir Oberai. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | import XUtils |
| 10 | import Foundation |
| 11 | |
| 12 | public protocol TwoFactorAuthDelegate: AnyObject, Sendable { |
| 13 | func fetchCode() async -> String? |
| 14 | } |
| 15 | |
| 16 | struct GrandSlamTwoFactorAuthenticateOperation { |
| 17 | |
| 18 | enum Error: Swift.Error { |
| 19 | case incorrectVerificationCode |
| 20 | case userCancelled |
| 21 | } |
| 22 | |
| 23 | let client: GrandSlamClient |
| 24 | let mode: GrandSlamAuthMode? |
| 25 | let loginData: GrandSlamLoginData |
| 26 | unowned let delegate: TwoFactorAuthDelegate |
| 27 | |
| 28 | private func performSMSAuth() async throws { |
| 29 | // TODO: parse phone number list from o=complete req? |
| 30 | let phoneNumberID = 1 |
| 31 | do { |
| 32 | try await client.send(GrandSlamSMSAuthRequest(loginData: loginData, phoneNumberID: phoneNumberID)) |
| 33 | } catch let error as HTTPResponseError where error.status.code == 403 { |
| 34 | d.out.print(""" |
| 35 | warning: Could not send verification code via SMS. Falling back to trusted device authentication... |
| 36 | """) |
| 37 | try await performTrustedDeviceAuth() |
| 38 | return |
| 39 | } |
| 40 | try await validateCode(phoneNumberID: phoneNumberID) |
| 41 | } |
| 42 | |
| 43 | private func validateCode(phoneNumberID: Int? = nil) async throws { |
| 44 | guard let code = await self.delegate.fetchCode() else { throw Error.userCancelled } |
| 45 | let request: any GrandSlamRequest = if let phoneNumberID { |
| 46 | GrandSlamValidateSMSRequest( |
| 47 | loginData: loginData, |
| 48 | phoneNumberID: phoneNumberID, |
| 49 | verificationCode: code |
| 50 | ) |
| 51 | } else { |
| 52 | GrandSlamValidateRequest(loginData: loginData, verificationCode: code) |
| 53 | } |
| 54 | do { |
| 55 | _ = try await client.send(request) |
| 56 | } catch let error as GrandSlamOperationError where error.code == -21669 { |
| 57 | throw Error.incorrectVerificationCode |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | private func performTrustedDeviceAuth() async throws { |
| 62 | let request = GrandSlamTrustedDeviceRequest(loginData: loginData) |
| 63 | do { |
| 64 | try await client.send(request) |
| 65 | } catch let error as HTTPResponseError where error.status.code == 403 { |
| 66 | d.out.print(""" |
| 67 | warning: Could not send verification code. Generate one manually. |
| 68 | See https://github.com/xtool-org/xtool/discussions/368 |
| 69 | """) |
| 70 | } |
| 71 | try await validateCode() |
| 72 | } |
| 73 | |
| 74 | func perform() async throws { |
| 75 | switch mode { |
| 76 | case .secondaryAuth: |
| 77 | try await self.performSMSAuth() |
| 78 | case .trustedDeviceSecondaryAuth: |
| 79 | try await self.performTrustedDeviceAuth() |
| 80 | case nil: // means 2FA was automatically requested |
| 81 | try await validateCode() |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | } |