From 694345bd5e8bd111b0d32abd2a7b7ab790f2bf1f Mon Sep 17 00:00:00 2001 From: Ammar Arif Date: Fri, 31 May 2024 15:31:18 -0400 Subject: [PATCH 1/2] add test for loading credentials file --- Cargo.lock | 19 ++++++++++++++ Cargo.toml | 1 + src/credential.rs | 65 ++++++++++++++++++++++++++++++++++++----------- 3 files changed, 70 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9d6fabe..2e1cb6b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1429,6 +1429,12 @@ dependencies = [ "uint", ] +[[package]] +name = "fastrand" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" + [[package]] name = "fixed-hash" version = "0.8.0" @@ -3275,6 +3281,7 @@ dependencies = [ "serde_json", "shellexpand", "starknet", + "tempfile", "thiserror", "tokio", "tower-http", @@ -3652,6 +3659,18 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" +[[package]] +name = "tempfile" +version = "3.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" +dependencies = [ + "cfg-if", + "fastrand", + "rustix", + "windows-sys 0.52.0", +] + [[package]] name = "term" version = "0.7.0" diff --git a/Cargo.toml b/Cargo.toml index 8fcbebb..e953864 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ urlencoding = "2" webbrowser = "0.8" starknet = "0.10.0" url = "2.2.2" +tempfile = "3.10.1" [[bin]] name = "slot" diff --git a/src/credential.rs b/src/credential.rs index 39cbf7d..231de80 100644 --- a/src/credential.rs +++ b/src/credential.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use std::fs; use std::io::{self}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use crate::command::auth::info::me::MeMe; @@ -25,9 +25,9 @@ pub struct AccessToken { } #[derive(Debug, Clone, Serialize, Deserialize)] -pub struct LegacyCredentials { - pub access_token: String, - pub token_type: String, +struct LegacyCredentials { + access_token: String, + token_type: String, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -46,17 +46,7 @@ impl Credentials { } pub fn load() -> Result { - let path = get_file_path(); - if !path.exists() { - return Err(Error::Unauthorized); - } - - let content = fs::read_to_string(path.clone())?; - let credentials: Result = serde_json::from_str(&content); - match credentials { - Ok(creds) => Ok(creds), - Err(_) => Err(Error::LegacyCredentials), - } + load_at_path(get_file_path()) } pub fn write(&self) -> io::Result<()> { @@ -70,9 +60,54 @@ impl Credentials { } } +fn load_at_path>(path: P) -> Result { + let path = path.as_ref(); + + if !path.exists() { + return Err(Error::Unauthorized); + } + + let content = fs::read_to_string(path)?; + let credentials = serde_json::from_str::(&content); + + match credentials { + Ok(creds) => Ok(creds), + Err(_) => { + // check if the file is in the legacy format + let legacy = serde_json::from_str::(&content); + match legacy { + Ok(_) => Err(Error::LegacyCredentials), + Err(e) => Err(Error::IO(e.into())), + } + } + } +} + /// Get the path to the credentials file. fn get_file_path() -> PathBuf { let mut path = dirs::config_local_dir().unwrap(); path.extend([SLOT_DIR, CREDENTIALS_FILE]); path } + +#[cfg(test)] +mod tests { + use super::{load_at_path, LegacyCredentials}; + use std::fs; + use tempfile::tempdir; + + #[test] + fn loading_legacy_credentials() { + let cred = LegacyCredentials { + access_token: "mytoken".to_string(), + token_type: "mytokentype".to_string(), + }; + + let dir = tempdir().unwrap(); + let path = dir.path().join("cred.json"); + fs::write(&path, serde_json::to_vec(&cred).unwrap()).unwrap(); + + let err = load_at_path(path).unwrap_err(); + assert!(err.to_string().contains("Legacy credentials found")) + } +} From 113de14a12ed3f413cbf4b85a9d8a0718a373097 Mon Sep 17 00:00:00 2001 From: Ammar Arif Date: Fri, 31 May 2024 15:31:34 -0400 Subject: [PATCH 2/2] include webauthn id in graphql query --- src/command/auth/info.graphql | 1 + 1 file changed, 1 insertion(+) diff --git a/src/command/auth/info.graphql b/src/command/auth/info.graphql index fbd519e..75a2cab 100644 --- a/src/command/auth/info.graphql +++ b/src/command/auth/info.graphql @@ -5,6 +5,7 @@ query Me { contractAddress credentials { webauthn { + id publicKey } }