Start setting up some common structures and building the client library

This commit is contained in:
Savanni D'Gerinel 2022-11-25 14:47:32 -05:00
parent deb3415c30
commit 8133ebc8bf
12 changed files with 580 additions and 199 deletions

View File

@ -6,6 +6,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
rusqlite = { version = "0.26" }
serde = { version = "1" } serde = { version = "1" }
serde_derive = { version = "1" } serde_derive = { version = "1" }
thiserror = { version = "1" } thiserror = { version = "1" }

View File

@ -1,4 +1,5 @@
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;
pub mod errors;
pub mod types; pub mod types;

View File

@ -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; pub use character::CharacterSheet;
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
@ -12,3 +15,145 @@ pub struct Roll {
dice: Vec<Die>, dice: Vec<Die>,
modifier: i8, 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<Self, Self::Err> {
Ok(SessionToken(s.to_owned()))
}
}
impl From<SessionToken> 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<Self, Self::Err> {
Ok(Invitation(s.to_owned()))
}
}
impl From<Invitation> 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<Self, Self::Err> {
Ok(UserId(s.to_owned()))
}
}
impl From<UserId> for String {
fn from(s: UserId) -> Self {
s.0.clone()
}
}
impl FromSql for UserId {
fn column_result(val: ValueRef<'_>) -> FromSqlResult<Self> {
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<Self, Self::Err> {
Ok(Username(s.to_owned()))
}
}
impl From<&Username> for String {
fn from(s: &Username) -> Self {
s.0.clone()
}
}
impl From<Username> for String {
fn from(s: Username) -> Self {
s.0.clone()
}
}
impl FromSql for Username {
fn column_result(val: ValueRef<'_>) -> FromSqlResult<Self> {
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,
}

11
server/Cargo.lock generated
View File

@ -74,6 +74,16 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "common"
version = "0.1.0"
dependencies = [
"rusqlite",
"serde",
"serde_derive",
"thiserror",
]
[[package]] [[package]]
name = "cpufeatures" name = "cpufeatures"
version = "0.2.5" version = "0.2.5"
@ -716,6 +726,7 @@ name = "server"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"common",
"rand", "rand",
"rusqlite", "rusqlite",
"serde", "serde",

View File

@ -8,6 +8,7 @@ default-run = "server"
[dependencies] [dependencies]
anyhow = { version = "1" } anyhow = { version = "1" }
common = { path = "../common" }
rand = { version = "0.8" } rand = { version = "0.8" }
rusqlite = { version = "0.26" } rusqlite = { version = "0.26" }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }

View File

@ -1,143 +1,10 @@
use crate::errors::{error, fatal, ok, AppResult, FatalError}; use common::{
use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ValueRef}; errors::{error, fatal, ok, AppResult, FatalError},
use serde::{Deserialize, Serialize}; types::{AuthenticationError, Invitation, SessionToken, UserId, Username},
};
use std::collections::HashMap; use std::collections::HashMap;
use std::{convert::Infallible, str::FromStr};
use thiserror::Error;
use uuid::{adapter::Hyphenated, Uuid}; 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<Self, Self::Err> {
Ok(SessionToken(s.to_owned()))
}
}
impl From<SessionToken> 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<Self, Self::Err> {
Ok(Invitation(s.to_owned()))
}
}
impl From<Invitation> 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<Self, Self::Err> {
Ok(UserId(s.to_owned()))
}
}
impl From<UserId> for String {
fn from(s: UserId) -> Self {
s.0.clone()
}
}
impl FromSql for UserId {
fn column_result(val: ValueRef<'_>) -> FromSqlResult<Self> {
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<Self, Self::Err> {
Ok(Username(s.to_owned()))
}
}
impl From<&Username> for String {
fn from(s: &Username) -> Self {
s.0.clone()
}
}
impl From<Username> for String {
fn from(s: Username) -> Self {
s.0.clone()
}
}
impl FromSql for Username {
fn column_result(val: ValueRef<'_>) -> FromSqlResult<Self> {
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 { pub trait AuthenticationDB: Send + Sync {
fn create_user(&mut self, username: Username) -> AppResult<UserId, AuthenticationError>; fn create_user(&mut self, username: Username) -> AppResult<UserId, AuthenticationError>;

View File

@ -1,4 +1,6 @@
use errors::{ok, AppResult}; use common::types::{
AuthenticateParams, AuthenticateResponse, Invitation, SessionToken, UserId, Username,
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{
net::{IpAddr, Ipv4Addr, SocketAddr}, net::{IpAddr, Ipv4Addr, SocketAddr},
@ -7,12 +9,9 @@ use std::{
use warp::Filter; use warp::Filter;
mod authentication; mod authentication;
use authentication::{ use authentication::{AuthenticationDB, MemoryAuth};
AuthenticationDB, AuthenticationError, Invitation, MemoryAuth, SessionToken, UserId, Username,
};
mod database; mod database;
mod errors;
#[derive(Debug)] #[derive(Debug)]
struct AuthenticationRefused; struct AuthenticationRefused;
@ -87,7 +86,6 @@ struct MakeInvitationResponse {
invitation: Invitation, invitation: Invitation,
} }
/*
fn make_invitation( fn make_invitation(
auth_ctx: Arc<RwLock<impl AuthenticationDB>>, auth_ctx: Arc<RwLock<impl AuthenticationDB>>,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone { ) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
@ -96,7 +94,7 @@ fn make_invitation(
.and(warp::body::json()) .and(warp::body::json())
.map(move |params: MakeInvitationParams| { .map(move |params: MakeInvitationParams| {
let mut auth_ctx = auth_ctx.write().unwrap(); let mut auth_ctx = auth_ctx.write().unwrap();
match (*auth_ctx).create_invitation(params.userid) { match (*auth_ctx).create_invitation(&params.userid) {
Ok(Ok(invitation)) => warp::reply::json(&MakeInvitationResponse { invitation }), Ok(Ok(invitation)) => warp::reply::json(&MakeInvitationResponse { invitation }),
Ok(Err(auth_error)) => warp::reply::json(&ErrorResponse { Ok(Err(auth_error)) => warp::reply::json(&ErrorResponse {
error: format!("{:?}", auth_error), 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( fn authenticate(
auth_ctx: Arc<RwLock<impl AuthenticationDB>>, auth_ctx: Arc<RwLock<impl AuthenticationDB>>,
@ -135,7 +122,6 @@ fn authenticate(
}) })
} }
/*
fn list_users( fn list_users(
auth_ctx: Arc<RwLock<impl AuthenticationDB>>, auth_ctx: Arc<RwLock<impl AuthenticationDB>>,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone { ) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
@ -147,7 +133,7 @@ fn list_users(
Ok(Ok(users)) => warp::reply::json( Ok(Ok(users)) => warp::reply::json(
&users &users
.iter() .iter()
.map(|u| String::from(u)) .map(|u| String::from(u.clone()))
.collect::<Vec<String>>(), .collect::<Vec<String>>(),
), ),
Ok(auth_error) => warp::reply::json(&ErrorResponse { Ok(auth_error) => warp::reply::json(&ErrorResponse {
@ -157,45 +143,15 @@ fn list_users(
} }
}) })
} }
*/
#[tokio::main] #[tokio::main]
pub async fn main() { pub async fn main() {
let auth_ctx: Arc<RwLock<MemoryAuth>> = Arc::new(RwLock::new(Default::default())); let auth_ctx: Arc<RwLock<MemoryAuth>> = Arc::new(RwLock::new(Default::default()));
let echo_unauthenticated = warp::path!("api" / "v1" / "echo" / String).map(|param: String| { let filter = make_user(auth_ctx.clone())
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(&param)
});
*/
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()))
.or(make_invitation(auth_ctx.clone())) .or(make_invitation(auth_ctx.clone()))
.or(authenticate(auth_ctx.clone())) .or(authenticate(auth_ctx.clone()))
.or(echo_authenticated) .or(list_users(auth_ctx.clone()));
.or(echo_unauthenticated);
*/
let filter = make_user(auth_ctx.clone())
.or(authenticate(auth_ctx.clone()))
.or(echo_authenticated)
.or(echo_unauthenticated);
let server = warp::serve(filter); let server = warp::serve(filter);
server server

View File

@ -14,6 +14,17 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" 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]] [[package]]
name = "aho-corasick" name = "aho-corasick"
version = "0.7.19" version = "0.7.19"
@ -48,12 +59,26 @@ checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6"
name = "app" name = "app"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"async-trait",
"common",
"reqwest",
"serde", "serde",
"serde_json", "serde_json",
"tauri", "tauri",
"tauri-build", "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]] [[package]]
name = "atk" name = "atk"
version = "0.15.1" version = "0.15.1"
@ -308,6 +333,16 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "common"
version = "0.1.0"
dependencies = [
"rusqlite",
"serde",
"serde_derive",
"thiserror",
]
[[package]] [[package]]
name = "convert_case" name = "convert_case"
version = "0.4.0" version = "0.4.0"
@ -587,6 +622,18 @@ dependencies = [
"cfg-if", "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]] [[package]]
name = "fastrand" name = "fastrand"
version = "1.8.0" version = "1.8.0"
@ -711,6 +758,12 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "futures-sink"
version = "0.3.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39c15cf1a4aa79df40f1bb462fb39676d0ad9e366c2a33b590d7c66f4f81fcf9"
[[package]] [[package]]
name = "futures-task" name = "futures-task"
version = "0.3.25" version = "0.3.25"
@ -1017,12 +1070,49 @@ dependencies = [
"syn", "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]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.12.3" version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" 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]] [[package]]
name = "heck" name = "heck"
version = "0.3.3" version = "0.3.3"
@ -1072,12 +1162,72 @@ dependencies = [
"itoa 1.0.4", "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]] [[package]]
name = "http-range" name = "http-range"
version = "0.1.5" version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21dec9db110f5f872ed9699c3ecf50cf16f423502706ba5c72462e28d3157573" 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]] [[package]]
name = "ico" name = "ico"
version = "0.1.0" version = "0.1.0"
@ -1142,7 +1292,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e"
dependencies = [ dependencies = [
"autocfg", "autocfg",
"hashbrown", "hashbrown 0.12.3",
] ]
[[package]] [[package]]
@ -1172,6 +1322,12 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "ipnet"
version = "2.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f88c5561171189e69df9d98bcf18fd5f9558300f7ea7b801eb8a0fd748bd8745"
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "0.4.8" version = "0.4.8"
@ -1280,6 +1436,16 @@ dependencies = [
"pkg-config", "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]] [[package]]
name = "line-wrap" name = "line-wrap"
version = "0.1.1" version = "0.1.1"
@ -1395,6 +1561,12 @@ dependencies = [
"autocfg", "autocfg",
] ]
[[package]]
name = "mime"
version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
[[package]] [[package]]
name = "miniz_oxide" name = "miniz_oxide"
version = "0.5.4" version = "0.5.4"
@ -1413,6 +1585,18 @@ dependencies = [
"adler", "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]] [[package]]
name = "native-tls" name = "native-tls"
version = "0.2.11" version = "0.2.11"
@ -2146,6 +2330,43 @@ dependencies = [
"winapi", "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]] [[package]]
name = "rfd" name = "rfd"
version = "0.10.0" version = "0.10.0"
@ -2170,6 +2391,21 @@ dependencies = [
"windows 0.37.0", "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]] [[package]]
name = "rustc_version" name = "rustc_version"
version = "0.3.3" version = "0.3.3"
@ -2466,6 +2702,16 @@ version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" 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]] [[package]]
name = "soup2" name = "soup2"
version = "0.2.1" version = "0.2.1"
@ -2954,9 +3200,37 @@ checksum = "a9e03c497dc955702ba729190dc4aac6f2a0ce97f913e5b1b5912fc5039d9099"
dependencies = [ dependencies = [
"autocfg", "autocfg",
"bytes", "bytes",
"libc",
"memchr", "memchr",
"mio",
"num_cpus", "num_cpus",
"pin-project-lite", "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]] [[package]]
@ -2968,6 +3242,12 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "tower-service"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52"
[[package]] [[package]]
name = "tracing" name = "tracing"
version = "0.1.37" version = "0.1.37"
@ -3039,6 +3319,12 @@ dependencies = [
"serde_json", "serde_json",
] ]
[[package]]
name = "try-lock"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642"
[[package]] [[package]]
name = "typenum" name = "typenum"
version = "1.15.0" version = "1.15.0"
@ -3152,6 +3438,16 @@ dependencies = [
"winapi-util", "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]] [[package]]
name = "wasi" name = "wasi"
version = "0.9.0+wasi-snapshot-preview1" 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" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5"
[[package]]
name = "winreg"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "winres" name = "winres"
version = "0.1.12" version = "0.1.12"

View File

@ -15,7 +15,10 @@ rust-version = "1.57"
tauri-build = { version = "1.0.4", features = [] } tauri-build = { version = "1.0.4", features = [] }
[dependencies] [dependencies]
serde_json = "1.0" 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"] } serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.0.5", features = ["api-all"] } tauri = { version = "1.0.5", features = ["api-all"] }

View File

@ -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<UserId, AuthenticationError>;
async fn create_invitation(&self, userid: UserId)
-> AppResult<Invitation, AuthenticationError>;
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<SessionToken, AuthenticationError>;
async fn list_users(&self) -> AppResult<Vec<UserId>, AuthenticationError>;
}
struct VisionsClient {
client: reqwest::Client,
url: Url,
}
#[async_trait]
impl Client for VisionsClient {
async fn create_user(&self, username: Username) -> AppResult<UserId, AuthenticationError> {
unimplemented!()
}
async fn create_invitation(
&self,
userid: UserId,
) -> AppResult<Invitation, AuthenticationError> {
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<SessionToken, AuthenticationError> {
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<Vec<UserId>, AuthenticationError> {
unimplemented!()
}
}

View File

@ -3,6 +3,8 @@
windows_subsystem = "windows" windows_subsystem = "windows"
)] )]
mod client;
fn main() { fn main() {
tauri::Builder::default() tauri::Builder::default()
.run(tauri::generate_context!()) .run(tauri::generate_context!())