155 lines
4.6 KiB
Rust
155 lines
4.6 KiB
Rust
use axum::{extract::State, http::StatusCode};
|
|
use serde::Deserialize;
|
|
use sqlx::PgPool;
|
|
use uuid::Uuid;
|
|
|
|
use super::{
|
|
dtos::user_app_secret::{
|
|
CreatedUserAppSecretResponse, UserAppSecretRequest, UserAppSecretResponse,
|
|
},
|
|
extractors::{Auth, Json, Path, Query},
|
|
};
|
|
use crate::{
|
|
crypto::{generate_secret, hash_secret},
|
|
error::AppError,
|
|
repository::{self, RepositoryError},
|
|
};
|
|
|
|
pub fn create_router() -> axum::Router<PgPool> {
|
|
axum::Router::new()
|
|
.route("/", axum::routing::get(list_user_app_secrets))
|
|
.route(
|
|
"/{secret_id}",
|
|
axum::routing::get(get_user_app_secret)
|
|
.put(upsert_user_app_secret)
|
|
.delete(delete_user_app_secret),
|
|
)
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct QueryParams {
|
|
pub limit: Option<u64>,
|
|
pub offset: Option<u64>,
|
|
}
|
|
|
|
#[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)),
|
|
offset: query.offset,
|
|
};
|
|
|
|
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
|
ensure_app_ownership(&mut tx, user_id, app_id).await?;
|
|
let secrets = repository::user_app_secrets::list(&mut *tx, query)
|
|
.await?
|
|
.into_iter()
|
|
.map(UserAppSecretResponse::from)
|
|
.collect();
|
|
tx.commit().await.map_err(RepositoryError::from)?;
|
|
|
|
Ok(Json(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)
|
|
.await?
|
|
.ok_or(AppError::NotFound)?;
|
|
tx.commit().await.map_err(RepositoryError::from)?;
|
|
|
|
Ok(Json(secret.into()))
|
|
}
|
|
|
|
#[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();
|
|
let generated_secret = generate_secret().map_err(|_| AppError::Unknown)?;
|
|
let hashed_secret = hash_secret(&generated_secret);
|
|
let secret = request.into_domain(secret_id, app_id, hashed_secret, now)?;
|
|
|
|
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
|
ensure_app_ownership(&mut tx, user_id, app_id).await?;
|
|
let upserted = repository::user_app_secrets::upsert(&mut *tx, secret).await?;
|
|
tx.commit().await.map_err(RepositoryError::from)?;
|
|
|
|
let status = if upserted.previous.is_some() {
|
|
StatusCode::OK
|
|
} else {
|
|
StatusCode::CREATED
|
|
};
|
|
let secret = upserted.current;
|
|
|
|
Ok((
|
|
status,
|
|
Json(CreatedUserAppSecretResponse {
|
|
credential: secret.into(),
|
|
secret: generated_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?;
|
|
tx.commit().await.map_err(RepositoryError::from)?;
|
|
|
|
deleted.ok_or(AppError::NotFound)?;
|
|
|
|
Ok(StatusCode::NO_CONTENT)
|
|
}
|
|
|
|
async fn ensure_app_ownership(
|
|
connection: &mut sqlx::PgConnection,
|
|
user_id: Uuid,
|
|
app_id: Uuid,
|
|
) -> Result<(), AppError> {
|
|
match repository::user_apps::find_owner_by_id(&mut *connection, app_id).await? {
|
|
Some(owner) if owner == user_id => Ok(()),
|
|
Some(_) | None => Err(AppError::NotFound),
|
|
}
|
|
}
|