Make some test endpoints and prototype an authentication filter

This commit is contained in:
Savanni D'Gerinel 2023-11-20 23:30:10 -05:00
parent bf93625225
commit 581979fc54
4 changed files with 56 additions and 6 deletions

6
.gitignore vendored
View File

@ -5,7 +5,7 @@ dist
result
*.tgz
*.tar.gz
file-service/*.sqlite
file-service/*.sqlite-shm
file-service/*.sqlite-wal
*.sqlite
*.sqlite-shm
*.sqlite-wal
file-service/var

2
Cargo.lock generated
View File

@ -4545,6 +4545,8 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
name = "visions"
version = "0.1.0"
dependencies = [
"authdb",
"tokio",
"warp",
]

View File

@ -6,4 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
warp = "0.3.6"
authdb = { path = "../../authdb/" }
tokio = { version = "1", features = [ "full" ] }
warp = { version = "0.3" }

View File

@ -1,3 +1,49 @@
fn main() {
println!("Hello, world!");
use authdb::{AuthDB, Username};
use std::{
net::{IpAddr, Ipv4Addr, SocketAddr},
path::PathBuf,
sync::{Arc, RwLock},
};
use warp::{header, Filter};
fn with_authentication(
auth_ctx: Arc<RwLock<AuthDB>>,
) -> impl Filter<Extract = (Username,), Error = warp::Rejection> + Clone {
header("authentication").map(|value: String| {
println!("value: {:?}", value);
Username::from("savanni")
})
}
#[tokio::main]
pub async fn main() {
let auth_db = AuthDB::new(PathBuf::from("./auth_db.sqlite"))
.await
.expect("AuthDB should initialize");
let auth_ctx: Arc<RwLock<AuthDB>> = Arc::new(RwLock::new(auth_db));
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(&param)
});
let echo_authenticated = warp::path!("api" / "v1" / "echo" / String)
.and(with_authentication(auth_ctx.clone()))
.map(|param: String, username: Username| {
println!("param: {:?}", username);
println!("param: {}", param);
warp::reply::json(&vec!["authenticated", username.as_str(), param.as_str()])
});
let filter = echo_authenticated.or(authenticate).or(echo_unauthenticated);
let server = warp::serve(filter);
server
.run(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8001))
.await;
}