Added user apps and token resources.
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
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::{json::Json, path::Path, query::Query},
|
||||
};
|
||||
use crate::{
|
||||
crypto::{generate_secret, hash_secret},
|
||||
domain::UserAppSecret,
|
||||
error::AppError,
|
||||
repository::{
|
||||
dtos::{Delete, Upsert},
|
||||
user_app_secrets as repository,
|
||||
},
|
||||
};
|
||||
|
||||
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(
|
||||
State(pool): State<PgPool>,
|
||||
Path((user_id, app_id)): Path<(Uuid, Uuid)>,
|
||||
Query(query): Query<QueryParams>,
|
||||
) -> Result<Json<Vec<UserAppSecretResponse>>, AppError> {
|
||||
let secrets = repository::list(
|
||||
&pool,
|
||||
repository::Query {
|
||||
app_id,
|
||||
limit: Some(query.limit.unwrap_or(20)),
|
||||
offset: query.offset,
|
||||
},
|
||||
)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(UserAppSecretResponse::from)
|
||||
.collect();
|
||||
|
||||
Ok(Json(secrets))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
#[tracing::instrument(skip(pool))]
|
||||
async fn get_user_app_secret(
|
||||
State(pool): State<PgPool>,
|
||||
Path((user_id, app_id, secret_id)): Path<(Uuid, Uuid, Uuid)>,
|
||||
) -> Result<Json<UserAppSecretResponse>, AppError> {
|
||||
let secret = repository::get(&pool, app_id, secret_id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
Ok(Json(secret.into()))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
#[tracing::instrument(skip(pool, request))]
|
||||
async fn upsert_user_app_secret(
|
||||
State(pool): State<PgPool>,
|
||||
Path((user_id, app_id, secret_id)): Path<(Uuid, Uuid, Uuid)>,
|
||||
Json(request): Json<UserAppSecretRequest>,
|
||||
) -> Result<(StatusCode, Json<CreatedUserAppSecretResponse>), AppError> {
|
||||
let expires_at = request.expires_at_utc();
|
||||
if expires_at <= chrono::Utc::now() {
|
||||
return Err(AppError::BadRequest(
|
||||
"expires_at must be in the future".to_owned(),
|
||||
));
|
||||
}
|
||||
|
||||
let generated_secret = generate_secret().map_err(|_| AppError::Internal)?;
|
||||
let secret = UserAppSecret {
|
||||
id: secret_id,
|
||||
app_id,
|
||||
secret_hash: hash_secret(&generated_secret),
|
||||
expires_at,
|
||||
created_at: chrono::Utc::now(),
|
||||
};
|
||||
let upsert = repository::upsert(&pool, secret).await?;
|
||||
|
||||
let (status, secret) = match upsert {
|
||||
Upsert::Created(secret) => (StatusCode::CREATED, secret),
|
||||
Upsert::Updated(secret) => (StatusCode::OK, secret),
|
||||
};
|
||||
|
||||
Ok((
|
||||
status,
|
||||
Json(CreatedUserAppSecretResponse {
|
||||
credential: secret.into(),
|
||||
secret: generated_secret,
|
||||
}),
|
||||
))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
#[tracing::instrument(skip(pool))]
|
||||
async fn delete_user_app_secret(
|
||||
State(pool): State<PgPool>,
|
||||
Path((user_id, app_id, secret_id)): Path<(Uuid, Uuid, Uuid)>,
|
||||
) -> Result<StatusCode, AppError> {
|
||||
match repository::delete(&pool, app_id, secret_id).await? {
|
||||
Delete::Deleted => Ok(StatusCode::NO_CONTENT),
|
||||
Delete::NotFound => Err(AppError::NotFound),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user