1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
macro_rules! api_get {
(($name:ident, $($datatype:ty)?, $($path:expr)?) [$auth:ident, $account:ident, $query:ident] $($do:tt)*) => {
fn $name(
auth_manager: std::sync::Arc<tokio::sync::Mutex<crate::auth::Auth>>
) -> warp::filters::BoxedFilter<(impl warp::Reply,)> {
use reqwest::StatusCode;
use warp::Filter;
use warp::Reply;
#[derive(Deserialize)]
struct UserToken {
user: String,
token: String,
}
warp::get()
$(.and($path))?
.and(warp::query::<UserToken>())
$(.and(warp::body::json::<$datatype>()))?
.and_then(move |auth_query: UserToken$(, $query: $datatype)?| {
let $auth = auth_manager.clone();
async move {
Ok::<warp::http::Response<warp::hyper::Body>, warp::Rejection>(
if crate::auth::Auth::is_authenticated(
$auth.clone(),
&auth_query.user,
auth_query.token,
)
.await
{
if let Ok($account) = crate::user::User::load(&auth_query.user).await {
$($do)*
} else {
warp::reply::with_status(
"Server failed to load the user.",
StatusCode::INTERNAL_SERVER_ERROR,
)
.into_response()
}
} else {
warp::reply::with_status(
"Invalid authentication details.",
StatusCode::FORBIDDEN,
)
.into_response()
},
)
}
})
.boxed()
}
};
}