Switch to Axum and implement the password check
This commit is contained in:
parent
e4c5ce0236
commit
c31870367f
178
Cargo.lock
generated
178
Cargo.lock
generated
@ -290,6 +290,61 @@ version = "1.4.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
|
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "axum"
|
||||||
|
version = "0.7.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "edca88bc138befd0323b20752846e6587272d3b03b0343c8ea28a6f819e6e71f"
|
||||||
|
dependencies = [
|
||||||
|
"async-trait",
|
||||||
|
"axum-core",
|
||||||
|
"bytes",
|
||||||
|
"futures-util",
|
||||||
|
"http 1.1.0",
|
||||||
|
"http-body 1.0.1",
|
||||||
|
"http-body-util",
|
||||||
|
"hyper 1.5.2",
|
||||||
|
"hyper-util",
|
||||||
|
"itoa",
|
||||||
|
"matchit",
|
||||||
|
"memchr",
|
||||||
|
"mime",
|
||||||
|
"percent-encoding",
|
||||||
|
"pin-project-lite",
|
||||||
|
"rustversion",
|
||||||
|
"serde 1.0.210",
|
||||||
|
"serde_json",
|
||||||
|
"serde_path_to_error",
|
||||||
|
"serde_urlencoded",
|
||||||
|
"sync_wrapper 1.0.2",
|
||||||
|
"tokio",
|
||||||
|
"tower",
|
||||||
|
"tower-layer",
|
||||||
|
"tower-service",
|
||||||
|
"tracing",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "axum-core"
|
||||||
|
version = "0.4.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "09f2bd6146b97ae3359fa0cc6d6b376d9539582c7b4220f041a33ec24c226199"
|
||||||
|
dependencies = [
|
||||||
|
"async-trait",
|
||||||
|
"bytes",
|
||||||
|
"futures-util",
|
||||||
|
"http 1.1.0",
|
||||||
|
"http-body 1.0.1",
|
||||||
|
"http-body-util",
|
||||||
|
"mime",
|
||||||
|
"pin-project-lite",
|
||||||
|
"rustversion",
|
||||||
|
"sync_wrapper 1.0.2",
|
||||||
|
"tower-layer",
|
||||||
|
"tower-service",
|
||||||
|
"tracing",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "az"
|
name = "az"
|
||||||
version = "1.2.1"
|
version = "1.2.1"
|
||||||
@ -1883,6 +1938,29 @@ dependencies = [
|
|||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "http-body"
|
||||||
|
version = "1.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184"
|
||||||
|
dependencies = [
|
||||||
|
"bytes",
|
||||||
|
"http 1.1.0",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "http-body-util"
|
||||||
|
version = "0.1.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "793429d76616a256bcb62c2a2ec2bed781c8307e797e2598c50010f2bee2544f"
|
||||||
|
dependencies = [
|
||||||
|
"bytes",
|
||||||
|
"futures-util",
|
||||||
|
"http 1.1.0",
|
||||||
|
"http-body 1.0.1",
|
||||||
|
"pin-project-lite",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "httparse"
|
name = "httparse"
|
||||||
version = "1.9.5"
|
version = "1.9.5"
|
||||||
@ -1913,7 +1991,7 @@ dependencies = [
|
|||||||
"futures-util",
|
"futures-util",
|
||||||
"h2",
|
"h2",
|
||||||
"http 0.2.12",
|
"http 0.2.12",
|
||||||
"http-body",
|
"http-body 0.4.6",
|
||||||
"httparse",
|
"httparse",
|
||||||
"httpdate",
|
"httpdate",
|
||||||
"itoa",
|
"itoa",
|
||||||
@ -1925,6 +2003,25 @@ dependencies = [
|
|||||||
"want",
|
"want",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hyper"
|
||||||
|
version = "1.5.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "256fb8d4bd6413123cc9d91832d78325c48ff41677595be797d90f42969beae0"
|
||||||
|
dependencies = [
|
||||||
|
"bytes",
|
||||||
|
"futures-channel",
|
||||||
|
"futures-util",
|
||||||
|
"http 1.1.0",
|
||||||
|
"http-body 1.0.1",
|
||||||
|
"httparse",
|
||||||
|
"httpdate",
|
||||||
|
"itoa",
|
||||||
|
"pin-project-lite",
|
||||||
|
"smallvec",
|
||||||
|
"tokio",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hyper-tls"
|
name = "hyper-tls"
|
||||||
version = "0.5.0"
|
version = "0.5.0"
|
||||||
@ -1932,12 +2029,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905"
|
checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bytes",
|
"bytes",
|
||||||
"hyper",
|
"hyper 0.14.30",
|
||||||
"native-tls",
|
"native-tls",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-native-tls",
|
"tokio-native-tls",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hyper-util"
|
||||||
|
version = "0.1.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "df2dcfbe0677734ab2f3ffa7fa7bfd4706bfdc1ef393f2ee30184aed67e631b4"
|
||||||
|
dependencies = [
|
||||||
|
"bytes",
|
||||||
|
"futures-util",
|
||||||
|
"http 1.1.0",
|
||||||
|
"http-body 1.0.1",
|
||||||
|
"hyper 1.5.2",
|
||||||
|
"pin-project-lite",
|
||||||
|
"tokio",
|
||||||
|
"tower-service",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "iana-time-zone"
|
name = "iana-time-zone"
|
||||||
version = "0.1.61"
|
version = "0.1.61"
|
||||||
@ -2284,6 +2397,12 @@ dependencies = [
|
|||||||
"value-bag",
|
"value-bag",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "matchit"
|
||||||
|
version = "0.7.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "md-5"
|
name = "md-5"
|
||||||
version = "0.10.6"
|
version = "0.10.6"
|
||||||
@ -3118,8 +3237,8 @@ dependencies = [
|
|||||||
"futures-util",
|
"futures-util",
|
||||||
"h2",
|
"h2",
|
||||||
"http 0.2.12",
|
"http 0.2.12",
|
||||||
"http-body",
|
"http-body 0.4.6",
|
||||||
"hyper",
|
"hyper 0.14.30",
|
||||||
"hyper-tls",
|
"hyper-tls",
|
||||||
"ipnet",
|
"ipnet",
|
||||||
"js-sys",
|
"js-sys",
|
||||||
@ -3133,7 +3252,7 @@ dependencies = [
|
|||||||
"serde 1.0.210",
|
"serde 1.0.210",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"serde_urlencoded",
|
"serde_urlencoded",
|
||||||
"sync_wrapper",
|
"sync_wrapper 0.1.2",
|
||||||
"system-configuration",
|
"system-configuration",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-native-tls",
|
"tokio-native-tls",
|
||||||
@ -3240,6 +3359,12 @@ dependencies = [
|
|||||||
"base64 0.21.7",
|
"base64 0.21.7",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustversion"
|
||||||
|
version = "1.0.19"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rusty-fork"
|
name = "rusty-fork"
|
||||||
version = "0.3.0"
|
version = "0.3.0"
|
||||||
@ -3369,6 +3494,16 @@ dependencies = [
|
|||||||
"serde 1.0.210",
|
"serde 1.0.210",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_path_to_error"
|
||||||
|
version = "0.1.16"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "af99884400da37c88f5e9146b7f1fd0fbcae8f6eec4e9da38b67d05486f814a6"
|
||||||
|
dependencies = [
|
||||||
|
"itoa",
|
||||||
|
"serde 1.0.210",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_spanned"
|
name = "serde_spanned"
|
||||||
version = "0.6.8"
|
version = "0.6.8"
|
||||||
@ -3800,6 +3935,12 @@ version = "0.1.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160"
|
checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sync_wrapper"
|
||||||
|
version = "1.0.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "system-configuration"
|
name = "system-configuration"
|
||||||
version = "0.5.1"
|
version = "0.5.1"
|
||||||
@ -4065,6 +4206,28 @@ dependencies = [
|
|||||||
"winnow",
|
"winnow",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tower"
|
||||||
|
version = "0.5.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d039ad9159c98b70ecfd540b2573b97f7f52c3e8d9f8ad57a24b916a536975f9"
|
||||||
|
dependencies = [
|
||||||
|
"futures-core",
|
||||||
|
"futures-util",
|
||||||
|
"pin-project-lite",
|
||||||
|
"sync_wrapper 1.0.2",
|
||||||
|
"tokio",
|
||||||
|
"tower-layer",
|
||||||
|
"tower-service",
|
||||||
|
"tracing",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tower-layer"
|
||||||
|
version = "0.3.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tower-service"
|
name = "tower-service"
|
||||||
version = "0.3.3"
|
version = "0.3.3"
|
||||||
@ -4336,9 +4499,9 @@ dependencies = [
|
|||||||
"async-std",
|
"async-std",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"authdb",
|
"authdb",
|
||||||
|
"axum",
|
||||||
"cool_asserts",
|
"cool_asserts",
|
||||||
"futures",
|
"futures",
|
||||||
"http 1.1.0",
|
|
||||||
"include_dir",
|
"include_dir",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"mime",
|
"mime",
|
||||||
@ -4355,7 +4518,6 @@ dependencies = [
|
|||||||
"typeshare",
|
"typeshare",
|
||||||
"urlencoding",
|
"urlencoding",
|
||||||
"uuid 1.11.0",
|
"uuid 1.11.0",
|
||||||
"warp",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -4387,7 +4549,7 @@ dependencies = [
|
|||||||
"futures-util",
|
"futures-util",
|
||||||
"headers",
|
"headers",
|
||||||
"http 0.2.12",
|
"http 0.2.12",
|
||||||
"hyper",
|
"hyper 0.14.30",
|
||||||
"log",
|
"log",
|
||||||
"mime",
|
"mime",
|
||||||
"mime_guess",
|
"mime_guess",
|
||||||
|
@ -9,13 +9,13 @@ edition = "2021"
|
|||||||
async-std = { version = "1.13.0" }
|
async-std = { version = "1.13.0" }
|
||||||
async-trait = { version = "0.1.83" }
|
async-trait = { version = "0.1.83" }
|
||||||
authdb = { path = "../../authdb/" }
|
authdb = { path = "../../authdb/" }
|
||||||
|
axum = { version = "0.7.9" }
|
||||||
futures = { version = "0.3.31" }
|
futures = { version = "0.3.31" }
|
||||||
http = { version = "1" }
|
|
||||||
include_dir = { version = "0.7.4" }
|
include_dir = { version = "0.7.4" }
|
||||||
lazy_static = { version = "1.5.0" }
|
lazy_static = { version = "1.5.0" }
|
||||||
mime = { version = "0.3.17" }
|
mime = { version = "0.3.17" }
|
||||||
mime_guess = { version = "2.0.5" }
|
mime_guess = { version = "2.0.5" }
|
||||||
pretty_env_logger = "0.5.0"
|
pretty_env_logger = { version = "0.5.0" }
|
||||||
result-extended = { path = "../../result-extended" }
|
result-extended = { path = "../../result-extended" }
|
||||||
rusqlite = { version = "0.32.1" }
|
rusqlite = { version = "0.32.1" }
|
||||||
rusqlite_migration = { version = "1.3.1", features = ["from-directory"] }
|
rusqlite_migration = { version = "1.3.1", features = ["from-directory"] }
|
||||||
@ -27,7 +27,6 @@ tokio-stream = { version = "0.1.16" }
|
|||||||
typeshare = { version = "1.0.4" }
|
typeshare = { version = "1.0.4" }
|
||||||
urlencoding = { version = "2.1.3" }
|
urlencoding = { version = "2.1.3" }
|
||||||
uuid = { version = "1.11.0", features = ["v4"] }
|
uuid = { version = "1.11.0", features = ["v4"] }
|
||||||
warp = { version = "0.3" }
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
cool_asserts = "2.0.3"
|
cool_asserts = "2.0.3"
|
||||||
|
@ -1,19 +1,9 @@
|
|||||||
mod user_management;
|
mod user_management;
|
||||||
pub use user_management::routes_user_management;
|
// pub use user_management::routes_user_management;
|
||||||
|
|
||||||
use warp::{
|
|
||||||
filters::cors::Builder,
|
|
||||||
http::{header::CONTENT_TYPE, HeaderName, Method, Response},
|
|
||||||
reply::*,
|
|
||||||
Filter,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
asset_db::AssetId,
|
asset_db::AssetId,
|
||||||
core::Core,
|
core::Core,
|
||||||
handlers::{
|
|
||||||
handle_available_images, handle_check_password, handle_connect_websocket, handle_file, handle_get_charsheet, handle_register_client, handle_server_status, handle_set_background_image, handle_unregister_client, RegisterRequest
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -26,6 +16,7 @@ use crate::{
|
|||||||
//
|
//
|
||||||
// The login function does not require authentication, but it should return a session ID
|
// The login function does not require authentication, but it should return a session ID
|
||||||
|
|
||||||
|
/*
|
||||||
fn cors<H, M>(methods: Vec<M>, headers: Vec<H>) -> Builder
|
fn cors<H, M>(methods: Vec<M>, headers: Vec<H>) -> Builder
|
||||||
where
|
where
|
||||||
M: Into<Method>,
|
M: Into<Method>,
|
||||||
@ -36,7 +27,9 @@ where
|
|||||||
.allow_methods(methods)
|
.allow_methods(methods)
|
||||||
.allow_headers(headers)
|
.allow_headers(headers)
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
pub fn route_healthcheck() -> impl Filter<Extract = (impl Reply,), Error = warp::Rejection> + Clone
|
pub fn route_healthcheck() -> impl Filter<Extract = (impl Reply,), Error = warp::Rejection> + Clone
|
||||||
{
|
{
|
||||||
warp::path!("api" / "v1" / "healthcheck")
|
warp::path!("api" / "v1" / "healthcheck")
|
||||||
@ -142,3 +135,4 @@ pub fn route_authenticate(
|
|||||||
})
|
})
|
||||||
.with(cors::<HeaderName, Method>(vec![Method::PUT], vec![]))
|
.with(cors::<HeaderName, Method>(vec![Method::PUT], vec![]))
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
@ -1,17 +1,10 @@
|
|||||||
use std::{convert::Infallible, future::Future};
|
use std::{convert::Infallible, future::Future};
|
||||||
|
|
||||||
use warp::{
|
|
||||||
http::{header::CONTENT_TYPE, HeaderName, Method, Response, StatusCode},
|
|
||||||
reject,
|
|
||||||
reply::Reply,
|
|
||||||
Filter,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
core::Core,
|
core::Core,
|
||||||
handlers::{handle_check_password, handle_get_users, handle_set_admin_password},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
async fn handle_rejection(err: warp::Rejection) -> Result<impl Reply, Infallible> {
|
async fn handle_rejection(err: warp::Rejection) -> Result<impl Reply, Infallible> {
|
||||||
println!("handle_rejection: {:?}", err);
|
println!("handle_rejection: {:?}", err);
|
||||||
if let Some(Unauthorized) = err.find() {
|
if let Some(Unauthorized) = err.find() {
|
||||||
@ -26,7 +19,9 @@ async fn handle_rejection(err: warp::Rejection) -> Result<impl Reply, Infallible
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Unauthorized;
|
struct Unauthorized;
|
||||||
|
|
||||||
@ -64,7 +59,9 @@ pub fn routes_user_management(
|
|||||||
) -> impl Filter<Extract = (impl Reply,), Error = warp::Rejection> + Clone {
|
) -> impl Filter<Extract = (impl Reply,), Error = warp::Rejection> + Clone {
|
||||||
route_get_users(core.clone()).or(route_set_admin_password(core.clone()))
|
route_get_users(core.clone()).or(route_set_admin_password(core.clone()))
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
pub fn route_check_password(
|
pub fn route_check_password(
|
||||||
core: Core,
|
core: Core,
|
||||||
) -> impl Filter<Extract = (impl Reply,), Error = warp::Rejection> + Clone {
|
) -> impl Filter<Extract = (impl Reply,), Error = warp::Rejection> + Clone {
|
||||||
@ -74,7 +71,9 @@ pub fn route_check_password(
|
|||||||
.then({ move |body| handle_check_password(core.clone(), body) })
|
.then({ move |body| handle_check_password(core.clone(), body) })
|
||||||
.with(cors::<HeaderName, Method>(vec![Method::PUT], vec![]))
|
.with(cors::<HeaderName, Method>(vec![Method::PUT], vec![]))
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use std::{collections::HashMap, path::PathBuf};
|
use std::{collections::HashMap, path::PathBuf};
|
||||||
@ -180,3 +179,4 @@ mod test {
|
|||||||
unimplemented!();
|
unimplemented!();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
@ -4,15 +4,47 @@ use futures::{SinkExt, StreamExt};
|
|||||||
use result_extended::{error, ok, return_error, ResultExt};
|
use result_extended::{error, ok, return_error, ResultExt};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use typeshare::typeshare;
|
use typeshare::typeshare;
|
||||||
use warp::{http::Response, http::StatusCode, reply::Reply, ws::Message};
|
use axum::{http::StatusCode, Json};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
asset_db::AssetId,
|
asset_db::AssetId,
|
||||||
core::Core,
|
core::Core,
|
||||||
database::{CharacterId, UserId},
|
database::{CharacterId, UserId, SessionId},
|
||||||
types::{AppError, FatalError},
|
types::{AppError, FatalError},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct HealthCheck {
|
||||||
|
ok: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn healthcheck(core: Core) -> Vec<u8> {
|
||||||
|
match core.status().await {
|
||||||
|
ResultExt::Ok(s) => serde_json::to_vec(&HealthCheck {
|
||||||
|
ok: s.admin_enabled,
|
||||||
|
})
|
||||||
|
.unwrap(),
|
||||||
|
ResultExt::Err(_) => serde_json::to_vec(&HealthCheck { ok: false }).unwrap(),
|
||||||
|
ResultExt::Fatal(err) => panic!("{}", err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
|
#[typeshare]
|
||||||
|
pub struct AuthRequest {
|
||||||
|
username: String,
|
||||||
|
password: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn check_password(core: Core, req: Json<AuthRequest>) -> (StatusCode, Json<Option<SessionId>>) {
|
||||||
|
let Json(AuthRequest { username, password }) = req;
|
||||||
|
match core.auth(&username, &password).await {
|
||||||
|
ResultExt::Ok(session_id) => (StatusCode::OK, Json(Some(session_id))),
|
||||||
|
ResultExt::Err(err) => (StatusCode::UNAUTHORIZED, Json(None)),
|
||||||
|
ResultExt::Fatal(err) => panic!("Fatal: {}", err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
pub async fn handle_auth(
|
pub async fn handle_auth(
|
||||||
auth_ctx: &AuthDB,
|
auth_ctx: &AuthDB,
|
||||||
@ -37,6 +69,7 @@ pub async fn handle_auth(
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
pub async fn handler<F>(f: F) -> impl Reply
|
pub async fn handler<F>(f: F) -> impl Reply
|
||||||
where
|
where
|
||||||
F: Future<Output = ResultExt<Response<Vec<u8>>, AppError, FatalError>>,
|
F: Future<Output = ResultExt<Response<Vec<u8>>, AppError, FatalError>>,
|
||||||
@ -285,3 +318,4 @@ pub async fn handle_check_password(core: Core, auth_request: AuthRequest) -> imp
|
|||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
*/
|
@ -1,90 +1,47 @@
|
|||||||
use std::{
|
use core::Core;
|
||||||
convert::Infallible,
|
use std::path::PathBuf;
|
||||||
net::{IpAddr, Ipv4Addr, SocketAddr},
|
|
||||||
path::PathBuf,
|
|
||||||
};
|
|
||||||
|
|
||||||
use asset_db::FsAssets;
|
use asset_db::FsAssets;
|
||||||
use authdb::AuthError;
|
use axum::{routing::{get, post}, Router, Json};
|
||||||
use database::DbConn;
|
use database::DbConn;
|
||||||
use filters::{route_authenticate, route_healthcheck, route_image};
|
|
||||||
use warp::{http::StatusCode, reply::Reply, Filter};
|
|
||||||
|
|
||||||
mod asset_db;
|
mod asset_db;
|
||||||
mod core;
|
mod core;
|
||||||
mod database;
|
mod database;
|
||||||
mod filters;
|
mod filters;
|
||||||
mod handlers;
|
mod handlers;
|
||||||
|
use handlers::{ healthcheck, check_password, AuthRequest};
|
||||||
mod types;
|
mod types;
|
||||||
|
|
||||||
/*
|
|
||||||
fn with_session(
|
|
||||||
auth_ctx: Arc<AuthDB>,
|
|
||||||
) -> impl Filter<Extract = (Username,), Error = warp::Rejection> + Clone {
|
|
||||||
header("authentication").and_then({
|
|
||||||
move |value: String| {
|
|
||||||
let auth_ctx = auth_ctx.clone();
|
|
||||||
async move {
|
|
||||||
match auth_ctx.validate_session(SessionToken::from(value)).await {
|
|
||||||
Ok(Some(username)) => Ok(username),
|
|
||||||
Ok(None) => Err(warp::reject::custom(Unauthorized)),
|
|
||||||
Err(err) => Err(warp::reject::custom(AuthDBError(err))),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn route_echo_unauthenticated() -> impl Filter<Extract = (Json,), Error = warp::Rejection> + Clone {
|
|
||||||
warp::path!("api" / "v1" / "echo" / String).map(|param: String| {
|
|
||||||
println!("param: {}", param);
|
|
||||||
warp::reply::json(&vec!["unauthenticated", param.as_str()])
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn route_authenticate(
|
|
||||||
auth_ctx: Arc<AuthDB>,
|
|
||||||
) -> impl Filter<Extract = (Json,), Error = warp::Rejection> + Clone {
|
|
||||||
let auth_ctx = auth_ctx.clone();
|
|
||||||
warp::path!("api" / "v1" / "auth")
|
|
||||||
.and(warp::post())
|
|
||||||
.and(warp::body::json())
|
|
||||||
.map(move |param: AuthToken| {
|
|
||||||
let res = handle_auth(&auth_ctx, param.clone());
|
|
||||||
warp::reply::json(¶m)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn route_echo_authenticated(
|
|
||||||
auth_ctx: Arc<AuthDB>,
|
|
||||||
) -> impl Filter<Extract = (Json,), Error = warp::Rejection> + Clone {
|
|
||||||
warp::path!("api" / "v1" / "echo" / String)
|
|
||||||
.and(with_session(auth_ctx.clone()))
|
|
||||||
.map(move |param: String, username: Username| {
|
|
||||||
println!("param: {:?}", username);
|
|
||||||
println!("param: {}", param);
|
|
||||||
warp::reply::json(&vec!["authenticated", username.as_str(), param.as_str()])
|
|
||||||
})
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
pub async fn main() {
|
pub async fn main() {
|
||||||
|
/*
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
|
|
||||||
let conn = DbConn::new(Some("/home/savanni/game.db"));
|
|
||||||
|
|
||||||
let core = core::Core::new(FsAssets::new(PathBuf::from("/home/savanni/Pictures")), conn);
|
|
||||||
|
|
||||||
let unauthenticated_endpoints = route_healthcheck().or(route_authenticate(core.clone()));
|
let unauthenticated_endpoints = route_healthcheck().or(route_authenticate(core.clone()));
|
||||||
let authenticated_endpoints = route_image(core.clone());
|
let authenticated_endpoints = route_image(core.clone());
|
||||||
|
*/
|
||||||
|
|
||||||
let server = warp::serve(
|
let conn = DbConn::new(Some("/home/savanni/game.db"));
|
||||||
unauthenticated_endpoints
|
let core = Core::new(FsAssets::new(PathBuf::from("/home/savanni/Pictures")), conn);
|
||||||
.or(authenticated_endpoints)
|
|
||||||
.with(warp::log("visions"))
|
let app = Router::new().route(
|
||||||
|
"/api/v1/health",
|
||||||
|
get({
|
||||||
|
let core = core.clone();
|
||||||
|
move || healthcheck(core)
|
||||||
|
}),
|
||||||
|
).route(
|
||||||
|
"/api/v1/auth",
|
||||||
|
post({
|
||||||
|
let core = core.clone();
|
||||||
|
move |req: Json<AuthRequest>| handlers::check_password(core, req)
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
server
|
|
||||||
.run(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8001))
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:8001")
|
||||||
.await;
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
axum::serve(listener, app).await.unwrap();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user