From ef8c6c0b152bd87ffaa096d78a99967da1990166 Mon Sep 17 00:00:00 2001 From: cosmasken Date: Sat, 1 Jun 2024 16:07:55 +0300 Subject: [PATCH] update auth.js --- canisters/ethlogin/Cargo.toml | 7 -- canisters/ethlogin/src/lib.rs | 0 canisters/siwe_ic/Cargo.toml | 18 --- canisters/siwe_ic/siwe_ic.did | 17 --- canisters/siwe_ic/src/lib.rs | 147 ---------------------- dfx.json | 3 +- frontend/App.vue | 43 ------- frontend/components/auth/AuthDropDown.vue | 84 ------------- frontend/components/auth/SignIn.vue | 61 ++------- frontend/images/favicon.ico | Bin 131200 -> 110612 bytes frontend/main.js | 5 - frontend/store/auth.js | 130 +------------------ package.json | 32 ----- 13 files changed, 10 insertions(+), 537 deletions(-) delete mode 100644 canisters/ethlogin/Cargo.toml delete mode 100644 canisters/ethlogin/src/lib.rs delete mode 100644 canisters/siwe_ic/Cargo.toml delete mode 100644 canisters/siwe_ic/siwe_ic.did delete mode 100644 canisters/siwe_ic/src/lib.rs delete mode 100644 frontend/components/auth/AuthDropDown.vue diff --git a/canisters/ethlogin/Cargo.toml b/canisters/ethlogin/Cargo.toml deleted file mode 100644 index 67cd6c26..00000000 --- a/canisters/ethlogin/Cargo.toml +++ /dev/null @@ -1,7 +0,0 @@ -[package] -name = "ethlogin" -version = "0.1.0" -edition = "2021" - -[dependencies] -ic_siwe = "0.0.6" diff --git a/canisters/ethlogin/src/lib.rs b/canisters/ethlogin/src/lib.rs deleted file mode 100644 index e69de29b..00000000 diff --git a/canisters/siwe_ic/Cargo.toml b/canisters/siwe_ic/Cargo.toml deleted file mode 100644 index 4da540be..00000000 --- a/canisters/siwe_ic/Cargo.toml +++ /dev/null @@ -1,18 +0,0 @@ -[package] -name = "siwe_ic" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[lib] -crate-type = ["cdylib"] - -[dependencies] -candid = "0.8.4" -futures = "0.3.28" -hex = "0.4.3" -ic-cdk = "0.7.4" -ic-cdk-macros = "0.6.10" -siwe = { git = "https://github.com/domwoe/siwe-rs" } -time = { version = "0.3.15", default-features = false } diff --git a/canisters/siwe_ic/siwe_ic.did b/canisters/siwe_ic/siwe_ic.did deleted file mode 100644 index 3b290101..00000000 --- a/canisters/siwe_ic/siwe_ic.did +++ /dev/null @@ -1,17 +0,0 @@ - -type Session = record { - "address": text; - "expires_at": int64; -}; - -type SessionResult = variant { - "Ok": Session; - "Err": text; -}; - -service : { - "create_session": (text, text) -> (SessionResult); - "get_session": () -> (SessionResult) query; - "clear_session": () -> (); - "greet": (text) -> (text) query; -}; diff --git a/canisters/siwe_ic/src/lib.rs b/canisters/siwe_ic/src/lib.rs deleted file mode 100644 index 69f1ed93..00000000 --- a/canisters/siwe_ic/src/lib.rs +++ /dev/null @@ -1,147 +0,0 @@ -use candid::{Principal, CandidType}; -use futures::executor::block_on; -use hex::FromHex; -use ic_cdk_macros::{query, update}; -use siwe::{Message, VerificationOpts}; -use std::collections::BTreeMap; -use std::str::FromStr; -use time::format_description::well_known::Rfc3339; -use time::OffsetDateTime; - - -const SESSION_EXPIRATION_IN_MINS: i64 = 5; -const CLEANUP_INTERCAL_IN_MINS: i64 = 15; - -#[derive(Clone, Debug, CandidType)] -struct Session { - address: String, - expires_at: i64, -} - -type SessionMap = BTreeMap; - -thread_local! { - static SESSIONS: std::cell::RefCell = std::cell::RefCell::new(BTreeMap::new()); - static LAST_CLEANUP: std::cell::RefCell = std::cell::RefCell::new(OffsetDateTime::from_unix_timestamp(0).unwrap()); -} - - -async fn validate(msg: &Message, sig: &str) -> [u8; 20] { - - let opts = VerificationOpts { - domain: None, - nonce: None, - timestamp: Some(OffsetDateTime::from_unix_timestamp((ic_cdk::api::time() / (1_000_000_000)) as i64).unwrap()) - }; - - // Check if uri is equal to the caller - msg.uri.to_string().eq(&format!("did:icp:{}",ic_cdk::api::caller().to_text())).then(|| ()).ok_or("Invoked by unauthorized principal").unwrap(); - - // Check if target (canister and method) is part of authorized resources - let target = format!("icp:{}",ic_cdk::api::id().to_text()); - msg.resources.clone().into_iter().find(|r| r.as_str().eq(&target)).ok_or(format!("Unauthorized for resource: {}", &target)).unwrap(); - - let sig = <[u8; 65]>::from_hex( sig.strip_prefix("0x").unwrap_or(sig)).unwrap(); - - block_on(msg.verify(&sig, &opts)).unwrap(); - - msg.address -} - -fn check_session() -> Result { - - let caller = ic_cdk::api::caller(); - let now = (ic_cdk::api::time() / (1_000_000_000)) as i64; - - ic_cdk::api::print(std::format!("Checking session for {}", ic_cdk::api::caller().to_text())); - - SESSIONS.with(|sessions| { - let mut sessions = sessions.borrow_mut(); - let session = sessions.get(&caller).ok_or("No session found")?; - if session.expires_at < now { - sessions.remove(&caller); - Err("Session expired".to_string()) - } else { - Ok(session.address.clone()) - } - - }) -} - - -#[query] -fn greet(name: String) -> String { - let caller = check_session().unwrap(); - ic_cdk::api::print(std::format!("Active session for {}", caller)); - format!("Hello, {}!", name) -} - - -#[update] -async fn create_session(siwe_msg: String, siwe_sig: String) -> Result { - - ic_cdk::api::print(std::format!("Creating session for {}...", ic_cdk::api::caller().to_text())); - - let msg = Message::from_str(&siwe_msg).unwrap(); - block_on(validate(&msg, &siwe_sig)); - - let address = hex::encode(msg.address); - - ic_cdk::api::print(std::format!("Associated ETH account {:?}", address)); - - let now = OffsetDateTime::from_unix_timestamp((ic_cdk::api::time() / 1_000_000_000) as i64).unwrap(); - - let issued_at = OffsetDateTime::parse(&msg.issued_at.to_string(), &Rfc3339).unwrap(); - let expires_at = issued_at + time::Duration::minutes(SESSION_EXPIRATION_IN_MINS); - - - - let session = Session { - address, - expires_at: expires_at.unix_timestamp(), - }; - - SESSIONS.with(|sessions| { - let mut sessions = sessions.borrow_mut(); - sessions.insert(ic_cdk::api::caller(), session.clone()); - }); - - ic_cdk::api::print(std::format!("Created session for {}, expires at {:?}", ic_cdk::api::caller().to_text(), expires_at)); - - LAST_CLEANUP.with(|last_cleanup| { - let mut last_cleanup = last_cleanup.borrow_mut(); - if now - *last_cleanup > time::Duration::minutes(CLEANUP_INTERCAL_IN_MINS) { - _cleanup_sessions(); - *last_cleanup = now; - } - }); - - Ok(session) -} - -#[query] -fn get_session() -> Result { - check_session().unwrap(); - SESSIONS.with(|sessions| { - let sessions = sessions.borrow(); - let session = sessions.get(&ic_cdk::api::caller()).ok_or("No session found. Please sign in.")?; - Ok(session.clone()) - }) -} - -#[update] -fn clear_session() { - SESSIONS.with(|sessions| { - let mut sessions = sessions.borrow_mut(); - sessions.remove(&ic_cdk::api::caller()); - }); -} - -fn _cleanup_sessions() { - let now = (ic_cdk::api::time() / 1_000_000_000) as i64; - - SESSIONS.with(|sessions| { - let mut sessions = sessions.borrow_mut(); - sessions.retain(|_, session| session.expires_at > now); - }); -} \ No newline at end of file diff --git a/dfx.json b/dfx.json index 3134ccdc..86d65d0b 100644 --- a/dfx.json +++ b/dfx.json @@ -7,7 +7,6 @@ "dependencies": [ "ckbtc_ledger" - ], "declarations": { "node_compatibility": true @@ -56,6 +55,8 @@ } } } + + }, "defaults": { "build": { diff --git a/frontend/App.vue b/frontend/App.vue index 756fc38f..b5e37f09 100644 --- a/frontend/App.vue +++ b/frontend/App.vue @@ -1,15 +1,3 @@ -<<<<<<< HEAD -