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
+14 -5
View File
@@ -7,10 +7,7 @@ use super::{
dtos::account::{AccountRequest, AccountResponse},
extractors::{auth::Auth, json::Json, path::Path, query::Query},
};
use crate::{
error::AppError,
repository::{self},
};
use crate::{error::AppError, repository};
pub fn create_router() -> axum::Router<PgPool> {
axum::Router::new()
@@ -37,24 +34,34 @@ async fn list_accounts(
State(pool): State<PgPool>,
Query(query): Query<QueryParams>,
) -> Result<Json<Vec<AccountResponse>>, AppError> {
let mut tx = pool
.begin()
.await
.map_err(repository::RepositoryError::from)?;
let query = repository::accounts::Query {
name: query.name,
limit: Some(query.limit.unwrap_or(20)),
offset: query.offset,
};
let accounts = repository::accounts::list(&pool, query)
let accounts = repository::accounts::list(&mut *tx, query)
.await?
.into_iter()
.map(AccountResponse::from)
.collect();
tx.commit()
.await
.map_err(repository::RepositoryError::from)?;
Ok(Json(accounts))
}
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn get_account(
Auth(auth): Auth,
State(pool): State<PgPool>,
Path(id): Path<Uuid>,
) -> Result<Json<AccountResponse>, AppError> {
@@ -68,6 +75,7 @@ async fn get_account(
#[axum::debug_handler]
#[tracing::instrument(skip(pool, account))]
async fn upsert_account(
Auth(auth): Auth,
State(pool): State<PgPool>,
Path(id): Path<Uuid>,
Json(account): Json<AccountRequest>,
@@ -87,6 +95,7 @@ async fn upsert_account(
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn delete_account(
Auth(auth): Auth,
Path(id): Path<Uuid>,
State(pool): State<PgPool>,
) -> Result<StatusCode, AppError> {
+31 -11
View File
@@ -35,12 +35,30 @@ async fn issue_token(
client_secret,
} => {
let secret_hash = hash_secret(&client_secret);
if !repository::user_app_secrets::verify(&pool, client_id, &secret_hash).await? {
let generated = generate_token_pair()?;
let mut tx = pool
.begin()
.await
.map_err(repository::RepositoryError::from)?;
if !repository::user_app_secrets::verify(&mut *tx, client_id, &secret_hash).await? {
return Err(AppError::Unauthorized);
}
let generated = generate_token_pair()?;
repository::tokens::issue(&pool, token(&generated)).await?;
let user_app = repository::user_apps::get_by_id(&mut *tx, client_id)
.await?
.ok_or(AppError::Unauthorized)?;
repository::tokens::issue(
&mut *tx,
token(user_app.user_id, Some(user_app.id), &generated),
)
.await?;
tx.commit()
.await
.map_err(repository::RepositoryError::from)?;
Ok(Json(token_response(
generated.access_token,
@@ -87,7 +105,7 @@ async fn revoke_token(
Ok(repository::tokens::revoke(&pool, &hash_secret(&request.token)).await?)
}
struct GeneratedTokenPair {
struct TokenPair {
access_token: String,
refresh_token: String,
access_token_hash: Vec<u8>,
@@ -96,11 +114,11 @@ struct GeneratedTokenPair {
refresh_expires_at: chrono::DateTime<Utc>,
}
fn generate_token_pair() -> Result<GeneratedTokenPair, AppError> {
fn generate_token_pair() -> Result<TokenPair, AppError> {
let access_token = generate_secret().map_err(|_| AppError::Internal)?;
let refresh_token = generate_secret().map_err(|_| AppError::Internal)?;
let now = Utc::now();
Ok(GeneratedTokenPair {
Ok(TokenPair {
access_token_hash: hash_secret(&access_token),
refresh_token_hash: hash_secret(&refresh_token),
access_expires_at: now + Duration::seconds(ACCESS_TOKEN_LIFETIME_SECONDS),
@@ -110,13 +128,15 @@ fn generate_token_pair() -> Result<GeneratedTokenPair, AppError> {
})
}
fn token(generated: &GeneratedTokenPair) -> Token {
fn token(user_id: Uuid, app_id: Option<Uuid>, token_pair: &TokenPair) -> Token {
Token {
id: Uuid::new_v4(),
access_token_hash: generated.access_token_hash.clone(),
refresh_token_hash: generated.refresh_token_hash.clone(),
access_expires_at: generated.access_expires_at,
refresh_expires_at: generated.refresh_expires_at,
user_id,
user_app_id: app_id,
access_token_hash: token_pair.access_token_hash.clone(),
refresh_token_hash: token_pair.refresh_token_hash.clone(),
access_expires_at: token_pair.access_expires_at,
refresh_expires_at: token_pair.refresh_expires_at,
refreshed_at: None,
revoked_at: None,
created_at: Utc::now(),
+1 -7
View File
@@ -45,8 +45,7 @@ where
.ok_or(AppError::Unauthorized)?;
Ok(Auth(Authorization {
user_id: token.id,
app_id: None,
user_id: token.user_id,
}))
}
}
@@ -54,15 +53,10 @@ where
#[derive(Debug)]
pub struct Authorization {
user_id: Uuid,
app_id: Option<Uuid>,
}
impl Authorization {
pub fn user_id(&self) -> Uuid {
self.user_id
}
pub fn app_id(&self) -> Option<Uuid> {
self.app_id
}
}
+5 -1
View File
@@ -10,7 +10,7 @@ use super::{
dtos::scheduled_transaction::{
RecurringTransactionResponse, ScheduledTransactionRequest, ScheduledTransactionResponse,
},
extractors::{json::Json, path::Path, query::Query},
extractors::{auth::Auth, json::Json, path::Path, query::Query},
};
use crate::{
domain,
@@ -38,6 +38,7 @@ pub struct QueryParams {
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn list_scheduled_transactions(
Auth(auth): Auth,
State(pool): State<PgPool>,
Query(query): Query<QueryParams>,
) -> Result<Json<Vec<ScheduledTransactionResponse>>, AppError> {
@@ -63,6 +64,7 @@ async fn list_scheduled_transactions(
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn get_scheduled_transaction(
Auth(auth): Auth,
State(pool): State<PgPool>,
Path(id): Path<Uuid>,
) -> Result<Json<ScheduledTransactionResponse>, AppError> {
@@ -80,6 +82,7 @@ async fn get_scheduled_transaction(
#[axum::debug_handler]
#[tracing::instrument(skip(pool, transaction))]
async fn upsert_scheduled_transaction(
Auth(auth): Auth,
State(pool): State<PgPool>,
Path(id): Path<Uuid>,
Json(transaction): Json<ScheduledTransactionRequest>,
@@ -108,6 +111,7 @@ async fn upsert_scheduled_transaction(
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn delete_scheduled_transaction(
Auth(auth): Auth,
State(pool): State<PgPool>,
Path(id): Path<Uuid>,
) -> Result<StatusCode, AppError> {
+5 -1
View File
@@ -7,7 +7,7 @@ use uuid::Uuid;
use super::{
dtos::transaction::{TransactionRequest, TransactionResponse},
extractors::{json::Json, path::Path, query::Query},
extractors::{auth::Auth, json::Json, path::Path, query::Query},
};
use crate::{
domain,
@@ -35,6 +35,7 @@ pub struct QueryParams {
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn list_transactions(
Auth(auth): Auth,
State(pool): State<PgPool>,
Query(query): Query<QueryParams>,
) -> Result<Json<Vec<TransactionResponse>>, AppError> {
@@ -59,6 +60,7 @@ async fn list_transactions(
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn get_transaction(
Auth(auth): Auth,
State(pool): State<PgPool>,
Path(id): Path<Uuid>,
) -> Result<Json<TransactionResponse>, AppError> {
@@ -76,6 +78,7 @@ async fn get_transaction(
#[axum::debug_handler]
#[tracing::instrument(skip(pool, transaction))]
async fn upsert_transaction(
Auth(auth): Auth,
State(pool): State<PgPool>,
Path(id): Path<Uuid>,
Json(transaction): Json<TransactionRequest>,
@@ -100,6 +103,7 @@ async fn upsert_transaction(
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn delete_transaction(
Auth(auth): Auth,
Path(id): Path<Uuid>,
State(pool): State<PgPool>,
) -> Result<StatusCode, AppError> {
+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?;
+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?
+6 -5
View File
@@ -5,12 +5,9 @@ use uuid::Uuid;
use super::{
dtos::user::{UserRequest, UserResponse},
extractors::{json::Json, path::Path, query::Query},
};
use crate::{
error::AppError,
repository::{self},
extractors::{auth::Auth, json::Json, path::Path, query::Query},
};
use crate::{error::AppError, repository};
pub fn create_router() -> axum::Router<PgPool> {
axum::Router::new()
@@ -32,6 +29,7 @@ pub struct QueryParams {
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn list_users(
Auth(auth): Auth,
State(pool): State<PgPool>,
Query(query): Query<QueryParams>,
) -> Result<Json<Vec<UserResponse>>, AppError> {
@@ -52,6 +50,7 @@ async fn list_users(
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn get_user(
Auth(auth): Auth,
State(pool): State<PgPool>,
Path(id): Path<Uuid>,
) -> Result<Json<UserResponse>, AppError> {
@@ -65,6 +64,7 @@ async fn get_user(
#[axum::debug_handler]
#[tracing::instrument(skip(pool, user))]
async fn upsert_user(
Auth(auth): Auth,
State(pool): State<PgPool>,
Path(id): Path<Uuid>,
Json(user): Json<UserRequest>,
@@ -84,6 +84,7 @@ async fn upsert_user(
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn delete_user(
Auth(auth): Auth,
Path(id): Path<Uuid>,
State(pool): State<PgPool>,
) -> Result<StatusCode, AppError> {