Added initial authorization layer.

This commit is contained in:
2026-10-09 17:22:46 +02:00
parent 7fb3abc93d
commit 1f6de34e5b
16 changed files with 184 additions and 64 deletions
+21 -1
View File
@@ -5,7 +5,7 @@ use uuid::Uuid;
use super::{
dtos::user_app::{UserAppRequest, UserAppResponse},
extractors::{json::Json, path::Path, query::Query},
extractors::{auth::Auth, json::Json, path::Path, query::Query},
};
use crate::{
error::AppError,
@@ -32,10 +32,15 @@ pub struct QueryParams {
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn list_user_apps(
Auth(auth): Auth,
State(pool): State<PgPool>,
Path(user_id): Path<Uuid>,
Query(query): Query<QueryParams>,
) -> Result<Json<Vec<UserAppResponse>>, AppError> {
if auth.user_id() != user_id {
return Err(AppError::NotFound);
}
let query = repository::user_apps::Query {
user_id,
limit: Some(query.limit.unwrap_or(20)),
@@ -54,9 +59,14 @@ async fn list_user_apps(
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn get_user_app(
Auth(auth): Auth,
State(pool): State<PgPool>,
Path((user_id, app_id)): Path<(Uuid, Uuid)>,
) -> Result<Json<UserAppResponse>, AppError> {
if auth.user_id() != user_id {
return Err(AppError::NotFound);
}
let app = repository::user_apps::get_by_id(&pool, app_id)
.await?
.filter(|app| app.user_id == user_id)
@@ -68,10 +78,15 @@ async fn get_user_app(
#[axum::debug_handler]
#[tracing::instrument(skip(pool, request))]
async fn upsert_user_app(
Auth(auth): Auth,
State(pool): State<PgPool>,
Path((user_id, app_id)): Path<(Uuid, Uuid)>,
Json(request): Json<UserAppRequest>,
) -> Result<(StatusCode, Json<UserAppResponse>), AppError> {
if auth.user_id() != user_id {
return Err(AppError::NotFound);
}
let app = request.into_domain(user_id, app_id);
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
@@ -97,9 +112,14 @@ async fn upsert_user_app(
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn delete_user_app(
Auth(auth): Auth,
State(pool): State<PgPool>,
Path((user_id, app_id)): Path<(Uuid, Uuid)>,
) -> Result<StatusCode, AppError> {
if auth.user_id() != user_id {
return Err(AppError::NotFound);
}
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
if repository::user_apps::delete_by_id(&mut *tx, app_id)
.await?