From 79335148b04434367286a12e62e81cfb76aa5a34 Mon Sep 17 00:00:00 2001 From: kabiroberai Date: Thu, 4 Feb 2021 04:10:01 +0530 Subject: [PATCH] Improve GrandSlam auth error messages --- .../GrandSlamAuthenticateOperation.swift | 29 ++++++++++++++----- ...ndSlamTwoFactorAuthenticateOperation.swift | 7 ++--- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/Supersign/GrandSlam/Operations/GrandSlamAuthenticateOperation.swift b/Supersign/GrandSlam/Operations/GrandSlamAuthenticateOperation.swift index 8236b4eb..37849b60 100644 --- a/Supersign/GrandSlam/Operations/GrandSlamAuthenticateOperation.swift +++ b/Supersign/GrandSlam/Operations/GrandSlamAuthenticateOperation.swift @@ -10,11 +10,26 @@ import Foundation class GrandSlamAuthenticateOperation { - enum Error: Swift.Error { - case failedChallenge - case invalidSession - case invalidResponse + enum Error: Swift.Error, LocalizedError { + case internalError case failedSecondaryAuth + + var errorDescription: String? { + switch self { + case .internalError: + return NSLocalizedString( + "apple_id_auth.error.internal", + value: "An internal authentication error occurred.", + comment: "" + ) + case .failedSecondaryAuth: + return NSLocalizedString( + "apple_id_auth.error.failed_secondary_auth", + value: "Could not complete 2-factor authentication.", + comment: "" + ) + } + } } private struct Response: Decodable { @@ -84,10 +99,10 @@ class GrandSlamAuthenticateOperation { srpClient.add(string: "|") guard srpClient.verify(hamk: completeResponse.hamk) && srpClient.verify(negProto: completeResponse.negProto) - else { return completion(.failure(Error.invalidSession)) } + else { return completion(.failure(Error.internalError)) } guard let rawLoginResponse = srpClient.decrypt(cbc: completeResponse.encryptedResponse) else { - return completion(.failure(Error.invalidResponse)) + return completion(.failure(Error.internalError)) } let response: Response @@ -134,7 +149,7 @@ class GrandSlamAuthenticateOperation { serverPublicKey: initResponse.serverPublicKey, isS2K: isS2K ) - guard let m1Data = mDataRaw else { return completion(.failure(Error.failedChallenge)) } + guard let m1Data = mDataRaw else { return completion(.failure(Error.internalError)) } let completeRequest = GrandSlamAuthCompleteRequest( username: username, cookie: initResponse.cookie, m1Data: m1Data diff --git a/Supersign/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swift b/Supersign/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swift index fd969210..bd4fb17e 100644 --- a/Supersign/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swift +++ b/Supersign/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swift @@ -69,10 +69,9 @@ class GrandSlamTwoFactorAuthenticateOperation { func perform(completion: @escaping (Result<(), Swift.Error>) -> Void) { switch mode { case .secondaryAuth: - // TODO: Does this always work? - // we *should* be calling performSecondaryAuth but that doesn't seem - // to actually trigger the 2fa prompt on trusted devices. Somehow - // using trustedDevice auth seems to work fine though + // TODO: We *should* be calling performSecondaryAuth – it does + // seem like performTrustedDeviceAuth sometimes works here but + // other times re-authenticating continues to 409 self.performTrustedDeviceAuth(completion: completion) case .trustedDeviceSecondaryAuth: self.performTrustedDeviceAuth(completion: completion)