Fixed middleware signature and added /me endpoint
This commit is contained in:
@@ -63,16 +63,10 @@ impl AppCfg {
|
|||||||
|
|
||||||
pub async fn route_allows(
|
pub async fn route_allows(
|
||||||
&self,
|
&self,
|
||||||
req: &Request,
|
method: &Method,
|
||||||
|
path: &str,
|
||||||
user_perms: UserPermissions,
|
user_perms: UserPermissions,
|
||||||
) -> Result<bool, StatusCode> {
|
) -> Result<bool, StatusCode> {
|
||||||
let method = req.method();
|
|
||||||
|
|
||||||
let path = req
|
|
||||||
.extensions()
|
|
||||||
.get::<MatchedPath>()
|
|
||||||
.map(|p| p.as_str())
|
|
||||||
.ok_or(StatusCode::INTERNAL_SERVER_ERROR)?;
|
|
||||||
let req_perms = match self.get_route_perms(method, path) {
|
let req_perms = match self.get_route_perms(method, path) {
|
||||||
Some(val) => val,
|
Some(val) => val,
|
||||||
None => return Ok(false),
|
None => return Ok(false),
|
||||||
|
|||||||
@@ -63,12 +63,8 @@ async fn main() -> Result<()> {
|
|||||||
route_perms: HashMap::new(),
|
route_perms: HashMap::new(),
|
||||||
};
|
};
|
||||||
|
|
||||||
config.insert_route_perms(
|
config.insert_route_perms(Method::GET, "/api/users", false, vec![]);
|
||||||
Method::GET,
|
config.insert_route_perms(Method::GET, "/api/users/{uuid}", false, vec![]);
|
||||||
"/api/users",
|
|
||||||
false,
|
|
||||||
vec![UserActions::ManageUsers],
|
|
||||||
);
|
|
||||||
config.insert_route_perms(
|
config.insert_route_perms(
|
||||||
Method::POST,
|
Method::POST,
|
||||||
"/api/users",
|
"/api/users",
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
Extension,
|
Extension,
|
||||||
extract::{Request, State},
|
extract::{MatchedPath, Request, State},
|
||||||
http::{self, Method, StatusCode, header::AUTHORIZATION},
|
http::{self, Method, StatusCode, header::AUTHORIZATION},
|
||||||
middleware::Next,
|
middleware::Next,
|
||||||
response::Response,
|
response::Response,
|
||||||
@@ -109,7 +109,7 @@ pub fn cors() -> CorsLayer {
|
|||||||
pub async fn permissions(
|
pub async fn permissions(
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Extension(user): Extension<InternalUser>,
|
Extension(user): Extension<InternalUser>,
|
||||||
mut req: Request,
|
req: Request,
|
||||||
next: Next,
|
next: Next,
|
||||||
) -> Result<Response, StatusCode> {
|
) -> Result<Response, StatusCode> {
|
||||||
let request_method = req.method().clone();
|
let request_method = req.method().clone();
|
||||||
@@ -117,13 +117,22 @@ pub async fn permissions(
|
|||||||
|
|
||||||
debug!(method = ?request_method, path = request_path, "permissions request started");
|
debug!(method = ?request_method, path = request_path, "permissions request started");
|
||||||
debug!("Calling user {}", user.username.clone());
|
debug!("Calling user {}", user.username.clone());
|
||||||
|
|
||||||
if user.permissions.root {
|
if user.permissions.root {
|
||||||
return Ok(next.run(req).await);
|
return Ok(next.run(req).await);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let method = req.method();
|
||||||
|
|
||||||
|
let path = req
|
||||||
|
.extensions()
|
||||||
|
.get::<MatchedPath>()
|
||||||
|
.map(|p| p.as_str())
|
||||||
|
.ok_or(StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||||
|
|
||||||
match state
|
match state
|
||||||
.config
|
.config
|
||||||
.route_allows(&req, user.permissions.clone())
|
.route_allows(method, path, user.permissions.clone())
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(true) => Ok(next.run(req).await),
|
Ok(true) => Ok(next.run(req).await),
|
||||||
|
|||||||
@@ -25,11 +25,8 @@ macro_rules! middleware {
|
|||||||
(cors_auth_perms, $state:expr) => {
|
(cors_auth_perms, $state:expr) => {
|
||||||
(
|
(
|
||||||
crate::router::middleware::cors(),
|
crate::router::middleware::cors(),
|
||||||
axum::middleware::from_fn_with_state($state.clone(), crate::router::middleware::auth),
|
axum::middleware::from_fn_with_state($state, crate::router::middleware::auth),
|
||||||
axum::middleware::from_fn_with_state(
|
axum::middleware::from_fn_with_state($state, crate::router::middleware::permissions),
|
||||||
$state.clone(),
|
|
||||||
crate::router::middleware::permissions,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -65,6 +62,12 @@ pub async fn init_router(app_state: Arc<AppState>) -> Router {
|
|||||||
post(user_routes::logout)
|
post(user_routes::logout)
|
||||||
.layer(middleware!(cors_auth, app_state.clone()))
|
.layer(middleware!(cors_auth, app_state.clone()))
|
||||||
.with_state(app_state.clone()),
|
.with_state(app_state.clone()),
|
||||||
|
)
|
||||||
|
.route(
|
||||||
|
"/api/me",
|
||||||
|
get(user_routes::me)
|
||||||
|
.layer(middleware!(cors_auth, app_state.clone()))
|
||||||
|
.with_state(app_state.clone()),
|
||||||
);
|
);
|
||||||
|
|
||||||
info!("router initialization completed");
|
info!("router initialization completed");
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
use crate::{domain::api::LoginData, prelude::*};
|
use crate::{
|
||||||
|
domain::{api::LoginData, user::InternalUser},
|
||||||
|
prelude::*,
|
||||||
|
};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@@ -8,7 +11,7 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use axum::{
|
use axum::{
|
||||||
Json,
|
Extension, Json,
|
||||||
extract::{Path, State},
|
extract::{Path, State},
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
};
|
};
|
||||||
@@ -73,3 +76,9 @@ pub async fn logout(jar: CookieJar) -> Result<CookieJar, StatusCode> {
|
|||||||
|
|
||||||
Ok(jar)
|
Ok(jar)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn me(Extension(user): Extension<InternalUser>) -> Result<Json<User>, StatusCode> {
|
||||||
|
let clean = User::from(user);
|
||||||
|
|
||||||
|
Ok(Json(clean))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user