From f2b078b92af3f0e77c4aad60d8e1757ef8f6dceb Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Tue, 3 Sep 2024 13:59:10 +0000 Subject: [PATCH 01/29] refactoring twitter helpers, extracting general web2 helpers --- .../src/web2/{twitter => }/helpers.rs | 21 ++-------------- .../identity-verification/src/web2/mod.rs | 3 ++- .../src/web2/twitter/mod.rs | 24 +++++++++++++++---- 3 files changed, 23 insertions(+), 25 deletions(-) rename tee-worker/litentry/core/identity-verification/src/web2/{twitter => }/helpers.rs (58%) diff --git a/tee-worker/litentry/core/identity-verification/src/web2/twitter/helpers.rs b/tee-worker/litentry/core/identity-verification/src/web2/helpers.rs similarity index 58% rename from tee-worker/litentry/core/identity-verification/src/web2/twitter/helpers.rs rename to tee-worker/litentry/core/identity-verification/src/web2/helpers.rs index 64cc5cd89a..59cebfd8b2 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/twitter/helpers.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/helpers.rs @@ -4,26 +4,9 @@ extern crate sgx_rand as rand; extern crate sgx_tstd as std; use rand::{thread_rng, Rng}; -use std::{string::String, vec::Vec}; +use std::string::String; -use crate::{Error, Result}; -use lc_data_providers::twitter_official::Tweet; -use litentry_primitives::{self, ErrorDetail}; - -pub(crate) fn get_code_verifier() -> String { - get_random_string(128) -} - -pub(crate) fn get_state_verifier() -> String { - get_random_string(32) -} - -pub(crate) fn payload_from_tweet(tweet: &Tweet) -> Result> { - hex::decode(tweet.text.strip_prefix("0x").unwrap_or(tweet.text.as_str())) - .map_err(|_| Error::LinkIdentityFailed(ErrorDetail::ParseError)) -} - -fn get_random_string(length: usize) -> String { +pub(crate) fn get_random_string(length: usize) -> String { let mut rng = thread_rng(); let charset: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; let random_string: String = (0..length) diff --git a/tee-worker/litentry/core/identity-verification/src/web2/mod.rs b/tee-worker/litentry/core/identity-verification/src/web2/mod.rs index 3b74f16506..250cbdeb97 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/mod.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/mod.rs @@ -21,6 +21,7 @@ extern crate sgx_tstd as std; compile_error!("feature \"std\" and feature \"sgx\" cannot be enabled at the same time"); mod discord; +mod helpers; pub mod twitter; use crate::{ensure, Error, Result}; @@ -69,7 +70,7 @@ pub fn verify( .query_user_by_id(user_id.into_bytes()) .map_err(|e| Error::LinkIdentityFailed(e.into_error_detail()))?; - let payload = twitter::helpers::payload_from_tweet(&tweet)?; + let payload = twitter::payload_from_tweet(&tweet)?; ensure!( payload.as_slice() == raw_msg, Error::LinkIdentityFailed(ErrorDetail::UnexpectedMessage) diff --git a/tee-worker/litentry/core/identity-verification/src/web2/twitter/mod.rs b/tee-worker/litentry/core/identity-verification/src/web2/twitter/mod.rs index 7d1e09594d..25df0e821f 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/twitter/mod.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/twitter/mod.rs @@ -1,11 +1,12 @@ mod oauth_store; pub use oauth_store::*; -pub(crate) mod helpers; - +use crate::{web2::helpers, Error, Result}; use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine}; +use lc_data_providers::twitter_official::Tweet; +use litentry_primitives::{self, ErrorDetail}; use sp_core::hashing::sha2_256; -use std::{format, string::String}; +use std::{format, string::String, vec::Vec}; #[derive(Debug)] pub struct AuthorizeData { @@ -18,8 +19,8 @@ const BASE_URL: &str = "https://twitter.com/i/oauth2/authorize"; const SCOPES: &str = "tweet.read%20users.read"; pub fn get_authorize_data(client_id: &str, redirect_uri: &str) -> AuthorizeData { - let state = helpers::get_state_verifier(); - let code_verifier = helpers::get_code_verifier(); + let state = get_state_verifier(); + let code_verifier = get_code_verifier(); let code_verifier_hash = sha2_256(code_verifier.as_bytes()); let code_challenge = URL_SAFE_NO_PAD.encode(code_verifier_hash); @@ -36,6 +37,19 @@ pub fn get_authorize_data(client_id: &str, redirect_uri: &str) -> AuthorizeData AuthorizeData { authorize_url, code_verifier, state } } +pub fn payload_from_tweet(tweet: &Tweet) -> Result> { + hex::decode(tweet.text.strip_prefix("0x").unwrap_or(tweet.text.as_str())) + .map_err(|_| Error::LinkIdentityFailed(ErrorDetail::ParseError)) +} + +fn get_code_verifier() -> String { + helpers::get_random_string(128) +} + +fn get_state_verifier() -> String { + helpers::get_random_string(32) +} + #[cfg(test)] mod tests { use super::*; From 1788243ff5d9c448ca890ec4338b2b37f0f2e194 Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Mon, 9 Sep 2024 12:52:41 +0000 Subject: [PATCH 02/29] adding dependencies to lc-identity-verification --- tee-worker/Cargo.lock | 79 ++++++++++--------- tee-worker/enclave-runtime/Cargo.lock | 20 +++-- .../core/identity-verification/Cargo.toml | 14 +++- 3 files changed, 68 insertions(+), 45 deletions(-) diff --git a/tee-worker/Cargo.lock b/tee-worker/Cargo.lock index 0a9fd4f4ea..d3e9ac19de 100644 --- a/tee-worker/Cargo.lock +++ b/tee-worker/Cargo.lock @@ -722,7 +722,7 @@ dependencies = [ "num-traits 0.2.16", "serde 1.0.204", "wasm-bindgen", - "windows-targets 0.52.5", + "windows-targets 0.52.6", ] [[package]] @@ -1101,9 +1101,9 @@ dependencies = [ [[package]] name = "data-encoding" -version = "2.4.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2e66c9d817f1720209181c316d28635c050fa304f9c79e47a520882661b7308" +checksum = "e8566979429cf69b49a5c740c60791108e86440e8be149bbea4fe54d2c32d6e2" [[package]] name = "der" @@ -3270,7 +3270,7 @@ name = "itc-rpc-server" version = "0.1.0" dependencies = [ "anyhow", - "env_logger 0.9.3", + "env_logger 0.10.0", "its-peer-fetch", "its-primitives", "its-storage", @@ -4615,7 +4615,10 @@ dependencies = [ "futures 0.3.8", "hex", "http 0.2.1", + "http 0.2.9", + "http_req 0.8.1 (git+https://github.com/integritee-network/http_req?branch=master)", "http_req 0.8.1 (git+https://github.com/integritee-network/http_req)", + "itc-rest-client", "itp-sgx-crypto", "itp-utils", "lazy_static", @@ -4626,11 +4629,13 @@ dependencies = [ "lru", "parity-scale-codec", "rand 0.8.5", + "serde 1.0.204", + "serde_json 1.0.103", "sgx_rand", "sgx_tstd", "sp-core", "thiserror 1.0.9", - "url 2.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "url 2.5.0 (git+https://github.com/domenukk/rust-url?branch=no_std)", ] [[package]] @@ -4797,9 +4802,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.147" +version = "0.2.158" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" +checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" [[package]] name = "libloading" @@ -5332,9 +5337,9 @@ dependencies = [ [[package]] name = "mio" -version = "0.8.8" +version = "0.8.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2" +checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c" dependencies = [ "libc", "wasi 0.11.0+wasi-snapshot-preview1", @@ -6414,9 +6419,9 @@ dependencies = [ [[package]] name = "pin-project-lite" -version = "0.2.10" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c40d25201921e5ff0c862a505c6557ea88568a4e3ace775ab55e93f2f4f9d57" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" [[package]] name = "pin-utils" @@ -8968,7 +8973,7 @@ dependencies = [ "backtrace", "bytes 1.4.0", "libc", - "mio 0.8.8", + "mio 0.8.11", "num_cpus 1.16.0", "parking_lot 0.12.1", "pin-project-lite", @@ -10019,18 +10024,18 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm 0.52.5", - "windows_aarch64_msvc 0.52.5", - "windows_i686_gnu 0.52.5", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", "windows_i686_gnullvm", - "windows_i686_msvc 0.52.5", - "windows_x86_64_gnu 0.52.5", - "windows_x86_64_gnullvm 0.52.5", - "windows_x86_64_msvc 0.52.5", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] [[package]] @@ -10047,9 +10052,9 @@ checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" [[package]] name = "windows_aarch64_gnullvm" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" @@ -10065,9 +10070,9 @@ checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" [[package]] name = "windows_aarch64_msvc" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" @@ -10083,15 +10088,15 @@ checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" [[package]] name = "windows_i686_gnu" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" [[package]] name = "windows_i686_gnullvm" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" @@ -10107,9 +10112,9 @@ checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" [[package]] name = "windows_i686_msvc" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" @@ -10125,9 +10130,9 @@ checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" [[package]] name = "windows_x86_64_gnu" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" @@ -10143,9 +10148,9 @@ checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" [[package]] name = "windows_x86_64_gnullvm" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" @@ -10161,9 +10166,9 @@ checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" [[package]] name = "windows_x86_64_msvc" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" diff --git a/tee-worker/enclave-runtime/Cargo.lock b/tee-worker/enclave-runtime/Cargo.lock index 3cb495b83b..9ad415caa6 100644 --- a/tee-worker/enclave-runtime/Cargo.lock +++ b/tee-worker/enclave-runtime/Cargo.lock @@ -506,11 +506,11 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.83" +version = "1.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" +checksum = "e9d013ecb737093c0e86b151a7b837993cf9ec6c502946cfb44bedc392421e0b" dependencies = [ - "libc", + "shlex", ] [[package]] @@ -727,9 +727,9 @@ dependencies = [ [[package]] name = "data-encoding" -version = "2.4.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2e66c9d817f1720209181c316d28635c050fa304f9c79e47a520882661b7308" +checksum = "e8566979429cf69b49a5c740c60791108e86440e8be149bbea4fe54d2c32d6e2" [[package]] name = "der" @@ -3138,6 +3138,7 @@ dependencies = [ "hex", "http", "http_req", + "itc-rest-client", "itp-sgx-crypto", "itp-utils", "lazy_static", @@ -3147,10 +3148,13 @@ dependencies = [ "log", "lru", "parity-scale-codec", + "serde 1.0.204", + "serde_json 1.0.103", "sgx_rand", "sgx_tstd", "sp-core", "thiserror", + "url 2.5.0", ] [[package]] @@ -4995,6 +4999,12 @@ dependencies = [ "keccak", ] +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + [[package]] name = "signature" version = "2.1.0" diff --git a/tee-worker/litentry/core/identity-verification/Cargo.toml b/tee-worker/litentry/core/identity-verification/Cargo.toml index ea71cc5514..63e849b308 100644 --- a/tee-worker/litentry/core/identity-verification/Cargo.toml +++ b/tee-worker/litentry/core/identity-verification/Cargo.toml @@ -10,6 +10,8 @@ base64 = { version = "0.22", default-features = false, features = ["alloc"] } hex = { version = "0.4.3", default-features = false } lru = "0.12.3" rand = { version = "0.8", optional = true } +http = { version = "0.2", optional = true } +http_req = { optional = true, features = ["rust-tls"], branch = "master", git = "https://github.com/integritee-network/http_req" } # sgx dependencies futures_sgx = { package = "futures", git = "https://github.com/mesalock-linux/futures-rs-sgx", optional = true } @@ -22,10 +24,14 @@ thiserror_sgx = { package = "thiserror", git = "https://github.com/mesalock-linu # no_std dependencies lazy_static = { version = "1.1.0", features = ["spin_no_std"] } log = { version = "0.4", default-features = false } +serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] } +serde_json = { version = "1.0", default-features = false, features = ["alloc"] } +url = { git = "https://github.com/domenukk/rust-url", branch = "no_std", default-features = false, features = ["alloc", "no_std_net"] } # internal dependencies itp-sgx-crypto = { path = "../../../core-primitives/sgx/crypto", default-features = false } itp-utils = { path = "../../../core-primitives/utils", default-features = false } +itc-rest-client = { path = "../../../core/rest-client", default-features = false } codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] } frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } @@ -35,9 +41,6 @@ lc-data-providers = { path = "../data-providers", default-features = false } lc-stf-task-sender = { path = "../stf-task/sender", default-features = false } litentry-primitives = { path = "../../primitives", default-features = false } -[dev-dependencies] -url = "2.5.0" - [features] default = ["std"] sgx = [ @@ -53,7 +56,12 @@ sgx = [ "lc-stf-task-sender/sgx", ] std = [ + "http", + "http_req", "log/std", + "serde/std", + "serde_json/std", + "url/std", "itp-sgx-crypto/std", "frame-support/std", "lc-data-providers/std", From deda07428a00e606054016786c3f8967c7ed3f2a Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Mon, 9 Sep 2024 12:55:31 +0000 Subject: [PATCH 03/29] adding email module to web2 --- .../core/identity-verification/src/web2/email/mod.rs | 7 +++++++ .../litentry/core/identity-verification/src/web2/mod.rs | 1 + 2 files changed, 8 insertions(+) create mode 100644 tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs new file mode 100644 index 0000000000..3b6cc6519a --- /dev/null +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs @@ -0,0 +1,7 @@ +use crate::web2::helpers; +use std::string::String; + +pub fn generate_verification_code() -> String { + helpers::get_random_string(32) +} + diff --git a/tee-worker/litentry/core/identity-verification/src/web2/mod.rs b/tee-worker/litentry/core/identity-verification/src/web2/mod.rs index 250cbdeb97..ba0cea7d32 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/mod.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/mod.rs @@ -21,6 +21,7 @@ extern crate sgx_tstd as std; compile_error!("feature \"std\" and feature \"sgx\" cannot be enabled at the same time"); mod discord; +pub mod email; mod helpers; pub mod twitter; From 7ebc4bb41535a6073240b5c85ea8fca95e8a0ef2 Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Mon, 9 Sep 2024 12:56:29 +0000 Subject: [PATCH 04/29] setting up email_verification_store --- .../web2/email/email_verification_store.rs | 35 +++++++++++++++++++ .../src/web2/email/mod.rs | 3 ++ 2 files changed, 38 insertions(+) create mode 100644 tee-worker/litentry/core/identity-verification/src/web2/email/email_verification_store.rs diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/email_verification_store.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/email_verification_store.rs new file mode 100644 index 0000000000..14d21cd35b --- /dev/null +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/email_verification_store.rs @@ -0,0 +1,35 @@ +use codec::Encode; +use core::result::Result; +use lazy_static::lazy_static; +use litentry_primitives::ParentchainAccountId as AccountId; +use lru::LruCache; +#[cfg(feature = "std")] +use std::sync::RwLock; +#[cfg(feature = "sgx")] +use std::sync::SgxRwLock as RwLock; +use std::{num::NonZeroUsize, string::String}; + +lazy_static! { + static ref STORE: RwLock> = + RwLock::new(LruCache::new(NonZeroUsize::new(250).unwrap())); +} + +pub struct EmailVerificationStore; + +impl EmailVerificationStore { + pub fn insert(account_id: AccountId, verification_code: String) -> Result<(), String> { + STORE + .write() + .map_err(|_| String::from("Lock poisoning"))? + .put(hex::encode(account_id.encode()), verification_code); + Ok(()) + } + + pub fn get(account_id: &AccountId) -> Result, String> { + let code = STORE + .write() + .map_err(|_| String::from("Lock poisoning"))? + .pop(hex::encode(account_id.encode()).as_str()); + Ok(code) + } +} diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs index 3b6cc6519a..9d71158ced 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs @@ -1,3 +1,6 @@ +mod email_verification_store; +pub use email_verification_store::*; + use crate::web2::helpers; use std::string::String; From 9992c1ab1bf629d13ff494abad0a8140f42ea398 Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Mon, 9 Sep 2024 12:57:15 +0000 Subject: [PATCH 05/29] adding mailer interface --- .../identity-verification/src/web2/email/mailer.rs | 11 +++++++++++ .../core/identity-verification/src/web2/email/mod.rs | 3 +++ 2 files changed, 14 insertions(+) create mode 100644 tee-worker/litentry/core/identity-verification/src/web2/email/mailer.rs diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/mailer.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer.rs new file mode 100644 index 0000000000..b2ccc2c9b2 --- /dev/null +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer.rs @@ -0,0 +1,11 @@ +use std::string::String; + +pub struct Mail { + pub to: String, + pub verification_code: String, + pub redirect_url: Option, +} + +pub trait Mailer { + fn send(&mut self, mail: Mail) -> Result<(), String>; +} diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs index 9d71158ced..b8e926b790 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs @@ -1,6 +1,9 @@ mod email_verification_store; pub use email_verification_store::*; +mod mailer; +pub use mailer::*; + use crate::web2::helpers; use std::string::String; From e7554bb9205744f871f41df14257734088ba5907 Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Tue, 10 Sep 2024 08:54:55 +0000 Subject: [PATCH 06/29] renaming email code verification store --- .../litentry/core/identity-verification/src/web2/email/mod.rs | 4 ++-- ...email_verification_store.rs => verification_code_store.rs} | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) rename tee-worker/litentry/core/identity-verification/src/web2/email/{email_verification_store.rs => verification_code_store.rs} (93%) diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs index b8e926b790..9c95966cde 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs @@ -1,5 +1,5 @@ -mod email_verification_store; -pub use email_verification_store::*; +mod verification_code_store; +pub use verification_code_store::*; mod mailer; pub use mailer::*; diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/email_verification_store.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/verification_code_store.rs similarity index 93% rename from tee-worker/litentry/core/identity-verification/src/web2/email/email_verification_store.rs rename to tee-worker/litentry/core/identity-verification/src/web2/email/verification_code_store.rs index 14d21cd35b..1ad03289dc 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/email/email_verification_store.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/verification_code_store.rs @@ -14,9 +14,9 @@ lazy_static! { RwLock::new(LruCache::new(NonZeroUsize::new(250).unwrap())); } -pub struct EmailVerificationStore; +pub struct VerificationCodeStore; -impl EmailVerificationStore { +impl VerificationCodeStore { pub fn insert(account_id: AccountId, verification_code: String) -> Result<(), String> { STORE .write() From 3dd1d441d69bddc1ccc668cce9884cfc4e84a14a Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Tue, 10 Sep 2024 08:56:02 +0000 Subject: [PATCH 07/29] refatoring mailer, setting up sendgrid mailer --- .../web2/email/{mailer.rs => mailer/mod.rs} | 2 + .../src/web2/email/mailer/sendgrid_mailer.rs | 123 ++++++++++++++++++ 2 files changed, 125 insertions(+) rename tee-worker/litentry/core/identity-verification/src/web2/email/{mailer.rs => mailer/mod.rs} (88%) create mode 100644 tee-worker/litentry/core/identity-verification/src/web2/email/mailer/sendgrid_mailer.rs diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/mailer.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/mod.rs similarity index 88% rename from tee-worker/litentry/core/identity-verification/src/web2/email/mailer.rs rename to tee-worker/litentry/core/identity-verification/src/web2/email/mailer/mod.rs index b2ccc2c9b2..8eb6c70fa9 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/email/mailer.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/mod.rs @@ -1,3 +1,5 @@ +pub mod sendgrid_mailer; + use std::string::String; pub struct Mail { diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/sendgrid_mailer.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/sendgrid_mailer.rs new file mode 100644 index 0000000000..49e3269928 --- /dev/null +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/sendgrid_mailer.rs @@ -0,0 +1,123 @@ +#[cfg(all(not(feature = "std"), feature = "sgx"))] +use crate::sgx_reexport_prelude::*; + +use super::{Mail, Mailer}; +use http::header::{AUTHORIZATION, CONTENT_TYPE}; +use http_req::response::Headers; +use itc_rest_client::{ + error::Error as HttpError, + http_client::{HttpClient, SendWithCertificateVerification}, + rest_client::RestClient, + RestGet, RestPath, RestPost, +}; +use serde::{Deserialize, Serialize}; +use std::{ + string::{String, ToString}, + vec, + vec::Vec, +}; +use url::Url; + +#[derive(Serialize)] +struct Personalization { + to: Vec, +} + +#[derive(Serialize, Clone)] +pub(crate) struct Email { + pub email: String, + pub name: Option, +} + +#[derive(Serialize)] +struct DynamicTemplateData { + redirect_url: String, +} + +#[derive(Serialize)] +struct SendGridEmail { + personalizations: Vec, + from: Email, + template_id: String, + dynamic_template_data: DynamicTemplateData, +} + +impl SendGridEmail { + pub fn new( + to: Email, + from: Email, + template_id: String, + verification_code: String, + redirect_url: String, + ) -> Self { + Self { + personalizations: vec![Personalization { to: vec![to] }], + from, + template_id, + dynamic_template_data: DynamicTemplateData { + redirect_url: std::format!( + "{}?verification_code={}", + redirect_url, + verification_code + ), + }, + } + } +} + +impl RestPath for SendGridEmail { + fn get_path(path: String) -> Result { + Ok(path) + } +} + +pub(crate) struct SendGridMailer { + api_key: String, + client: RestClient>, + from: Email, + template_id: String, +} + +impl SendGridMailer { + pub fn new(api_key: String, from_email: Email, template_id: String) -> Self { + let base_url = Url::parse("https://api.sendgrid.com/v3/mail/send").unwrap(); + let authorization = std::format!("Bearer {}", api_key); + + let mut headers = Headers::new(); + headers.insert(AUTHORIZATION.as_str(), &authorization); + headers.insert(CONTENT_TYPE.as_str(), "application/json"); + + Self { + api_key, + client: RestClient::new( + HttpClient::new( + SendWithCertificateVerification::new(vec![]), + true, + None, + Some(headers), + None, + ), + base_url, + ), + from: from_email, + template_id, + } + } +} + +impl Mailer for SendGridMailer { + fn send(&mut self, mail: Mail) -> Result<(), String> { + let sendgrid_email = SendGridEmail::new( + Email { email: mail.to, name: None }, + self.from.clone(), + self.template_id.clone(), + mail.redirect_url.unwrap_or_default(), + mail.verification_code, + ); + self.client + .post(String::default(), &sendgrid_email) + .map_err(|e| std::format!("Failed to send verification email: {:?}", e))?; + + Ok(()) + } +} From 49f32a8a5ac24828d0e908096e8de09677ef874f Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Tue, 10 Sep 2024 08:56:30 +0000 Subject: [PATCH 08/29] adding sgx predules for http and http_req --- .../core/identity-verification/src/web2/mod.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tee-worker/litentry/core/identity-verification/src/web2/mod.rs b/tee-worker/litentry/core/identity-verification/src/web2/mod.rs index ba0cea7d32..68062f2d06 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/mod.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/mod.rs @@ -17,6 +17,16 @@ #[cfg(all(not(feature = "std"), feature = "sgx"))] extern crate sgx_tstd as std; +// re-export module to properly feature gate sgx and regular std environment +#[cfg(all(not(feature = "std"), feature = "sgx"))] +pub mod sgx_reexport_prelude { + pub use http_req_sgx as http_req; + pub use http_sgx as http; +} + +#[cfg(all(not(feature = "std"), feature = "sgx"))] +use crate::sgx_reexport_prelude::*; + #[cfg(all(feature = "std", feature = "sgx"))] compile_error!("feature \"std\" and feature \"sgx\" cannot be enabled at the same time"); From 0d832fc8e5b6b557a033d1f20307ce7995b8b050 Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Tue, 10 Sep 2024 15:11:06 +0000 Subject: [PATCH 09/29] adding verification email template --- .../src/web2/email/mailer/template.rs | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 tee-worker/litentry/core/identity-verification/src/web2/email/mailer/template.rs diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/template.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/template.rs new file mode 100644 index 0000000000..8e641a4f82 --- /dev/null +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/template.rs @@ -0,0 +1,91 @@ +pub const VERYFY_EMAIL_TEMPLATE: &str = r#" + + + + + + + + + + + Verify your email address + + + +
+ Please verify your email address in order to link your identity. +  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏ +
+
+
+ + + + +
+ + + + + + + +
+

+ Email Identity +

+

+ Please verify your email address to link your identity. +

+
+ +
+
+
+
+
+ + +"#; From 0846ac0f4e203a1006dfe1962b88ef4202166015 Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Tue, 10 Sep 2024 15:15:47 +0000 Subject: [PATCH 10/29] adding data-providers config for sendgrid --- .../litentry/core/data-providers/src/lib.rs | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tee-worker/litentry/core/data-providers/src/lib.rs b/tee-worker/litentry/core/data-providers/src/lib.rs index 35150f6e82..89c6c5e09b 100644 --- a/tee-worker/litentry/core/data-providers/src/lib.rs +++ b/tee-worker/litentry/core/data-providers/src/lib.rs @@ -213,6 +213,9 @@ pub struct DataProviderConfig { pub blockchain_info_api_retry_delay: u64, pub blockchain_info_api_retry_times: u16, pub blockchain_info_api_url: String, + pub sendgrid_api_key: String, + pub sendgrid_from_email: String, + pub sendgrid_redirect_url: String, } impl DataProviderConfig { @@ -268,6 +271,9 @@ impl DataProviderConfig { blockchain_info_api_retry_delay: 5000, blockchain_info_api_retry_times: 2, blockchain_info_api_url: "https://blockchain.info/".to_string(), + sendgrid_api_key: "".to_string(), + sendgrid_from_email: "".to_string(), + sendgrid_redirect_url: "".to_string(), }; // we allow to override following config properties for non prod dev @@ -413,6 +419,16 @@ impl DataProviderConfig { if let Ok(v) = env::var("MAGIC_CRAFT_API_KEY") { config.set_magic_craft_api_key(v); } + if let Ok(v) = env::var("SENDGRID_API_KEY") { + config.set_sendgrid_api_key(v); + } + if let Ok(v) = env::var("SENDGRID_FROM_EMAIL") { + config.set_sendgrid_from_email(v); + } + if let Ok(v) = env::var("SENDGRID_REDIRECT_URL") { + config.set_sendgrid_redirect_url(v); + } + Ok(config) } pub fn set_twitter_official_url(&mut self, v: String) -> Result<(), Error> { @@ -631,6 +647,20 @@ impl DataProviderConfig { self.blockchain_info_api_url = v; Ok(()) } + pub fn set_sendgrid_api_key(&mut self, v: String) { + debug!("set_sendgrid_api_key: {:?}", v); + self.sendgrid_api_key = v; + } + pub fn set_sendgrid_from_email(&mut self, v: String) { + debug!("set_sendgrid_from_email: {:?}", v); + self.sendgrid_from_email = v; + } + pub fn set_sendgrid_redirect_url(&mut self, v: String) -> Result<(), Error> { + check_url(&v)?; + debug!("set_sendgrid_redirect_url: {:?}", v); + self.sendgrid_redirect_url = v; + Ok(()) + } } fn check_url(v: &String) -> Result<(), Error> { From 28dd7db2a7f660091c0e700ec5198da8ae49886a Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Tue, 10 Sep 2024 15:55:30 +0000 Subject: [PATCH 11/29] refactoring mailer --- .../src/web2/email/mailer/mod.rs | 5 +- .../src/web2/email/mailer/sendgrid_mailer.rs | 79 +++++++------------ 2 files changed, 32 insertions(+), 52 deletions(-) diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/mod.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/mod.rs index 8eb6c70fa9..b14ce38738 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/mod.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/mod.rs @@ -1,11 +1,12 @@ pub mod sendgrid_mailer; +pub(crate) mod template; use std::string::String; pub struct Mail { pub to: String, - pub verification_code: String, - pub redirect_url: Option, + pub subject: String, + pub body: String, } pub trait Mailer { diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/sendgrid_mailer.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/sendgrid_mailer.rs index 49e3269928..64b09833a3 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/sendgrid_mailer.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/sendgrid_mailer.rs @@ -1,7 +1,7 @@ #[cfg(all(not(feature = "std"), feature = "sgx"))] use crate::sgx_reexport_prelude::*; -use super::{Mail, Mailer}; +use super::{template::VERYFY_EMAIL_TEMPLATE, Mail, Mailer}; use http::header::{AUTHORIZATION, CONTENT_TYPE}; use http_req::response::Headers; use itc_rest_client::{ @@ -30,56 +30,47 @@ pub(crate) struct Email { } #[derive(Serialize)] -struct DynamicTemplateData { - redirect_url: String, +pub(crate) struct Content { + content_type: String, + value: String, } #[derive(Serialize)] -struct SendGridEmail { +pub(crate) struct SendGridMail { personalizations: Vec, from: Email, - template_id: String, - dynamic_template_data: DynamicTemplateData, + subject: String, + content: Vec, } -impl SendGridEmail { - pub fn new( - to: Email, - from: Email, - template_id: String, - verification_code: String, - redirect_url: String, - ) -> Self { +impl SendGridMail { + pub fn new(from_email: String, mail: Mail) -> Self { + let content = vec![Content { content_type: String::from("text/html"), value: mail.body }]; + let to = Email { email: mail.to, name: None }; + let from = Email { email: from_email, name: Some(String::from("Litentry")) }; Self { personalizations: vec![Personalization { to: vec![to] }], from, - template_id, - dynamic_template_data: DynamicTemplateData { - redirect_url: std::format!( - "{}?verification_code={}", - redirect_url, - verification_code - ), - }, + subject: mail.subject, + content, } } } -impl RestPath for SendGridEmail { +impl RestPath for SendGridMail { fn get_path(path: String) -> Result { Ok(path) } } -pub(crate) struct SendGridMailer { +pub struct SendGridMailer { api_key: String, client: RestClient>, - from: Email, - template_id: String, + from: String, } impl SendGridMailer { - pub fn new(api_key: String, from_email: Email, template_id: String) -> Self { + pub fn new(api_key: String, from_email: String) -> Self { let base_url = Url::parse("https://api.sendgrid.com/v3/mail/send").unwrap(); let authorization = std::format!("Bearer {}", api_key); @@ -87,35 +78,23 @@ impl SendGridMailer { headers.insert(AUTHORIZATION.as_str(), &authorization); headers.insert(CONTENT_TYPE.as_str(), "application/json"); - Self { - api_key, - client: RestClient::new( - HttpClient::new( - SendWithCertificateVerification::new(vec![]), - true, - None, - Some(headers), - None, - ), - base_url, - ), - from: from_email, - template_id, - } + let http_client = HttpClient::new( + SendWithCertificateVerification::new(vec![]), + true, + None, + Some(headers), + None, + ); + + Self { api_key, client: RestClient::new(http_client, base_url), from: from_email } } } impl Mailer for SendGridMailer { fn send(&mut self, mail: Mail) -> Result<(), String> { - let sendgrid_email = SendGridEmail::new( - Email { email: mail.to, name: None }, - self.from.clone(), - self.template_id.clone(), - mail.redirect_url.unwrap_or_default(), - mail.verification_code, - ); + let sendgrid_mail = SendGridMail::new(self.from.clone(), mail); self.client - .post(String::default(), &sendgrid_email) + .post(String::default(), &sendgrid_mail) .map_err(|e| std::format!("Failed to send verification email: {:?}", e))?; Ok(()) From c20cb7fe9932b896711b4727543ceb5db6399f35 Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Tue, 10 Sep 2024 15:55:51 +0000 Subject: [PATCH 12/29] implementing send_verification_email --- .../identity-verification/src/web2/email/mod.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs index 9c95966cde..59e72af8c4 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs @@ -11,3 +11,16 @@ pub fn generate_verification_code() -> String { helpers::get_random_string(32) } +pub fn send_verification_email( + mailer: &mut impl Mailer, + to_email: String, + redirect_url: String, +) -> Result<(), String> { + let mail = mailer::Mail { + to: to_email, + subject: String::from("Verify your email address"), + body: template::VERYFY_EMAIL_TEMPLATE.replace("{{ redirect_url }}", &redirect_url), + }; + + mailer.send(mail) +} From a417fdd3511686de79f34b3ceb2bfccdb6ee6c1d Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Tue, 10 Sep 2024 15:56:25 +0000 Subject: [PATCH 13/29] adding identity_requestEmailVerification to request email verification --- .../enclave-runtime/src/rpc/common_api.rs | 58 +++++++++++++++++-- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/tee-worker/enclave-runtime/src/rpc/common_api.rs b/tee-worker/enclave-runtime/src/rpc/common_api.rs index 13860c1591..12679566b3 100644 --- a/tee-worker/enclave-runtime/src/rpc/common_api.rs +++ b/tee-worker/enclave-runtime/src/rpc/common_api.rs @@ -29,7 +29,7 @@ use itp_utils::{FromHexPrefixed, ToHexPrefixed}; use its_rpc_handler::direct_top_pool_api::add_top_pool_direct_rpc_methods; use jsonrpc_core::{serde_json::json, IoHandler, Params, Value}; use lc_data_providers::DataProviderConfig; -use lc_identity_verification::web2::twitter; +use lc_identity_verification::web2::{email, twitter}; use litentry_macros::{if_development, if_development_or}; use litentry_primitives::{aes_decrypt, AesRequest, DecryptableRequest, Identity}; use log::debug; @@ -418,6 +418,8 @@ pub fn add_common_api Ok(json!(compute_hex_encoded_return_error("Could not parse params"))), } }); + + io_handler.add_sync_method("identity_requestEmailVerification", move |params: Params| { + match params.parse::<(String, String)>() { + Ok((encoded_did, email)) => { + let account_id = match Identity::from_did(encoded_did.as_str()) { + Ok(identity) => + if let Some(account_id) = identity.to_account_id() { + account_id + } else { + return Ok(json!(compute_hex_encoded_return_error("Invalid identity"))) + }, + Err(_) => + return Ok(json!(compute_hex_encoded_return_error( + "Could not parse identity" + ))), + }; + let verification_code = email::generate_verification_code(); + let redirect_url = std::format!( + "{}?verification_code={}", + data_provider_config.sendgrid_redirect_url.clone(), + verification_code + ); + let mut mailer = email::sendgrid_mailer::SendGridMailer::new( + data_provider_config.sendgrid_api_key.clone(), + data_provider_config.sendgrid_from_email.clone(), + ); + + match email::VerificationCodeStore::insert(account_id, verification_code.clone()) { + Ok(_) => { + if let Err(_) = + email::send_verification_email(&mut mailer, email, redirect_url) + { + return Ok(json!(compute_hex_encoded_return_error( + "Could not send verification email" + ))) + } + let json_value = + RpcReturnValue::new(vec![], false, DirectRequestStatus::Ok); + Ok(json!(json_value.to_hex())) + }, + Err(_) => + return Ok(json!(compute_hex_encoded_return_error( + "Could not save verification code" + ))), + } + }, + Err(_) => Ok(json!(compute_hex_encoded_return_error("Could not parse params"))), + } + }); } #[deprecated(note = "`state_executeAesGetter` should be preferred")] From fa9b6281507210ed820f4a8f2799729500209125 Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Wed, 11 Sep 2024 09:53:10 +0000 Subject: [PATCH 14/29] clean up unused imports --- .../src/web2/email/mailer/sendgrid_mailer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/sendgrid_mailer.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/sendgrid_mailer.rs index 64b09833a3..cd022f6859 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/sendgrid_mailer.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/sendgrid_mailer.rs @@ -1,7 +1,7 @@ #[cfg(all(not(feature = "std"), feature = "sgx"))] use crate::sgx_reexport_prelude::*; -use super::{template::VERYFY_EMAIL_TEMPLATE, Mail, Mailer}; +use super::{Mail, Mailer}; use http::header::{AUTHORIZATION, CONTENT_TYPE}; use http_req::response::Headers; use itc_rest_client::{ From 99fdbf587845a9e383dc5a7cb51f0362acfdf99e Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Wed, 11 Sep 2024 09:53:51 +0000 Subject: [PATCH 15/29] handling error in data-providers config --- tee-worker/litentry/core/data-providers/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tee-worker/litentry/core/data-providers/src/lib.rs b/tee-worker/litentry/core/data-providers/src/lib.rs index 89c6c5e09b..364888a190 100644 --- a/tee-worker/litentry/core/data-providers/src/lib.rs +++ b/tee-worker/litentry/core/data-providers/src/lib.rs @@ -426,7 +426,7 @@ impl DataProviderConfig { config.set_sendgrid_from_email(v); } if let Ok(v) = env::var("SENDGRID_REDIRECT_URL") { - config.set_sendgrid_redirect_url(v); + config.set_sendgrid_redirect_url(v)?; } Ok(config) From 2817af02031cd8341264fe4f9919128af43ba7de Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Wed, 11 Sep 2024 09:54:20 +0000 Subject: [PATCH 16/29] covering Email variant of Identity enum --- tee-worker/litentry/core/evm-dynamic-assertions/src/lib.rs | 1 + tee-worker/service/src/prometheus_metrics.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/tee-worker/litentry/core/evm-dynamic-assertions/src/lib.rs b/tee-worker/litentry/core/evm-dynamic-assertions/src/lib.rs index 9a84a4dcec..1499ad1e88 100644 --- a/tee-worker/litentry/core/evm-dynamic-assertions/src/lib.rs +++ b/tee-worker/litentry/core/evm-dynamic-assertions/src/lib.rs @@ -203,6 +203,7 @@ pub fn identity_with_networks_to_token(identity: &IdentityNetworkTuple) -> Token Identity::Evm(addr) => (4, addr.as_ref().to_vec()), Identity::Bitcoin(addr) => (5, addr.as_ref().to_vec()), Identity::Solana(addr) => (6, addr.as_ref().to_vec()), + Identity::Email(str) => (7, str.inner_ref().to_vec()), }; let networks: Vec = identity.1.iter().map(network_to_token).collect(); Token::Tuple(vec![Token::Uint(type_index.into()), Token::Bytes(value), Token::Array(networks)]) diff --git a/tee-worker/service/src/prometheus_metrics.rs b/tee-worker/service/src/prometheus_metrics.rs index 69235686ec..53102d0b72 100644 --- a/tee-worker/service/src/prometheus_metrics.rs +++ b/tee-worker/service/src/prometheus_metrics.rs @@ -288,6 +288,7 @@ fn handle_stf_call_request(req: RequestType, time: f64) { Identity::Twitter(_) => "Twitter".into(), Identity::Discord(_) => "Discord".into(), Identity::Github(_) => "Github".into(), + Identity::Email(_) => "Email".into(), Identity::Substrate(_) => "Substrate".into(), Identity::Evm(_) => "Evm".into(), Identity::Bitcoin(_) => "Bitcoin".into(), From 5d30fb56b483096b219afa81cec04ec57cc855b5 Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Wed, 11 Sep 2024 09:55:22 +0000 Subject: [PATCH 17/29] adding validation data for email identity --- .../identity-verification/src/web2/mod.rs | 43 +++++++++++++++++++ .../primitives/src/validation_data.rs | 9 ++++ 2 files changed, 52 insertions(+) diff --git a/tee-worker/litentry/core/identity-verification/src/web2/mod.rs b/tee-worker/litentry/core/identity-verification/src/web2/mod.rs index 68062f2d06..2b762fb3ce 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/mod.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/mod.rs @@ -212,6 +212,44 @@ pub fn verify( Ok(user.username) }, }, + Web2ValidationData::Email(data) => { + let email = vec_to_string(data.email.to_vec()) + .map_err(|e| Error::LinkIdentityFailed(e.into_error_detail()))?; + let verification_code = vec_to_string(data.verification_code.to_vec()) + .map_err(|e| Error::LinkIdentityFailed(e.into_error_detail()))?; + 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(ErrorDetail::StfError( + ErrorString::truncate_from( + std::format!("failed to get verification code: {}", e,) + .as_bytes() + .to_vec(), + ), + ))), + }; + + ensure!( + verification_code == stored_verification_code, + Error::LinkIdentityFailed(ErrorDetail::StfError(ErrorString::truncate_from( + "verification code mismatch".as_bytes().to_vec() + ))) + ); + + Ok(email) + }, }?; // compare the username: @@ -231,6 +269,11 @@ pub fn verify( .map_err(|_| Error::LinkIdentityFailed(ErrorDetail::ParseError))?; ensure!(username.eq(handle), Error::LinkIdentityFailed(ErrorDetail::WrongWeb2Handle)); }, + Identity::Email(address) => { + let handle = std::str::from_utf8(address.inner_ref()) + .map_err(|_| Error::LinkIdentityFailed(ErrorDetail::ParseError))?; + ensure!(username.eq(handle), Error::LinkIdentityFailed(ErrorDetail::WrongWeb2Handle)); + }, _ => return Err(Error::LinkIdentityFailed(ErrorDetail::InvalidIdentity)), } diff --git a/tee-worker/litentry/primitives/src/validation_data.rs b/tee-worker/litentry/primitives/src/validation_data.rs index 93601b05e6..51f92856ad 100644 --- a/tee-worker/litentry/primitives/src/validation_data.rs +++ b/tee-worker/litentry/primitives/src/validation_data.rs @@ -45,6 +45,13 @@ pub enum DiscordValidationData { }, } +#[derive(Encode, Decode, Clone, Debug, PartialEq, Eq, TypeInfo, MaxEncodedLen)] +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +pub struct EmailValidationData { + pub email: ValidationString, + pub verification_code: ValidationString, +} + #[derive(Encode, Decode, Clone, Debug, PartialEq, Eq, TypeInfo, MaxEncodedLen)] #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] pub struct Web3CommonValidationData { @@ -60,6 +67,8 @@ pub enum Web2ValidationData { Twitter(TwitterValidationData), #[codec(index = 1)] Discord(DiscordValidationData), + #[codec(index = 2)] + Email(EmailValidationData), } #[derive(Encode, Decode, Clone, Debug, PartialEq, Eq, TypeInfo, MaxEncodedLen)] From c2b0b32db2eceb7c97d131ad0646558441b2ae10 Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Wed, 11 Sep 2024 10:11:34 +0000 Subject: [PATCH 18/29] cleaning up sendgrid_mailer --- .../src/web2/email/mailer/sendgrid_mailer.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/sendgrid_mailer.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/sendgrid_mailer.rs index cd022f6859..abef8b20cf 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/sendgrid_mailer.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/sendgrid_mailer.rs @@ -8,14 +8,10 @@ use itc_rest_client::{ error::Error as HttpError, http_client::{HttpClient, SendWithCertificateVerification}, rest_client::RestClient, - RestGet, RestPath, RestPost, -}; -use serde::{Deserialize, Serialize}; -use std::{ - string::{String, ToString}, - vec, - vec::Vec, + RestPath, RestPost, }; +use serde::Serialize; +use std::{string::String, vec, vec::Vec}; use url::Url; #[derive(Serialize)] @@ -64,7 +60,6 @@ impl RestPath for SendGridMail { } pub struct SendGridMailer { - api_key: String, client: RestClient>, from: String, } @@ -86,7 +81,7 @@ impl SendGridMailer { None, ); - Self { api_key, client: RestClient::new(http_client, base_url), from: from_email } + Self { client: RestClient::new(http_client, base_url), from: from_email } } } From 3564bbbe7884253073191258d8ff9580abc2004b Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Wed, 11 Sep 2024 14:46:05 +0000 Subject: [PATCH 19/29] refactoring email template to send only verification code instead of a link to verify --- .../enclave-runtime/src/rpc/common_api.rs | 9 ++---- .../src/web2/email/mailer/template.rs | 32 ++++++------------- .../src/web2/email/mod.rs | 5 +-- 3 files changed, 14 insertions(+), 32 deletions(-) diff --git a/tee-worker/enclave-runtime/src/rpc/common_api.rs b/tee-worker/enclave-runtime/src/rpc/common_api.rs index 12679566b3..77c5cb9c36 100644 --- a/tee-worker/enclave-runtime/src/rpc/common_api.rs +++ b/tee-worker/enclave-runtime/src/rpc/common_api.rs @@ -475,21 +475,16 @@ pub fn add_common_api { if let Err(_) = - email::send_verification_email(&mut mailer, email, redirect_url) + email::send_verification_email(&mut mailer, email, verification_code) { return Ok(json!(compute_hex_encoded_return_error( "Could not send verification email" diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/template.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/template.rs index 8e641a4f82..33e75347d1 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/template.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/template.rs @@ -20,7 +20,7 @@ pub const VERYFY_EMAIL_TEMPLATE: &str = r#" td,th,div,p,a,h1,h2,h3,h4,h5,h6 {font-family: "Segoe UI", sans-serif; mso-line-height-rule: exactly;} - Verify your email address + Email verification - +
- Please verify your email address in order to link your identity. + Please use this verification code to link your email identity.  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏
-
-
+
+
- - -
@@ -55,31 +55,17 @@ pub const VERYFY_EMAIL_TEMPLATE: &str = r#"

- Email Identity + Email Verification

- Please verify your email address to link your identity. + Please use this verification code to link your email identity.

-
- - - - Verify Now - - - +
+

{{ verification_code }}

-
diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs index 59e72af8c4..bc750c227b 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs @@ -14,12 +14,13 @@ pub fn generate_verification_code() -> String { pub fn send_verification_email( mailer: &mut impl Mailer, to_email: String, - redirect_url: String, + verification_code: String, ) -> Result<(), String> { let mail = mailer::Mail { to: to_email, subject: String::from("Verify your email address"), - body: template::VERYFY_EMAIL_TEMPLATE.replace("{{ redirect_url }}", &redirect_url), + body: template::VERYFY_EMAIL_TEMPLATE + .replace("{{ verification_code }}", &verification_code), }; mailer.send(mail) From 06c870b2bb19c46c9e861dcce05eedbace7f2e94 Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Wed, 11 Sep 2024 14:46:37 +0000 Subject: [PATCH 20/29] cleaning up data-providers config --- tee-worker/litentry/core/data-providers/src/lib.rs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/tee-worker/litentry/core/data-providers/src/lib.rs b/tee-worker/litentry/core/data-providers/src/lib.rs index 364888a190..9a3a280162 100644 --- a/tee-worker/litentry/core/data-providers/src/lib.rs +++ b/tee-worker/litentry/core/data-providers/src/lib.rs @@ -215,7 +215,6 @@ pub struct DataProviderConfig { pub blockchain_info_api_url: String, pub sendgrid_api_key: String, pub sendgrid_from_email: String, - pub sendgrid_redirect_url: String, } impl DataProviderConfig { @@ -273,7 +272,6 @@ impl DataProviderConfig { blockchain_info_api_url: "https://blockchain.info/".to_string(), sendgrid_api_key: "".to_string(), sendgrid_from_email: "".to_string(), - sendgrid_redirect_url: "".to_string(), }; // we allow to override following config properties for non prod dev @@ -425,9 +423,6 @@ impl DataProviderConfig { if let Ok(v) = env::var("SENDGRID_FROM_EMAIL") { config.set_sendgrid_from_email(v); } - if let Ok(v) = env::var("SENDGRID_REDIRECT_URL") { - config.set_sendgrid_redirect_url(v)?; - } Ok(config) } @@ -655,12 +650,6 @@ impl DataProviderConfig { debug!("set_sendgrid_from_email: {:?}", v); self.sendgrid_from_email = v; } - pub fn set_sendgrid_redirect_url(&mut self, v: String) -> Result<(), Error> { - check_url(&v)?; - debug!("set_sendgrid_redirect_url: {:?}", v); - self.sendgrid_redirect_url = v; - Ok(()) - } } fn check_url(v: &String) -> Result<(), Error> { From b77df086568d6b8c43fe9705db613a24807208ac Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Wed, 11 Sep 2024 14:50:14 +0000 Subject: [PATCH 21/29] fixing typo --- .../identity-verification/src/web2/email/mailer/template.rs | 2 +- .../litentry/core/identity-verification/src/web2/email/mod.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/template.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/template.rs index 33e75347d1..b30cf260a2 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/template.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/template.rs @@ -1,4 +1,4 @@ -pub const VERYFY_EMAIL_TEMPLATE: &str = r#" +pub const EMAIL_VERIFICATION_TEMPLATE: &str = r#" diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs index bc750c227b..c1fea32607 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs @@ -19,7 +19,7 @@ pub fn send_verification_email( let mail = mailer::Mail { to: to_email, subject: String::from("Verify your email address"), - body: template::VERYFY_EMAIL_TEMPLATE + body: template::EMAIL_VERIFICATION_TEMPLATE .replace("{{ verification_code }}", &verification_code), }; From 616816ce24453059d4aa3a4e5d5d0c8e4b00720c Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Wed, 11 Sep 2024 15:22:44 +0000 Subject: [PATCH 22/29] fixing formatting --- tee-worker/litentry/core/identity-verification/Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tee-worker/litentry/core/identity-verification/Cargo.toml b/tee-worker/litentry/core/identity-verification/Cargo.toml index 63e849b308..aa4e117de4 100644 --- a/tee-worker/litentry/core/identity-verification/Cargo.toml +++ b/tee-worker/litentry/core/identity-verification/Cargo.toml @@ -8,10 +8,10 @@ version = "0.1.0" # std dependencies base64 = { version = "0.22", default-features = false, features = ["alloc"] } hex = { version = "0.4.3", default-features = false } -lru = "0.12.3" -rand = { version = "0.8", optional = true } http = { version = "0.2", optional = true } http_req = { optional = true, features = ["rust-tls"], branch = "master", git = "https://github.com/integritee-network/http_req" } +lru = "0.12.3" +rand = { version = "0.8", optional = true } # sgx dependencies futures_sgx = { package = "futures", git = "https://github.com/mesalock-linux/futures-rs-sgx", optional = true } @@ -29,9 +29,9 @@ serde_json = { version = "1.0", default-features = false, features = ["alloc"] } url = { git = "https://github.com/domenukk/rust-url", branch = "no_std", default-features = false, features = ["alloc", "no_std_net"] } # internal dependencies +itc-rest-client = { path = "../../../core/rest-client", default-features = false } itp-sgx-crypto = { path = "../../../core-primitives/sgx/crypto", default-features = false } itp-utils = { path = "../../../core-primitives/utils", default-features = false } -itc-rest-client = { path = "../../../core/rest-client", default-features = false } codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] } frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } From 9b5e3b7f54b5ad92a93858c4803a850b1c7c9e4a Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Fri, 13 Sep 2024 09:10:23 +0000 Subject: [PATCH 23/29] import types from alloc instead of std --- tee-worker/litentry/core/identity-verification/src/lib.rs | 2 ++ .../identity-verification/src/web2/email/mailer/mod.rs | 2 +- .../src/web2/email/mailer/sendgrid_mailer.rs | 2 +- .../core/identity-verification/src/web2/email/mod.rs | 3 +-- .../src/web2/email/verification_code_store.rs | 3 ++- .../core/identity-verification/src/web2/helpers.rs | 2 +- .../core/identity-verification/src/web2/twitter/mod.rs | 7 +++++-- 7 files changed, 13 insertions(+), 8 deletions(-) diff --git a/tee-worker/litentry/core/identity-verification/src/lib.rs b/tee-worker/litentry/core/identity-verification/src/lib.rs index f85d43f32b..300b612dc8 100644 --- a/tee-worker/litentry/core/identity-verification/src/lib.rs +++ b/tee-worker/litentry/core/identity-verification/src/lib.rs @@ -16,6 +16,8 @@ #![cfg_attr(not(feature = "std"), no_std)] +pub extern crate alloc; + #[cfg(all(not(feature = "std"), feature = "sgx"))] extern crate sgx_tstd as std; diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/mod.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/mod.rs index b14ce38738..d0733ded01 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/mod.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/mod.rs @@ -1,7 +1,7 @@ pub mod sendgrid_mailer; pub(crate) mod template; -use std::string::String; +use crate::alloc::string::String; pub struct Mail { pub to: String, diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/sendgrid_mailer.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/sendgrid_mailer.rs index abef8b20cf..7d86cf49d8 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/sendgrid_mailer.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/mailer/sendgrid_mailer.rs @@ -2,6 +2,7 @@ use crate::sgx_reexport_prelude::*; use super::{Mail, Mailer}; +use crate::alloc::{string::String, vec, vec::Vec}; use http::header::{AUTHORIZATION, CONTENT_TYPE}; use http_req::response::Headers; use itc_rest_client::{ @@ -11,7 +12,6 @@ use itc_rest_client::{ RestPath, RestPost, }; use serde::Serialize; -use std::{string::String, vec, vec::Vec}; use url::Url; #[derive(Serialize)] diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs index c1fea32607..51c58c1e5a 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/mod.rs @@ -4,8 +4,7 @@ pub use verification_code_store::*; mod mailer; pub use mailer::*; -use crate::web2::helpers; -use std::string::String; +use crate::{alloc::string::String, web2::helpers}; pub fn generate_verification_code() -> String { helpers::get_random_string(32) diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/verification_code_store.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/verification_code_store.rs index 1ad03289dc..48c229ae6f 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/email/verification_code_store.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/verification_code_store.rs @@ -1,13 +1,14 @@ +use crate::alloc::string::String; use codec::Encode; use core::result::Result; use lazy_static::lazy_static; use litentry_primitives::ParentchainAccountId as AccountId; use lru::LruCache; +use std::num::NonZeroUsize; #[cfg(feature = "std")] use std::sync::RwLock; #[cfg(feature = "sgx")] use std::sync::SgxRwLock as RwLock; -use std::{num::NonZeroUsize, string::String}; lazy_static! { static ref STORE: RwLock> = diff --git a/tee-worker/litentry/core/identity-verification/src/web2/helpers.rs b/tee-worker/litentry/core/identity-verification/src/web2/helpers.rs index 59cebfd8b2..8e09634cae 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/helpers.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/helpers.rs @@ -3,8 +3,8 @@ extern crate sgx_rand as rand; #[cfg(all(not(feature = "std"), feature = "sgx"))] extern crate sgx_tstd as std; +use crate::alloc::string::String; use rand::{thread_rng, Rng}; -use std::string::String; pub(crate) fn get_random_string(length: usize) -> String { let mut rng = thread_rng(); diff --git a/tee-worker/litentry/core/identity-verification/src/web2/twitter/mod.rs b/tee-worker/litentry/core/identity-verification/src/web2/twitter/mod.rs index 25df0e821f..df620133b3 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/twitter/mod.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/twitter/mod.rs @@ -1,12 +1,15 @@ mod oauth_store; pub use oauth_store::*; -use crate::{web2::helpers, Error, Result}; +use crate::{ + alloc::{format, string::String, vec::Vec}, + web2::helpers, + Error, Result, +}; use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine}; use lc_data_providers::twitter_official::Tweet; use litentry_primitives::{self, ErrorDetail}; use sp_core::hashing::sha2_256; -use std::{format, string::String, vec::Vec}; #[derive(Debug)] pub struct AuthorizeData { From bf6e31e6829e79e6f40e8d85849d3543cbfb8f30 Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Fri, 13 Sep 2024 12:50:44 +0000 Subject: [PATCH 24/29] updating cargo.lock --- tee-worker/Cargo.lock | 47 ++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/tee-worker/Cargo.lock b/tee-worker/Cargo.lock index ec7fe7b9d1..613d843308 100644 --- a/tee-worker/Cargo.lock +++ b/tee-worker/Cargo.lock @@ -875,7 +875,7 @@ checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" [[package]] name = "core-primitives" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" dependencies = [ "base58", "frame-support", @@ -1006,7 +1006,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ "generic-array 0.14.7", - "typenum 1.16.0 (registry+https://github.com/rust-lang/crates.io-index)", + "typenum", ] [[package]] @@ -2267,7 +2267,7 @@ version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ffdf9f34f1447443d37393cc6c2b8313aebddcd96906caf34e54c68d8e57d7bd" dependencies = [ - "typenum 1.16.0 (registry+https://github.com/rust-lang/crates.io-index)", + "typenum", ] [[package]] @@ -2276,7 +2276,7 @@ version = "0.14.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" dependencies = [ - "typenum 1.16.0 (registry+https://github.com/rust-lang/crates.io-index)", + "typenum", "version_check", "zeroize", ] @@ -4852,7 +4852,7 @@ dependencies = [ "rand 0.8.5", "serde 1.0.204", "sha2 0.9.9", - "typenum 1.16.0 (registry+https://github.com/rust-lang/crates.io-index)", + "typenum", ] [[package]] @@ -4993,7 +4993,7 @@ dependencies = [ [[package]] name = "litentry-hex-utils" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" dependencies = [ "hex", ] @@ -5005,7 +5005,7 @@ version = "0.1.0" [[package]] name = "litentry-macros" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" [[package]] name = "litentry-primitives" @@ -5035,7 +5035,7 @@ dependencies = [ [[package]] name = "litentry-proc-macros" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" dependencies = [ "cargo_toml", "proc-macro2", @@ -5467,7 +5467,7 @@ dependencies = [ "num-rational 0.4.1", "num-traits 0.2.16", "simba", - "typenum 1.16.0 (registry+https://github.com/rust-lang/crates.io-index)", + "typenum", ] [[package]] @@ -6021,7 +6021,7 @@ dependencies = [ [[package]] name = "pallet-parachain-staking" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" dependencies = [ "core-primitives", "frame-support", @@ -6088,7 +6088,7 @@ dependencies = [ [[package]] name = "pallet-teebag" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" dependencies = [ "base64 0.13.1", "chrono 0.4.38", @@ -8769,11 +8769,21 @@ dependencies = [ [[package]] name = "substrate-fixed" version = "0.5.9" -source = "git+https://github.com/encointer/substrate-fixed#a4fb461aae6205ffc55bed51254a40c52be04e5d" +source = "git+https://github.com/encointer/substrate-fixed#879c58bcc6fd676a74315dcd38b598f28708b0b5" +dependencies = [ + "parity-scale-codec", + "scale-info", + "substrate-typenum", +] + +[[package]] +name = "substrate-typenum" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0f0091e93c2c75b233ae39424c52cb8a662c0811fb68add149e20e5d7e8a788" dependencies = [ "parity-scale-codec", "scale-info", - "typenum 1.16.0 (git+https://github.com/encointer/typenum?tag=v1.16.0)", ] [[package]] @@ -9318,7 +9328,7 @@ version = "1.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" dependencies = [ - "cfg-if 1.0.0", + "cfg-if 0.1.10", "digest 0.10.7", "rand 0.8.5", "static_assertions", @@ -9341,15 +9351,6 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" -[[package]] -name = "typenum" -version = "1.16.0" -source = "git+https://github.com/encointer/typenum?tag=v1.16.0#4c8dddaa8bdd13130149e43b4085ad14e960617f" -dependencies = [ - "parity-scale-codec", - "scale-info", -] - [[package]] name = "ucd-trie" version = "0.1.6" From 1050b92e626d6e05dd89a8608e201cfc12f670f9 Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Fri, 13 Sep 2024 13:16:18 +0000 Subject: [PATCH 25/29] updating enclave-runtime Cargo.lock --- tee-worker/enclave-runtime/Cargo.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tee-worker/enclave-runtime/Cargo.lock b/tee-worker/enclave-runtime/Cargo.lock index 373571ff42..e589c28d07 100644 --- a/tee-worker/enclave-runtime/Cargo.lock +++ b/tee-worker/enclave-runtime/Cargo.lock @@ -600,7 +600,7 @@ checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" [[package]] name = "core-primitives" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" dependencies = [ "base58", "frame-support", @@ -3362,7 +3362,7 @@ dependencies = [ [[package]] name = "litentry-hex-utils" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" dependencies = [ "hex", ] @@ -3374,7 +3374,7 @@ version = "0.1.0" [[package]] name = "litentry-macros" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" [[package]] name = "litentry-primitives" @@ -3412,7 +3412,7 @@ dependencies = [ [[package]] name = "litentry-proc-macros" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" dependencies = [ "cargo_toml", "proc-macro2", @@ -3805,7 +3805,7 @@ dependencies = [ [[package]] name = "pallet-parachain-staking" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" dependencies = [ "core-primitives", "frame-support", @@ -3872,7 +3872,7 @@ dependencies = [ [[package]] name = "pallet-teebag" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" dependencies = [ "base64 0.13.1", "chrono 0.4.31", From 492622a6c85b3420f2604cd368c3dd98f37204f4 Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Fri, 13 Sep 2024 13:19:42 +0000 Subject: [PATCH 26/29] Revert "updating cargo.lock" This reverts commit bf6e31e6829e79e6f40e8d85849d3543cbfb8f30. --- tee-worker/Cargo.lock | 47 +++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/tee-worker/Cargo.lock b/tee-worker/Cargo.lock index 613d843308..ec7fe7b9d1 100644 --- a/tee-worker/Cargo.lock +++ b/tee-worker/Cargo.lock @@ -875,7 +875,7 @@ checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" [[package]] name = "core-primitives" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" dependencies = [ "base58", "frame-support", @@ -1006,7 +1006,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ "generic-array 0.14.7", - "typenum", + "typenum 1.16.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2267,7 +2267,7 @@ version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ffdf9f34f1447443d37393cc6c2b8313aebddcd96906caf34e54c68d8e57d7bd" dependencies = [ - "typenum", + "typenum 1.16.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2276,7 +2276,7 @@ version = "0.14.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" dependencies = [ - "typenum", + "typenum 1.16.0 (registry+https://github.com/rust-lang/crates.io-index)", "version_check", "zeroize", ] @@ -4852,7 +4852,7 @@ dependencies = [ "rand 0.8.5", "serde 1.0.204", "sha2 0.9.9", - "typenum", + "typenum 1.16.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -4993,7 +4993,7 @@ dependencies = [ [[package]] name = "litentry-hex-utils" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" dependencies = [ "hex", ] @@ -5005,7 +5005,7 @@ version = "0.1.0" [[package]] name = "litentry-macros" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" [[package]] name = "litentry-primitives" @@ -5035,7 +5035,7 @@ dependencies = [ [[package]] name = "litentry-proc-macros" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" dependencies = [ "cargo_toml", "proc-macro2", @@ -5467,7 +5467,7 @@ dependencies = [ "num-rational 0.4.1", "num-traits 0.2.16", "simba", - "typenum", + "typenum 1.16.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -6021,7 +6021,7 @@ dependencies = [ [[package]] name = "pallet-parachain-staking" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" dependencies = [ "core-primitives", "frame-support", @@ -6088,7 +6088,7 @@ dependencies = [ [[package]] name = "pallet-teebag" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" dependencies = [ "base64 0.13.1", "chrono 0.4.38", @@ -8769,21 +8769,11 @@ dependencies = [ [[package]] name = "substrate-fixed" version = "0.5.9" -source = "git+https://github.com/encointer/substrate-fixed#879c58bcc6fd676a74315dcd38b598f28708b0b5" -dependencies = [ - "parity-scale-codec", - "scale-info", - "substrate-typenum", -] - -[[package]] -name = "substrate-typenum" -version = "1.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0f0091e93c2c75b233ae39424c52cb8a662c0811fb68add149e20e5d7e8a788" +source = "git+https://github.com/encointer/substrate-fixed#a4fb461aae6205ffc55bed51254a40c52be04e5d" dependencies = [ "parity-scale-codec", "scale-info", + "typenum 1.16.0 (git+https://github.com/encointer/typenum?tag=v1.16.0)", ] [[package]] @@ -9328,7 +9318,7 @@ version = "1.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" dependencies = [ - "cfg-if 0.1.10", + "cfg-if 1.0.0", "digest 0.10.7", "rand 0.8.5", "static_assertions", @@ -9351,6 +9341,15 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" +[[package]] +name = "typenum" +version = "1.16.0" +source = "git+https://github.com/encointer/typenum?tag=v1.16.0#4c8dddaa8bdd13130149e43b4085ad14e960617f" +dependencies = [ + "parity-scale-codec", + "scale-info", +] + [[package]] name = "ucd-trie" version = "0.1.6" From be4cd6e877868c1011e982cdd364ca783eb92957 Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Fri, 13 Sep 2024 13:22:26 +0000 Subject: [PATCH 27/29] updating tee-worker's Cargo.lock --- tee-worker/Cargo.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tee-worker/Cargo.lock b/tee-worker/Cargo.lock index ec7fe7b9d1..3f7693cab8 100644 --- a/tee-worker/Cargo.lock +++ b/tee-worker/Cargo.lock @@ -875,7 +875,7 @@ checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" [[package]] name = "core-primitives" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" dependencies = [ "base58", "frame-support", @@ -4993,7 +4993,7 @@ dependencies = [ [[package]] name = "litentry-hex-utils" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" dependencies = [ "hex", ] @@ -5005,7 +5005,7 @@ version = "0.1.0" [[package]] name = "litentry-macros" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" [[package]] name = "litentry-primitives" @@ -5035,7 +5035,7 @@ dependencies = [ [[package]] name = "litentry-proc-macros" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" dependencies = [ "cargo_toml", "proc-macro2", @@ -6021,7 +6021,7 @@ dependencies = [ [[package]] name = "pallet-parachain-staking" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" dependencies = [ "core-primitives", "frame-support", @@ -6088,7 +6088,7 @@ dependencies = [ [[package]] name = "pallet-teebag" version = "0.1.0" -source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#d2249ff0f8a4b20e590c5cd2c0f68baa79e13aa4" +source = "git+https://github.com/litentry/litentry-parachain?branch=release-v0.9.19#cdbe9b02c1c58ca3d0063bf2eaf26a1f9da314e9" dependencies = [ "base64 0.13.1", "chrono 0.4.38", From e8742d7e494ff74464f2c2432e6dbe5eb55d5960 Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Fri, 13 Sep 2024 14:05:55 +0000 Subject: [PATCH 28/29] improving errors a bit --- .../src/web2/email/verification_code_store.rs | 40 ++++++++++++++++--- .../identity-verification/src/web2/mod.rs | 9 +---- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/tee-worker/litentry/core/identity-verification/src/web2/email/verification_code_store.rs b/tee-worker/litentry/core/identity-verification/src/web2/email/verification_code_store.rs index 48c229ae6f..44a86f13e8 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/email/verification_code_store.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/email/verification_code_store.rs @@ -1,8 +1,10 @@ -use crate::alloc::string::String; +use crate::alloc::{fmt, format, string::String}; use codec::Encode; use core::result::Result; use lazy_static::lazy_static; -use litentry_primitives::ParentchainAccountId as AccountId; +use litentry_primitives::{ + ErrorDetail, ErrorString, IntoErrorDetail, ParentchainAccountId as AccountId, +}; use lru::LruCache; use std::num::NonZeroUsize; #[cfg(feature = "std")] @@ -10,6 +12,29 @@ use std::sync::RwLock; #[cfg(feature = "sgx")] use std::sync::SgxRwLock as RwLock; +#[derive(Debug)] +pub enum VerificationCodeStoreError { + LockPoisoning, + Other(String), +} + +impl fmt::Display for VerificationCodeStoreError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + VerificationCodeStoreError::LockPoisoning => write!(f, "Lock poisoning"), + VerificationCodeStoreError::Other(msg) => write!(f, "{}", msg), + } + } +} + +impl std::error::Error for VerificationCodeStoreError {} + +impl IntoErrorDetail for VerificationCodeStoreError { + fn into_error_detail(self) -> ErrorDetail { + ErrorDetail::StfError(ErrorString::truncate_from(format!("{}", self).into())) + } +} + lazy_static! { static ref STORE: RwLock> = RwLock::new(LruCache::new(NonZeroUsize::new(250).unwrap())); @@ -18,18 +43,21 @@ lazy_static! { pub struct VerificationCodeStore; impl VerificationCodeStore { - pub fn insert(account_id: AccountId, verification_code: String) -> Result<(), String> { + pub fn insert( + account_id: AccountId, + verification_code: String, + ) -> Result<(), VerificationCodeStoreError> { STORE .write() - .map_err(|_| String::from("Lock poisoning"))? + .map_err(|_| VerificationCodeStoreError::LockPoisoning)? .put(hex::encode(account_id.encode()), verification_code); Ok(()) } - pub fn get(account_id: &AccountId) -> Result, String> { + pub fn get(account_id: &AccountId) -> Result, VerificationCodeStoreError> { let code = STORE .write() - .map_err(|_| String::from("Lock poisoning"))? + .map_err(|_| VerificationCodeStoreError::LockPoisoning)? .pop(hex::encode(account_id.encode()).as_str()); Ok(code) } diff --git a/tee-worker/litentry/core/identity-verification/src/web2/mod.rs b/tee-worker/litentry/core/identity-verification/src/web2/mod.rs index 2b762fb3ce..c444f0612d 100644 --- a/tee-worker/litentry/core/identity-verification/src/web2/mod.rs +++ b/tee-worker/litentry/core/identity-verification/src/web2/mod.rs @@ -231,14 +231,7 @@ pub fn verify( .to_vec(), ))) })?, - Err(e) => - return Err(Error::LinkIdentityFailed(ErrorDetail::StfError( - ErrorString::truncate_from( - std::format!("failed to get verification code: {}", e,) - .as_bytes() - .to_vec(), - ), - ))), + Err(e) => return Err(Error::LinkIdentityFailed(e.into_error_detail())), }; ensure!( From dfd494f15473541f081a1b42f15a676744e36d12 Mon Sep 17 00:00:00 2001 From: Francisco Silva Date: Fri, 13 Sep 2024 14:15:06 +0000 Subject: [PATCH 29/29] fixing clippy issues --- tee-worker/enclave-runtime/src/rpc/common_api.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tee-worker/enclave-runtime/src/rpc/common_api.rs b/tee-worker/enclave-runtime/src/rpc/common_api.rs index 77c5cb9c36..cf0d63514b 100644 --- a/tee-worker/enclave-runtime/src/rpc/common_api.rs +++ b/tee-worker/enclave-runtime/src/rpc/common_api.rs @@ -483,8 +483,8 @@ pub fn add_common_api { - if let Err(_) = - email::send_verification_email(&mut mailer, email, verification_code) + if email::send_verification_email(&mut mailer, email, verification_code) + .is_err() { return Ok(json!(compute_hex_encoded_return_error( "Could not send verification email" @@ -494,10 +494,9 @@ pub fn add_common_api - return Ok(json!(compute_hex_encoded_return_error( - "Could not save verification code" - ))), + Err(_) => Ok(json!(compute_hex_encoded_return_error( + "Could not save verification code" + ))), } }, Err(_) => Ok(json!(compute_hex_encoded_return_error("Could not parse params"))),