Set up a bare memory auth context and a few endpoints

This commit is contained in:
Savanni D'Gerinel 2022-11-17 22:18:51 -05:00
parent 6d32a641b1
commit 23b28144a1
4 changed files with 100 additions and 3 deletions

15
server/Cargo.lock generated
View File

@ -673,6 +673,20 @@ name = "serde"
version = "1.0.147" version = "1.0.147"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d193d69bae983fc11a79df82342761dfbf28a99fc8d203dca4c3c1b590948965" checksum = "d193d69bae983fc11a79df82342761dfbf28a99fc8d203dca4c3c1b590948965"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.147"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f1d362ca8fc9c3e3a7484440752472d68a6caa98f1ab81d99b5dfe517cec852"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "serde_json" name = "serde_json"
@ -704,6 +718,7 @@ dependencies = [
"anyhow", "anyhow",
"rand", "rand",
"rusqlite", "rusqlite",
"serde",
"sha2", "sha2",
"tempfile", "tempfile",
"thiserror", "thiserror",

View File

@ -2,6 +2,7 @@
name = "server" name = "server"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
default-run = "server"
# 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
@ -9,6 +10,7 @@ edition = "2021"
anyhow = { version = "1" } anyhow = { version = "1" }
rand = { version = "0.8" } rand = { version = "0.8" }
rusqlite = { version = "0.26" } rusqlite = { version = "0.26" }
serde = { version = "1.0", features = ["derive"] }
sha2 = { version = "0.10" } sha2 = { version = "0.10" }
thiserror = { version = "1" } thiserror = { version = "1" }
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }

View File

@ -117,6 +117,12 @@ impl FromStr for Username {
} }
} }
impl From<&Username> for String {
fn from(s: &Username) -> Self {
s.0.clone()
}
}
impl From<Username> for String { impl From<Username> for String {
fn from(s: Username) -> Self { fn from(s: Username) -> Self {
s.0.clone() s.0.clone()
@ -153,9 +159,11 @@ pub trait AuthenticationDB {
fn validate_session(&self, session: SessionToken) -> AppResult<(), AuthenticationError>; fn validate_session(&self, session: SessionToken) -> AppResult<(), AuthenticationError>;
fn get_user_id(&self, username: Username) -> AppResult<UserId, AuthenticationError>; fn get_user_id(&self, username: Username) -> AppResult<UserId, AuthenticationError>;
fn list_users(&self) -> AppResult<Vec<Username>, AuthenticationError>;
} }
#[derive(Default)] #[derive(Debug, Default)]
pub struct MemoryAuth { pub struct MemoryAuth {
users: HashMap<Username, UserId>, users: HashMap<Username, UserId>,
inverse_users: HashMap<UserId, Username>, inverse_users: HashMap<UserId, Username>,
@ -268,6 +276,10 @@ impl AuthenticationDB for MemoryAuth {
.map(|u| u.clone()) .map(|u| u.clone())
.ok_or(AuthenticationError::UserNotFound)) .ok_or(AuthenticationError::UserNotFound))
} }
fn list_users(&self) -> AppResult<Vec<Username>, AuthenticationError> {
ok(self.users.keys().cloned().collect::<Vec<Username>>())
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -1,12 +1,77 @@
use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use serde::{Deserialize, Serialize};
use std::{
net::{IpAddr, Ipv4Addr, SocketAddr},
sync::{Arc, RwLock},
};
use warp::Filter; use warp::Filter;
mod authentication; mod authentication;
use authentication::{AuthenticationDB, MemoryAuth, Username};
mod database; mod database;
mod errors; mod errors;
#[derive(Serialize)]
struct ErrorResponse {
error: String,
}
#[derive(Deserialize)]
struct MakeUserParameters {
username: String,
}
#[derive(Serialize)]
struct MakeUserResponse {
userid: String,
}
#[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 make_user = {
let auth_ctx = auth_ctx.clone();
warp::path!("api" / "v1" / "user")
.and(warp::put())
.and(warp::body::json())
.map(move |params: MakeUserParameters| {
let mut auth_ctx = auth_ctx.write().unwrap();
println!("{:?}", *auth_ctx);
match (*auth_ctx).create_user(Username::from(params.username.as_str())) {
Ok(Ok(userid)) => warp::reply::json(&MakeUserResponse {
userid: String::from(userid),
}),
Ok(auth_error) => warp::reply::json(&ErrorResponse {
error: format!("{:?}", auth_error),
}),
Err(err) => panic!("{}", err),
}
})
};
let list_users = {
let auth_ctx = auth_ctx.clone();
warp::path!("api" / "v1" / "user")
.and(warp::get())
.map(move || {
let auth_ctx = auth_ctx.read().unwrap();
println!("{:?}", *auth_ctx);
match (*auth_ctx).list_users() {
Ok(Ok(users)) => warp::reply::json(
&users
.iter()
.map(|u| String::from(u))
.collect::<Vec<String>>(),
),
Ok(auth_error) => warp::reply::json(&ErrorResponse {
error: format!("{:?}", auth_error),
}),
Err(err) => panic!("{}", err),
}
})
};
let echo_unauthenticated = warp::path!("api" / "v1" / "echo" / String).map(|param: String| { let echo_unauthenticated = warp::path!("api" / "v1" / "echo" / String).map(|param: String| {
println!("param: {}", param); println!("param: {}", param);
warp::reply::json(&vec!["unauthenticated", param.as_str()]) warp::reply::json(&vec!["unauthenticated", param.as_str()])
@ -27,7 +92,10 @@ pub async fn main() {
warp::reply::json(&vec!["authed", param.as_str()]) warp::reply::json(&vec!["authed", param.as_str()])
}); });
let filter = echo_authenticated.or(echo_unauthenticated); let filter = list_users
.or(make_user)
.or(echo_authenticated)
.or(echo_unauthenticated);
let server = warp::serve(filter); let server = warp::serve(filter);
server server