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