From 4d70eeff992f1faf973cd54a87c034ad0ca74b67 Mon Sep 17 00:00:00 2001 From: Szilard Szaloki Date: Tue, 28 Apr 2026 14:58:21 -0600 Subject: [PATCH] Account: unblocks `AuthenticationObserver` usage in iOS Settings (#35925) --- .../BraveAccountSectionController.java | 50 +- .../getting_started_page/brave_account_row.ts | 67 +- .../brave_account/brave_account_service.cc | 271 ++++--- .../brave_account/brave_account_service.h | 4 +- .../brave_account_service_unittest.cc | 764 +++++++++--------- .../brave_account/brave_account_utils.h | 2 + .../endpoints/auth_logout_unittest.cc | 3 +- .../endpoints/auth_validate_unittest.cc | 4 +- .../endpoints/login_finalize_unittest.cc | 4 +- .../endpoints/login_init_unittest.cc | 4 +- .../endpoints/password_finalize_unittest.cc | 50 +- .../endpoints/password_init_unittest.cc | 68 +- .../endpoints/service_token_unittest.cc | 72 +- .../endpoints/verify_complete_unittest.cc | 2 + .../endpoints/verify_delete_unittest.cc | 49 +- .../endpoints/verify_init_unittest.cc | 3 +- .../endpoints/verify_resend_unittest.cc | 4 +- .../mock_brave_account_authentication.h | 5 +- .../brave_account/mojom/brave_account.mojom | 193 +++-- .../resources/brave_account_browser_proxy.ts | 6 +- .../resources/brave_account_common.ts | 101 ++- .../resources/brave_account_create_dialog.ts | 9 +- .../resources/brave_account_otp_dialog.ts | 12 +- .../resources/brave_account_sign_in_dialog.ts | 12 +- .../email_aliases/email_aliases_auth.cc | 8 +- ...rave_account_authentication_bridge_impl.mm | 43 +- 26 files changed, 1038 insertions(+), 772 deletions(-) diff --git a/android/java/org/chromium/chrome/browser/settings/BraveAccountSectionController.java b/android/java/org/chromium/chrome/browser/settings/BraveAccountSectionController.java index 9432270e1c3..96c2f551b2e 100644 --- a/android/java/org/chromium/chrome/browser/settings/BraveAccountSectionController.java +++ b/android/java/org/chromium/chrome/browser/settings/BraveAccountSectionController.java @@ -21,8 +21,9 @@ import org.chromium.brave_account.mojom.AccountState; import org.chromium.brave_account.mojom.Authentication; import org.chromium.brave_account.mojom.AuthenticationObserver; import org.chromium.brave_account.mojom.ResendConfirmationEmailError; -import org.chromium.brave_account.mojom.ResendConfirmationEmailErrorCode; import org.chromium.brave_account.mojom.ResendConfirmationEmailResult; +import org.chromium.brave_account.mojom.ResendConfirmationEmailServerError; +import org.chromium.brave_account.mojom.ResendConfirmationEmailServerErrorCode; import org.chromium.build.annotations.NullMarked; import org.chromium.build.annotations.Nullable; import org.chromium.chrome.R; @@ -40,11 +41,11 @@ import java.util.Map; @NullMarked public class BraveAccountSectionController implements AuthenticationObserver, ConnectionErrorHandler { - private static final Map ERROR_STRINGS = + private static final Map SERVER_ERROR_STRINGS = Map.of( - ResendConfirmationEmailErrorCode.MAXIMUM_EMAIL_SEND_ATTEMPTS_EXCEEDED, + ResendConfirmationEmailServerErrorCode.MAXIMUM_EMAIL_SEND_ATTEMPTS_EXCEEDED, R.string.brave_account_resend_confirmation_email_maximum_send_attempts_exceeded, - ResendConfirmationEmailErrorCode.EMAIL_ALREADY_VERIFIED, + ResendConfirmationEmailServerErrorCode.EMAIL_ALREADY_VERIFIED, R.string.brave_account_resend_confirmation_email_already_verified); private static final String PREF_BRAVE_ACCOUNT_SECTION = "brave_account_section"; @@ -242,27 +243,22 @@ public class BraveAccountSectionController return mFragment.getString(R.string.brave_account_resend_confirmation_email_success); } - if (error.netErrorOrHttpStatus == null) { - // client-side error + if (error.which() == ResendConfirmationEmailError.Tag.ClientError) { return mFragment .getString(R.string.brave_account_client_error) .replace( "$1", - error.errorCode != null - ? String.format( - Locale.ROOT, - " (%s=%d)", - mFragment.getString(R.string.brave_account_error), - error.errorCode) - : ""); + String.format( + Locale.ROOT, + " (%s=%d)", + mFragment.getString(R.string.brave_account_error), + error.getClientError().errorCode)); } - // server-side error - if (error.errorCode != null) { - Integer stringId = ERROR_STRINGS.get(error.errorCode); - if (stringId != null) { - return mFragment.getString(stringId); - } + ResendConfirmationEmailServerError serverError = error.getServerError(); + Integer stringId = SERVER_ERROR_STRINGS.get(serverError.errorCode); + if (stringId != null) { + return mFragment.getString(stringId); } return mFragment @@ -272,17 +268,15 @@ public class BraveAccountSectionController String.format( Locale.ROOT, "%s=%d", - error.netErrorOrHttpStatus > 0 ? "HTTP" : "NET", - error.netErrorOrHttpStatus)) + serverError.netErrorOrHttpStatus > 0 ? "HTTP" : "NET", + serverError.netErrorOrHttpStatus)) .replace( "$2", - error.errorCode != null - ? String.format( - Locale.ROOT, - ", %s=%d", - mFragment.getString(R.string.brave_account_error), - error.errorCode) - : ""); + String.format( + Locale.ROOT, + ", %s=%d", + mFragment.getString(R.string.brave_account_error), + serverError.errorCode)); } private void showAlertDialog( diff --git a/browser/resources/settings/getting_started_page/brave_account_row.ts b/browser/resources/settings/getting_started_page/brave_account_row.ts index 438905204fd..f70e2c20fbe 100644 --- a/browser/resources/settings/getting_started_page/brave_account_row.ts +++ b/browser/resources/settings/getting_started_page/brave_account_row.ts @@ -19,9 +19,12 @@ import { BraveAccountSettingsStrings } from '../brave_components_webui_strings.j import { AccountState, AccountStateFieldTags, + ResendConfirmationEmailClientErrorCode, ResendConfirmationEmailError, - ResendConfirmationEmailErrorCode, + ResendConfirmationEmailErrorFieldTags, + ResendConfirmationEmailServerErrorCode, whichAccountState, + whichResendConfirmationEmailError, } from '../brave_account.mojom-webui.js' import { getCss } from './brave_account_row.css.js' import { getHtml } from './brave_account_row.html.js' @@ -105,7 +108,11 @@ export class SettingsBraveAccountRowElement extends I18nMixinLit(CrLitElement) { error = e as ResendConfirmationEmailError } else { console.error('Unexpected error:', e) - error = { netErrorOrHttpStatus: null, errorCode: null } + error = { + clientError: { + errorCode: ResendConfirmationEmailClientErrorCode.kUnexpected, + }, + } } } @@ -236,46 +243,42 @@ export class SettingsBraveAccountRowElement extends I18nMixinLit(CrLitElement) { : stateHtml[whichAccountState(this.state)]() } - private getErrorMessage(details: ResendConfirmationEmailError): string { - const ERROR_STRINGS: Partial< - Record + private getErrorMessage(error: ResendConfirmationEmailError): string { + const SERVER_ERROR_STRINGS: Partial< + Record > = { - [ResendConfirmationEmailErrorCode.kMaximumEmailSendAttemptsExceeded]: - this.i18n( - BraveAccountSettingsStrings - .BRAVE_ACCOUNT_RESEND_CONFIRMATION_EMAIL_MAXIMUM_SEND_ATTEMPTS_EXCEEDED), - [ResendConfirmationEmailErrorCode.kEmailAlreadyVerified]: - this.i18n( - BraveAccountSettingsStrings - .BRAVE_ACCOUNT_RESEND_CONFIRMATION_EMAIL_ALREADY_VERIFIED), + [ResendConfirmationEmailServerErrorCode + .kMaximumEmailSendAttemptsExceeded]: + BraveAccountSettingsStrings + .BRAVE_ACCOUNT_RESEND_CONFIRMATION_EMAIL_MAXIMUM_SEND_ATTEMPTS_EXCEEDED, + [ResendConfirmationEmailServerErrorCode.kEmailAlreadyVerified]: + BraveAccountSettingsStrings + .BRAVE_ACCOUNT_RESEND_CONFIRMATION_EMAIL_ALREADY_VERIFIED, } - const { netErrorOrHttpStatus, errorCode } = details + const errorLabel = this.i18n( + BraveAccountSettingsStrings.BRAVE_ACCOUNT_ERROR) - if (netErrorOrHttpStatus == null) { - // client-side error + if (whichResendConfirmationEmailError(error) + === ResendConfirmationEmailErrorFieldTags.CLIENT_ERROR) { return this.i18n( BraveAccountSettingsStrings.BRAVE_ACCOUNT_CLIENT_ERROR, - errorCode != null - ? ` (${this.i18n( - BraveAccountSettingsStrings - .BRAVE_ACCOUNT_ERROR)}=${errorCode})` - : '', + ` (${errorLabel}=${error.clientError!.errorCode})`, ) } - // server-side error - return ( - (errorCode != null ? ERROR_STRINGS[errorCode] : null) - ?? this.i18n( + const serverError = error.serverError! + const stringId = SERVER_ERROR_STRINGS[serverError.errorCode] + if (stringId) { + return this.i18n(stringId) + } + + return this.i18n( BraveAccountSettingsStrings.BRAVE_ACCOUNT_SERVER_ERROR, - `${netErrorOrHttpStatus > 0 ? 'HTTP' : 'NET'}=${netErrorOrHttpStatus}`, - errorCode != null - ? `, ${this.i18n( - BraveAccountSettingsStrings - .BRAVE_ACCOUNT_ERROR)}=${errorCode}` - : '', - ) + `${serverError.netErrorOrHttpStatus > 0 ? 'HTTP' : 'NET'}=${ + serverError.netErrorOrHttpStatus + }`, + `, ${errorLabel}=${serverError.errorCode}`, ) } diff --git a/components/brave_account/brave_account_service.cc b/components/brave_account/brave_account_service.cc index 7448e9b9ce0..41e31c6019d 100644 --- a/components/brave_account/brave_account_service.cc +++ b/components/brave_account/brave_account_service.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_account/brave_account_service.h" +#include +#include #include #include "base/base64.h" @@ -101,21 +103,54 @@ auto MakeRequest() { return request; } -template -auto MakeMojomError(int status_code, ErrorBody error_body) { - auto mojom_error = MojomError::New(status_code, std::nullopt); +template +using ClientErrorOf = typename std::remove_cvref_t< + decltype(std::declval().get_client_error())>::element_type; - if (!error_body.code.is_int()) { - return mojom_error; +template +using ClientErrorCodeOf = decltype(ClientErrorOf::error_code); + +template +using ServerErrorOf = typename std::remove_cvref_t< + decltype(std::declval().get_server_error())>::element_type; + +template +using ServerErrorCodeOf = decltype(ServerErrorOf::error_code); + +template + requires std::same_as> +auto MakeClientError(ClientErrorCode client_error_code) { + return Error::NewClientError(ClientErrorOf::New(client_error_code)); +} + +template +auto MakeServerErrorCode(ErrorBody error_body) { + if (error_body.code.is_none()) { + return ServerErrorCode::kNull; + } else if (error_body.code.is_int()) { + if (const auto error_code = + static_cast(error_body.code.GetInt()); + mojom::IsKnownEnumValue(error_code)) { + return error_code; + } } - const auto error_code = - static_casterrorCode)::value_type>( - error_body.code.GetInt()); - mojom_error->errorCode = mojom::IsKnownEnumValue(error_code) - ? std::optional(error_code) - : std::nullopt; - return mojom_error; + return ServerErrorCode::kUnknown; +} + +template + requires std::same_as> +auto MakeServerError(int net_error_or_http_status, + ServerErrorCode server_error_code) { + return Error::NewServerError( + ServerErrorOf::New(net_error_or_http_status, server_error_code)); +} + +template +auto MakeServerError(int net_error_or_http_status, ErrorBody error_body) { + return MakeServerError( + net_error_or_http_status, + MakeServerErrorCode>(std::move(error_body))); } } // namespace @@ -186,20 +221,17 @@ void BraveAccountService::AddObserver( } void BraveAccountService::RegisterInitialize( - std::optional initiating_service, + mojom::Service initiating_service, const std::string& email, const std::string& blinded_message, RegisterInitializeCallback callback) { - if (email.empty() || blinded_message.empty()) { - return std::move(callback).Run( - base::unexpected(mojom::RegisterError::New())); - } + CHECK(!email.empty()); + CHECK(!blinded_message.empty()); auto request = MakeRequest(); request.body.blinded_message = blinded_message; request.body.initiating_service_name = - initiating_service ? kServiceToString.at(*initiating_service) - : "accounts"; + kServiceToString.at(initiating_service); request.body.new_account_email = email; request.body.serialize_response = true; Client::Send( @@ -212,16 +244,14 @@ void BraveAccountService::RegisterFinalize( const std::string& encrypted_verification_token, const std::string& serialized_record, RegisterFinalizeCallback callback) { - if (encrypted_verification_token.empty() || serialized_record.empty()) { - return std::move(callback).Run( - base::unexpected(mojom::RegisterError::New())); - } + CHECK(!encrypted_verification_token.empty()); + CHECK(!serialized_record.empty()); const std::string verification_token = Decrypt(encrypted_verification_token); if (verification_token.empty()) { - return std::move(callback).Run(base::unexpected(mojom::RegisterError::New( - std::nullopt, - mojom::RegisterErrorCode::kVerificationTokenDecryptionFailed))); + return std::move(callback).Run(base::unexpected(MakeClientError< + mojom::RegisterError>( + mojom::RegisterClientErrorCode::kVerificationTokenDecryptionFailed))); } auto request = MakeRequest>(); @@ -236,24 +266,21 @@ void BraveAccountService::RegisterFinalize( void BraveAccountService::RegisterVerify(const std::string& code, RegisterVerifyCallback callback) { - if (code.empty()) { - return std::move(callback).Run( - base::unexpected(mojom::RegisterError::New())); - } + CHECK(!code.empty()); const auto encrypted_verification_token = pref_service_->GetString(prefs::kBraveAccountVerificationToken); if (encrypted_verification_token.empty()) { - return std::move(callback).Run(base::unexpected(mojom::RegisterError::New( - std::nullopt, - mojom::RegisterErrorCode::kUserNotInTheVerificationState))); + return std::move(callback).Run( + base::unexpected(MakeClientError( + mojom::RegisterClientErrorCode::kUserNotInTheVerificationState))); } const auto verification_token = Decrypt(encrypted_verification_token); if (verification_token.empty()) { - return std::move(callback).Run(base::unexpected(mojom::RegisterError::New( - std::nullopt, - mojom::RegisterErrorCode::kVerificationTokenDecryptionFailed))); + return std::move(callback).Run(base::unexpected(MakeClientError< + mojom::RegisterError>( + mojom::RegisterClientErrorCode::kVerificationTokenDecryptionFailed))); } auto request = MakeRequest>(); @@ -271,17 +298,17 @@ void BraveAccountService::ResendConfirmationEmail( pref_service_->GetString(prefs::kBraveAccountVerificationToken); if (encrypted_verification_token.empty()) { return std::move(callback).Run( - base::unexpected(mojom::ResendConfirmationEmailError::New( - std::nullopt, mojom::ResendConfirmationEmailErrorCode:: - kUserNotInTheVerificationState))); + base::unexpected(MakeClientError( + mojom::ResendConfirmationEmailClientErrorCode:: + kUserNotInTheVerificationState))); } const auto verification_token = Decrypt(encrypted_verification_token); if (verification_token.empty()) { return std::move(callback).Run( - base::unexpected(mojom::ResendConfirmationEmailError::New( - std::nullopt, mojom::ResendConfirmationEmailErrorCode:: - kVerificationTokenDecryptionFailed))); + base::unexpected(MakeClientError( + mojom::ResendConfirmationEmailClientErrorCode:: + kVerificationTokenDecryptionFailed))); } auto request = MakeRequest>(); @@ -315,20 +342,17 @@ void BraveAccountService::CancelRegistration() { base::BindOnce([](VerifyDelete::Response) {})); } -void BraveAccountService::LoginInitialize( - std::optional initiating_service, - const std::string& email, - const std::string& serialized_ke1, - LoginInitializeCallback callback) { - if (email.empty() || serialized_ke1.empty()) { - return std::move(callback).Run(base::unexpected(mojom::LoginError::New())); - } +void BraveAccountService::LoginInitialize(mojom::Service initiating_service, + const std::string& email, + const std::string& serialized_ke1, + LoginInitializeCallback callback) { + CHECK(!email.empty()); + CHECK(!serialized_ke1.empty()); auto request = MakeRequest(); request.body.email = email; request.body.initiating_service_name = - initiating_service ? kServiceToString.at(*initiating_service) - : "accounts"; + kServiceToString.at(initiating_service); request.body.serialized_ke1 = serialized_ke1; Client::Send( url_loader_factory_, std::move(request), @@ -340,14 +364,14 @@ void BraveAccountService::LoginFinalize( const std::string& encrypted_login_token, const std::string& client_mac, LoginFinalizeCallback callback) { - if (encrypted_login_token.empty() || client_mac.empty()) { - return std::move(callback).Run(base::unexpected(mojom::LoginError::New())); - } + CHECK(!encrypted_login_token.empty()); + CHECK(!client_mac.empty()); const std::string login_token = Decrypt(encrypted_login_token); if (login_token.empty()) { - return std::move(callback).Run(base::unexpected(mojom::LoginError::New( - std::nullopt, mojom::LoginErrorCode::kLoginTokenDecryptionFailed))); + return std::move(callback).Run( + base::unexpected(MakeClientError( + mojom::LoginClientErrorCode::kLoginTokenDecryptionFailed))); } auto request = MakeRequest>(); @@ -380,6 +404,7 @@ void BraveAccountService::LogOut() { void BraveAccountService::GetServiceToken(mojom::Service service, GetServiceTokenCallback callback) { + CHECK(service != mojom::Service::kAccounts); std::string service_name(kServiceToString.at(service)); if (auto service_token = GetCachedServiceToken(service_name); !service_token.empty()) { @@ -391,16 +416,16 @@ void BraveAccountService::GetServiceToken(mojom::Service service, pref_service_->GetString(prefs::kBraveAccountAuthenticationToken); if (encrypted_authentication_token.empty()) { return std::move(callback).Run( - base::unexpected(mojom::GetServiceTokenError::New( - std::nullopt, mojom::GetServiceTokenErrorCode::kUserNotLoggedIn))); + base::unexpected(MakeClientError( + mojom::GetServiceTokenClientErrorCode::kUserNotLoggedIn))); } const auto authentication_token = Decrypt(encrypted_authentication_token); if (authentication_token.empty()) { return std::move(callback).Run( - base::unexpected(mojom::GetServiceTokenError::New( - std::nullopt, mojom::GetServiceTokenErrorCode:: - kAuthenticationTokenDecryptionFailed))); + base::unexpected(MakeClientError( + mojom::GetServiceTokenClientErrorCode:: + kAuthenticationTokenDecryptionFailed))); } auto request = MakeRequest>(); @@ -418,8 +443,10 @@ void BraveAccountService::OnRegisterInitialize( RegisterInitializeCallback callback, PasswordInit::Response response) { if (!response.body) { - return std::move(callback).Run(base::unexpected(mojom::RegisterError::New( - response.status_code.value_or(response.net_error), std::nullopt))); + return std::move(callback).Run( + base::unexpected(MakeServerError( + response.status_code.value_or(response.net_error), + mojom::RegisterServerErrorCode::kInvalidResponse))); } const auto status_code = CHECK_DEREF(response.status_code); @@ -429,8 +456,8 @@ void BraveAccountService::OnRegisterInitialize( // expected ==> // expected .transform_error([&](auto error_body) { - return MakeMojomError(status_code, - std::move(error_body)); + return MakeServerError(status_code, + std::move(error_body)); }) // expected<[SuccessBody ], RegisterErrorPtr> ==> // expected<[RegisterInitializeResultPtr], RegisterErrorPtr> @@ -439,16 +466,17 @@ void BraveAccountService::OnRegisterInitialize( mojom::RegisterErrorPtr> { if (success_body.verification_token.empty() || success_body.serialized_response.empty()) { - return base::unexpected( - mojom::RegisterError::New(status_code, std::nullopt)); + return base::unexpected(MakeServerError( + status_code, + mojom::RegisterServerErrorCode::kInvalidResponse)); } std::string encrypted_verification_token = Encrypt(success_body.verification_token); if (encrypted_verification_token.empty()) { - return base::unexpected(mojom::RegisterError::New( - std::nullopt, mojom::RegisterErrorCode:: - kVerificationTokenEncryptionFailed)); + return base::unexpected(MakeClientError( + mojom::RegisterClientErrorCode:: + kVerificationTokenEncryptionFailed)); } return mojom::RegisterInitializeResult::New( @@ -464,8 +492,10 @@ void BraveAccountService::OnRegisterFinalize( const std::string& encrypted_verification_token, PasswordFinalize::Response response) { if (!response.body) { - return std::move(callback).Run(base::unexpected(mojom::RegisterError::New( - response.status_code.value_or(response.net_error), std::nullopt))); + return std::move(callback).Run( + base::unexpected(MakeServerError( + response.status_code.value_or(response.net_error), + mojom::RegisterServerErrorCode::kInvalidResponse))); } const auto status_code = CHECK_DEREF(response.status_code); @@ -475,8 +505,8 @@ void BraveAccountService::OnRegisterFinalize( // expected ==> // expected .transform_error([&](auto error_body) { - return MakeMojomError(status_code, - std::move(error_body)); + return MakeServerError(status_code, + std::move(error_body)); }) // expected<[SuccessBody ], RegisterErrorPtr> ==> // expected<[RegisterFinalizeResultPtr], RegisterErrorPtr> @@ -495,8 +525,10 @@ void BraveAccountService::OnRegisterFinalize( void BraveAccountService::OnRegisterVerify(RegisterVerifyCallback callback, VerifyComplete::Response response) { if (!response.body) { - return std::move(callback).Run(base::unexpected(mojom::RegisterError::New( - response.status_code.value_or(response.net_error), std::nullopt))); + return std::move(callback).Run( + base::unexpected(MakeServerError( + response.status_code.value_or(response.net_error), + mojom::RegisterServerErrorCode::kInvalidResponse))); } const auto status_code = CHECK_DEREF(response.status_code); @@ -506,8 +538,8 @@ void BraveAccountService::OnRegisterVerify(RegisterVerifyCallback callback, // expected ==> // expected .transform_error([&](auto error_body) { - return MakeMojomError(status_code, - std::move(error_body)); + return MakeServerError(status_code, + std::move(error_body)); }) // expected<[SuccessBody ], RegisterErrorPtr> ==> // expected<[RegisterVerifyResultPtr], RegisterErrorPtr> @@ -515,16 +547,17 @@ void BraveAccountService::OnRegisterVerify(RegisterVerifyCallback callback, -> base::expected { if (success_body.auth_token.empty() || success_body.email.empty()) { - return base::unexpected( - mojom::RegisterError::New(status_code, std::nullopt)); + return base::unexpected(MakeServerError( + status_code, + mojom::RegisterServerErrorCode::kInvalidResponse)); } const std::string encrypted_authentication_token = Encrypt(success_body.auth_token); if (encrypted_authentication_token.empty()) { - return base::unexpected(mojom::RegisterError::New( - std::nullopt, mojom::RegisterErrorCode:: - kAuthenticationTokenEncryptionFailed)); + return base::unexpected(MakeClientError( + mojom::RegisterClientErrorCode:: + kAuthenticationTokenEncryptionFailed)); } pref_service_->SetString(prefs::kBraveAccountEmailAddress, @@ -548,12 +581,13 @@ void BraveAccountService::OnResendConfirmationEmail( if (!response.body || response.body->has_value()) { return std::move(callback).Run( - base::unexpected(mojom::ResendConfirmationEmailError::New( - response.status_code.value_or(response.net_error), std::nullopt))); + base::unexpected(MakeServerError( + response.status_code.value_or(response.net_error), + mojom::ResendConfirmationEmailServerErrorCode::kInvalidResponse))); } std::move(callback).Run( - base::unexpected(MakeMojomError( + base::unexpected(MakeServerError( CHECK_DEREF(response.status_code), std::move(response.body->error())))); } @@ -565,8 +599,10 @@ void BraveAccountService::OnVerificationTokenChanged() { void BraveAccountService::OnLoginInitialize(LoginInitializeCallback callback, LoginInit::Response response) { if (!response.body) { - return std::move(callback).Run(base::unexpected(mojom::LoginError::New( - response.status_code.value_or(response.net_error), std::nullopt))); + return std::move(callback).Run( + base::unexpected(MakeServerError( + response.status_code.value_or(response.net_error), + mojom::LoginServerErrorCode::kInvalidResponse))); } const auto status_code = CHECK_DEREF(response.status_code); @@ -576,8 +612,8 @@ void BraveAccountService::OnLoginInitialize(LoginInitializeCallback callback, // expected ==> // expected .transform_error([&](auto error_body) { - return MakeMojomError(status_code, - std::move(error_body)); + return MakeServerError(status_code, + std::move(error_body)); }) // expected<[SuccessBody ], LoginErrorPtr> ==> // expected<[LoginInitializeResultPtr], LoginErrorPtr> @@ -586,16 +622,15 @@ void BraveAccountService::OnLoginInitialize(LoginInitializeCallback callback, mojom::LoginErrorPtr> { if (success_body.login_token.empty() || success_body.serialized_ke2.empty()) { - return base::unexpected( - mojom::LoginError::New(status_code, std::nullopt)); + return base::unexpected(MakeServerError( + status_code, mojom::LoginServerErrorCode::kInvalidResponse)); } std::string encrypted_login_token = Encrypt(success_body.login_token); if (encrypted_login_token.empty()) { - return base::unexpected(mojom::LoginError::New( - std::nullopt, - mojom::LoginErrorCode::kLoginTokenEncryptionFailed)); + return base::unexpected(MakeClientError( + mojom::LoginClientErrorCode::kLoginTokenEncryptionFailed)); } return mojom::LoginInitializeResult::New( @@ -609,8 +644,10 @@ void BraveAccountService::OnLoginInitialize(LoginInitializeCallback callback, void BraveAccountService::OnLoginFinalize(LoginFinalizeCallback callback, LoginFinalize::Response response) { if (!response.body) { - return std::move(callback).Run(base::unexpected(mojom::LoginError::New( - response.status_code.value_or(response.net_error), std::nullopt))); + return std::move(callback).Run( + base::unexpected(MakeServerError( + response.status_code.value_or(response.net_error), + mojom::LoginServerErrorCode::kInvalidResponse))); } const auto status_code = CHECK_DEREF(response.status_code); @@ -620,8 +657,8 @@ void BraveAccountService::OnLoginFinalize(LoginFinalizeCallback callback, // expected ==> // expected .transform_error([&](auto error_body) { - return MakeMojomError(status_code, - std::move(error_body)); + return MakeServerError(status_code, + std::move(error_body)); }) // expected<[SuccessBody ], LoginErrorPtr> ==> // expected<[LoginFinalizeResultPtr], LoginErrorPtr> @@ -629,16 +666,16 @@ void BraveAccountService::OnLoginFinalize(LoginFinalizeCallback callback, -> base::expected { if (success_body.auth_token.empty() || success_body.email.empty()) { - return base::unexpected( - mojom::LoginError::New(status_code, std::nullopt)); + return base::unexpected(MakeServerError( + status_code, mojom::LoginServerErrorCode::kInvalidResponse)); } const std::string encrypted_authentication_token = Encrypt(success_body.auth_token); if (encrypted_authentication_token.empty()) { - return base::unexpected(mojom::LoginError::New( - std::nullopt, - mojom::LoginErrorCode::kAuthenticationTokenEncryptionFailed)); + return base::unexpected(MakeClientError( + mojom::LoginClientErrorCode:: + kAuthenticationTokenEncryptionFailed)); } pref_service_->SetString(prefs::kBraveAccountEmailAddress, @@ -770,15 +807,16 @@ void BraveAccountService::OnGetServiceToken( current_encrypted_authentication_token != expected_encrypted_authentication_token) { return std::move(callback).Run( - base::unexpected(mojom::GetServiceTokenError::New( - std::nullopt, - mojom::GetServiceTokenErrorCode::kAuthenticationSessionChanged))); + base::unexpected(MakeClientError( + mojom::GetServiceTokenClientErrorCode:: + kAuthenticationSessionChanged))); } if (!response.body) { return std::move(callback).Run( - base::unexpected(mojom::GetServiceTokenError::New( - response.status_code.value_or(response.net_error), std::nullopt))); + base::unexpected(MakeServerError( + response.status_code.value_or(response.net_error), + mojom::GetServiceTokenServerErrorCode::kInvalidResponse))); } const auto status_code = CHECK_DEREF(response.status_code); @@ -788,7 +826,7 @@ void BraveAccountService::OnGetServiceToken( // expected ==> // expected .transform_error([&](auto error_body) { - return MakeMojomError( + return MakeServerError( status_code, std::move(error_body)); }) // expected<[SuccessBody ], GetServiceTokenErrorPtr> ==> @@ -798,14 +836,17 @@ void BraveAccountService::OnGetServiceToken( mojom::GetServiceTokenErrorPtr> { if (success_body.auth_token.empty()) { return base::unexpected( - mojom::GetServiceTokenError::New(status_code, std::nullopt)); + MakeServerError( + status_code, + mojom::GetServiceTokenServerErrorCode::kInvalidResponse)); } auto encrypted_service_token = Encrypt(success_body.auth_token); if (encrypted_service_token.empty()) { - return base::unexpected(mojom::GetServiceTokenError::New( - std::nullopt, mojom::GetServiceTokenErrorCode:: - kServiceTokenEncryptionFailed)); + return base::unexpected( + MakeClientError( + mojom::GetServiceTokenClientErrorCode:: + kServiceTokenEncryptionFailed)); } auto service_tokens = diff --git a/components/brave_account/brave_account_service.h b/components/brave_account/brave_account_service.h index 868177a2345..7f67c4115c9 100644 --- a/components/brave_account/brave_account_service.h +++ b/components/brave_account/brave_account_service.h @@ -81,7 +81,7 @@ class BraveAccountService : public KeyedService, public mojom::Authentication { void AddObserver( mojo::PendingRemote observer) override; - void RegisterInitialize(std::optional initiating_service, + void RegisterInitialize(mojom::Service initiating_service, const std::string& email, const std::string& blinded_message, RegisterInitializeCallback callback) override; @@ -98,7 +98,7 @@ class BraveAccountService : public KeyedService, public mojom::Authentication { void CancelRegistration() override; - void LoginInitialize(std::optional initiating_service, + void LoginInitialize(mojom::Service initiating_service, const std::string& email, const std::string& serialized_ke1, LoginInitializeCallback callback) override; diff --git a/components/brave_account/brave_account_service_unittest.cc b/components/brave_account/brave_account_service_unittest.cc index a38709becb6..a2a7f47cff0 100644 --- a/components/brave_account/brave_account_service_unittest.cc +++ b/components/brave_account/brave_account_service_unittest.cc @@ -246,9 +246,9 @@ struct RegisterInitializeTestCase { base::test::TaskEnvironment& task_environment, mojom::Authentication& authentication, base::OnceCallback callback) { - authentication.RegisterInitialize(std::nullopt, test_case.email, - test_case.blinded_message, - std::move(callback)); + authentication.RegisterInitialize( + mojom::Service::kAccounts, test_case.email, test_case.blinded_message, + std::move(callback)); } std::string test_name; @@ -262,34 +262,6 @@ struct RegisterInitializeTestCase { namespace { -const RegisterInitializeTestCase* RegisterInitializeEmailEmpty() { - static const base::NoDestructor - kRegisterInitializeEmailEmpty({ - .test_name = "register_initialize_email_empty", - .email = "", - .blinded_message = {}, // not used - .fail_encryption = {}, // not used - .fail_decryption = {}, // not used - .endpoint_response = {}, // not used - .mojo_expected = base::unexpected(mojom::RegisterError::New()), - }); - return kRegisterInitializeEmailEmpty.get(); -} - -const RegisterInitializeTestCase* RegisterInitializeBlindedMessageEmpty() { - static const base::NoDestructor - kRegisterInitializeBlindedMessageEmpty({ - .test_name = "register_initialize_blinded_message_empty", - .email = kEmailAddress, - .blinded_message = "", - .fail_encryption = {}, // not used - .fail_decryption = {}, // not used - .endpoint_response = {}, // not used - .mojo_expected = base::unexpected(mojom::RegisterError::New()), - }); - return kRegisterInitializeBlindedMessageEmpty.get(); -} - const RegisterInitializeTestCase* RegisterInitializeBodyMissingOrFailedToParse() { static const base::NoDestructor @@ -302,8 +274,11 @@ RegisterInitializeBodyMissingOrFailedToParse() { .endpoint_response = {{.net_error = net::OK, .status_code = net::HTTP_INTERNAL_SERVER_ERROR, .body = std::nullopt}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - net::HTTP_INTERNAL_SERVER_ERROR, std::nullopt)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_INTERNAL_SERVER_ERROR, + mojom::RegisterServerErrorCode::kInvalidResponse))), }); return kRegisterInitializeBodyMissingOrFailedToParse.get(); } @@ -323,8 +298,11 @@ const RegisterInitializeTestCase* RegisterInitializeErrorCodeIsNull() { body.code = base::Value(); return body; }())}}, - .mojo_expected = base::unexpected( - mojom::RegisterError::New(net::HTTP_BAD_REQUEST, std::nullopt)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_BAD_REQUEST, + mojom::RegisterServerErrorCode::kNull))), }); return kRegisterInitializeErrorCodeIsNull.get(); } @@ -344,9 +322,11 @@ const RegisterInitializeTestCase* RegisterInitializeNewAccountEmailRequired() { body.code = base::Value(11005); return body; }())}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - net::HTTP_BAD_REQUEST, - mojom::RegisterErrorCode::kNewAccountEmailRequired)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_BAD_REQUEST, mojom::RegisterServerErrorCode:: + kNewAccountEmailRequired))), }); return kRegisterInitializeNewAccountEmailRequired.get(); } @@ -366,9 +346,11 @@ const RegisterInitializeTestCase* RegisterInitializeIntentNotAllowed() { body.code = base::Value(13003); return body; }())}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - net::HTTP_BAD_REQUEST, - mojom::RegisterErrorCode::kIntentNotAllowed)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_BAD_REQUEST, + mojom::RegisterServerErrorCode::kIntentNotAllowed))), }); return kRegisterInitializeIntentNotAllowed.get(); } @@ -388,9 +370,11 @@ const RegisterInitializeTestCase* RegisterInitializeTooManyVerifications() { body.code = base::Value(13001); return body; }())}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - net::HTTP_BAD_REQUEST, - mojom::RegisterErrorCode::kTooManyVerifications)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_BAD_REQUEST, + mojom::RegisterServerErrorCode::kTooManyVerifications))), }); return kRegisterInitializeTooManyVerifications.get(); } @@ -410,8 +394,11 @@ const RegisterInitializeTestCase* RegisterInitializeAccountExists() { body.code = base::Value(13004); return body; }())}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - net::HTTP_BAD_REQUEST, mojom::RegisterErrorCode::kAccountExists)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_BAD_REQUEST, + mojom::RegisterServerErrorCode::kAccountExists))), }); return kRegisterInitializeAccountExists.get(); } @@ -431,9 +418,11 @@ const RegisterInitializeTestCase* RegisterInitializeEmailDomainNotSupported() { body.code = base::Value(13006); return body; }())}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - net::HTTP_BAD_REQUEST, - mojom::RegisterErrorCode::kEmailDomainNotSupported)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_BAD_REQUEST, mojom::RegisterServerErrorCode:: + kEmailDomainNotSupported))), }); return kRegisterInitializeEmailDomainNotSupported.get(); } @@ -453,9 +442,11 @@ const RegisterInitializeTestCase* RegisterInitializeUnauthorized() { body.code = base::Value(0); return body; }())}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - net::HTTP_UNAUTHORIZED, - mojom::RegisterErrorCode::kMiscServerError)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_UNAUTHORIZED, + mojom::RegisterServerErrorCode::kNull))), }); return kRegisterInitializeUnauthorized.get(); } @@ -475,9 +466,11 @@ const RegisterInitializeTestCase* RegisterInitializeServerError() { body.code = base::Value(0); return body; }())}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - net::HTTP_INTERNAL_SERVER_ERROR, - mojom::RegisterErrorCode::kMiscServerError)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_INTERNAL_SERVER_ERROR, + mojom::RegisterServerErrorCode::kNull))), }); return kRegisterInitializeServerError.get(); } @@ -497,8 +490,11 @@ const RegisterInitializeTestCase* RegisterInitializeUnknown() { body.code = base::Value(42); return body; }())}}, - .mojo_expected = base::unexpected( - mojom::RegisterError::New(net::HTTP_TOO_EARLY, std::nullopt)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_TOO_EARLY, + mojom::RegisterServerErrorCode::kUnknown))), }); return kRegisterInitializeUnknown.get(); } @@ -521,8 +517,11 @@ const RegisterInitializeTestCase* RegisterInitializeVerificationTokenEmpty() { "serialized_response"; return body; }()}}, - .mojo_expected = base::unexpected( - mojom::RegisterError::New(net::HTTP_OK, std::nullopt)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_OK, + mojom::RegisterServerErrorCode::kInvalidResponse))), }); return kRegisterInitializeVerificationTokenEmpty.get(); } @@ -545,8 +544,11 @@ const RegisterInitializeTestCase* RegisterInitializeSerializedResponseEmpty() { body.serialized_response = ""; return body; }()}}, - .mojo_expected = base::unexpected( - mojom::RegisterError::New(net::HTTP_OK, std::nullopt)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_OK, + mojom::RegisterServerErrorCode::kInvalidResponse))), }); return kRegisterInitializeSerializedResponseEmpty.get(); } @@ -572,9 +574,11 @@ RegisterInitializeVerificationTokenFailedToEncrypt() { "serialized_response"; return body; }()}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - std::nullopt, - mojom::RegisterErrorCode::kVerificationTokenEncryptionFailed)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewClientError( + mojom::RegisterClientError::New( + mojom::RegisterClientErrorCode:: + kVerificationTokenEncryptionFailed))), }); return kRegisterInitializeVerificationTokenFailedToEncrypt.get(); } @@ -617,9 +621,7 @@ TEST_P(BraveAccountServiceRegisterInitializeTest, INSTANTIATE_TEST_SUITE_P( BraveAccountServiceTests, BraveAccountServiceRegisterInitializeTest, - testing::Values(RegisterInitializeEmailEmpty(), - RegisterInitializeBlindedMessageEmpty(), - RegisterInitializeBodyMissingOrFailedToParse(), + testing::Values(RegisterInitializeBodyMissingOrFailedToParse(), RegisterInitializeErrorCodeIsNull(), RegisterInitializeNewAccountEmailRequired(), RegisterInitializeIntentNotAllowed(), @@ -662,35 +664,6 @@ struct RegisterFinalizeTestCase { namespace { -const RegisterFinalizeTestCase* -RegisterFinalizeEncryptedVerificationTokenEmpty() { - static const base::NoDestructor - kRegisterFinalizeEncryptedVerificationTokenEmpty({ - .test_name = "register_finalize_encrypted_verification_token_empty", - .encrypted_verification_token = "", - .serialized_record = {}, // not used - .fail_encryption = {}, // not used - .fail_decryption = {}, // not used - .endpoint_response = {}, // not used - .mojo_expected = base::unexpected(mojom::RegisterError::New()), - }); - return kRegisterFinalizeEncryptedVerificationTokenEmpty.get(); -} - -const RegisterFinalizeTestCase* RegisterFinalizeSerializedRecordEmpty() { - static const base::NoDestructor - kRegisterFinalizeSerializedRecordEmpty({ - .test_name = "register_finalize_serialized_record_empty", - .encrypted_verification_token = EncryptedVerificationToken(), - .serialized_record = "", - .fail_encryption = {}, // not used - .fail_decryption = {}, // not used - .endpoint_response = {}, // not used - .mojo_expected = base::unexpected(mojom::RegisterError::New()), - }); - return kRegisterFinalizeSerializedRecordEmpty.get(); -} - const RegisterFinalizeTestCase* RegisterFinalizeVerificationTokenFailedToDecrypt() { static const base::NoDestructor @@ -701,9 +674,11 @@ RegisterFinalizeVerificationTokenFailedToDecrypt() { .fail_encryption = {}, // not used .fail_decryption = true, .endpoint_response = {}, // not used - .mojo_expected = base::unexpected(mojom::RegisterError::New( - std::nullopt, - mojom::RegisterErrorCode::kVerificationTokenDecryptionFailed)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewClientError( + mojom::RegisterClientError::New( + mojom::RegisterClientErrorCode:: + kVerificationTokenDecryptionFailed))), }); return kRegisterFinalizeVerificationTokenFailedToDecrypt.get(); } @@ -719,8 +694,11 @@ const RegisterFinalizeTestCase* RegisterFinalizeBodyMissingOrFailedToParse() { .endpoint_response = {{.net_error = net::OK, .status_code = net::HTTP_INTERNAL_SERVER_ERROR, .body = std::nullopt}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - net::HTTP_INTERNAL_SERVER_ERROR, std::nullopt)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_INTERNAL_SERVER_ERROR, + mojom::RegisterServerErrorCode::kInvalidResponse))), }); return kRegisterFinalizeBodyMissingOrFailedToParse.get(); } @@ -740,8 +718,11 @@ const RegisterFinalizeTestCase* RegisterFinalizeErrorCodeIsNull() { body.code = base::Value(); return body; }())}}, - .mojo_expected = base::unexpected( - mojom::RegisterError::New(net::HTTP_NOT_FOUND, std::nullopt)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_NOT_FOUND, + mojom::RegisterServerErrorCode::kNull))), }); return kRegisterFinalizeErrorCodeIsNull.get(); } @@ -761,9 +742,11 @@ const RegisterFinalizeTestCase* RegisterFinalizeInterimPasswordStateNotFound() { body.code = base::Value(14001); return body; }())}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - net::HTTP_NOT_FOUND, - mojom::RegisterErrorCode::kInterimPasswordStateNotFound)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_NOT_FOUND, mojom::RegisterServerErrorCode:: + kInterimPasswordStateNotFound))), }); return kRegisterFinalizeInterimPasswordStateNotFound.get(); } @@ -783,9 +766,12 @@ const RegisterFinalizeTestCase* RegisterFinalizeInterimPasswordStateExpired() { body.code = base::Value(14002); return body; }())}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - net::HTTP_BAD_REQUEST, - mojom::RegisterErrorCode::kInterimPasswordStateExpired)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_BAD_REQUEST, + mojom::RegisterServerErrorCode:: + kInterimPasswordStateExpired))), }); return kRegisterFinalizeInterimPasswordStateExpired.get(); } @@ -805,9 +791,11 @@ const RegisterFinalizeTestCase* RegisterFinalizeUnauthorized() { body.code = base::Value(0); return body; }())}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - net::HTTP_UNAUTHORIZED, - mojom::RegisterErrorCode::kMiscServerError)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_UNAUTHORIZED, + mojom::RegisterServerErrorCode::kNull))), }); return kRegisterFinalizeUnauthorized.get(); } @@ -827,8 +815,11 @@ const RegisterFinalizeTestCase* RegisterFinalizeForbidden() { body.code = base::Value(0); return body; }())}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - net::HTTP_FORBIDDEN, mojom::RegisterErrorCode::kMiscServerError)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_FORBIDDEN, + mojom::RegisterServerErrorCode::kNull))), }); return kRegisterFinalizeForbidden.get(); } @@ -848,9 +839,11 @@ const RegisterFinalizeTestCase* RegisterFinalizeServerError() { body.code = base::Value(0); return body; }())}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - net::HTTP_INTERNAL_SERVER_ERROR, - mojom::RegisterErrorCode::kMiscServerError)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_INTERNAL_SERVER_ERROR, + mojom::RegisterServerErrorCode::kNull))), }); return kRegisterFinalizeServerError.get(); } @@ -870,8 +863,11 @@ const RegisterFinalizeTestCase* RegisterFinalizeUnknown() { body.code = base::Value(42); return body; }())}}, - .mojo_expected = base::unexpected( - mojom::RegisterError::New(net::HTTP_TOO_EARLY, std::nullopt)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_TOO_EARLY, + mojom::RegisterServerErrorCode::kUnknown))), }); return kRegisterFinalizeUnknown.get(); } @@ -912,9 +908,7 @@ TEST_P(BraveAccountServiceRegisterFinalizeTest, INSTANTIATE_TEST_SUITE_P( BraveAccountServiceTests, BraveAccountServiceRegisterFinalizeTest, - testing::Values(RegisterFinalizeEncryptedVerificationTokenEmpty(), - RegisterFinalizeSerializedRecordEmpty(), - RegisterFinalizeVerificationTokenFailedToDecrypt(), + testing::Values(RegisterFinalizeVerificationTokenFailedToDecrypt(), RegisterFinalizeBodyMissingOrFailedToParse(), RegisterFinalizeErrorCodeIsNull(), RegisterFinalizeInterimPasswordStateNotFound(), @@ -987,20 +981,6 @@ struct RegisterVerifyTestCase { namespace { -const RegisterVerifyTestCase* RegisterVerifyCodeEmpty() { - static const base::NoDestructor - kRegisterVerifyCodeEmpty({ - .test_name = "register_verify_code_empty", - .code = "", - .encrypted_verification_token = {}, // not used - .fail_decryption = {}, // not used - .fail_encryption = {}, // not used - .endpoint_response = {}, // not used - .mojo_expected = base::unexpected(mojom::RegisterError::New()), - }); - return kRegisterVerifyCodeEmpty.get(); -} - const RegisterVerifyTestCase* RegisterVerifyVerificationTokenEmpty() { static const base::NoDestructor kRegisterVerifyVerificationTokenEmpty({ @@ -1010,9 +990,11 @@ const RegisterVerifyTestCase* RegisterVerifyVerificationTokenEmpty() { .fail_decryption = {}, // not used .fail_encryption = {}, // not used .endpoint_response = {}, // not used - .mojo_expected = base::unexpected(mojom::RegisterError::New( - std::nullopt, - mojom::RegisterErrorCode::kUserNotInTheVerificationState)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewClientError( + mojom::RegisterClientError::New( + mojom::RegisterClientErrorCode:: + kUserNotInTheVerificationState))), }); return kRegisterVerifyVerificationTokenEmpty.get(); } @@ -1026,9 +1008,11 @@ const RegisterVerifyTestCase* RegisterVerifyVerificationTokenFailedToDecrypt() { .fail_decryption = true, .fail_encryption = {}, // not used .endpoint_response = {}, // not used - .mojo_expected = base::unexpected(mojom::RegisterError::New( - std::nullopt, - mojom::RegisterErrorCode::kVerificationTokenDecryptionFailed)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewClientError( + mojom::RegisterClientError::New( + mojom::RegisterClientErrorCode:: + kVerificationTokenDecryptionFailed))), }); return kRegisterVerifyVerificationTokenFailedToDecrypt.get(); } @@ -1044,8 +1028,11 @@ const RegisterVerifyTestCase* RegisterVerifyNetworkError() { .endpoint_response = {{.net_error = net::ERR_CONNECTION_REFUSED, .status_code = std::nullopt, .body = std::nullopt}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - net::ERR_CONNECTION_REFUSED, std::nullopt)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::ERR_CONNECTION_REFUSED, + mojom::RegisterServerErrorCode::kInvalidResponse))), }); return kRegisterVerifyNetworkError.get(); } @@ -1061,8 +1048,11 @@ const RegisterVerifyTestCase* RegisterVerifyBodyMissingOrFailedToParse() { .endpoint_response = {{.net_error = net::OK, .status_code = net::HTTP_INTERNAL_SERVER_ERROR, .body = std::nullopt}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - net::HTTP_INTERNAL_SERVER_ERROR, std::nullopt)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_INTERNAL_SERVER_ERROR, + mojom::RegisterServerErrorCode::kInvalidResponse))), }); return kRegisterVerifyBodyMissingOrFailedToParse.get(); } @@ -1082,8 +1072,11 @@ const RegisterVerifyTestCase* RegisterVerifyErrorCodeIsNull() { body.code = base::Value(); return body; }())}}, - .mojo_expected = base::unexpected( - mojom::RegisterError::New(net::HTTP_UNAUTHORIZED, std::nullopt)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_UNAUTHORIZED, + mojom::RegisterServerErrorCode::kNull))), }); return kRegisterVerifyErrorCodeIsNull.get(); } @@ -1105,9 +1098,12 @@ RegisterVerifyVerificationNotFoundOrInvalidIdOrCode() { body.code = base::Value(13002); return body; }())}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - net::HTTP_NOT_FOUND, mojom::RegisterErrorCode:: - kVerificationNotFoundOrInvalidIdOrCode)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_NOT_FOUND, + mojom::RegisterServerErrorCode:: + kVerificationNotFoundOrInvalidIdOrCode))), }); return kRegisterVerifyVerificationNotFoundOrInvalidIdOrCode.get(); } @@ -1127,9 +1123,11 @@ const RegisterVerifyTestCase* RegisterVerifyEmailAlreadyVerified() { body.code = base::Value(13009); return body; }())}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - net::HTTP_BAD_REQUEST, - mojom::RegisterErrorCode::kEmailAlreadyVerified)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_BAD_REQUEST, + mojom::RegisterServerErrorCode::kEmailAlreadyVerified))), }); return kRegisterVerifyEmailAlreadyVerified.get(); } @@ -1151,10 +1149,12 @@ RegisterVerifyMaximumCodeVerificationAttemptsExceeded() { body.code = base::Value(13010); return body; }())}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - net::HTTP_BAD_REQUEST, - mojom::RegisterErrorCode:: - kMaximumCodeVerificationAttemptsExceeded)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_BAD_REQUEST, + mojom::RegisterServerErrorCode:: + kMaximumCodeVerificationAttemptsExceeded))), }); return kRegisterVerifyMaximumCodeVerificationAttemptsExceeded.get(); } @@ -1174,9 +1174,11 @@ const RegisterVerifyTestCase* RegisterVerifyInvalidVerificationCode() { body.code = base::Value(13011); return body; }())}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - net::HTTP_BAD_REQUEST, - mojom::RegisterErrorCode::kInvalidVerificationCode)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_BAD_REQUEST, mojom::RegisterServerErrorCode:: + kInvalidVerificationCode))), }); return kRegisterVerifyInvalidVerificationCode.get(); } @@ -1196,8 +1198,11 @@ const RegisterVerifyTestCase* RegisterVerifyServerError() { body.code = base::Value(); return body; }())}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - net::HTTP_INTERNAL_SERVER_ERROR, std::nullopt)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_INTERNAL_SERVER_ERROR, + mojom::RegisterServerErrorCode::kNull))), }); return kRegisterVerifyServerError.get(); } @@ -1220,8 +1225,11 @@ const RegisterVerifyTestCase* RegisterVerifyAuthTokenEmpty() { body.email = kEmailAddress; return body; }()}}, - .mojo_expected = base::unexpected( - mojom::RegisterError::New(net::HTTP_OK, std::nullopt)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_OK, + mojom::RegisterServerErrorCode::kInvalidResponse))), }); return kRegisterVerifyAuthTokenEmpty.get(); } @@ -1244,8 +1252,11 @@ const RegisterVerifyTestCase* RegisterVerifyEmailEmpty() { body.email = ""; return body; }()}}, - .mojo_expected = base::unexpected( - mojom::RegisterError::New(net::HTTP_OK, std::nullopt)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewServerError( + mojom::RegisterServerError::New( + net::HTTP_OK, + mojom::RegisterServerErrorCode::kInvalidResponse))), }); return kRegisterVerifyEmailEmpty.get(); } @@ -1269,9 +1280,11 @@ RegisterVerifyAuthenticationTokenEncryptionFailed() { body.email = kEmailAddress; return body; }()}}, - .mojo_expected = base::unexpected(mojom::RegisterError::New( - std::nullopt, - mojom::RegisterErrorCode::kAuthenticationTokenEncryptionFailed)), + .mojo_expected = + base::unexpected(mojom::RegisterError::NewClientError( + mojom::RegisterClientError::New( + mojom::RegisterClientErrorCode:: + kAuthenticationTokenEncryptionFailed))), }); return kRegisterVerifyAuthenticationTokenEncryptionFailed.get(); } @@ -1312,8 +1325,7 @@ TEST_P(BraveAccountServiceRegisterVerifyTest, INSTANTIATE_TEST_SUITE_P( BraveAccountServiceTests, BraveAccountServiceRegisterVerifyTest, - testing::Values(RegisterVerifyCodeEmpty(), - RegisterVerifyVerificationTokenEmpty(), + testing::Values(RegisterVerifyVerificationTokenEmpty(), RegisterVerifyVerificationTokenFailedToDecrypt(), RegisterVerifyNetworkError(), RegisterVerifyBodyMissingOrFailedToParse(), @@ -1365,10 +1377,11 @@ ResendConfirmationEmailVerificationTokenEmpty() { .encrypted_verification_token = "", .fail_decryption = {}, // not used .endpoint_response = {}, // not used - .mojo_expected = - base::unexpected(mojom::ResendConfirmationEmailError::New( - std::nullopt, mojom::ResendConfirmationEmailErrorCode:: - kUserNotInTheVerificationState)), + .mojo_expected = base::unexpected( + mojom::ResendConfirmationEmailError::NewClientError( + mojom::ResendConfirmationEmailClientError::New( + mojom::ResendConfirmationEmailClientErrorCode:: + kUserNotInTheVerificationState))), }); return kResendConfirmationEmailVerificationTokenEmpty.get(); } @@ -1382,10 +1395,11 @@ ResendConfirmationEmailVerificationTokenFailedToDecrypt() { .encrypted_verification_token = EncryptedVerificationToken(), .fail_decryption = true, .endpoint_response = {}, // not used - .mojo_expected = - base::unexpected(mojom::ResendConfirmationEmailError::New( - std::nullopt, mojom::ResendConfirmationEmailErrorCode:: - kVerificationTokenDecryptionFailed)), + .mojo_expected = base::unexpected( + mojom::ResendConfirmationEmailError::NewClientError( + mojom::ResendConfirmationEmailClientError::New( + mojom::ResendConfirmationEmailClientErrorCode:: + kVerificationTokenDecryptionFailed))), }); return kResendConfirmationEmailVerificationTokenFailedToDecrypt.get(); } @@ -1413,9 +1427,12 @@ const ResendConfirmationEmailTestCase* ResendConfirmationEmailNetworkError() { .endpoint_response = {{.net_error = net::ERR_CONNECTION_REFUSED, .status_code = std::nullopt, .body = std::nullopt}}, - .mojo_expected = - base::unexpected(mojom::ResendConfirmationEmailError::New( - net::ERR_CONNECTION_REFUSED, std::nullopt)), + .mojo_expected = base::unexpected( + mojom::ResendConfirmationEmailError::NewServerError( + mojom::ResendConfirmationEmailServerError::New( + net::ERR_CONNECTION_REFUSED, + mojom::ResendConfirmationEmailServerErrorCode:: + kInvalidResponse))), }); return kResendConfirmationEmailNetworkError.get(); } @@ -1431,9 +1448,12 @@ ResendConfirmationEmailBodyMissingOrFailedToParse() { .endpoint_response = {{.net_error = net::OK, .status_code = net::HTTP_INTERNAL_SERVER_ERROR, .body = std::nullopt}}, - .mojo_expected = - base::unexpected(mojom::ResendConfirmationEmailError::New( - net::HTTP_INTERNAL_SERVER_ERROR, std::nullopt)), + .mojo_expected = base::unexpected( + mojom::ResendConfirmationEmailError::NewServerError( + mojom::ResendConfirmationEmailServerError::New( + net::HTTP_INTERNAL_SERVER_ERROR, + mojom::ResendConfirmationEmailServerErrorCode:: + kInvalidResponse))), }); return kResendConfirmationEmailBodyMissingOrFailedToParse.get(); } @@ -1453,9 +1473,11 @@ ResendConfirmationEmailBadRequestWithNullErrorCode() { body.code = base::Value(); return body; }())}}, - .mojo_expected = - base::unexpected(mojom::ResendConfirmationEmailError::New( - net::HTTP_BAD_REQUEST, std::nullopt)), + .mojo_expected = base::unexpected( + mojom::ResendConfirmationEmailError::NewServerError( + mojom::ResendConfirmationEmailServerError::New( + net::HTTP_BAD_REQUEST, + mojom::ResendConfirmationEmailServerErrorCode::kNull))), }); return kResendConfirmationEmailBadRequestWithNullErrorCode.get(); } @@ -1475,11 +1497,12 @@ ResendConfirmationEmailMaximumEmailSendAttemptsExceeded() { body.code = base::Value(13008); return body; }())}}, - .mojo_expected = - base::unexpected(mojom::ResendConfirmationEmailError::New( - net::HTTP_BAD_REQUEST, - mojom::ResendConfirmationEmailErrorCode:: - kMaximumEmailSendAttemptsExceeded)), + .mojo_expected = base::unexpected( + mojom::ResendConfirmationEmailError::NewServerError( + mojom::ResendConfirmationEmailServerError::New( + net::HTTP_BAD_REQUEST, + mojom::ResendConfirmationEmailServerErrorCode:: + kMaximumEmailSendAttemptsExceeded))), }); return kResendConfirmationEmailMaximumEmailSendAttemptsExceeded.get(); } @@ -1498,11 +1521,12 @@ ResendConfirmationEmailEmailAlreadyVerified() { body.code = base::Value(13009); return body; }())}}, - .mojo_expected = - base::unexpected(mojom::ResendConfirmationEmailError::New( - net::HTTP_BAD_REQUEST, - mojom::ResendConfirmationEmailErrorCode:: - kEmailAlreadyVerified)), + .mojo_expected = base::unexpected( + mojom::ResendConfirmationEmailError::NewServerError( + mojom::ResendConfirmationEmailServerError::New( + net::HTTP_BAD_REQUEST, + mojom::ResendConfirmationEmailServerErrorCode:: + kEmailAlreadyVerified))), }); return kResendConfirmationEmailEmailAlreadyVerified.get(); } @@ -1520,9 +1544,11 @@ const ResendConfirmationEmailTestCase* ResendConfirmationEmailServerError() { body.code = base::Value(); return body; }())}}, - .mojo_expected = - base::unexpected(mojom::ResendConfirmationEmailError::New( - net::HTTP_INTERNAL_SERVER_ERROR, std::nullopt)), + .mojo_expected = base::unexpected( + mojom::ResendConfirmationEmailError::NewServerError( + mojom::ResendConfirmationEmailServerError::New( + net::HTTP_INTERNAL_SERVER_ERROR, + mojom::ResendConfirmationEmailServerErrorCode::kNull))), }); return kResendConfirmationEmailServerError.get(); } @@ -1540,9 +1566,12 @@ const ResendConfirmationEmailTestCase* ResendConfirmationEmailUnknown() { body.code = base::Value(42); return body; }())}}, - .mojo_expected = - base::unexpected(mojom::ResendConfirmationEmailError::New( - net::HTTP_TOO_EARLY, std::nullopt)), + .mojo_expected = base::unexpected( + mojom::ResendConfirmationEmailError::NewServerError( + mojom::ResendConfirmationEmailServerError::New( + net::HTTP_TOO_EARLY, + mojom::ResendConfirmationEmailServerErrorCode:: + kUnknown))), }); return kResendConfirmationEmailUnknown.get(); } @@ -1904,7 +1933,7 @@ struct LoginInitializeTestCase { base::test::TaskEnvironment& task_environment, mojom::Authentication& authentication, base::OnceCallback callback) { - authentication.LoginInitialize(std::nullopt, test_case.email, + authentication.LoginInitialize(mojom::Service::kAccounts, test_case.email, test_case.serialized_ke1, std::move(callback)); } @@ -1920,34 +1949,6 @@ struct LoginInitializeTestCase { namespace { -const LoginInitializeTestCase* LoginInitializeEmailEmpty() { - static const base::NoDestructor - kLoginInitializeEmailEmpty({ - .test_name = "login_initialize_email_empty", - .email = "", - .serialized_ke1 = {}, // not used - .fail_encryption = {}, // not used - .fail_decryption = {}, // not used - .endpoint_response = {}, // not used - .mojo_expected = base::unexpected(mojom::LoginError::New()), - }); - return kLoginInitializeEmailEmpty.get(); -} - -const LoginInitializeTestCase* LoginInitializeSerializedKe1Empty() { - static const base::NoDestructor - kLoginInitializeSerializedKe1Empty({ - .test_name = "login_initialize_serialized_ke1_empty", - .email = kEmailAddress, - .serialized_ke1 = "", - .fail_encryption = {}, // not used - .fail_decryption = {}, // not used - .endpoint_response = {}, // not used - .mojo_expected = base::unexpected(mojom::LoginError::New()), - }); - return kLoginInitializeSerializedKe1Empty.get(); -} - const LoginInitializeTestCase* LoginInitializeBodyMissingOrFailedToParse() { static const base::NoDestructor kLoginInitializeBodyMissingOrFailedToParse({ @@ -1959,8 +1960,10 @@ const LoginInitializeTestCase* LoginInitializeBodyMissingOrFailedToParse() { .endpoint_response = {{.net_error = net::OK, .status_code = net::HTTP_INTERNAL_SERVER_ERROR, .body = std::nullopt}}, - .mojo_expected = base::unexpected(mojom::LoginError::New( - net::HTTP_INTERNAL_SERVER_ERROR, std::nullopt)), + .mojo_expected = base::unexpected( + mojom::LoginError::NewServerError(mojom::LoginServerError::New( + net::HTTP_INTERNAL_SERVER_ERROR, + mojom::LoginServerErrorCode::kInvalidResponse))), }); return kLoginInitializeBodyMissingOrFailedToParse.get(); } @@ -1981,7 +1984,8 @@ const LoginInitializeTestCase* LoginInitializeErrorCodeIsNull() { return body; }())}}, .mojo_expected = base::unexpected( - mojom::LoginError::New(net::HTTP_BAD_REQUEST, std::nullopt)), + mojom::LoginError::NewServerError(mojom::LoginServerError::New( + net::HTTP_BAD_REQUEST, mojom::LoginServerErrorCode::kNull))), }); return kLoginInitializeErrorCodeIsNull.get(); } @@ -2002,8 +2006,9 @@ const LoginInitializeTestCase* LoginInitializeEmailNotVerified() { return body; }())}}, .mojo_expected = base::unexpected( - mojom::LoginError::New(net::HTTP_UNAUTHORIZED, - mojom::LoginErrorCode::kEmailNotVerified)), + mojom::LoginError::NewServerError(mojom::LoginServerError::New( + net::HTTP_UNAUTHORIZED, + mojom::LoginServerErrorCode::kEmailNotVerified))), }); return kLoginInitializeEmailNotVerified.get(); } @@ -2023,9 +2028,10 @@ const LoginInitializeTestCase* LoginInitializeIncorrectCredentials() { body.code = base::Value(14004); return body; }())}}, - .mojo_expected = base::unexpected(mojom::LoginError::New( - net::HTTP_UNAUTHORIZED, - mojom::LoginErrorCode::kIncorrectCredentials)), + .mojo_expected = base::unexpected( + mojom::LoginError::NewServerError(mojom::LoginServerError::New( + net::HTTP_UNAUTHORIZED, + mojom::LoginServerErrorCode::kIncorrectCredentials))), }); return kLoginInitializeIncorrectCredentials.get(); } @@ -2045,8 +2051,10 @@ const LoginInitializeTestCase* LoginInitializeIncorrectEmail() { body.code = base::Value(14005); return body; }())}}, - .mojo_expected = base::unexpected(mojom::LoginError::New( - net::HTTP_UNAUTHORIZED, mojom::LoginErrorCode::kIncorrectEmail)), + .mojo_expected = base::unexpected( + mojom::LoginError::NewServerError(mojom::LoginServerError::New( + net::HTTP_UNAUTHORIZED, + mojom::LoginServerErrorCode::kIncorrectEmail))), }); return kLoginInitializeIncorrectEmail.get(); } @@ -2066,9 +2074,10 @@ const LoginInitializeTestCase* LoginInitializeIncorrectPassword() { body.code = base::Value(14006); return body; }())}}, - .mojo_expected = base::unexpected(mojom::LoginError::New( - net::HTTP_UNAUTHORIZED, - mojom::LoginErrorCode::kIncorrectPassword)), + .mojo_expected = base::unexpected( + mojom::LoginError::NewServerError(mojom::LoginServerError::New( + net::HTTP_UNAUTHORIZED, + mojom::LoginServerErrorCode::kIncorrectPassword))), }); return kLoginInitializeIncorrectPassword.get(); } @@ -2089,8 +2098,9 @@ const LoginInitializeTestCase* LoginInitializeServerError() { return body; }())}}, .mojo_expected = base::unexpected( - mojom::LoginError::New(net::HTTP_INTERNAL_SERVER_ERROR, - mojom::LoginErrorCode::kMiscServerError)), + mojom::LoginError::NewServerError(mojom::LoginServerError::New( + net::HTTP_INTERNAL_SERVER_ERROR, + mojom::LoginServerErrorCode::kNull))), }); return kLoginInitializeServerError.get(); } @@ -2111,7 +2121,8 @@ const LoginInitializeTestCase* LoginInitializeUnknown() { return body; }())}}, .mojo_expected = base::unexpected( - mojom::LoginError::New(net::HTTP_TOO_EARLY, std::nullopt)), + mojom::LoginError::NewServerError(mojom::LoginServerError::New( + net::HTTP_TOO_EARLY, mojom::LoginServerErrorCode::kUnknown))), }); return kLoginInitializeUnknown.get(); } @@ -2134,7 +2145,9 @@ const LoginInitializeTestCase* LoginInitializeLoginTokenEmpty() { return body; }()}}, .mojo_expected = base::unexpected( - mojom::LoginError::New(net::HTTP_OK, std::nullopt)), + mojom::LoginError::NewServerError(mojom::LoginServerError::New( + net::HTTP_OK, + mojom::LoginServerErrorCode::kInvalidResponse))), }); return kLoginInitializeLoginTokenEmpty.get(); } @@ -2157,7 +2170,9 @@ const LoginInitializeTestCase* LoginInitializeSerializedKe2Empty() { return body; }()}}, .mojo_expected = base::unexpected( - mojom::LoginError::New(net::HTTP_OK, std::nullopt)), + mojom::LoginError::NewServerError(mojom::LoginServerError::New( + net::HTTP_OK, + mojom::LoginServerErrorCode::kInvalidResponse))), }); return kLoginInitializeSerializedKe2Empty.get(); } @@ -2179,9 +2194,9 @@ const LoginInitializeTestCase* LoginInitializeLoginTokenFailedToEncrypt() { body.serialized_ke2 = "serialized_ke2"; return body; }()}}, - .mojo_expected = base::unexpected(mojom::LoginError::New( - std::nullopt, - mojom::LoginErrorCode::kLoginTokenEncryptionFailed)), + .mojo_expected = base::unexpected( + mojom::LoginError::NewClientError(mojom::LoginClientError::New( + mojom::LoginClientErrorCode::kLoginTokenEncryptionFailed))), }); return kLoginInitializeLoginTokenFailedToEncrypt.get(); } @@ -2222,9 +2237,7 @@ TEST_P(BraveAccountServiceLoginInitializeTest, INSTANTIATE_TEST_SUITE_P( BraveAccountServiceTests, BraveAccountServiceLoginInitializeTest, - testing::Values(LoginInitializeEmailEmpty(), - LoginInitializeSerializedKe1Empty(), - LoginInitializeBodyMissingOrFailedToParse(), + testing::Values(LoginInitializeBodyMissingOrFailedToParse(), LoginInitializeErrorCodeIsNull(), LoginInitializeEmailNotVerified(), LoginInitializeIncorrectCredentials(), @@ -2278,38 +2291,6 @@ struct LoginFinalizeTestCase { namespace { -const LoginFinalizeTestCase* LoginFinalizeEncryptedLoginTokenEmpty() { - static const base::NoDestructor - kLoginFinalizeEncryptedLoginTokenEmpty({ - .test_name = "login_finalize_encrypted_login_token_empty", - .encrypted_login_token = "", - .client_mac = {}, // not used - .fail_encryption = {}, // not used - .fail_decryption = {}, // not used - .endpoint_response = {}, // not used - .expected_email = "", - .expected_authentication_token = "", - .mojo_expected = base::unexpected(mojom::LoginError::New()), - }); - return kLoginFinalizeEncryptedLoginTokenEmpty.get(); -} - -const LoginFinalizeTestCase* LoginFinalizeClientMacEmpty() { - static const base::NoDestructor - kLoginFinalizeClientMacEmpty({ - .test_name = "login_finalize_client_mac_empty", - .encrypted_login_token = EncryptedLoginToken(), - .client_mac = "", - .fail_encryption = {}, // not used - .fail_decryption = {}, // not used - .endpoint_response = {}, // not used - .expected_email = "", - .expected_authentication_token = "", - .mojo_expected = base::unexpected(mojom::LoginError::New()), - }); - return kLoginFinalizeClientMacEmpty.get(); -} - const LoginFinalizeTestCase* LoginFinalizeLoginTokenFailedToDecrypt() { static const base::NoDestructor kLoginFinalizeLoginTokenFailedToDecrypt({ @@ -2321,9 +2302,9 @@ const LoginFinalizeTestCase* LoginFinalizeLoginTokenFailedToDecrypt() { .endpoint_response = {}, // not used .expected_email = "", .expected_authentication_token = "", - .mojo_expected = base::unexpected(mojom::LoginError::New( - std::nullopt, - mojom::LoginErrorCode::kLoginTokenDecryptionFailed)), + .mojo_expected = base::unexpected( + mojom::LoginError::NewClientError(mojom::LoginClientError::New( + mojom::LoginClientErrorCode::kLoginTokenDecryptionFailed))), }); return kLoginFinalizeLoginTokenFailedToDecrypt.get(); } @@ -2341,8 +2322,10 @@ const LoginFinalizeTestCase* LoginFinalizeBodyMissingOrFailedToParse() { .body = std::nullopt}}, .expected_email = "", .expected_authentication_token = "", - .mojo_expected = base::unexpected(mojom::LoginError::New( - net::HTTP_INTERNAL_SERVER_ERROR, std::nullopt)), + .mojo_expected = base::unexpected( + mojom::LoginError::NewServerError(mojom::LoginServerError::New( + net::HTTP_INTERNAL_SERVER_ERROR, + mojom::LoginServerErrorCode::kInvalidResponse))), }); return kLoginFinalizeBodyMissingOrFailedToParse.get(); } @@ -2365,7 +2348,8 @@ const LoginFinalizeTestCase* LoginFinalizeErrorCodeIsNull() { .expected_email = "", .expected_authentication_token = "", .mojo_expected = base::unexpected( - mojom::LoginError::New(net::HTTP_BAD_REQUEST, std::nullopt)), + mojom::LoginError::NewServerError(mojom::LoginServerError::New( + net::HTTP_BAD_REQUEST, mojom::LoginServerErrorCode::kNull))), }); return kLoginFinalizeErrorCodeIsNull.get(); } @@ -2387,9 +2371,10 @@ const LoginFinalizeTestCase* LoginFinalizeInterimPasswordStateMismatch() { }())}}, .expected_email = "", .expected_authentication_token = "", - .mojo_expected = base::unexpected(mojom::LoginError::New( - net::HTTP_BAD_REQUEST, - mojom::LoginErrorCode::kInterimPasswordStateMismatch)), + .mojo_expected = base::unexpected( + mojom::LoginError::NewServerError(mojom::LoginServerError::New( + net::HTTP_BAD_REQUEST, + mojom::LoginServerErrorCode::kInterimPasswordStateMismatch))), }); return kLoginFinalizeInterimPasswordStateMismatch.get(); } @@ -2411,9 +2396,10 @@ const LoginFinalizeTestCase* LoginFinalizeInterimPasswordStateNotFound() { }())}}, .expected_email = "", .expected_authentication_token = "", - .mojo_expected = base::unexpected(mojom::LoginError::New( - net::HTTP_UNAUTHORIZED, - mojom::LoginErrorCode::kInterimPasswordStateNotFound)), + .mojo_expected = base::unexpected( + mojom::LoginError::NewServerError(mojom::LoginServerError::New( + net::HTTP_UNAUTHORIZED, + mojom::LoginServerErrorCode::kInterimPasswordStateNotFound))), }); return kLoginFinalizeInterimPasswordStateNotFound.get(); } @@ -2435,9 +2421,11 @@ const LoginFinalizeTestCase* LoginFinalizeInterimPasswordStateHasExpired() { }())}}, .expected_email = "", .expected_authentication_token = "", - .mojo_expected = base::unexpected(mojom::LoginError::New( - net::HTTP_UNAUTHORIZED, - mojom::LoginErrorCode::kInterimPasswordStateHasExpired)), + .mojo_expected = base::unexpected( + mojom::LoginError::NewServerError(mojom::LoginServerError::New( + net::HTTP_UNAUTHORIZED, + mojom::LoginServerErrorCode:: + kInterimPasswordStateHasExpired))), }); return kLoginFinalizeInterimPasswordStateHasExpired.get(); } @@ -2459,9 +2447,10 @@ const LoginFinalizeTestCase* LoginFinalizeIncorrectCredentials() { }())}}, .expected_email = "", .expected_authentication_token = "", - .mojo_expected = base::unexpected(mojom::LoginError::New( - net::HTTP_UNAUTHORIZED, - mojom::LoginErrorCode::kIncorrectCredentials)), + .mojo_expected = base::unexpected( + mojom::LoginError::NewServerError(mojom::LoginServerError::New( + net::HTTP_UNAUTHORIZED, + mojom::LoginServerErrorCode::kIncorrectCredentials))), }); return kLoginFinalizeIncorrectCredentials.get(); } @@ -2483,8 +2472,10 @@ const LoginFinalizeTestCase* LoginFinalizeIncorrectEmail() { }())}}, .expected_email = "", .expected_authentication_token = "", - .mojo_expected = base::unexpected(mojom::LoginError::New( - net::HTTP_UNAUTHORIZED, mojom::LoginErrorCode::kIncorrectEmail)), + .mojo_expected = base::unexpected( + mojom::LoginError::NewServerError(mojom::LoginServerError::New( + net::HTTP_UNAUTHORIZED, + mojom::LoginServerErrorCode::kIncorrectEmail))), }); return kLoginFinalizeIncorrectEmail.get(); } @@ -2506,9 +2497,10 @@ const LoginFinalizeTestCase* LoginFinalizeIncorrectPassword() { }())}}, .expected_email = "", .expected_authentication_token = "", - .mojo_expected = base::unexpected(mojom::LoginError::New( - net::HTTP_UNAUTHORIZED, - mojom::LoginErrorCode::kIncorrectPassword)), + .mojo_expected = base::unexpected( + mojom::LoginError::NewServerError(mojom::LoginServerError::New( + net::HTTP_UNAUTHORIZED, + mojom::LoginServerErrorCode::kIncorrectPassword))), }); return kLoginFinalizeIncorrectPassword.get(); } @@ -2531,8 +2523,9 @@ const LoginFinalizeTestCase* LoginFinalizeServerError() { .expected_email = "", .expected_authentication_token = "", .mojo_expected = base::unexpected( - mojom::LoginError::New(net::HTTP_INTERNAL_SERVER_ERROR, - mojom::LoginErrorCode::kMiscServerError)), + mojom::LoginError::NewServerError(mojom::LoginServerError::New( + net::HTTP_INTERNAL_SERVER_ERROR, + mojom::LoginServerErrorCode::kNull))), }); return kLoginFinalizeServerError.get(); } @@ -2554,7 +2547,8 @@ const LoginFinalizeTestCase* LoginFinalizeUnknown() { .expected_email = "", .expected_authentication_token = "", .mojo_expected = base::unexpected( - mojom::LoginError::New(net::HTTP_TOO_EARLY, std::nullopt)), + mojom::LoginError::NewServerError(mojom::LoginServerError::New( + net::HTTP_TOO_EARLY, mojom::LoginServerErrorCode::kUnknown))), }); return kLoginFinalizeUnknown.get(); } @@ -2580,7 +2574,9 @@ const LoginFinalizeTestCase* LoginFinalizeAuthTokenEmpty() { .expected_email = "", .expected_authentication_token = "", .mojo_expected = base::unexpected( - mojom::LoginError::New(net::HTTP_OK, std::nullopt)), + mojom::LoginError::NewServerError(mojom::LoginServerError::New( + net::HTTP_OK, + mojom::LoginServerErrorCode::kInvalidResponse))), }); return kLoginFinalizeAuthTokenEmpty.get(); } @@ -2606,7 +2602,9 @@ const LoginFinalizeTestCase* LoginFinalizeEmailEmpty() { .expected_email = "", .expected_authentication_token = "", .mojo_expected = base::unexpected( - mojom::LoginError::New(net::HTTP_OK, std::nullopt)), + mojom::LoginError::NewServerError(mojom::LoginServerError::New( + net::HTTP_OK, + mojom::LoginServerErrorCode::kInvalidResponse))), }); return kLoginFinalizeEmailEmpty.get(); } @@ -2631,9 +2629,10 @@ const LoginFinalizeTestCase* LoginFinalizeAuthenticationTokenFailedToEncrypt() { }()}}, .expected_email = "", .expected_authentication_token = "", - .mojo_expected = base::unexpected(mojom::LoginError::New( - std::nullopt, - mojom::LoginErrorCode::kAuthenticationTokenEncryptionFailed)), + .mojo_expected = base::unexpected( + mojom::LoginError::NewClientError(mojom::LoginClientError::New( + mojom::LoginClientErrorCode:: + kAuthenticationTokenEncryptionFailed))), }); return kLoginFinalizeAuthenticationTokenFailedToEncrypt.get(); } @@ -2674,9 +2673,7 @@ TEST_P(BraveAccountServiceLoginFinalizeTest, INSTANTIATE_TEST_SUITE_P( BraveAccountServiceTests, BraveAccountServiceLoginFinalizeTest, - testing::Values(LoginFinalizeEncryptedLoginTokenEmpty(), - LoginFinalizeClientMacEmpty(), - LoginFinalizeLoginTokenFailedToDecrypt(), + testing::Values(LoginFinalizeLoginTokenFailedToDecrypt(), LoginFinalizeBodyMissingOrFailedToParse(), LoginFinalizeErrorCodeIsNull(), LoginFinalizeInterimPasswordStateMismatch(), @@ -2805,8 +2802,11 @@ const GetServiceTokenTestCase* GetServiceTokenUserNotLoggedIn() { .fail_encryption = {}, // not used .time_advance = {}, // not used .endpoint_response = {}, // not used - .mojo_expected = base::unexpected(mojom::GetServiceTokenError::New( - std::nullopt, mojom::GetServiceTokenErrorCode::kUserNotLoggedIn)), + .mojo_expected = + base::unexpected(mojom::GetServiceTokenError::NewClientError( + mojom::GetServiceTokenClientError::New( + mojom::GetServiceTokenClientErrorCode:: + kUserNotLoggedIn))), }); return kGetServiceTokenUserNotLoggedIn.get(); } @@ -2825,9 +2825,11 @@ GetServiceTokenAuthenticationTokenDecryptionFailed() { .fail_encryption = {}, // not used .time_advance = {}, // not used .endpoint_response = {}, // not used - .mojo_expected = base::unexpected(mojom::GetServiceTokenError::New( - std::nullopt, mojom::GetServiceTokenErrorCode:: - kAuthenticationTokenDecryptionFailed)), + .mojo_expected = + base::unexpected(mojom::GetServiceTokenError::NewClientError( + mojom::GetServiceTokenClientError::New( + mojom::GetServiceTokenClientErrorCode:: + kAuthenticationTokenDecryptionFailed))), }); return kGetServiceTokenAuthenticationTokenDecryptionFailed.get(); } @@ -2852,9 +2854,11 @@ const GetServiceTokenTestCase* GetServiceTokenAuthenticationSessionChanged() { "fetched_service_token"; return body; }()}}, - .mojo_expected = base::unexpected(mojom::GetServiceTokenError::New( - std::nullopt, - mojom::GetServiceTokenErrorCode::kAuthenticationSessionChanged)), + .mojo_expected = + base::unexpected(mojom::GetServiceTokenError::NewClientError( + mojom::GetServiceTokenClientError::New( + mojom::GetServiceTokenClientErrorCode:: + kAuthenticationSessionChanged))), }); return kGetServiceTokenAuthenticationSessionChanged.get(); } @@ -2873,8 +2877,12 @@ const GetServiceTokenTestCase* GetServiceTokenNetworkError() { .endpoint_response = {{.net_error = net::ERR_CONNECTION_REFUSED, .status_code = std::nullopt, .body = std::nullopt}}, - .mojo_expected = base::unexpected(mojom::GetServiceTokenError::New( - net::ERR_CONNECTION_REFUSED, std::nullopt)), + .mojo_expected = + base::unexpected(mojom::GetServiceTokenError::NewServerError( + mojom::GetServiceTokenServerError::New( + net::ERR_CONNECTION_REFUSED, + mojom::GetServiceTokenServerErrorCode:: + kInvalidResponse))), }); return kGetServiceTokenNetworkError.get(); } @@ -2893,8 +2901,12 @@ const GetServiceTokenTestCase* GetServiceTokenBodyMissingOrFailedToParse() { .endpoint_response = {{.net_error = net::OK, .status_code = net::HTTP_INTERNAL_SERVER_ERROR, .body = std::nullopt}}, - .mojo_expected = base::unexpected(mojom::GetServiceTokenError::New( - net::HTTP_INTERNAL_SERVER_ERROR, std::nullopt)), + .mojo_expected = + base::unexpected(mojom::GetServiceTokenError::NewServerError( + mojom::GetServiceTokenServerError::New( + net::HTTP_INTERNAL_SERVER_ERROR, + mojom::GetServiceTokenServerErrorCode:: + kInvalidResponse))), }); return kGetServiceTokenBodyMissingOrFailedToParse.get(); } @@ -2917,8 +2929,11 @@ const GetServiceTokenTestCase* GetServiceTokenErrorCodeIsNull() { body.code = base::Value(); return body; }())}}, - .mojo_expected = base::unexpected(mojom::GetServiceTokenError::New( - net::HTTP_BAD_REQUEST, std::nullopt)), + .mojo_expected = + base::unexpected(mojom::GetServiceTokenError::NewServerError( + mojom::GetServiceTokenServerError::New( + net::HTTP_BAD_REQUEST, + mojom::GetServiceTokenServerErrorCode::kNull))), }); return kGetServiceTokenErrorCodeIsNull.get(); } @@ -2941,9 +2956,12 @@ const GetServiceTokenTestCase* GetServiceTokenEmailDomainNotSupported() { body.code = base::Value(13006); return body; }())}}, - .mojo_expected = base::unexpected(mojom::GetServiceTokenError::New( - net::HTTP_BAD_REQUEST, - mojom::GetServiceTokenErrorCode::kEmailDomainNotSupported)), + .mojo_expected = + base::unexpected(mojom::GetServiceTokenError::NewServerError( + mojom::GetServiceTokenServerError::New( + net::HTTP_BAD_REQUEST, + mojom::GetServiceTokenServerErrorCode:: + kEmailDomainNotSupported))), }); return kGetServiceTokenEmailDomainNotSupported.get(); } @@ -2966,9 +2984,12 @@ const GetServiceTokenTestCase* GetServiceTokenIncorrectCredentials() { body.code = base::Value(14004); return body; }())}}, - .mojo_expected = base::unexpected(mojom::GetServiceTokenError::New( - net::HTTP_FORBIDDEN, - mojom::GetServiceTokenErrorCode::kIncorrectCredentials)), + .mojo_expected = + base::unexpected(mojom::GetServiceTokenError::NewServerError( + mojom::GetServiceTokenServerError::New( + net::HTTP_FORBIDDEN, + mojom::GetServiceTokenServerErrorCode:: + kIncorrectCredentials))), }); return kGetServiceTokenIncorrectCredentials.get(); } @@ -2991,9 +3012,12 @@ const GetServiceTokenTestCase* GetServiceTokenInvalidTokenAudience() { body.code = base::Value(14007); return body; }())}}, - .mojo_expected = base::unexpected(mojom::GetServiceTokenError::New( - net::HTTP_FORBIDDEN, - mojom::GetServiceTokenErrorCode::kInvalidTokenAudience)), + .mojo_expected = + base::unexpected(mojom::GetServiceTokenError::NewServerError( + mojom::GetServiceTokenServerError::New( + net::HTTP_FORBIDDEN, + mojom::GetServiceTokenServerErrorCode:: + kInvalidTokenAudience))), }); return kGetServiceTokenInvalidTokenAudience.get(); } @@ -3016,9 +3040,11 @@ const GetServiceTokenTestCase* GetServiceTokenBadRequest() { body.code = base::Value(0); return body; }())}}, - .mojo_expected = base::unexpected(mojom::GetServiceTokenError::New( - net::HTTP_BAD_REQUEST, - mojom::GetServiceTokenErrorCode::kMiscServerError)), + .mojo_expected = + base::unexpected(mojom::GetServiceTokenError::NewServerError( + mojom::GetServiceTokenServerError::New( + net::HTTP_BAD_REQUEST, + mojom::GetServiceTokenServerErrorCode::kNull))), }); return kGetServiceTokenBadRequest.get(); } @@ -3041,9 +3067,11 @@ const GetServiceTokenTestCase* GetServiceTokenUnauthorized() { body.code = base::Value(0); return body; }())}}, - .mojo_expected = base::unexpected(mojom::GetServiceTokenError::New( - net::HTTP_UNAUTHORIZED, - mojom::GetServiceTokenErrorCode::kMiscServerError)), + .mojo_expected = + base::unexpected(mojom::GetServiceTokenError::NewServerError( + mojom::GetServiceTokenServerError::New( + net::HTTP_UNAUTHORIZED, + mojom::GetServiceTokenServerErrorCode::kNull))), }); return kGetServiceTokenUnauthorized.get(); } @@ -3066,9 +3094,11 @@ const GetServiceTokenTestCase* GetServiceTokenServerError() { body.code = base::Value(0); return body; }())}}, - .mojo_expected = base::unexpected(mojom::GetServiceTokenError::New( - net::HTTP_INTERNAL_SERVER_ERROR, - mojom::GetServiceTokenErrorCode::kMiscServerError)), + .mojo_expected = + base::unexpected(mojom::GetServiceTokenError::NewServerError( + mojom::GetServiceTokenServerError::New( + net::HTTP_INTERNAL_SERVER_ERROR, + mojom::GetServiceTokenServerErrorCode::kNull))), }); return kGetServiceTokenInternalServerError.get(); } @@ -3091,8 +3121,11 @@ const GetServiceTokenTestCase* GetServiceTokenUnknown() { body.code = base::Value(42); return body; }())}}, - .mojo_expected = base::unexpected(mojom::GetServiceTokenError::New( - net::HTTP_TOO_EARLY, std::nullopt)), + .mojo_expected = + base::unexpected(mojom::GetServiceTokenError::NewServerError( + mojom::GetServiceTokenServerError::New( + net::HTTP_TOO_EARLY, + mojom::GetServiceTokenServerErrorCode::kUnknown))), }); return kGetServiceTokenUnknown.get(); } @@ -3116,8 +3149,11 @@ const GetServiceTokenTestCase* GetServiceTokenServiceTokenEmpty() { body.auth_token = ""; return body; }()}}, - .mojo_expected = base::unexpected( - mojom::GetServiceTokenError::New(net::HTTP_OK, std::nullopt)), + .mojo_expected = + base::unexpected(mojom::GetServiceTokenError::NewServerError( + mojom::GetServiceTokenServerError::New( + net::HTTP_OK, mojom::GetServiceTokenServerErrorCode:: + kInvalidResponse))), }); return kGetServiceTokenServiceTokenEmpty.get(); } @@ -3142,9 +3178,11 @@ const GetServiceTokenTestCase* GetServiceTokenServiceTokenEncryptionFailed() { "fetched_service_token"; return body; }()}}, - .mojo_expected = base::unexpected(mojom::GetServiceTokenError::New( - std::nullopt, - mojom::GetServiceTokenErrorCode::kServiceTokenEncryptionFailed)), + .mojo_expected = + base::unexpected(mojom::GetServiceTokenError::NewClientError( + mojom::GetServiceTokenClientError::New( + mojom::GetServiceTokenClientErrorCode:: + kServiceTokenEncryptionFailed))), }); return kGetServiceTokenServiceTokenEncryptionFailed.get(); } diff --git a/components/brave_account/brave_account_utils.h b/components/brave_account/brave_account_utils.h index 55470546fb9..676b43c86b6 100644 --- a/components/brave_account/brave_account_utils.h +++ b/components/brave_account/brave_account_utils.h @@ -16,6 +16,7 @@ namespace brave_account { inline constexpr auto kServiceToString = base::MakeFixedFlatMap({ + {mojom::Service::kAccounts, "accounts"}, {mojom::Service::kEmailAliases, "email-aliases"}, {mojom::Service::kPremium, "premium"}, {mojom::Service::kSync, "sync"}, @@ -26,6 +27,7 @@ static_assert(kServiceToString.size() == inline constexpr auto kServiceFromString = base::MakeFixedFlatMap({ + {"accounts", mojom::Service::kAccounts}, {"email-aliases", mojom::Service::kEmailAliases}, {"premium", mojom::Service::kPremium}, {"sync", mojom::Service::kSync}, diff --git a/components/brave_account/endpoints/auth_logout_unittest.cc b/components/brave_account/endpoints/auth_logout_unittest.cc index b74da597777..cdd2dc6e795 100644 --- a/components/brave_account/endpoints/auth_logout_unittest.cc +++ b/components/brave_account/endpoints/auth_logout_unittest.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_account/endpoints/auth_logout.h" +#include + #include "base/no_destructor.h" #include "base/types/expected.h" #include "brave/components/brave_account/endpoints/endpoint_test.h" @@ -36,7 +38,6 @@ const AuthLogoutTestCase* Success() { .expected_response = {.net_error = net::OK, .status_code = net::HTTP_NO_CONTENT, .body = std::nullopt}}); - return kSuccess.get(); } diff --git a/components/brave_account/endpoints/auth_validate_unittest.cc b/components/brave_account/endpoints/auth_validate_unittest.cc index 5c468b742a5..969011ba705 100644 --- a/components/brave_account/endpoints/auth_validate_unittest.cc +++ b/components/brave_account/endpoints/auth_validate_unittest.cc @@ -5,9 +5,12 @@ #include "brave/components/brave_account/endpoints/auth_validate.h" +#include + #include "base/no_destructor.h" #include "base/types/expected.h" #include "brave/components/brave_account/endpoints/endpoint_test.h" +#include "net/base/net_errors.h" #include "net/http/http_status_code.h" #include "testing/gtest/include/gtest/gtest.h" @@ -33,7 +36,6 @@ const AuthValidateTestCase* Success() { body.email = "email"; return body; }()}}); - return kSuccess.get(); } diff --git a/components/brave_account/endpoints/login_finalize_unittest.cc b/components/brave_account/endpoints/login_finalize_unittest.cc index b35c0243600..72e837b219a 100644 --- a/components/brave_account/endpoints/login_finalize_unittest.cc +++ b/components/brave_account/endpoints/login_finalize_unittest.cc @@ -10,6 +10,7 @@ #include "base/no_destructor.h" #include "base/types/expected.h" #include "brave/components/brave_account/endpoints/endpoint_test.h" +#include "net/base/net_errors.h" #include "net/http/http_status_code.h" #include "testing/gtest/include/gtest/gtest.h" @@ -37,7 +38,6 @@ const LoginFinalizeTestCase* Success() { body.email = "email"; return body; }()}}); - return kSuccess.get(); } @@ -53,7 +53,7 @@ const LoginFinalizeTestCase* Success() { // - { "code": 14005, "error": "incorrect email", "status": 401 } // - { "code": 14006, "error": "incorrect password", "status": 401 } // - HTTP 5XX: -// - { "code": 0, "error": "Internal Server Error", "status": <5xx> } +// - { "code": null, "error": "Internal Server Error", "status": <5xx> } // clang-format on const LoginFinalizeTestCase* ApplicationJsonErrorCodeIsNull() { static const base::NoDestructor diff --git a/components/brave_account/endpoints/login_init_unittest.cc b/components/brave_account/endpoints/login_init_unittest.cc index 7832799ef26..ee5ebd9f3e5 100644 --- a/components/brave_account/endpoints/login_init_unittest.cc +++ b/components/brave_account/endpoints/login_init_unittest.cc @@ -10,6 +10,7 @@ #include "base/no_destructor.h" #include "base/types/expected.h" #include "brave/components/brave_account/endpoints/endpoint_test.h" +#include "net/base/net_errors.h" #include "net/http/http_status_code.h" #include "testing/gtest/include/gtest/gtest.h" @@ -38,7 +39,6 @@ const LoginInitTestCase* Success() { body.serialized_ke2 = "34c375d933e3c"; return body; }()}}); - return kSuccess.get(); } @@ -52,7 +52,7 @@ const LoginInitTestCase* Success() { // - { "code": 14005, "error": "incorrect email", "status": 401 } // - { "code": 14006, "error": "incorrect password", "status": 401 } // - HTTP 5XX: -// - { "code": 0, "error": "Internal Server Error", "status": <5xx> } +// - { "code": null, "error": "Internal Server Error", "status": <5xx> } // clang-format on const LoginInitTestCase* ApplicationJsonErrorCodeIsNull() { static const base::NoDestructor diff --git a/components/brave_account/endpoints/password_finalize_unittest.cc b/components/brave_account/endpoints/password_finalize_unittest.cc index 63a5b3a3a50..305dcc90589 100644 --- a/components/brave_account/endpoints/password_finalize_unittest.cc +++ b/components/brave_account/endpoints/password_finalize_unittest.cc @@ -10,6 +10,7 @@ #include "base/no_destructor.h" #include "base/types/expected.h" #include "brave/components/brave_account/endpoints/endpoint_test.h" +#include "net/base/net_errors.h" #include "net/http/http_status_code.h" #include "testing/gtest/include/gtest/gtest.h" @@ -36,28 +37,46 @@ const PasswordFinalizeTestCase* Success() { .net_error = net::OK, .status_code = net::HTTP_OK, .body = PasswordFinalize::Response::SuccessBody()}}); - return kSuccess.get(); } // clang-format off // application/json errors: // - HTTP 400: +// - { "code": null, "error": "Bad Request", "status": 400 } // - { "code": 14002, "error": "interim password state has expired", "status": 400 } -// - { "code": 0, "error": "Bad Request", "status": 400 } // - HTTP 401: -// - { "code": 0, "error": "Unauthorized", "status": 401 } +// - { "code": null, "error": "Unauthorized", "status": 401 } // - HTTP 403: -// - { "code": 0, "error": "Forbidden", "status": 403 } +// - { "code": null, "error": "Forbidden", "status": 403 } // - HTTP 404: // - { "code": 14001, "error": "interim password state not found", "status": 404 } // - HTTP 5XX: -// - { "code": 0, "error": "Internal Server Error", "status": <5xx> } +// - { "code": null, "error": "Internal Server Error", "status": <5xx> } // clang-format on -const PasswordFinalizeTestCase* ApplicationJsonError() { +const PasswordFinalizeTestCase* ApplicationJsonErrorCodeIsNull() { static const base::NoDestructor - kApplicationJsonError( - {.test_name = "application_json_error", + kApplicationJsonErrorCodeIsNull( + {.test_name = "application_json_error_code_is_null", + .http_status_code = net::HTTP_BAD_REQUEST, + .raw_response_body = + R"({ "code": null, + "error": "Bad Request", + "status": 400 })", + .expected_response = {.net_error = net::OK, + .status_code = net::HTTP_BAD_REQUEST, + .body = base::unexpected([] { + PasswordFinalize::Response::ErrorBody body; + body.code = base::Value(); + return body; + }())}}); + return kApplicationJsonErrorCodeIsNull.get(); +} + +const PasswordFinalizeTestCase* ApplicationJsonErrorCodeIsNotNull() { + static const base::NoDestructor + kApplicationJsonErrorCodeIsNotNull( + {.test_name = "application_json_error_code_is_not_null", .http_status_code = net::HTTP_BAD_REQUEST, .raw_response_body = R"({ "code": 14002, @@ -66,11 +85,11 @@ const PasswordFinalizeTestCase* ApplicationJsonError() { .expected_response = {.net_error = net::OK, .status_code = net::HTTP_BAD_REQUEST, .body = base::unexpected([] { - PasswordFinalize::Response::ErrorBody error; - error.code = base::Value(14002); - return error; + PasswordFinalize::Response::ErrorBody body; + body.code = base::Value(14002); + return body; }())}}); - return kApplicationJsonError.get(); + return kApplicationJsonErrorCodeIsNotNull.get(); } // non-application/json errors: @@ -88,10 +107,10 @@ const PasswordFinalizeTestCase* NonApplicationJsonError() { return kNonApplicationJsonError.get(); } -} // namespace - using PasswordFinalizeTest = EndpointTest; +} // namespace + TEST_P(PasswordFinalizeTest, HandlesReplies) { RunTestCase(); } @@ -99,7 +118,8 @@ TEST_P(PasswordFinalizeTest, HandlesReplies) { INSTANTIATE_TEST_SUITE_P(PasswordFinalizeTestCases, PasswordFinalizeTest, testing::Values(Success(), - ApplicationJsonError(), + ApplicationJsonErrorCodeIsNull(), + ApplicationJsonErrorCodeIsNotNull(), NonApplicationJsonError()), PasswordFinalizeTest::kNameGenerator); diff --git a/components/brave_account/endpoints/password_init_unittest.cc b/components/brave_account/endpoints/password_init_unittest.cc index 4f744c90e9f..56bcd145556 100644 --- a/components/brave_account/endpoints/password_init_unittest.cc +++ b/components/brave_account/endpoints/password_init_unittest.cc @@ -10,6 +10,7 @@ #include "base/no_destructor.h" #include "base/types/expected.h" #include "brave/components/brave_account/endpoints/endpoint_test.h" +#include "net/base/net_errors.h" #include "net/http/http_status_code.h" #include "testing/gtest/include/gtest/gtest.h" @@ -21,10 +22,10 @@ bool operator==(const PasswordInit::Response::SuccessBody& lhs, lhs.verification_token == rhs.verification_token; } -using PasswordInitTestCase = EndpointTestCase; - namespace { +using PasswordInitTestCase = EndpointTestCase; + const PasswordInitTestCase* Success() { static const base::NoDestructor kSuccess( {.test_name = "success", @@ -50,26 +51,46 @@ const PasswordInitTestCase* Success() { // - { "code": 13004, "error": "account already exists", "status": 400 } // - { "code": 13006, "error": "email domain is not supported", "status": 400 } // - HTTP 401: -// - { "code": 0, "error": "Unauthorized", "status": 401 } +// - { "code": null, "error": "Unauthorized", "status": 401 } // - HTTP 5XX: -// - { "code": 0, "error": "Internal Server Error", "status": <5xx> } +// - { "code": null, "error": "Internal Server Error", "status": <5xx> } // clang-format on -const PasswordInitTestCase* ApplicationJsonError() { - static const base::NoDestructor kApplicationJsonError( - {.test_name = "application_json_error", - .http_status_code = net::HTTP_BAD_REQUEST, - .raw_response_body = - R"({ "code": 13004, - "error": "account already exists", - "status": 400 })", - .expected_response = {.net_error = net::OK, - .status_code = net::HTTP_BAD_REQUEST, - .body = base::unexpected([] { - PasswordInit::Response::ErrorBody error; - error.code = base::Value(13004); - return error; - }())}}); - return kApplicationJsonError.get(); +const PasswordInitTestCase* ApplicationJsonErrorCodeIsNull() { + static const base::NoDestructor + kApplicationJsonErrorCodeIsNull( + {.test_name = "application_json_error_code_is_null", + .http_status_code = net::HTTP_UNAUTHORIZED, + .raw_response_body = + R"({ "code": null, + "error": "Unauthorized", + "status": 401 })", + .expected_response = {.net_error = net::OK, + .status_code = net::HTTP_UNAUTHORIZED, + .body = base::unexpected([] { + PasswordInit::Response::ErrorBody body; + body.code = base::Value(); + return body; + }())}}); + return kApplicationJsonErrorCodeIsNull.get(); +} + +const PasswordInitTestCase* ApplicationJsonErrorCodeIsNotNull() { + static const base::NoDestructor + kApplicationJsonErrorCodeIsNotNull( + {.test_name = "application_json_error_code_is_not_null", + .http_status_code = net::HTTP_BAD_REQUEST, + .raw_response_body = + R"({ "code": 13004, + "error": "account already exists", + "status": 400 })", + .expected_response = {.net_error = net::OK, + .status_code = net::HTTP_BAD_REQUEST, + .body = base::unexpected([] { + PasswordInit::Response::ErrorBody body; + body.code = base::Value(13004); + return body; + }())}}); + return kApplicationJsonErrorCodeIsNotNull.get(); } // non-application/json errors: @@ -87,10 +108,10 @@ const PasswordInitTestCase* NonApplicationJsonError() { return kNonApplicationJsonError.get(); } -} // namespace - using PasswordInitTest = EndpointTest; +} // namespace + TEST_P(PasswordInitTest, HandlesReplies) { RunTestCase(); } @@ -98,7 +119,8 @@ TEST_P(PasswordInitTest, HandlesReplies) { INSTANTIATE_TEST_SUITE_P(PasswordInitTestCases, PasswordInitTest, testing::Values(Success(), - ApplicationJsonError(), + ApplicationJsonErrorCodeIsNull(), + ApplicationJsonErrorCodeIsNotNull(), NonApplicationJsonError()), PasswordInitTest::kNameGenerator); diff --git a/components/brave_account/endpoints/service_token_unittest.cc b/components/brave_account/endpoints/service_token_unittest.cc index 2d5b2ef3b81..cdb92c68960 100644 --- a/components/brave_account/endpoints/service_token_unittest.cc +++ b/components/brave_account/endpoints/service_token_unittest.cc @@ -5,9 +5,12 @@ #include "brave/components/brave_account/endpoints/service_token.h" +#include + #include "base/no_destructor.h" #include "base/types/expected.h" #include "brave/components/brave_account/endpoints/endpoint_test.h" +#include "net/base/net_errors.h" #include "net/http/http_status_code.h" #include "testing/gtest/include/gtest/gtest.h" @@ -18,10 +21,10 @@ bool operator==(const ServiceToken::Response::SuccessBody& lhs, return lhs.auth_token == rhs.auth_token; } -using ServiceTokenTestCase = EndpointTestCase; - namespace { +using ServiceTokenTestCase = EndpointTestCase; + const ServiceTokenTestCase* Success() { static const base::NoDestructor kSuccess( {.test_name = "success", @@ -39,32 +42,52 @@ const ServiceTokenTestCase* Success() { // clang-format off // application/json errors: // - HTTP 400: -// - { "code": 0, "error": "Bad Request", "status": 400 } +// - { "code": null, "error": "Bad Request", "status": 400 } // - { "code": 13006, "error": "email domain is not supported", "status": 400 } // - HTTP 401: -// - { "code": 0, "error": "Unauthorized", "status": 401 } +// - { "code": null, "error": "Unauthorized", "status": 401 } // - HTTP 403: // - { "code": 14004, "error": "incorrect credentials", "status": 403 } // - { "code": 14007, "error": "invalid token audience", "status": 403 } // - HTTP 5XX: -// - { "code": 0, "error": "Internal Server Error", "status": <5xx> } +// - { "code": null, "error": "Internal Server Error", "status": <5xx> } // clang-format on -const ServiceTokenTestCase* ApplicationJsonError() { - static const base::NoDestructor kApplicationJsonError( - {.test_name = "application_json_error", - .http_status_code = net::HTTP_BAD_REQUEST, - .raw_response_body = - R"({ "code": 13006, - "error": "email domain is not supported", - "status": 400 })", - .expected_response = {.net_error = net::OK, - .status_code = net::HTTP_BAD_REQUEST, - .body = base::unexpected([] { - ServiceToken::Response::ErrorBody error; - error.code = base::Value(13006); - return error; - }())}}); - return kApplicationJsonError.get(); +const ServiceTokenTestCase* ApplicationJsonErrorCodeIsNull() { + static const base::NoDestructor + kApplicationJsonErrorCodeIsNull( + {.test_name = "application_json_error_code_is_null", + .http_status_code = net::HTTP_BAD_REQUEST, + .raw_response_body = + R"({ "code": null, + "error": "Bad Request", + "status": 400 })", + .expected_response = {.net_error = net::OK, + .status_code = net::HTTP_BAD_REQUEST, + .body = base::unexpected([] { + ServiceToken::Response::ErrorBody body; + body.code = base::Value(); + return body; + }())}}); + return kApplicationJsonErrorCodeIsNull.get(); +} + +const ServiceTokenTestCase* ApplicationJsonErrorCodeIsNotNull() { + static const base::NoDestructor + kApplicationJsonErrorCodeIsNotNull( + {.test_name = "application_json_error_code_is_not_null", + .http_status_code = net::HTTP_BAD_REQUEST, + .raw_response_body = + R"({ "code": 13006, + "error": "email domain is not supported", + "status": 400 })", + .expected_response = {.net_error = net::OK, + .status_code = net::HTTP_BAD_REQUEST, + .body = base::unexpected([] { + ServiceToken::Response::ErrorBody body; + body.code = base::Value(13006); + return body; + }())}}); + return kApplicationJsonErrorCodeIsNotNull.get(); } // non-application/json errors: @@ -82,10 +105,10 @@ const ServiceTokenTestCase* NonApplicationJsonError() { return kNonApplicationJsonError.get(); } -} // namespace - using ServiceTokenTest = EndpointTest; +} // namespace + TEST_P(ServiceTokenTest, HandlesReplies) { RunTestCase(); } @@ -93,7 +116,8 @@ TEST_P(ServiceTokenTest, HandlesReplies) { INSTANTIATE_TEST_SUITE_P(ServiceTokenTestCases, ServiceTokenTest, testing::Values(Success(), - ApplicationJsonError(), + ApplicationJsonErrorCodeIsNull(), + ApplicationJsonErrorCodeIsNotNull(), NonApplicationJsonError()), ServiceTokenTest::kNameGenerator); diff --git a/components/brave_account/endpoints/verify_complete_unittest.cc b/components/brave_account/endpoints/verify_complete_unittest.cc index e9a286dd917..86de7b1fae8 100644 --- a/components/brave_account/endpoints/verify_complete_unittest.cc +++ b/components/brave_account/endpoints/verify_complete_unittest.cc @@ -5,9 +5,11 @@ #include "brave/components/brave_account/endpoints/verify_complete.h" +#include #include #include "base/no_destructor.h" +#include "base/types/expected.h" #include "brave/components/brave_account/endpoints/endpoint_test.h" #include "net/base/net_errors.h" #include "net/http/http_status_code.h" diff --git a/components/brave_account/endpoints/verify_delete_unittest.cc b/components/brave_account/endpoints/verify_delete_unittest.cc index c878295b8ef..1165efe1deb 100644 --- a/components/brave_account/endpoints/verify_delete_unittest.cc +++ b/components/brave_account/endpoints/verify_delete_unittest.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_account/endpoints/verify_delete.h" +#include + #include "base/no_destructor.h" #include "base/types/expected.h" #include "brave/components/brave_account/endpoints/endpoint_test.h" @@ -36,7 +38,6 @@ const VerifyDeleteTestCase* Success() { .expected_response = {.net_error = net::OK, .status_code = net::HTTP_NO_CONTENT, .body = std::nullopt}}); - return kSuccess.get(); } @@ -51,19 +52,36 @@ const VerifyDeleteTestCase* Success() { // - HTTP 5XX: // - { "code": null, "error": "Internal Server Error", "status": <5xx> } // clang-format on -const VerifyDeleteTestCase* ApplicationJsonError() { - static const base::NoDestructor kApplicationJsonError( - {.test_name = "application_json_error", - .http_status_code = net::HTTP_BAD_REQUEST, - .raw_response_body = - R"({ "code": null, - "error": "Bad Request", - "status": 400 })", - .expected_response = { - .net_error = net::OK, - .status_code = net::HTTP_BAD_REQUEST, - .body = base::unexpected(VerifyDelete::Response::ErrorBody())}}); - return kApplicationJsonError.get(); +const VerifyDeleteTestCase* ApplicationJsonErrorCodeIsNull() { + static const base::NoDestructor + kApplicationJsonErrorCodeIsNull( + {.test_name = "application_json_error_code_is_null", + .http_status_code = net::HTTP_BAD_REQUEST, + .raw_response_body = + R"({ "code": null, + "error": "Bad Request", + "status": 400 })", + .expected_response = { + .net_error = net::OK, + .status_code = net::HTTP_BAD_REQUEST, + .body = base::unexpected(VerifyDelete::Response::ErrorBody())}}); + return kApplicationJsonErrorCodeIsNull.get(); +} + +const VerifyDeleteTestCase* ApplicationJsonErrorCodeIsNotNull() { + static const base::NoDestructor + kApplicationJsonErrorCodeIsNotNull( + {.test_name = "application_json_error_code_is_not_null", + .http_status_code = net::HTTP_BAD_REQUEST, + .raw_response_body = + R"({ "code": 13003, + "error": "intent not allowed", + "status": 400 })", + .expected_response = { + .net_error = net::OK, + .status_code = net::HTTP_BAD_REQUEST, + .body = base::unexpected(VerifyDelete::Response::ErrorBody())}}); + return kApplicationJsonErrorCodeIsNotNull.get(); } // non-application/json errors: @@ -92,7 +110,8 @@ TEST_P(VerifyDeleteTest, HandlesReplies) { INSTANTIATE_TEST_SUITE_P(VerifyDeleteTestCases, VerifyDeleteTest, testing::Values(Success(), - ApplicationJsonError(), + ApplicationJsonErrorCodeIsNull(), + ApplicationJsonErrorCodeIsNotNull(), NonApplicationJsonError()), VerifyDeleteTest::kNameGenerator); diff --git a/components/brave_account/endpoints/verify_init_unittest.cc b/components/brave_account/endpoints/verify_init_unittest.cc index 5ba29421235..110ef4e5762 100644 --- a/components/brave_account/endpoints/verify_init_unittest.cc +++ b/components/brave_account/endpoints/verify_init_unittest.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_account/endpoints/verify_init.h" +#include + #include "base/no_destructor.h" #include "base/types/expected.h" #include "brave/components/brave_account/endpoints/endpoint_test.h" @@ -34,7 +36,6 @@ const VerifyInitTestCase* Success() { body.verification_token = "eyJhbGciOiJFUz"; return body; }()}}); - return kSuccess.get(); } diff --git a/components/brave_account/endpoints/verify_resend_unittest.cc b/components/brave_account/endpoints/verify_resend_unittest.cc index 4cf9237f1bc..b50004e0170 100644 --- a/components/brave_account/endpoints/verify_resend_unittest.cc +++ b/components/brave_account/endpoints/verify_resend_unittest.cc @@ -5,9 +5,12 @@ #include "brave/components/brave_account/endpoints/verify_resend.h" +#include + #include "base/no_destructor.h" #include "base/types/expected.h" #include "brave/components/brave_account/endpoints/endpoint_test.h" +#include "net/base/net_errors.h" #include "net/http/http_status_code.h" #include "testing/gtest/include/gtest/gtest.h" @@ -30,7 +33,6 @@ const VerifyResendTestCase* Success() { .expected_response = {.net_error = net::OK, .status_code = net::HTTP_NO_CONTENT, .body = std::nullopt}}); - return kSuccess.get(); } diff --git a/components/brave_account/mock_brave_account_authentication.h b/components/brave_account/mock_brave_account_authentication.h index a9dfd5a7db0..4c17201aaf5 100644 --- a/components/brave_account/mock_brave_account_authentication.h +++ b/components/brave_account/mock_brave_account_authentication.h @@ -6,7 +6,6 @@ #ifndef BRAVE_COMPONENTS_BRAVE_ACCOUNT_MOCK_BRAVE_ACCOUNT_AUTHENTICATION_H_ #define BRAVE_COMPONENTS_BRAVE_ACCOUNT_MOCK_BRAVE_ACCOUNT_AUTHENTICATION_H_ -#include #include #include "brave/components/brave_account/mojom/brave_account.mojom.h" @@ -28,7 +27,7 @@ class MockBraveAccountAuthentication : public mojom::Authentication { MOCK_METHOD(void, RegisterInitialize, - (std::optional initiating_service, + (mojom::Service initiating_service, const std::string& email, const std::string& blinded_message, RegisterInitializeCallback callback), @@ -55,7 +54,7 @@ class MockBraveAccountAuthentication : public mojom::Authentication { MOCK_METHOD(void, LoginInitialize, - (std::optional initiating_service, + (mojom::Service initiating_service, const std::string& email, const std::string& serialized_ke1, LoginInitializeCallback callback), diff --git a/components/brave_account/mojom/brave_account.mojom b/components/brave_account/mojom/brave_account.mojom index b4bf49c4572..1ebf5b50898 100644 --- a/components/brave_account/mojom/brave_account.mojom +++ b/components/brave_account/mojom/brave_account.mojom @@ -34,15 +34,27 @@ struct RegisterVerifyResult { // Currently empty, serves as a placeholder for success. }; -enum RegisterErrorCode { - // Client-side errors: - kOpaqueError = -5, - kVerificationTokenEncryptionFailed = -4, - kVerificationTokenDecryptionFailed = -3, - kUserNotInTheVerificationState = -2, - kAuthenticationTokenEncryptionFailed = -1, - // Server-side errors: - kMiscServerError = 0, +enum RegisterClientErrorCode { + kUnexpected, + kOpaqueError, + kVerificationTokenEncryptionFailed, + kVerificationTokenDecryptionFailed, + kUserNotInTheVerificationState, + kAuthenticationTokenEncryptionFailed, +}; + +struct RegisterClientError { + RegisterClientErrorCode error_code; +}; + +enum RegisterServerErrorCode { + // Sentinel: server returned a malformed response + // (e.g. success body missing required fields). + kInvalidResponse = -1, + // Sentinel: server returned "code": null in the JSON body. + kNull = 0, + // Sentinel: server returned an unrecognized "code" in the JSON body. + kUnknown = 1, // /v2/accounts/password/init: // - internal: kNewAccountEmailRequired = 11005, @@ -65,13 +77,17 @@ enum RegisterErrorCode { kInvalidVerificationCode = 13011, }; -struct RegisterError { - // null: client-side error +struct RegisterServerError { // < 0: network error code (net_error_list.h) // >= 0: HTTP status code (http_status_code_list.h) if available, // otherwise net::OK - int32? netErrorOrHttpStatus; - RegisterErrorCode? errorCode; + int32 net_error_or_http_status; + RegisterServerErrorCode error_code; +}; + +union RegisterError { + RegisterClientError client_error; + RegisterServerError server_error; }; struct LoginInitializeResult { @@ -85,68 +101,104 @@ struct LoginFinalizeResult { // Currently empty, serves as a placeholder for success. }; -enum LoginErrorCode { - // Client-side errors: - kOpaqueError = -4, - kLoginTokenEncryptionFailed = -3, - kLoginTokenDecryptionFailed = -2, - kAuthenticationTokenEncryptionFailed = -1, - // Server-side errors: - kMiscServerError = 0, +enum LoginClientErrorCode { + kUnexpected, + kOpaqueError, + kLoginTokenEncryptionFailed, + kLoginTokenDecryptionFailed, + kAuthenticationTokenEncryptionFailed, +}; + +struct LoginClientError { + LoginClientErrorCode error_code; +}; + +enum LoginServerErrorCode { + // Sentinel: server returned a malformed response + // (e.g. success body missing required fields). + kInvalidResponse = -1, + // Sentinel: server returned "code": null in the JSON body. + kNull = 0, + // Sentinel: server returned an unrecognized "code" in the JSON body. + kUnknown = 1, // /v2/auth/login/init: // - internal: - // - user-facing: - kEmailNotVerified = 11003, + kEmailNotVerified = 11003, // /v2/auth/login/finalize: // - internal: - kInterimPasswordStateNotFound = 14001, - kInterimPasswordStateHasExpired = 14002, - kInterimPasswordStateMismatch = 14009, + kInterimPasswordStateNotFound = 14001, + kInterimPasswordStateHasExpired = 14002, + kInterimPasswordStateMismatch = 14009, // - user-facing: - // common for both endpoints: // - internal: - kIncorrectCredentials = 14004, + kIncorrectCredentials = 14004, // - user-facing: - kIncorrectEmail = 14005, - kIncorrectPassword = 14006, + kIncorrectEmail = 14005, + kIncorrectPassword = 14006, }; -struct LoginError { - // null: client-side error +struct LoginServerError { // < 0: network error code (net_error_list.h) // >= 0: HTTP status code (http_status_code_list.h) if available, // otherwise net::OK - int32? netErrorOrHttpStatus; - LoginErrorCode? errorCode; + int32 net_error_or_http_status; + LoginServerErrorCode error_code; +}; + +union LoginError { + LoginClientError client_error; + LoginServerError server_error; }; struct ResendConfirmationEmailResult { // Currently empty, serves as a placeholder for success. }; -enum ResendConfirmationEmailErrorCode { - // Client-side errors: - kUserNotInTheVerificationState = -2, - kVerificationTokenDecryptionFailed = -1, - // Server-side errors (/v2/verify/resend): - kMiscServerError = 0, - kMaximumEmailSendAttemptsExceeded = 13008, - kEmailAlreadyVerified = 13009, +enum ResendConfirmationEmailClientErrorCode { + kUnexpected, + kUserNotInTheVerificationState, + kVerificationTokenDecryptionFailed, }; -struct ResendConfirmationEmailError { - // null: client-side error +struct ResendConfirmationEmailClientError { + ResendConfirmationEmailClientErrorCode error_code; +}; + +enum ResendConfirmationEmailServerErrorCode { + // Sentinel: server returned a malformed response + // (e.g. success body missing required fields). + kInvalidResponse = -1, + // Sentinel: server returned "code": null in the JSON body. + kNull = 0, + // Sentinel: server returned an unrecognized "code" in the JSON body. + kUnknown = 1, + // /v2/verify/resend: + // - internal: - + // - user-facing: + kMaximumEmailSendAttemptsExceeded = 13008, + kEmailAlreadyVerified = 13009, +}; + +struct ResendConfirmationEmailServerError { // < 0: network error code (net_error_list.h) // >= 0: HTTP status code (http_status_code_list.h) if available, // otherwise net::OK - int32? netErrorOrHttpStatus; - ResendConfirmationEmailErrorCode? errorCode; + int32 net_error_or_http_status; + ResendConfirmationEmailServerErrorCode error_code; +}; + +union ResendConfirmationEmailError { + ResendConfirmationEmailClientError client_error; + ResendConfirmationEmailServerError server_error; }; // Services that depend on Brave Account. // Note: do not assign explicit values to enumerators, // as kMaxValue is used to validate that all services are handled. enum Service { + kAccounts, kEmailAliases, kPremium, kSync, @@ -159,26 +211,45 @@ struct GetServiceTokenResult { string serviceToken; }; -enum GetServiceTokenErrorCode { - // Client-side errors: - kUserNotLoggedIn = -4, - kAuthenticationTokenDecryptionFailed = -3, - kAuthenticationSessionChanged = -2, - kServiceTokenEncryptionFailed = -1, - // Server-side errors (/v2/auth/service_token): - kMiscServerError = 0, - kEmailDomainNotSupported = 13006, - kIncorrectCredentials = 14004, - kInvalidTokenAudience = 14007, +enum GetServiceTokenClientErrorCode { + kUnexpected, + kUserNotLoggedIn, + kAuthenticationTokenDecryptionFailed, + kAuthenticationSessionChanged, + kServiceTokenEncryptionFailed, }; -struct GetServiceTokenError { - // null: client-side error +struct GetServiceTokenClientError { + GetServiceTokenClientErrorCode error_code; +}; + +enum GetServiceTokenServerErrorCode { + // Sentinel: server returned a malformed response + // (e.g. success body missing required fields). + kInvalidResponse = -1, + // Sentinel: server returned "code": null in the JSON body. + kNull = 0, + // Sentinel: server returned an unrecognized "code" in the JSON body. + kUnknown = 1, + // /v2/auth/service_token: + // - internal: + kEmailDomainNotSupported = 13006, + kIncorrectCredentials = 14004, + kInvalidTokenAudience = 14007, + // - user-facing: - +}; + +struct GetServiceTokenServerError { // < 0: network error code (net_error_list.h) // >= 0: HTTP status code (http_status_code_list.h) if available, // otherwise net::OK - int32? netErrorOrHttpStatus; - GetServiceTokenErrorCode? errorCode; + int32 net_error_or_http_status; + GetServiceTokenServerErrorCode error_code; +}; + +union GetServiceTokenError { + GetServiceTokenClientError client_error; + GetServiceTokenServerError server_error; }; interface AuthenticationObserver { @@ -199,7 +270,7 @@ interface Authentication { // to be sent to the server. // Returns `RegisterInitializeResult` on success, or // `RegisterError` on failure. - RegisterInitialize(Service? initiatingService, + RegisterInitialize(Service initiatingService, string email, string blindedMessage) => result; @@ -243,7 +314,7 @@ interface Authentication { // to be sent to the server. // Returns `LoginInitializeResult` on success, or // `LoginError` on failure. - LoginInitialize(Service? initiatingService, + LoginInitialize(Service initiatingService, string email, string serializedKE1) => result; diff --git a/components/brave_account/resources/brave_account_browser_proxy.ts b/components/brave_account/resources/brave_account_browser_proxy.ts index e3d174c5503..f384e7feda5 100644 --- a/components/brave_account/resources/brave_account_browser_proxy.ts +++ b/components/brave_account/resources/brave_account_browser_proxy.ts @@ -23,7 +23,7 @@ export interface BraveAccountBrowserProxy { dialog_controller: DialogControllerInterface password_strength_meter: PasswordStrengthMeterInterface closeDialog: () => void - getInitiatingService: () => Service | null + getInitiatingService: () => Service } export class BraveAccountBrowserProxyImpl implements BraveAccountBrowserProxy { @@ -48,11 +48,11 @@ export class BraveAccountBrowserProxyImpl implements BraveAccountBrowserProxy { this.dialog_controller.closeDialog() } - getInitiatingService(): Service | null { + getInitiatingService(): Service { const id = 'initiatingService' return loadTimeData.valueExists(id) ? (loadTimeData.getInteger(id) as Service) - : null + : Service.kAccounts } static getInstance(): BraveAccountBrowserProxy { diff --git a/components/brave_account/resources/brave_account_common.ts b/components/brave_account/resources/brave_account_common.ts index 446fa950358..31615d591c4 100644 --- a/components/brave_account/resources/brave_account_common.ts +++ b/components/brave_account/resources/brave_account_common.ts @@ -8,12 +8,15 @@ import { leoShowAlert } from '//resources/brave/leo.bundle.js' import { loadTimeData } from '//resources/js/load_time_data.js' import { + LoginClientErrorCode, LoginError, - LoginErrorCode, + LoginServerErrorCode, + RegisterClientErrorCode, RegisterError, - RegisterErrorCode, + RegisterServerErrorCode, + ResendConfirmationEmailClientErrorCode, ResendConfirmationEmailError, - ResendConfirmationEmailErrorCode, + ResendConfirmationEmailServerErrorCode, } from './brave_account.mojom-webui.js' import { BraveAccountStrings } from './brave_components_webui_strings.js' @@ -22,87 +25,97 @@ export type Error = | { kind: 'register'; details: RegisterError } | { kind: 'resendConfirmationEmail'; details: ResendConfirmationEmailError } -const LOGIN_ERROR_STRINGS: Partial> = { - [LoginErrorCode.kEmailNotVerified]: +const LOGIN_SERVER_ERROR_STRINGS: Partial< + Record +> = { + [LoginServerErrorCode.kEmailNotVerified]: BraveAccountStrings.BRAVE_ACCOUNT_LOGIN_EMAIL_NOT_VERIFIED, - [LoginErrorCode.kIncorrectEmail]: + [LoginServerErrorCode.kIncorrectEmail]: BraveAccountStrings.BRAVE_ACCOUNT_LOGIN_INCORRECT_EMAIL, - [LoginErrorCode.kIncorrectPassword]: + [LoginServerErrorCode.kIncorrectPassword]: BraveAccountStrings.BRAVE_ACCOUNT_LOGIN_INCORRECT_PASSWORD, } -const REGISTER_ERROR_STRINGS: Partial> = { - [RegisterErrorCode.kAccountExists]: +const REGISTER_SERVER_ERROR_STRINGS: Partial< + Record +> = { + [RegisterServerErrorCode.kAccountExists]: BraveAccountStrings.BRAVE_ACCOUNT_REGISTER_ACCOUNT_EXISTS, - [RegisterErrorCode.kEmailDomainNotSupported]: + [RegisterServerErrorCode.kEmailDomainNotSupported]: BraveAccountStrings.BRAVE_ACCOUNT_REGISTER_EMAIL_DOMAIN_NOT_SUPPORTED, - [RegisterErrorCode.kTooManyVerifications]: + [RegisterServerErrorCode.kTooManyVerifications]: BraveAccountStrings.BRAVE_ACCOUNT_REGISTER_TOO_MANY_VERIFICATIONS, - [RegisterErrorCode.kVerificationNotFoundOrInvalidIdOrCode]: + [RegisterServerErrorCode.kVerificationNotFoundOrInvalidIdOrCode]: BraveAccountStrings.BRAVE_ACCOUNT_REGISTER_VERIFICATION_NOT_FOUND_OR_INVALID_ID_OR_CODE, - [RegisterErrorCode.kMaximumCodeVerificationAttemptsExceeded]: + [RegisterServerErrorCode.kMaximumCodeVerificationAttemptsExceeded]: BraveAccountStrings.BRAVE_ACCOUNT_REGISTER_MAXIMUM_CODE_VERIFICATION_ATTEMPTS_EXCEEDED, - [RegisterErrorCode.kInvalidVerificationCode]: + [RegisterServerErrorCode.kInvalidVerificationCode]: BraveAccountStrings.BRAVE_ACCOUNT_REGISTER_INVALID_VERIFICATION_CODE, } -const RESEND_CONFIRMATION_EMAIL_ERROR_STRINGS: Partial< - Record +const RESEND_CONFIRMATION_EMAIL_SERVER_ERROR_STRINGS: Partial< + Record > = { - [ResendConfirmationEmailErrorCode.kMaximumEmailSendAttemptsExceeded]: + [ResendConfirmationEmailServerErrorCode.kMaximumEmailSendAttemptsExceeded]: BraveAccountStrings.BRAVE_ACCOUNT_RESEND_CONFIRMATION_EMAIL_MAXIMUM_SEND_ATTEMPTS_EXCEEDED, - [ResendConfirmationEmailErrorCode.kEmailAlreadyVerified]: + [ResendConfirmationEmailServerErrorCode.kEmailAlreadyVerified]: BraveAccountStrings.BRAVE_ACCOUNT_RESEND_CONFIRMATION_EMAIL_ALREADY_VERIFIED, } function getErrorMessageImpl< - T extends - | LoginErrorCode - | RegisterErrorCode - | ResendConfirmationEmailErrorCode, + ClientErrorCode extends + | LoginClientErrorCode + | RegisterClientErrorCode + | ResendConfirmationEmailClientErrorCode, + ServerErrorCode extends + | LoginServerErrorCode + | RegisterServerErrorCode + | ResendConfirmationEmailServerErrorCode, >( - errorStrings: Partial>, - details: { netErrorOrHttpStatus: number | null; errorCode: T | null }, + serverErrorStrings: Partial>, + error: { + clientError?: { errorCode: ClientErrorCode } | null + serverError?: { + netErrorOrHttpStatus: number + errorCode: ServerErrorCode + } | null + }, ): string { - const { netErrorOrHttpStatus, errorCode } = details - const errorLabel = loadTimeData.getString( BraveAccountStrings.BRAVE_ACCOUNT_ERROR, ) - if (netErrorOrHttpStatus == null) { - // client-side error + if (error.clientError) { return loadTimeData.getStringF( BraveAccountStrings.BRAVE_ACCOUNT_CLIENT_ERROR, - errorCode != null ? ` (${errorLabel}=${errorCode})` : '', + ` (${errorLabel}=${error.clientError.errorCode})`, ) } - // server-side error - const specificErrorMessage = - errorCode != null && errorStrings[errorCode] - ? loadTimeData.getString(errorStrings[errorCode]) - : null + const serverError = error.serverError! + const stringId = serverErrorStrings[serverError.errorCode] + if (stringId) { + return loadTimeData.getString(stringId) + } - return ( - specificErrorMessage - ?? loadTimeData.getStringF( - BraveAccountStrings.BRAVE_ACCOUNT_SERVER_ERROR, - `${netErrorOrHttpStatus > 0 ? 'HTTP' : 'NET'}=${netErrorOrHttpStatus}`, - errorCode != null ? `, ${errorLabel}=${errorCode}` : '', - ) + return loadTimeData.getStringF( + BraveAccountStrings.BRAVE_ACCOUNT_SERVER_ERROR, + `${serverError.netErrorOrHttpStatus > 0 ? 'HTTP' : 'NET'}=${ + serverError.netErrorOrHttpStatus + }`, + `, ${errorLabel}=${serverError.errorCode}`, ) } function getErrorMessage(error: Error): string { switch (error.kind) { case 'login': - return getErrorMessageImpl(LOGIN_ERROR_STRINGS, error.details) + return getErrorMessageImpl(LOGIN_SERVER_ERROR_STRINGS, error.details) case 'register': - return getErrorMessageImpl(REGISTER_ERROR_STRINGS, error.details) + return getErrorMessageImpl(REGISTER_SERVER_ERROR_STRINGS, error.details) case 'resendConfirmationEmail': return getErrorMessageImpl( - RESEND_CONFIRMATION_EMAIL_ERROR_STRINGS, + RESEND_CONFIRMATION_EMAIL_SERVER_ERROR_STRINGS, error.details, ) } diff --git a/components/brave_account/resources/brave_account_create_dialog.ts b/components/brave_account/resources/brave_account_create_dialog.ts index df716b90b07..b0aabe45074 100644 --- a/components/brave_account/resources/brave_account_create_dialog.ts +++ b/components/brave_account/resources/brave_account_create_dialog.ts @@ -11,8 +11,8 @@ import { } from './brave_account_browser_proxy.js' import { getHtml } from './brave_account_create_dialog.html.js' import { + RegisterClientErrorCode, RegisterError, - RegisterErrorCode, } from './brave_account.mojom-webui.js' import { showError } from './brave_account_common.js' @@ -72,12 +72,13 @@ export class BraveAccountCreateDialogElement extends CrLitElement { error = e as RegisterError } else if (typeof e === 'string') { error = { - netErrorOrHttpStatus: null, - errorCode: RegisterErrorCode.kOpaqueError, + clientError: { errorCode: RegisterClientErrorCode.kOpaqueError }, } } else { console.error('Unexpected error:', e) - error = { netErrorOrHttpStatus: null, errorCode: null } + error = { + clientError: { errorCode: RegisterClientErrorCode.kUnexpected }, + } } showError({ kind: 'register', details: error }) diff --git a/components/brave_account/resources/brave_account_otp_dialog.ts b/components/brave_account/resources/brave_account_otp_dialog.ts index 5a4427c92d3..1b02713b353 100644 --- a/components/brave_account/resources/brave_account_otp_dialog.ts +++ b/components/brave_account/resources/brave_account_otp_dialog.ts @@ -13,7 +13,9 @@ import { BraveAccountStrings } from './brave_components_webui_strings.js' import { showError, showSuccess } from './brave_account_common.js' import { getHtml } from './brave_account_otp_dialog.html.js' import { + RegisterClientErrorCode, RegisterError, + ResendConfirmationEmailClientErrorCode, ResendConfirmationEmailError, } from './brave_account.mojom-webui.js' @@ -44,7 +46,9 @@ export class BraveAccountOtpDialogElement extends CrLitElement { error = e as RegisterError } else { console.error('Unexpected error:', e) - error = { netErrorOrHttpStatus: null, errorCode: null } + error = { + clientError: { errorCode: RegisterClientErrorCode.kUnexpected }, + } } showError({ kind: 'register', details: error }) @@ -64,7 +68,11 @@ export class BraveAccountOtpDialogElement extends CrLitElement { error = e as ResendConfirmationEmailError } else { console.error('Unexpected error:', e) - error = { netErrorOrHttpStatus: null, errorCode: null } + error = { + clientError: { + errorCode: ResendConfirmationEmailClientErrorCode.kUnexpected, + }, + } } } diff --git a/components/brave_account/resources/brave_account_sign_in_dialog.ts b/components/brave_account/resources/brave_account_sign_in_dialog.ts index e7515feec0d..ed6d5e5c7ff 100644 --- a/components/brave_account/resources/brave_account_sign_in_dialog.ts +++ b/components/brave_account/resources/brave_account_sign_in_dialog.ts @@ -11,7 +11,10 @@ import { } from './brave_account_browser_proxy.js' import { getCss } from './brave_account_sign_in_dialog.css.js' import { getHtml } from './brave_account_sign_in_dialog.html.js' -import { LoginError, LoginErrorCode } from './brave_account.mojom-webui.js' +import { + LoginClientErrorCode, + LoginError, +} from './brave_account.mojom-webui.js' import { showError } from './brave_account_common.js' // @ts-expect-error @@ -72,12 +75,13 @@ export class BraveAccountSignInDialogElement extends CrLitElement { error = e as LoginError } else if (typeof e === 'string') { error = { - netErrorOrHttpStatus: null, - errorCode: LoginErrorCode.kOpaqueError, + clientError: { errorCode: LoginClientErrorCode.kOpaqueError }, } } else { console.error('Unexpected error:', e) - error = { netErrorOrHttpStatus: null, errorCode: null } + error = { + clientError: { errorCode: LoginClientErrorCode.kUnexpected }, + } } showError({ kind: 'login', details: error }) diff --git a/components/email_aliases/email_aliases_auth.cc b/components/email_aliases/email_aliases_auth.cc index 09891ec4296..865361e01d5 100644 --- a/components/email_aliases/email_aliases_auth.cc +++ b/components/email_aliases/email_aliases_auth.cc @@ -64,8 +64,12 @@ void EmailAliasesAuth::GetServiceToken( brave_account_auth_->GetServiceToken( brave_account::mojom::Service::kEmailAliases, std::move(callback)); } else { - auto error = brave_account::mojom::GetServiceTokenError::New(); - std::move(callback).Run(base::unexpected(std::move(error))); + // TODO(https://github.com/brave/brave-browser/issues/54976) + std::move(callback).Run(base::unexpected( + brave_account::mojom::GetServiceTokenError::NewClientError( + brave_account::mojom::GetServiceTokenClientError::New( + brave_account::mojom::GetServiceTokenClientErrorCode:: + kUserNotLoggedIn)))); } } diff --git a/ios/browser/brave_account/brave_account_authentication_bridge_impl.mm b/ios/browser/brave_account/brave_account_authentication_bridge_impl.mm index 4f6bcec69e0..ea76aa4181d 100644 --- a/ios/browser/brave_account/brave_account_authentication_bridge_impl.mm +++ b/ios/browser/brave_account/brave_account_authentication_bridge_impl.mm @@ -46,12 +46,12 @@ NSString* GetAlertTitle( NSString* GetAlertMessage( const brave_account::mojom::ResendConfirmationEmailErrorPtr& error) { - static const auto kErrorStrings = base::MakeFixedFlatMap< - brave_account::mojom::ResendConfirmationEmailErrorCode, int>({ - {brave_account::mojom::ResendConfirmationEmailErrorCode:: + static const auto kServerErrorStrings = base::MakeFixedFlatMap< + brave_account::mojom::ResendConfirmationEmailServerErrorCode, int>({ + {brave_account::mojom::ResendConfirmationEmailServerErrorCode:: kMaximumEmailSendAttemptsExceeded, IDS_BRAVE_ACCOUNT_RESEND_CONFIRMATION_EMAIL_MAXIMUM_SEND_ATTEMPTS_EXCEEDED}, - {brave_account::mojom::ResendConfirmationEmailErrorCode:: + {brave_account::mojom::ResendConfirmationEmailServerErrorCode:: kEmailAlreadyVerified, IDS_BRAVE_ACCOUNT_RESEND_CONFIRMATION_EMAIL_ALREADY_VERIFIED}, }); @@ -61,35 +61,30 @@ NSString* GetAlertMessage( IDS_BRAVE_ACCOUNT_RESEND_CONFIRMATION_EMAIL_SUCCESS); } - if (!error->netErrorOrHttpStatus) { - // client-side error + const std::string error_label = + l10n_util::GetStringUTF8(IDS_BRAVE_ACCOUNT_ERROR); + + if (error->is_client_error()) { return l10n_util::GetNSStringF( IDS_BRAVE_ACCOUNT_CLIENT_ERROR, - error->errorCode - ? base::UTF8ToUTF16(absl::StrFormat( - " (%s=%d)", l10n_util::GetStringUTF8(IDS_BRAVE_ACCOUNT_ERROR), - static_cast(*error->errorCode))) - : u""); + base::UTF8ToUTF16(absl::StrFormat( + " (%s=%d)", error_label, + static_cast(error->get_client_error()->error_code)))); } - // server-side error - if (error->errorCode) { - if (const auto* string_id = - base::FindOrNull(kErrorStrings, *error->errorCode)) { - return l10n_util::GetNSString(*string_id); - } + const auto& server_error = error->get_server_error(); + if (const auto* string_id = + base::FindOrNull(kServerErrorStrings, server_error->error_code)) { + return l10n_util::GetNSString(*string_id); } return l10n_util::GetNSStringF( IDS_BRAVE_ACCOUNT_SERVER_ERROR, base::UTF8ToUTF16(absl::StrFormat( - "%s=%d", *error->netErrorOrHttpStatus > 0 ? "HTTP" : "NET", - *error->netErrorOrHttpStatus)), - error->errorCode - ? base::UTF8ToUTF16(absl::StrFormat( - ", %s=%d", l10n_util::GetStringUTF8(IDS_BRAVE_ACCOUNT_ERROR), - static_cast(*error->errorCode))) - : u""); + "%s=%d", server_error->net_error_or_http_status > 0 ? "HTTP" : "NET", + server_error->net_error_or_http_status)), + base::UTF8ToUTF16(absl::StrFormat( + ", %s=%d", error_label, static_cast(server_error->error_code)))); } } // namespace