From 8133ebc8bf790469a27debcf39da9fa434225d7d Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Fri, 25 Nov 2022 14:47:32 -0500 Subject: [PATCH] Start setting up some common structures and building the client library --- common/Cargo.toml | 1 + {server => common}/src/errors.rs | 0 common/src/lib.rs | 1 + common/src/types/mod.rs | 147 ++++++++++++++- server/Cargo.lock | 11 ++ server/Cargo.toml | 1 + server/src/authentication.rs | 141 +------------- server/src/main.rs | 60 +----- v-client/src-tauri/Cargo.lock | 307 ++++++++++++++++++++++++++++++- v-client/src-tauri/Cargo.toml | 9 +- v-client/src-tauri/src/client.rs | 89 +++++++++ v-client/src-tauri/src/main.rs | 12 +- 12 files changed, 580 insertions(+), 199 deletions(-) rename {server => common}/src/errors.rs (100%) create mode 100644 v-client/src-tauri/src/client.rs diff --git a/common/Cargo.toml b/common/Cargo.toml index 8dc262a..16d83b2 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -6,6 +6,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +rusqlite = { version = "0.26" } serde = { version = "1" } serde_derive = { version = "1" } thiserror = { version = "1" } diff --git a/server/src/errors.rs b/common/src/errors.rs similarity index 100% rename from server/src/errors.rs rename to common/src/errors.rs diff --git a/common/src/lib.rs b/common/src/lib.rs index e496787..b3a0e71 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -1,4 +1,5 @@ #[macro_use] extern crate serde_derive; +pub mod errors; pub mod types; diff --git a/common/src/types/mod.rs b/common/src/types/mod.rs index 01f979c..cd63ae8 100644 --- a/common/src/types/mod.rs +++ b/common/src/types/mod.rs @@ -1,5 +1,8 @@ -mod character; +use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ValueRef}; +use std::{convert::Infallible, str::FromStr}; +use thiserror::Error; +mod character; pub use character::CharacterSheet; #[derive(Clone, Debug, Serialize, Deserialize)] @@ -12,3 +15,145 @@ pub struct Roll { dice: Vec, modifier: i8, } + +#[derive(Debug, Error, PartialEq)] +pub enum AuthenticationError { + #[error("username already exists")] + DuplicateUsername, + #[error("invitation is not valid")] + InvalidInvitation, + #[error("session token not found")] + InvalidSession, + #[error("user not found")] + UserNotFound, +} + +#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub struct SessionToken(String); + +impl From<&str> for SessionToken { + fn from(s: &str) -> Self { + SessionToken(s.to_owned()) + } +} + +impl FromStr for SessionToken { + type Err = Infallible; + + fn from_str(s: &str) -> Result { + Ok(SessionToken(s.to_owned())) + } +} + +impl From for String { + fn from(s: SessionToken) -> Self { + s.0.clone() + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub struct Invitation(String); + +impl From<&str> for Invitation { + fn from(s: &str) -> Self { + Invitation(s.to_owned()) + } +} + +impl FromStr for Invitation { + type Err = Infallible; + + fn from_str(s: &str) -> Result { + Ok(Invitation(s.to_owned())) + } +} + +impl From for String { + fn from(s: Invitation) -> Self { + s.0.clone() + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub struct UserId(String); + +impl From<&str> for UserId { + fn from(s: &str) -> Self { + UserId(s.to_owned()) + } +} + +impl FromStr for UserId { + type Err = Infallible; + + fn from_str(s: &str) -> Result { + Ok(UserId(s.to_owned())) + } +} + +impl From for String { + fn from(s: UserId) -> Self { + s.0.clone() + } +} + +impl FromSql for UserId { + fn column_result(val: ValueRef<'_>) -> FromSqlResult { + match val { + ValueRef::Text(t) => Ok(UserId::from( + String::from_utf8(Vec::from(t)).unwrap().as_ref(), + )), + _ => Err(FromSqlError::InvalidType), + } + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub struct Username(String); + +impl From<&str> for Username { + fn from(s: &str) -> Self { + Username(s.to_owned()) + } +} + +impl FromStr for Username { + type Err = Infallible; + + fn from_str(s: &str) -> Result { + Ok(Username(s.to_owned())) + } +} + +impl From<&Username> for String { + fn from(s: &Username) -> Self { + s.0.clone() + } +} + +impl From for String { + fn from(s: Username) -> Self { + s.0.clone() + } +} + +impl FromSql for Username { + fn column_result(val: ValueRef<'_>) -> FromSqlResult { + match val { + ValueRef::Text(t) => Ok(Username::from( + String::from_utf8(Vec::from(t)).unwrap().as_ref(), + )), + _ => Err(FromSqlError::InvalidType), + } + } +} + +#[derive(Deserialize, Serialize)] +pub struct AuthenticateParams { + pub invitation: Invitation, +} + +#[derive(Deserialize, Serialize)] +pub struct AuthenticateResponse { + pub session_token: SessionToken, +} diff --git a/server/Cargo.lock b/server/Cargo.lock index 7f7abd5..184322b 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -74,6 +74,16 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "common" +version = "0.1.0" +dependencies = [ + "rusqlite", + "serde", + "serde_derive", + "thiserror", +] + [[package]] name = "cpufeatures" version = "0.2.5" @@ -716,6 +726,7 @@ name = "server" version = "0.1.0" dependencies = [ "anyhow", + "common", "rand", "rusqlite", "serde", diff --git a/server/Cargo.toml b/server/Cargo.toml index a459342..2f853f3 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -8,6 +8,7 @@ default-run = "server" [dependencies] anyhow = { version = "1" } +common = { path = "../common" } rand = { version = "0.8" } rusqlite = { version = "0.26" } serde = { version = "1.0", features = ["derive"] } diff --git a/server/src/authentication.rs b/server/src/authentication.rs index fd9b15d..9c7a4a4 100644 --- a/server/src/authentication.rs +++ b/server/src/authentication.rs @@ -1,143 +1,10 @@ -use crate::errors::{error, fatal, ok, AppResult, FatalError}; -use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ValueRef}; -use serde::{Deserialize, Serialize}; +use common::{ + errors::{error, fatal, ok, AppResult, FatalError}, + types::{AuthenticationError, Invitation, SessionToken, UserId, Username}, +}; use std::collections::HashMap; -use std::{convert::Infallible, str::FromStr}; -use thiserror::Error; use uuid::{adapter::Hyphenated, Uuid}; -#[derive(Debug, Error, PartialEq)] -pub enum AuthenticationError { - #[error("username already exists")] - DuplicateUsername, - #[error("invitation is not valid")] - InvalidInvitation, - #[error("session token not found")] - InvalidSession, - #[error("user not found")] - UserNotFound, -} - -#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] -pub struct SessionToken(String); - -impl From<&str> for SessionToken { - fn from(s: &str) -> Self { - SessionToken(s.to_owned()) - } -} - -impl FromStr for SessionToken { - type Err = Infallible; - - fn from_str(s: &str) -> Result { - Ok(SessionToken(s.to_owned())) - } -} - -impl From for String { - fn from(s: SessionToken) -> Self { - s.0.clone() - } -} - -#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] -pub struct Invitation(String); - -impl From<&str> for Invitation { - fn from(s: &str) -> Self { - Invitation(s.to_owned()) - } -} - -impl FromStr for Invitation { - type Err = Infallible; - - fn from_str(s: &str) -> Result { - Ok(Invitation(s.to_owned())) - } -} - -impl From for String { - fn from(s: Invitation) -> Self { - s.0.clone() - } -} - -#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] -pub struct UserId(String); - -impl From<&str> for UserId { - fn from(s: &str) -> Self { - UserId(s.to_owned()) - } -} - -impl FromStr for UserId { - type Err = Infallible; - - fn from_str(s: &str) -> Result { - Ok(UserId(s.to_owned())) - } -} - -impl From for String { - fn from(s: UserId) -> Self { - s.0.clone() - } -} - -impl FromSql for UserId { - fn column_result(val: ValueRef<'_>) -> FromSqlResult { - match val { - ValueRef::Text(t) => Ok(UserId::from( - String::from_utf8(Vec::from(t)).unwrap().as_ref(), - )), - _ => Err(FromSqlError::InvalidType), - } - } -} - -#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] -pub struct Username(String); - -impl From<&str> for Username { - fn from(s: &str) -> Self { - Username(s.to_owned()) - } -} - -impl FromStr for Username { - type Err = Infallible; - - fn from_str(s: &str) -> Result { - Ok(Username(s.to_owned())) - } -} - -impl From<&Username> for String { - fn from(s: &Username) -> Self { - s.0.clone() - } -} - -impl From for String { - fn from(s: Username) -> Self { - s.0.clone() - } -} - -impl FromSql for Username { - fn column_result(val: ValueRef<'_>) -> FromSqlResult { - match val { - ValueRef::Text(t) => Ok(Username::from( - String::from_utf8(Vec::from(t)).unwrap().as_ref(), - )), - _ => Err(FromSqlError::InvalidType), - } - } -} - pub trait AuthenticationDB: Send + Sync { fn create_user(&mut self, username: Username) -> AppResult; diff --git a/server/src/main.rs b/server/src/main.rs index 547b424..a60c593 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,4 +1,6 @@ -use errors::{ok, AppResult}; +use common::types::{ + AuthenticateParams, AuthenticateResponse, Invitation, SessionToken, UserId, Username, +}; use serde::{Deserialize, Serialize}; use std::{ net::{IpAddr, Ipv4Addr, SocketAddr}, @@ -7,12 +9,9 @@ use std::{ use warp::Filter; mod authentication; -use authentication::{ - AuthenticationDB, AuthenticationError, Invitation, MemoryAuth, SessionToken, UserId, Username, -}; +use authentication::{AuthenticationDB, MemoryAuth}; mod database; -mod errors; #[derive(Debug)] struct AuthenticationRefused; @@ -87,7 +86,6 @@ struct MakeInvitationResponse { invitation: Invitation, } -/* fn make_invitation( auth_ctx: Arc>, ) -> impl Filter + Clone { @@ -96,7 +94,7 @@ fn make_invitation( .and(warp::body::json()) .map(move |params: MakeInvitationParams| { let mut auth_ctx = auth_ctx.write().unwrap(); - match (*auth_ctx).create_invitation(params.userid) { + match (*auth_ctx).create_invitation(¶ms.userid) { Ok(Ok(invitation)) => warp::reply::json(&MakeInvitationResponse { invitation }), Ok(Err(auth_error)) => warp::reply::json(&ErrorResponse { error: format!("{:?}", auth_error), @@ -105,17 +103,6 @@ fn make_invitation( } }) } -*/ - -#[derive(Deserialize)] -struct AuthenticateParams { - invitation: Invitation, -} - -#[derive(Serialize)] -struct AuthenticateResponse { - session_token: SessionToken, -} fn authenticate( auth_ctx: Arc>, @@ -135,7 +122,6 @@ fn authenticate( }) } -/* fn list_users( auth_ctx: Arc>, ) -> impl Filter + Clone { @@ -147,7 +133,7 @@ fn list_users( Ok(Ok(users)) => warp::reply::json( &users .iter() - .map(|u| String::from(u)) + .map(|u| String::from(u.clone())) .collect::>(), ), Ok(auth_error) => warp::reply::json(&ErrorResponse { @@ -157,45 +143,15 @@ fn list_users( } }) } -*/ #[tokio::main] pub async fn main() { let auth_ctx: Arc> = Arc::new(RwLock::new(Default::default())); - let echo_unauthenticated = warp::path!("api" / "v1" / "echo" / String).map(|param: String| { - println!("param: {}", param); - warp::reply::json(&vec!["unauthenticated", param.as_str()]) - }); - - /* - let authenticate = warp::path!("api" / "v1" / "auth" / String).map(|param: String| { - println!("param: {}", param); - warp::reply::json(¶m) - }); - */ - - let echo_authenticated = warp::path!("api" / "v1" / "echo" / String) - .and(with_session(auth_ctx.clone())) - .map(|param: String, (username, userid)| { - println!("param: {:?}", username); - println!("param: {:?}", userid); - println!("param: {}", param); - warp::reply::json(&vec!["authed", param.as_str()]) - }); - - /* - let filter = list_users(auth_ctx.clone()) - .or(make_user(auth_ctx.clone())) + let filter = make_user(auth_ctx.clone()) .or(make_invitation(auth_ctx.clone())) .or(authenticate(auth_ctx.clone())) - .or(echo_authenticated) - .or(echo_unauthenticated); - */ - let filter = make_user(auth_ctx.clone()) - .or(authenticate(auth_ctx.clone())) - .or(echo_authenticated) - .or(echo_unauthenticated); + .or(list_users(auth_ctx.clone())); let server = warp::serve(filter); server diff --git a/v-client/src-tauri/Cargo.lock b/v-client/src-tauri/Cargo.lock index 102a019..db57231 100644 --- a/v-client/src-tauri/Cargo.lock +++ b/v-client/src-tauri/Cargo.lock @@ -14,6 +14,17 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" +[[package]] +name = "ahash" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" +dependencies = [ + "getrandom 0.2.8", + "once_cell", + "version_check", +] + [[package]] name = "aho-corasick" version = "0.7.19" @@ -48,12 +59,26 @@ checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6" name = "app" version = "0.1.0" dependencies = [ + "async-trait", + "common", + "reqwest", "serde", "serde_json", "tauri", "tauri-build", ] +[[package]] +name = "async-trait" +version = "0.1.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e805d94e6b5001b651426cf4cd446b1ab5f319d27bab5c644f61de0a804360c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "atk" version = "0.15.1" @@ -308,6 +333,16 @@ dependencies = [ "memchr", ] +[[package]] +name = "common" +version = "0.1.0" +dependencies = [ + "rusqlite", + "serde", + "serde_derive", + "thiserror", +] + [[package]] name = "convert_case" version = "0.4.0" @@ -587,6 +622,18 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "fallible-iterator" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" + +[[package]] +name = "fallible-streaming-iterator" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" + [[package]] name = "fastrand" version = "1.8.0" @@ -711,6 +758,12 @@ dependencies = [ "syn", ] +[[package]] +name = "futures-sink" +version = "0.3.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39c15cf1a4aa79df40f1bb462fb39676d0ad9e366c2a33b590d7c66f4f81fcf9" + [[package]] name = "futures-task" version = "0.3.25" @@ -1017,12 +1070,49 @@ dependencies = [ "syn", ] +[[package]] +name = "h2" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f9f29bc9dda355256b2916cf526ab02ce0aeaaaf2bad60d65ef3f12f11dd0f4" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbrown" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" +dependencies = [ + "ahash", +] + [[package]] name = "hashbrown" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +[[package]] +name = "hashlink" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7249a3129cbc1ffccd74857f81464a323a152173cdb134e0fd81bc803b29facf" +dependencies = [ + "hashbrown 0.11.2", +] + [[package]] name = "heck" version = "0.3.3" @@ -1072,12 +1162,72 @@ dependencies = [ "itoa 1.0.4", ] +[[package]] +name = "http-body" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1" +dependencies = [ + "bytes", + "http", + "pin-project-lite", +] + [[package]] name = "http-range" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "21dec9db110f5f872ed9699c3ecf50cf16f423502706ba5c72462e28d3157573" +[[package]] +name = "httparse" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" + +[[package]] +name = "httpdate" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" + +[[package]] +name = "hyper" +version = "0.14.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "034711faac9d2166cb1baf1a2fb0b60b1f277f8492fd72176c17f3515e1abd3c" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2", + "http", + "http-body", + "httparse", + "httpdate", + "itoa 1.0.4", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", + "want", +] + +[[package]] +name = "hyper-tls" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" +dependencies = [ + "bytes", + "hyper", + "native-tls", + "tokio", + "tokio-native-tls", +] + [[package]] name = "ico" version = "0.1.0" @@ -1142,7 +1292,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" dependencies = [ "autocfg", - "hashbrown", + "hashbrown 0.12.3", ] [[package]] @@ -1172,6 +1322,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "ipnet" +version = "2.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f88c5561171189e69df9d98bcf18fd5f9558300f7ea7b801eb8a0fd748bd8745" + [[package]] name = "itoa" version = "0.4.8" @@ -1280,6 +1436,16 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "libsqlite3-sys" +version = "0.23.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cafc7c74096c336d9d27145f7ebd4f4b6f95ba16aa5a282387267e6925cb58" +dependencies = [ + "pkg-config", + "vcpkg", +] + [[package]] name = "line-wrap" version = "0.1.1" @@ -1395,6 +1561,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "mime" +version = "0.3.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" + [[package]] name = "miniz_oxide" version = "0.5.4" @@ -1413,6 +1585,18 @@ dependencies = [ "adler", ] +[[package]] +name = "mio" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5d732bc30207a6423068df043e3d02e0735b155ad7ce1a6f76fe2baa5b158de" +dependencies = [ + "libc", + "log", + "wasi 0.11.0+wasi-snapshot-preview1", + "windows-sys 0.42.0", +] + [[package]] name = "native-tls" version = "0.2.11" @@ -2146,6 +2330,43 @@ dependencies = [ "winapi", ] +[[package]] +name = "reqwest" +version = "0.11.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68cc60575865c7831548863cc02356512e3f1dc2f3f82cb837d7fc4cc8f3c97c" +dependencies = [ + "base64", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "h2", + "http", + "http-body", + "hyper", + "hyper-tls", + "ipnet", + "js-sys", + "log", + "mime", + "native-tls", + "once_cell", + "percent-encoding", + "pin-project-lite", + "serde", + "serde_json", + "serde_urlencoded", + "tokio", + "tokio-native-tls", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "winreg", +] + [[package]] name = "rfd" version = "0.10.0" @@ -2170,6 +2391,21 @@ dependencies = [ "windows 0.37.0", ] +[[package]] +name = "rusqlite" +version = "0.26.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ba4d3462c8b2e4d7f4fcfcf2b296dc6b65404fbbc7b63daa37fd485c149daf7" +dependencies = [ + "bitflags", + "fallible-iterator", + "fallible-streaming-iterator", + "hashlink", + "libsqlite3-sys", + "memchr", + "smallvec", +] + [[package]] name = "rustc_version" version = "0.3.3" @@ -2466,6 +2702,16 @@ version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" +[[package]] +name = "socket2" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02e2d2db9033d13a1567121ddd7a095ee144db4e1ca1b1bda3419bc0da294ebd" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "soup2" version = "0.2.1" @@ -2954,9 +3200,37 @@ checksum = "a9e03c497dc955702ba729190dc4aac6f2a0ce97f913e5b1b5912fc5039d9099" dependencies = [ "autocfg", "bytes", + "libc", "memchr", + "mio", "num_cpus", "pin-project-lite", + "socket2", + "winapi", +] + +[[package]] +name = "tokio-native-tls" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7d995660bd2b7f8c1568414c1126076c13fbb725c40112dc0120b78eb9b717b" +dependencies = [ + "native-tls", + "tokio", +] + +[[package]] +name = "tokio-util" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bb2e075f03b3d66d8d8785356224ba688d2906a371015e225beeb65ca92c740" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", + "tracing", ] [[package]] @@ -2968,6 +3242,12 @@ dependencies = [ "serde", ] +[[package]] +name = "tower-service" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" + [[package]] name = "tracing" version = "0.1.37" @@ -3039,6 +3319,12 @@ dependencies = [ "serde_json", ] +[[package]] +name = "try-lock" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" + [[package]] name = "typenum" version = "1.15.0" @@ -3152,6 +3438,16 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "want" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" +dependencies = [ + "log", + "try-lock", +] + [[package]] name = "wasi" version = "0.9.0+wasi-snapshot-preview1" @@ -3618,6 +3914,15 @@ version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" +[[package]] +name = "winreg" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d" +dependencies = [ + "winapi", +] + [[package]] name = "winres" version = "0.1.12" diff --git a/v-client/src-tauri/Cargo.toml b/v-client/src-tauri/Cargo.toml index 2f1dbb2..8dc12db 100644 --- a/v-client/src-tauri/Cargo.toml +++ b/v-client/src-tauri/Cargo.toml @@ -15,9 +15,12 @@ rust-version = "1.57" tauri-build = { version = "1.0.4", features = [] } [dependencies] -serde_json = "1.0" -serde = { version = "1.0", features = ["derive"] } -tauri = { version = "1.0.5", features = ["api-all"] } +async-trait = { version = "0.1" } +common = { path = "../../common" } +reqwest = { version = "0.11", features = [ "json" ] } +serde_json = { verison = "1.0" } +serde = { version = "1.0", features = ["derive"] } +tauri = { version = "1.0.5", features = ["api-all"] } [features] # by default Tauri runs in production mode diff --git a/v-client/src-tauri/src/client.rs b/v-client/src-tauri/src/client.rs new file mode 100644 index 0000000..9ee1f1e --- /dev/null +++ b/v-client/src-tauri/src/client.rs @@ -0,0 +1,89 @@ +use async_trait::async_trait; +use common::{ + errors::{AppResult, FatalError}, + types::{ + AuthenticateParams, AuthenticateResponse, AuthenticationError, Invitation, SessionToken, + UserId, Username, + }, +}; +use reqwest::{self, Url}; +use serde_json::json; + +#[async_trait] +trait Client { + async fn create_user(&self, username: Username) -> AppResult; + async fn create_invitation(&self, userid: UserId) + -> AppResult; + async fn delete_user(&self, userid: UserId) -> AppResult<(), AuthenticationError>; + async fn delete_invitation(&self, invitation: Invitation) + -> AppResult<(), AuthenticationError>; + async fn delete_session(&self, session: SessionToken) -> AppResult<(), AuthenticationError>; + async fn authenticate( + &self, + invitation: Invitation, + ) -> AppResult; + async fn list_users(&self) -> AppResult, AuthenticationError>; +} + +struct VisionsClient { + client: reqwest::Client, + url: Url, +} + +#[async_trait] +impl Client for VisionsClient { + async fn create_user(&self, username: Username) -> AppResult { + unimplemented!() + } + + async fn create_invitation( + &self, + userid: UserId, + ) -> AppResult { + unimplemented!() + } + + async fn delete_user(&self, userid: UserId) -> AppResult<(), AuthenticationError> { + unimplemented!() + } + + async fn delete_invitation( + &self, + invitation: Invitation, + ) -> AppResult<(), AuthenticationError> { + unimplemented!() + } + + async fn delete_session(&self, session: SessionToken) -> AppResult<(), AuthenticationError> { + unimplemented!() + } + + async fn authenticate( + &self, + invitation: Invitation, + ) -> AppResult { + let mut url = self.url.clone(); + url.set_path("api/v1/invitations"); + match self + .client + .put(url) + .json(&AuthenticateParams { invitation }) + .send() + .await + { + Ok(resp) => { + let js = resp.json().await; + println!("authenticate response: {:?}", js); + match js { + Ok(response) => Ok(Ok(response)), + Err(err) => panic!("could not decode response {:?}", err), + } + } + Err(err) => panic!("http client error {:?}", err), + } + } + + async fn list_users(&self) -> AppResult, AuthenticationError> { + unimplemented!() + } +} diff --git a/v-client/src-tauri/src/main.rs b/v-client/src-tauri/src/main.rs index e994ea4..700eda5 100644 --- a/v-client/src-tauri/src/main.rs +++ b/v-client/src-tauri/src/main.rs @@ -1,10 +1,12 @@ #![cfg_attr( - all(not(debug_assertions), target_os = "windows"), - windows_subsystem = "windows" + all(not(debug_assertions), target_os = "windows"), + windows_subsystem = "windows" )] +mod client; + fn main() { - tauri::Builder::default() - .run(tauri::generate_context!()) - .expect("error while running tauri application"); + tauri::Builder::default() + .run(tauri::generate_context!()) + .expect("error while running tauri application"); }