diff --git a/native/account/log-in-panel.react.js b/native/account/log-in-panel.react.js --- a/native/account/log-in-panel.react.js +++ b/native/account/log-in-panel.react.js @@ -339,7 +339,7 @@ } catch (e) { const messageForException = getMessageForException(e); if ( - messageForException === 'user not found' || + messageForException === 'user_not_found' || messageForException === 'login failed' ) { Alert.alert( diff --git a/native/account/registration/registration-server-call.js b/native/account/registration/registration-server-call.js --- a/native/account/registration/registration-server-call.js +++ b/native/account/registration/registration-server-call.js @@ -119,14 +119,14 @@ ); } catch (e) { const messageForException = getMessageForException(e); - if (messageForException === 'username reserved') { + if (messageForException === 'username_reserved') { Alert.alert( usernameReservedAlertDetails.title, usernameReservedAlertDetails.message, [{ text: 'OK', onPress: onAlertAcknowledged }], { cancelable: !onAlertAcknowledged }, ); - } else if (messageForException === 'username already exists') { + } else if (messageForException === 'username_already_exists') { Alert.alert( usernameTakenAlertDetails.title, usernameTakenAlertDetails.message, diff --git a/services/identity/src/client_service.rs b/services/identity/src/client_service.rs --- a/services/identity/src/client_service.rs +++ b/services/identity/src/client_service.rs @@ -13,7 +13,7 @@ // Workspace crate imports use crate::config::CONFIG; -use crate::constants::error_types; +use crate::constants::{error_types, tonic_status_messages}; use crate::database::{ DBDeviceTypeInt, DatabaseClient, DeviceType, KeyPayload }; @@ -124,11 +124,15 @@ .map_err(handle_db_error)?; if username_in_reserved_usernames_table { - return Err(tonic::Status::already_exists("username already exists")); + return Err(tonic::Status::already_exists( + tonic_status_messages::USERNAME_ALREADY_EXISTS, + )); } if RESERVED_USERNAME_SET.contains(&message.username) { - return Err(tonic::Status::invalid_argument("username reserved")); + return Err(tonic::Status::invalid_argument( + tonic_status_messages::USERNAME_RESERVED, + )); } if let Some(fid) = &message.farcaster_id { @@ -173,7 +177,9 @@ self.check_username_taken(&message.username).await?; if RESERVED_USERNAME_SET.contains(&message.username) { - return Err(tonic::Status::invalid_argument("username reserved")); + return Err(tonic::Status::invalid_argument( + tonic_status_messages::USERNAME_RESERVED, + )); } let username_in_reserved_usernames_table = self @@ -317,7 +323,9 @@ )); } - return Err(tonic::Status::not_found("user not found")); + return Err(tonic::Status::not_found( + tonic_status_messages::USER_NOT_FOUND, + )); }; let flattened_device_key_upload = @@ -481,7 +489,9 @@ )); } - return Err(tonic::Status::not_found("user not found")); + return Err(tonic::Status::not_found( + tonic_status_messages::USER_NOT_FOUND, + )); }; self @@ -563,7 +573,7 @@ if username_in_reserved_usernames_table { return Err(tonic::Status::already_exists( - "wallet address already exists", + tonic_status_messages::WALLET_ADDRESS_TAKEN, )); } @@ -646,7 +656,7 @@ .map_err(handle_db_error)?; if !wallet_address_in_reserved_usernames_table { return Err(tonic::Status::permission_denied( - "wallet address not reserved", + tonic_status_messages::WALLET_ADDRESS_NOT_RESERVED, )); } @@ -727,7 +737,9 @@ .get_user_identity(&user_id) .await .map_err(handle_db_error)? - .ok_or_else(|| tonic::Status::not_found("user not found"))?; + .ok_or_else(|| { + tonic::Status::not_found(tonic_status_messages::USER_NOT_FOUND) + })?; let Some(device_list) = self .client @@ -799,9 +811,10 @@ self.client.get_user_identity(&user_id), self.client.get_current_device_list(&user_id) ); - let user_identity = identity_response - .map_err(handle_db_error)? - .ok_or_else(|| tonic::Status::not_found("user not found"))?; + let user_identity = + identity_response.map_err(handle_db_error)?.ok_or_else(|| { + tonic::Status::not_found(tonic_status_messages::USER_NOT_FOUND) + })?; let device_list = device_list_response .map_err(handle_db_error)? @@ -1008,7 +1021,9 @@ .await .map_err(handle_db_error)?; if username_taken { - return Err(tonic::Status::already_exists("username already exists")); + return Err(tonic::Status::already_exists( + tonic_status_messages::USERNAME_ALREADY_EXISTS, + )); } Ok(()) } @@ -1024,7 +1039,7 @@ .map_err(handle_db_error)?; if wallet_address_taken { return Err(tonic::Status::already_exists( - "wallet address already exists", + tonic_status_messages::WALLET_ADDRESS_TAKEN, )); } Ok(()) @@ -1174,8 +1189,9 @@ fn construct_flattened_device_key_upload( message: &impl DeviceKeyUploadActions, ) -> Result { - let key_info = KeyPayload::from_str(&message.payload()?) - .map_err(|_| tonic::Status::invalid_argument("malformed payload"))?; + let key_info = KeyPayload::from_str(&message.payload()?).map_err(|_| { + tonic::Status::invalid_argument(tonic_status_messages::MALFORMED_PAYLOAD) + })?; let flattened_device_key_upload = FlattenedDeviceKeyUpload { device_id_key: key_info.primary_identity_public_keys.ed25519, diff --git a/services/identity/src/constants.rs b/services/identity/src/constants.rs --- a/services/identity/src/constants.rs +++ b/services/identity/src/constants.rs @@ -221,6 +221,16 @@ // Tonic Status Messages pub mod tonic_status_messages { pub const UNEXPECTED_MESSAGE_DATA: &str = "unexpected_message_data"; + pub const SIGNATURE_INVALID: &str = "signature_invalid"; + pub const MALFORMED_KEY: &str = "malformed_key"; + pub const VERIFICATION_FAILED: &str = "verification_failed"; + pub const MALFORMED_PAYLOAD: &str = "malformed_payload"; + pub const INVALID_DEVICE_LIST_PAYLOAD: &str = "invalid_device_list_payload"; + pub const USERNAME_ALREADY_EXISTS: &str = "username_already_exists"; + pub const USERNAME_RESERVED: &str = "username_reserved"; + pub const WALLET_ADDRESS_TAKEN: &str = "wallet_address_taken"; + pub const WALLET_ADDRESS_NOT_RESERVED: &str = "wallet_address_not_reserved"; + pub const USER_NOT_FOUND: &str = "user_not_found"; } // Tunnelbroker diff --git a/services/identity/src/device_list.rs b/services/identity/src/device_list.rs --- a/services/identity/src/device_list.rs +++ b/services/identity/src/device_list.rs @@ -3,7 +3,9 @@ use tracing::{debug, error, warn}; use crate::{ - constants::{error_types, DEVICE_LIST_TIMESTAMP_VALID_FOR}, + constants::{ + error_types, tonic_status_messages, DEVICE_LIST_TIMESTAMP_VALID_FOR, + }, database::{DeviceListRow, DeviceListUpdate}, error::DeviceListError, grpc_services::protos::auth::UpdateDeviceListRequest, @@ -44,7 +46,9 @@ serde_json::from_str(&self.raw_device_list.replace(r#"\""#, r#"""#)) .map_err(|err| { warn!("Failed to deserialize raw device list: {}", err); - tonic::Status::invalid_argument("invalid device list payload") + tonic::Status::invalid_argument( + tonic_status_messages::INVALID_DEVICE_LIST_PAYLOAD, + ) }) } @@ -89,7 +93,9 @@ fn try_from(request: UpdateDeviceListRequest) -> Result { request.new_device_list.parse().map_err(|err| { warn!("Failed to deserialize device list update: {}", err); - tonic::Status::invalid_argument("invalid device list payload") + tonic::Status::invalid_argument( + tonic_status_messages::INVALID_DEVICE_LIST_PAYLOAD, + ) }) } } diff --git a/services/identity/src/grpc_services/authenticated.rs b/services/identity/src/grpc_services/authenticated.rs --- a/services/identity/src/grpc_services/authenticated.rs +++ b/services/identity/src/grpc_services/authenticated.rs @@ -6,7 +6,7 @@ use crate::error::consume_error; use crate::{ client_service::{handle_db_error, UpdateState, WorkflowInProgress}, - constants::{error_types, request_metadata}, + constants::{error_types, request_metadata, tonic_status_messages}, database::DatabaseClient, grpc_services::shared::{get_platform_metadata, get_value}, }; @@ -134,7 +134,9 @@ .get_keys_for_user(user_id, true) .await .map_err(handle_db_error)? - .ok_or_else(|| tonic::Status::not_found("user not found"))?; + .ok_or_else(|| { + tonic::Status::not_found(tonic_status_messages::USER_NOT_FOUND) + })?; let transformed_devices = devices_map .into_iter() @@ -159,7 +161,9 @@ .get_keys_for_user(user_id, false) .await .map_err(handle_db_error)? - .ok_or_else(|| tonic::Status::not_found("user not found"))?; + .ok_or_else(|| { + tonic::Status::not_found(tonic_status_messages::USER_NOT_FOUND) + })?; let transformed_devices = devices_map .into_iter() @@ -171,7 +175,9 @@ .get_user_identity(user_id) .await .map_err(handle_db_error)? - .ok_or_else(|| tonic::Status::not_found("user not found"))?; + .ok_or_else(|| { + tonic::Status::not_found(tonic_status_messages::USER_NOT_FOUND) + })?; Ok(tonic::Response::new(InboundKeysForUserResponse { devices: transformed_devices, @@ -191,7 +197,9 @@ .get_user_identity(&message.user_id) .await .map_err(handle_db_error)? - .ok_or_else(|| tonic::Status::not_found("user not found"))?; + .ok_or_else(|| { + tonic::Status::not_found(tonic_status_messages::USER_NOT_FOUND) + })?; let Some(keyserver_info) = self .db_client @@ -450,7 +458,9 @@ let Some((username, password_file_bytes)) = maybe_username_and_password_file else { - return Err(tonic::Status::not_found("user not found")); + return Err(tonic::Status::not_found( + tonic_status_messages::USER_NOT_FOUND, + )); }; let mut server_login = comm_opaque2::server::Login::new(); diff --git a/services/identity/src/grpc_utils.rs b/services/identity/src/grpc_utils.rs --- a/services/identity/src/grpc_utils.rs +++ b/services/identity/src/grpc_utils.rs @@ -67,21 +67,30 @@ ) -> Result<(), Status> { let signature_bytes = general_purpose::STANDARD_NO_PAD .decode(signature) - .map_err(|_| Status::invalid_argument("signature invalid"))?; + .map_err(|_| { + Status::invalid_argument(tonic_status_messages::SIGNATURE_INVALID) + })?; - let signature = Signature::from_bytes(&signature_bytes) - .map_err(|_| Status::invalid_argument("signature invalid"))?; + let signature = Signature::from_bytes(&signature_bytes).map_err(|_| { + Status::invalid_argument(tonic_status_messages::SIGNATURE_INVALID) + })?; let public_key_bytes = general_purpose::STANDARD_NO_PAD .decode(signing_public_key) - .map_err(|_| Status::failed_precondition("malformed key"))?; + .map_err(|_| { + Status::failed_precondition(tonic_status_messages::MALFORMED_KEY) + })?; let public_key: PublicKey = PublicKey::from_bytes(&public_key_bytes) - .map_err(|_| Status::failed_precondition("malformed key"))?; + .map_err(|_| { + Status::failed_precondition(tonic_status_messages::MALFORMED_KEY) + })?; public_key .verify(message.as_bytes(), &signature) - .map_err(|_| Status::permission_denied("verification failed"))?; + .map_err(|_| { + Status::permission_denied(tonic_status_messages::VERIFICATION_FAILED) + })?; Ok(()) } @@ -312,13 +321,14 @@ } let signed_list: SignedDeviceList = payload.parse().map_err(|err| { warn!("Failed to deserialize initial device list: {}", err); - tonic::Status::invalid_argument("invalid device list payload") + tonic::Status::invalid_argument( + tonic_status_messages::INVALID_DEVICE_LIST_PAYLOAD, + ) })?; - let key_info = self - .payload()? - .parse::() - .map_err(|_| tonic::Status::invalid_argument("malformed payload"))?; + let key_info = self.payload()?.parse::().map_err(|_| { + tonic::Status::invalid_argument(tonic_status_messages::MALFORMED_PAYLOAD) + })?; let primary_device_id = key_info.primary_identity_public_keys.ed25519; let update_payload = DeviceListUpdate::try_from(signed_list.clone())?; diff --git a/web/account/siwe-login-form.react.js b/web/account/siwe-login-form.react.js --- a/web/account/siwe-login-form.react.js +++ b/web/account/siwe-login-form.react.js @@ -174,7 +174,7 @@ return await walletLogIn(walletAddress, siweMessage, siweSignature); } catch (e) { const messageForException = getMessageForException(e); - if (messageForException === 'user not found') { + if (messageForException === 'user_not_found') { setError('account_does_not_exist'); } else if ( messageForException === 'client_version_unsupported' || diff --git a/web/account/traditional-login-form.react.js b/web/account/traditional-login-form.react.js --- a/web/account/traditional-login-form.react.js +++ b/web/account/traditional-login-form.react.js @@ -128,7 +128,7 @@ setPassword(''); const messageForException = getMessageForException(e); if ( - messageForException === 'user not found' || + messageForException === 'user_not_found' || messageForException === 'login failed' ) { setErrorMessage('incorrect username or password');