Changeset View
Changeset View
Standalone View
Standalone View
services/identity/src/database.rs
Show First 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | use crate::constants::{ | ||||
ACCESS_TOKEN_TABLE_AUTH_TYPE_ATTRIBUTE, ACCESS_TOKEN_TABLE_CREATED_ATTRIBUTE, | ACCESS_TOKEN_TABLE_AUTH_TYPE_ATTRIBUTE, ACCESS_TOKEN_TABLE_CREATED_ATTRIBUTE, | ||||
ACCESS_TOKEN_TABLE_PARTITION_KEY, ACCESS_TOKEN_TABLE_TOKEN_ATTRIBUTE, | ACCESS_TOKEN_TABLE_PARTITION_KEY, ACCESS_TOKEN_TABLE_TOKEN_ATTRIBUTE, | ||||
ACCESS_TOKEN_TABLE_VALID_ATTRIBUTE, NONCE_TABLE, | ACCESS_TOKEN_TABLE_VALID_ATTRIBUTE, NONCE_TABLE, | ||||
NONCE_TABLE_CREATED_ATTRIBUTE, NONCE_TABLE_EXPIRATION_TIME_ATTRIBUTE, | NONCE_TABLE_CREATED_ATTRIBUTE, NONCE_TABLE_EXPIRATION_TIME_ATTRIBUTE, | ||||
NONCE_TABLE_EXPIRATION_TIME_UNIX_ATTRIBUTE, NONCE_TABLE_PARTITION_KEY, | NONCE_TABLE_EXPIRATION_TIME_UNIX_ATTRIBUTE, NONCE_TABLE_PARTITION_KEY, | ||||
RESERVED_USERNAMES_TABLE, RESERVED_USERNAMES_TABLE_PARTITION_KEY, | RESERVED_USERNAMES_TABLE, RESERVED_USERNAMES_TABLE_PARTITION_KEY, | ||||
RESERVED_USERNAMES_TABLE_USER_ID_ATTRIBUTE, USERS_TABLE, | RESERVED_USERNAMES_TABLE_USER_ID_ATTRIBUTE, USERS_TABLE, | ||||
USERS_TABLE_DEVICES_MAP_DEVICE_TYPE_ATTRIBUTE_NAME, | USERS_TABLE_DEVICES_MAP_DEVICE_TYPE_ATTRIBUTE_NAME, | ||||
USERS_TABLE_PARTITION_KEY, USERS_TABLE_REGISTRATION_ATTRIBUTE, | USERS_TABLE_FARCASTER_ID_ATTRIBUTE_NAME, USERS_TABLE_PARTITION_KEY, | ||||
USERS_TABLE_USERNAME_ATTRIBUTE, USERS_TABLE_USERNAME_INDEX, | USERS_TABLE_REGISTRATION_ATTRIBUTE, USERS_TABLE_USERNAME_ATTRIBUTE, | ||||
USERS_TABLE_WALLET_ADDRESS_ATTRIBUTE, USERS_TABLE_WALLET_ADDRESS_INDEX, | USERS_TABLE_USERNAME_INDEX, USERS_TABLE_WALLET_ADDRESS_ATTRIBUTE, | ||||
USERS_TABLE_WALLET_ADDRESS_INDEX, | |||||
}; | }; | ||||
use crate::id::generate_uuid; | use crate::id::generate_uuid; | ||||
use crate::nonce::NonceData; | use crate::nonce::NonceData; | ||||
use crate::token::{AccessTokenData, AuthType}; | use crate::token::{AccessTokenData, AuthType}; | ||||
pub use grpc_clients::identity::DeviceType; | pub use grpc_clients::identity::DeviceType; | ||||
mod device_list; | mod device_list; | ||||
mod workflows; | mod workflows; | ||||
▲ Show 20 Lines • Show All 105 Lines • ▼ Show 20 Lines | impl DatabaseClient { | ||||
) -> Result<String, Error> { | ) -> Result<String, Error> { | ||||
let device_key_upload = registration_state.flattened_device_key_upload; | let device_key_upload = registration_state.flattened_device_key_upload; | ||||
let user_id = self | let user_id = self | ||||
.add_user_to_users_table( | .add_user_to_users_table( | ||||
device_key_upload.clone(), | device_key_upload.clone(), | ||||
Some((registration_state.username, Blob::new(password_file))), | Some((registration_state.username, Blob::new(password_file))), | ||||
None, | None, | ||||
registration_state.user_id, | registration_state.user_id, | ||||
registration_state.farcaster_id, | |||||
) | ) | ||||
.await?; | .await?; | ||||
self | self | ||||
.add_device( | .add_device( | ||||
&user_id, | &user_id, | ||||
device_key_upload, | device_key_upload, | ||||
code_version, | code_version, | ||||
access_token_creation_time, | access_token_creation_time, | ||||
) | ) | ||||
.await?; | .await?; | ||||
Ok(user_id) | Ok(user_id) | ||||
} | } | ||||
pub async fn add_wallet_user_to_users_table( | pub async fn add_wallet_user_to_users_table( | ||||
&self, | &self, | ||||
flattened_device_key_upload: FlattenedDeviceKeyUpload, | flattened_device_key_upload: FlattenedDeviceKeyUpload, | ||||
wallet_address: String, | wallet_address: String, | ||||
social_proof: String, | social_proof: String, | ||||
user_id: Option<String>, | user_id: Option<String>, | ||||
code_version: u64, | code_version: u64, | ||||
access_token_creation_time: DateTime<Utc>, | access_token_creation_time: DateTime<Utc>, | ||||
farcaster_id: Option<String>, | |||||
) -> Result<String, Error> { | ) -> Result<String, Error> { | ||||
let wallet_identity = EthereumIdentity { | let wallet_identity = EthereumIdentity { | ||||
wallet_address, | wallet_address, | ||||
social_proof, | social_proof, | ||||
}; | }; | ||||
let user_id = self | let user_id = self | ||||
.add_user_to_users_table( | .add_user_to_users_table( | ||||
flattened_device_key_upload.clone(), | flattened_device_key_upload.clone(), | ||||
None, | None, | ||||
Some(wallet_identity), | Some(wallet_identity), | ||||
user_id, | user_id, | ||||
farcaster_id, | |||||
) | ) | ||||
.await?; | .await?; | ||||
self | self | ||||
.add_device( | .add_device( | ||||
&user_id, | &user_id, | ||||
flattened_device_key_upload, | flattened_device_key_upload, | ||||
code_version, | code_version, | ||||
access_token_creation_time, | access_token_creation_time, | ||||
) | ) | ||||
.await?; | .await?; | ||||
Ok(user_id) | Ok(user_id) | ||||
} | } | ||||
async fn add_user_to_users_table( | async fn add_user_to_users_table( | ||||
&self, | &self, | ||||
flattened_device_key_upload: FlattenedDeviceKeyUpload, | flattened_device_key_upload: FlattenedDeviceKeyUpload, | ||||
username_and_password_file: Option<(String, Blob)>, | username_and_password_file: Option<(String, Blob)>, | ||||
wallet_identity: Option<EthereumIdentity>, | wallet_identity: Option<EthereumIdentity>, | ||||
user_id: Option<String>, | user_id: Option<String>, | ||||
farcaster_id: Option<String>, | |||||
) -> Result<String, Error> { | ) -> Result<String, Error> { | ||||
let user_id = user_id.unwrap_or_else(generate_uuid); | let user_id = user_id.unwrap_or_else(generate_uuid); | ||||
let mut user = HashMap::from([( | let mut user = HashMap::from([( | ||||
USERS_TABLE_PARTITION_KEY.to_string(), | USERS_TABLE_PARTITION_KEY.to_string(), | ||||
AttributeValue::S(user_id.clone()), | AttributeValue::S(user_id.clone()), | ||||
)]); | )]); | ||||
if let Some((username, password_file)) = username_and_password_file { | if let Some((username, password_file)) = username_and_password_file { | ||||
Show All 13 Lines | if let Some(eth_identity) = wallet_identity { | ||||
AttributeValue::S(eth_identity.wallet_address), | AttributeValue::S(eth_identity.wallet_address), | ||||
); | ); | ||||
user.insert( | user.insert( | ||||
USERS_TABLE_SOCIAL_PROOF_ATTRIBUTE_NAME.to_string(), | USERS_TABLE_SOCIAL_PROOF_ATTRIBUTE_NAME.to_string(), | ||||
AttributeValue::S(eth_identity.social_proof), | AttributeValue::S(eth_identity.social_proof), | ||||
); | ); | ||||
} | } | ||||
if let Some(fid) = farcaster_id { | |||||
user.insert( | |||||
USERS_TABLE_FARCASTER_ID_ATTRIBUTE_NAME.to_string(), | |||||
AttributeValue::S(fid), | |||||
); | |||||
} | |||||
self | self | ||||
.client | .client | ||||
.put_item() | .put_item() | ||||
.table_name(USERS_TABLE) | .table_name(USERS_TABLE) | ||||
.set_item(Some(user)) | .set_item(Some(user)) | ||||
// make sure we don't accidentaly overwrite existing row | // make sure we don't accidentaly overwrite existing row | ||||
.condition_expression("attribute_not_exists(#pk)") | .condition_expression("attribute_not_exists(#pk)") | ||||
.expression_attribute_names("#pk", USERS_TABLE_PARTITION_KEY) | .expression_attribute_names("#pk", USERS_TABLE_PARTITION_KEY) | ||||
▲ Show 20 Lines • Show All 1,167 Lines • Show Last 20 Lines |