Skip to content

Commit

Permalink
Email identity verification improvement (#3086)
Browse files Browse the repository at this point in the history
  • Loading branch information
silva-fj authored Sep 24, 2024
1 parent b6e8d80 commit 76f1833
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
6 changes: 5 additions & 1 deletion tee-worker/identity/enclave-runtime/src/rpc/common_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,11 @@ pub fn add_common_api<Author, GetterExecutor, AccessShieldingKey, OcallApi, Stat
);
let verification_code = email::generate_verification_code();

match email::VerificationCodeStore::insert(account_id, verification_code.clone()) {
match email::VerificationCodeStore::insert(
account_id,
email.clone(),
verification_code.clone(),
) {
Ok(_) => {
if email::send_verification_email(&mut mailer, email, verification_code)
.is_err()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,24 @@ pub struct VerificationCodeStore;
impl VerificationCodeStore {
pub fn insert(
account_id: AccountId,
email: String,
verification_code: String,
) -> Result<(), VerificationCodeStoreError> {
STORE
.write()
.map_err(|_| VerificationCodeStoreError::LockPoisoning)?
.put(hex::encode(account_id.encode()), verification_code);
.put(hex::encode((account_id, email).encode()), verification_code);
Ok(())
}

pub fn get(account_id: &AccountId) -> Result<Option<String>, VerificationCodeStoreError> {
pub fn get(
account_id: &AccountId,
email: &str,
) -> Result<Option<String>, VerificationCodeStoreError> {
let code = STORE
.write()
.map_err(|_| VerificationCodeStoreError::LockPoisoning)?
.pop(hex::encode(account_id.encode()).as_str());
.pop(hex::encode((account_id, email).encode()).as_str());
Ok(code)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,23 @@ pub fn verify(
let Some(account_id) = who.to_account_id() else {
return Err(Error::LinkIdentityFailed(ErrorDetail::ParseError));
};
let stored_verification_code = match email::VerificationCodeStore::get(&account_id) {
Ok(data) => data.ok_or_else(|| {
Error::LinkIdentityFailed(ErrorDetail::StfError(ErrorString::truncate_from(
std::format!(
"no verification code found for {}",
account_id_to_string(&account_id)
)
.as_bytes()
.to_vec(),
)))
})?,
Err(e) => return Err(Error::LinkIdentityFailed(e.into_error_detail())),
};
let stored_verification_code =
match email::VerificationCodeStore::get(&account_id, &email) {
Ok(data) => data.ok_or_else(|| {
Error::LinkIdentityFailed(ErrorDetail::StfError(
ErrorString::truncate_from(
std::format!(
"no verification code found for {}:{}",
account_id_to_string(&account_id),
&email
)
.as_bytes()
.to_vec(),
),
))
})?,
Err(e) => return Err(Error::LinkIdentityFailed(e.into_error_detail())),
};

ensure!(
verification_code == stored_verification_code,
Expand Down

0 comments on commit 76f1833

Please sign in to comment.