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
@@ -7,7 +7,7 @@ use super::{
dtos::user_app_secret::{
CreatedUserAppSecretResponse, UserAppSecretRequest, UserAppSecretResponse,
},
extractors::{json::Json, path::Path, query::Query},
extractors::{auth::Auth, json::Json, path::Path, query::Query},
};
use crate::{
crypto::{generate_secret, hash_secret},
@@ -36,10 +36,15 @@ pub struct QueryParams {
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn list_user_app_secrets(
Auth(auth): Auth,
State(pool): State<PgPool>,
Path((user_id, app_id)): Path<(Uuid, Uuid)>,
Query(query): Query<QueryParams>,
) -> Result<Json<Vec<UserAppSecretResponse>>, AppError> {
if auth.user_id() != user_id {
return Err(AppError::NotFound);
}
let query = repository::user_app_secrets::Query {
app_id,
limit: Some(query.limit.unwrap_or(20)),
@@ -61,9 +66,14 @@ async fn list_user_app_secrets(
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn get_user_app_secret(
Auth(auth): Auth,
State(pool): State<PgPool>,
Path((user_id, app_id, secret_id)): Path<(Uuid, Uuid, Uuid)>,
) -> Result<Json<UserAppSecretResponse>, AppError> {
if auth.user_id() != user_id {
return Err(AppError::NotFound);
}
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
ensure_app_ownership(&mut tx, user_id, app_id).await?;
let secret = repository::user_app_secrets::get_by_id(&mut *tx, app_id, secret_id)
@@ -77,10 +87,15 @@ async fn get_user_app_secret(
#[axum::debug_handler]
#[tracing::instrument(skip(pool, request))]
async fn upsert_user_app_secret(
Auth(auth): Auth,
State(pool): State<PgPool>,
Path((user_id, app_id, secret_id)): Path<(Uuid, Uuid, Uuid)>,
Json(request): Json<UserAppSecretRequest>,
) -> Result<(StatusCode, Json<CreatedUserAppSecretResponse>), AppError> {
if auth.user_id() != user_id {
return Err(AppError::NotFound);
}
let now = chrono::Utc::now();
if request.expires_at <= now {
return Err(AppError::BadRequest(
@@ -121,9 +136,14 @@ async fn upsert_user_app_secret(
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn delete_user_app_secret(
Auth(auth): Auth,
State(pool): State<PgPool>,
Path((user_id, app_id, secret_id)): Path<(Uuid, 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)?;
ensure_app_ownership(&mut tx, user_id, app_id).await?;
let deleted = repository::user_app_secrets::delete_by_id(&mut *tx, app_id, secret_id).await?;