Add /auth/status

This commit is contained in:
2022-09-18 15:04:33 +00:00
committed by GitHub
parent 35935e80a4
commit b00d326044
11 changed files with 88 additions and 19 deletions

3
src/server/auth/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
pub mod status;
pub use status::{AuthStatusContext, AuthStatusRoute};

54
src/server/auth/status.rs Normal file
View File

@@ -0,0 +1,54 @@
use super::super::preclude::*;
pub struct AuthStatusContext {
ctx: Arc<ServerContext>,
}
impl AuthStatusContext {
pub fn new(ctx: Arc<ServerContext>) -> Self {
Self { ctx }
}
}
#[async_trait]
impl ResponseJsonFor<Body> for AuthStatusContext {
async fn response_json(
&self,
req: Request<Body>,
) -> Result<Response<JsonValue>, PixivDownloaderError> {
filter_http_methods!(
req,
json::object! {},
true,
self.ctx,
allow_headers = [CONTENT_TYPE],
GET,
OPTIONS,
POST,
);
let has_root_user = self.ctx.db.get_user(0).await?.is_some();
Ok(builder.body(json::object! {"has_root_user": has_root_user})?)
}
}
pub struct AuthStatusRoute {
regex: Regex,
}
impl AuthStatusRoute {
pub fn new() -> Self {
Self {
regex: Regex::new(r"^(/+api)?/+auth(/+status(/.*)?)?$").unwrap(),
}
}
}
impl MatchRoute<Body, Body> for AuthStatusRoute {
fn get_route(&self, ctx: Arc<ServerContext>) -> Box<ResponseForType> {
Box::new(AuthStatusContext::new(ctx))
}
fn match_route(&self, req: &http::Request<Body>) -> bool {
self.regex.is_match(req.uri().path())
}
}

View File

@@ -1,6 +1,10 @@
/// Routes about authentication
pub mod auth;
pub mod context;
/// CORS Handle
pub mod cors;
/// Predefined includes
pub mod preclude;
/// Routes
pub mod route;
/// Services

11
src/server/preclude.rs Normal file
View File

@@ -0,0 +1,11 @@
pub use super::context::ServerContext;
pub use super::route::ResponseForType;
pub use super::traits::{MatchRoute, ResponseFor, ResponseJsonFor};
pub use crate::error::PixivDownloaderError;
pub use hyper::Body;
pub use hyper::Request;
pub use hyper::Response;
pub use json::JsonValue;
pub use proc_macros::filter_http_methods;
pub use regex::Regex;
pub use std::sync::Arc;

View File

@@ -1,3 +1,4 @@
use super::auth::*;
use super::context::ServerContext;
use super::traits::MatchRoute;
use super::traits::ResponseFor;
@@ -17,6 +18,7 @@ impl ServerRoutes {
pub fn new() -> Self {
let mut routes: Vec<Box<RouteType>> = Vec::new();
routes.push(Box::new(VersionRoute::new()));
routes.push(Box::new(AuthStatusRoute::new()));
Self { routes }
}

View File

@@ -1,15 +1,4 @@
use super::context::ServerContext;
use super::route::ResponseForType;
use super::traits::MatchRoute;
use super::traits::ResponseJsonFor;
use crate::error::PixivDownloaderError;
use hyper::Body;
use hyper::Request;
use hyper::Response;
use json::JsonValue;
use proc_macros::filter_http_methods;
use regex::Regex;
use std::sync::Arc;
use super::preclude::*;
pub struct VersionContext {
ctx: Arc<ServerContext>,
@@ -32,7 +21,7 @@ impl ResponseJsonFor<Body> for VersionContext {
json::object! {},
true,
self.ctx,
allow_headers = [CONTENT_TYPE, X_TOKEN],
allow_headers = [CONTENT_TYPE],
GET,
OPTIONS,
POST,