Skip to content

Commit 6dd0523

Browse files
committed
📝 除了 oauth 入口路由以外,其余的所有接口路由将强制验证身份。
1 parent 29170e3 commit 6dd0523

File tree

4 files changed

+36
-24
lines changed

4 files changed

+36
-24
lines changed

packages/router/src/middlewares/auth_extrator.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::Result;
1+
use anyhow::{anyhow, Result};
22

33
use axum::{
44
extract::FromRequestParts,
@@ -15,7 +15,7 @@ use _utils::{
1515
models::CommonResponse,
1616
};
1717

18-
pub struct ExtractAuthInfo(pub Option<AuthInfo>);
18+
pub struct ExtractAuthInfo(pub AuthInfo);
1919

2020
impl<S> FromRequestParts<S> for ExtractAuthInfo
2121
where
@@ -37,14 +37,22 @@ where
3737
.into_response()
3838
})?;
3939

40-
return Ok(Self(Some(AuthInfo {
40+
return Ok(Self(AuthInfo {
4141
token,
4242
user_id: claims.sub,
4343
created_at: claims.iat,
4444
expires_at: claims.exp,
45-
})));
45+
}));
4646
}
4747

48-
Ok(Self(None))
48+
let ret = (
49+
StatusCode::UNAUTHORIZED,
50+
serde_json::to_string(
51+
&CommonResponse::<()>::new(Err(anyhow!("No Authorization header found")))
52+
.with_status(StatusCode::UNAUTHORIZED.as_u16()),
53+
)
54+
.expect("Failed to serialize error response"),
55+
);
56+
Err(ret.into_response())
4957
}
5058
}

packages/router/src/routes/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ use anyhow::Result;
55

66
use axum::{
77
extract::DefaultBodyLimit, http::StatusCode, middleware::from_extractor,
8-
response::IntoResponse, Router,
8+
response::IntoResponse, routing::post, Router,
99
};
1010

1111
pub async fn router() -> Result<Router> {
1212
let ret = Router::new()
13+
.route("/oauth/token", post(system::oauth::oauth))
1314
.merge(system::router().await?)
1415
.merge(api::router().await?)
1516
.fallback(|| async { (StatusCode::NOT_IMPLEMENTED, "Not Implemented").into_response() })

packages/router/src/routes/system/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod action_log;
22
mod archive;
33
mod device;
44
mod invitation;
5-
mod oauth;
5+
pub mod oauth;
66
mod role;
77
mod user;
88

@@ -11,7 +11,7 @@ use anyhow::Result;
1111
use axum::{routing::post, Router};
1212

1313
pub async fn router() -> Result<Router> {
14-
let ret = Router::new().route("/oauth/token", post(oauth::oauth));
14+
let ret = Router::new();
1515

1616
Ok(ret)
1717
}

packages/utils/src/models/wrapper.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,24 @@ impl<T> CommonResponse<T> {
3232
}
3333
}
3434

35-
pub fn new_with_users(result: Result<T>, users: Vec<SysUserVO>) -> Self {
36-
match result {
37-
Ok(data) => Self {
38-
error: false,
39-
data: Some(data),
40-
users,
41-
..Default::default()
42-
},
43-
Err(err) => Self {
44-
error: true,
45-
message: err.to_string(),
46-
users,
47-
..Default::default()
48-
},
49-
}
35+
pub fn with_users(mut self, users: Vec<SysUserVO>) -> Self {
36+
self.users = users;
37+
self
38+
}
39+
40+
pub fn with_status(mut self, status: u16) -> Self {
41+
self.error_status = status;
42+
self
43+
}
44+
45+
pub fn with_error_data(mut self, error_data: serde_json::Value) -> Self {
46+
self.error_data = Some(error_data);
47+
self
48+
}
49+
50+
pub fn with_message(mut self, message: String) -> Self {
51+
self.message = message;
52+
self
5053
}
5154
}
5255

@@ -66,7 +69,7 @@ impl<T> Default for CommonResponse<T> {
6669

6770
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
6871
#[serde(rename_all = "camelCase")]
69-
pub struct Page {
72+
pub struct Pagination {
7073
pub current: u32,
7174
pub size: u32,
7275
}

0 commit comments

Comments
 (0)