mirror of
https://github.com/amtoaer/bili-sync.git
synced 2026-06-08 17:20:19 +08:00
feat: 添加部分简单 API,相应修改程序入口的初始化流程 (#251)
This commit is contained in:
21
crates/bili_sync/src/api/auth.rs
Normal file
21
crates/bili_sync/src/api/auth.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use axum::extract::Request;
|
||||
use axum::http::HeaderMap;
|
||||
use axum::middleware::Next;
|
||||
use axum::response::Response;
|
||||
use reqwest::StatusCode;
|
||||
|
||||
use crate::config::CONFIG;
|
||||
|
||||
pub async fn auth(headers: HeaderMap, request: Request, next: Next) -> Result<Response, StatusCode> {
|
||||
if request.uri().path().starts_with("/api") && get_token(&headers) != CONFIG.auth_token {
|
||||
return Err(StatusCode::UNAUTHORIZED);
|
||||
}
|
||||
Ok(next.run(request).await)
|
||||
}
|
||||
|
||||
fn get_token(headers: &HeaderMap) -> Option<String> {
|
||||
headers
|
||||
.get("Authorization")
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.map(Into::into)
|
||||
}
|
||||
Reference in New Issue
Block a user