Added initial authorization layer.
This commit is contained in:
+32
-8
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n SELECT *\n FROM tokens\n WHERE access_token_hash = $1\n AND access_expires_at > now()\n AND revoked_at IS NULL\n ",
|
||||
"query": "\n SELECT\n id,\n user_id,\n user_app_id,\n access_token_hash,\n refresh_token_hash,\n access_expires_at,\n refresh_expires_at,\n refreshed_at,\n revoked_at,\n created_at\n FROM tokens\n WHERE access_token_hash = $1\n AND access_expires_at > now()\n AND revoked_at IS NULL\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
@@ -16,6 +16,28 @@
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "user_id",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "tokens",
|
||||
"name": "user_id"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 2,
|
||||
"name": "user_app_id",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "tokens",
|
||||
"name": "user_app_id"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 3,
|
||||
"name": "access_token_hash",
|
||||
"type_info": "Bytea",
|
||||
"origin": {
|
||||
@@ -26,7 +48,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 2,
|
||||
"ordinal": 4,
|
||||
"name": "refresh_token_hash",
|
||||
"type_info": "Bytea",
|
||||
"origin": {
|
||||
@@ -37,7 +59,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 3,
|
||||
"ordinal": 5,
|
||||
"name": "access_expires_at",
|
||||
"type_info": "Timestamptz",
|
||||
"origin": {
|
||||
@@ -48,7 +70,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 4,
|
||||
"ordinal": 6,
|
||||
"name": "refresh_expires_at",
|
||||
"type_info": "Timestamptz",
|
||||
"origin": {
|
||||
@@ -59,7 +81,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 5,
|
||||
"ordinal": 7,
|
||||
"name": "refreshed_at",
|
||||
"type_info": "Timestamptz",
|
||||
"origin": {
|
||||
@@ -70,7 +92,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 6,
|
||||
"ordinal": 8,
|
||||
"name": "revoked_at",
|
||||
"type_info": "Timestamptz",
|
||||
"origin": {
|
||||
@@ -81,7 +103,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 7,
|
||||
"ordinal": 9,
|
||||
"name": "created_at",
|
||||
"type_info": "Timestamptz",
|
||||
"origin": {
|
||||
@@ -100,6 +122,8 @@
|
||||
"nullable": [
|
||||
false,
|
||||
false,
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
@@ -108,5 +132,5 @@
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "458c87cbcf275d5b6f05abb9e5bec0633eff66b73beed646069ad4db7450fd33"
|
||||
"hash": "755b789df9d3d7f92243481e8fc8b724cae757c55579da8f86adb778eefdbc48"
|
||||
}
|
||||
-21
@@ -1,21 +0,0 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n INSERT INTO tokens (\n id, access_token_hash, refresh_token_hash,\n access_expires_at, refresh_expires_at, refreshed_at, revoked_at, created_at\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8)\n ",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Uuid",
|
||||
"Bytea",
|
||||
"Bytea",
|
||||
"Timestamptz",
|
||||
"Timestamptz",
|
||||
"Timestamptz",
|
||||
"Timestamptz",
|
||||
"Timestamptz"
|
||||
]
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "8f11aa4a2d8b3208181f12fc5b6f87f80fbc7eade88f4ceaf17f9716622ad12a"
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n INSERT INTO tokens (\n id, user_id, user_app_id, access_token_hash, refresh_token_hash,\n access_expires_at, refresh_expires_at, refreshed_at, revoked_at, created_at\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)\n ",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Uuid",
|
||||
"Uuid",
|
||||
"Uuid",
|
||||
"Bytea",
|
||||
"Bytea",
|
||||
"Timestamptz",
|
||||
"Timestamptz",
|
||||
"Timestamptz",
|
||||
"Timestamptz",
|
||||
"Timestamptz"
|
||||
]
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "f448274b39bb13ec500a7634fab177916a194192f3ea14f8439bd42c467844a0"
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
CREATE TABLE tokens (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
|
||||
user_id UUID NOT NULL CONSTRAINT fk_tokens_user REFERENCES users (id) ON DELETE CASCADE,
|
||||
user_app_id UUID CONSTRAINT fk_tokens_app REFERENCES user_apps (id) ON DELETE CASCADE,
|
||||
access_token_hash BYTEA NOT NULL UNIQUE CHECK (
|
||||
octet_length(access_token_hash) = 32
|
||||
),
|
||||
|
||||
@@ -3,6 +3,8 @@ use uuid::Uuid;
|
||||
|
||||
pub struct TokenRow {
|
||||
pub id: Uuid,
|
||||
pub user_id: Uuid,
|
||||
pub user_app_id: Option<Uuid>,
|
||||
pub access_token_hash: Vec<u8>,
|
||||
pub refresh_token_hash: Vec<u8>,
|
||||
pub access_expires_at: DateTime<Utc>,
|
||||
|
||||
@@ -12,7 +12,17 @@ where
|
||||
let row = sqlx::query_as!(
|
||||
TokenRow,
|
||||
r#"
|
||||
SELECT *
|
||||
SELECT
|
||||
id,
|
||||
user_id,
|
||||
user_app_id,
|
||||
access_token_hash,
|
||||
refresh_token_hash,
|
||||
access_expires_at,
|
||||
refresh_expires_at,
|
||||
refreshed_at,
|
||||
revoked_at,
|
||||
created_at
|
||||
FROM tokens
|
||||
WHERE access_token_hash = $1
|
||||
AND access_expires_at > now()
|
||||
@@ -34,12 +44,14 @@ where
|
||||
sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO tokens (
|
||||
id, access_token_hash, refresh_token_hash,
|
||||
id, user_id, user_app_id, access_token_hash, refresh_token_hash,
|
||||
access_expires_at, refresh_expires_at, refreshed_at, revoked_at, created_at
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
"#,
|
||||
token.id,
|
||||
token.user_id,
|
||||
token.user_app_id,
|
||||
token.access_token_hash,
|
||||
token.refresh_token_hash,
|
||||
token.access_expires_at,
|
||||
|
||||
@@ -4,6 +4,8 @@ use uuid::Uuid;
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct Token {
|
||||
pub id: Uuid,
|
||||
pub user_id: Uuid,
|
||||
pub user_app_id: Option<Uuid>,
|
||||
pub access_token_hash: Vec<u8>,
|
||||
pub refresh_token_hash: Vec<u8>,
|
||||
pub access_expires_at: DateTime<Utc>,
|
||||
|
||||
@@ -60,6 +60,8 @@ where
|
||||
fn decode(token: TokenRow) -> Result<Token, RepositoryError> {
|
||||
Ok(Token {
|
||||
id: token.id,
|
||||
user_id: token.user_id,
|
||||
user_app_id: token.user_app_id,
|
||||
access_token_hash: token.access_token_hash,
|
||||
refresh_token_hash: token.refresh_token_hash,
|
||||
access_expires_at: token.access_expires_at,
|
||||
@@ -73,6 +75,8 @@ fn decode(token: TokenRow) -> Result<Token, RepositoryError> {
|
||||
fn encode(token: Token) -> TokenRow {
|
||||
TokenRow {
|
||||
id: token.id,
|
||||
user_id: token.user_id,
|
||||
user_app_id: token.user_app_id,
|
||||
access_token_hash: token.access_token_hash,
|
||||
refresh_token_hash: token.refresh_token_hash,
|
||||
access_expires_at: token.access_expires_at,
|
||||
|
||||
@@ -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> {
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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> {
|
||||
|
||||
@@ -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> {
|
||||
|
||||
@@ -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?;
|
||||
|
||||
@@ -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?
|
||||
|
||||
@@ -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> {
|
||||
|
||||
Reference in New Issue
Block a user