Added ownership and app existence validation to the user app secrets routes.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use chrono::{DateTime, FixedOffset, Utc};
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -6,13 +6,7 @@ use crate::domain;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct UserAppSecretRequest {
|
||||
pub expires_at: DateTime<FixedOffset>,
|
||||
}
|
||||
|
||||
impl UserAppSecretRequest {
|
||||
pub fn expires_at_utc(&self) -> DateTime<Utc> {
|
||||
self.expires_at.with_timezone(&Utc)
|
||||
}
|
||||
pub expires_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
@@ -50,10 +44,7 @@ mod tests {
|
||||
let request: UserAppSecretRequest =
|
||||
serde_json::from_str(r#"{"expires_at":"2030-01-01T12:00:00+02:00"}"#).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
request.expires_at_utc().to_rfc3339(),
|
||||
"2030-01-01T10:00:00+00:00"
|
||||
);
|
||||
assert_eq!(request.expires_at.to_rfc3339(), "2030-01-01T10:00:00+00:00");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -14,7 +14,7 @@ use crate::{
|
||||
domain::UserAppSecret,
|
||||
error::AppError,
|
||||
repository::{
|
||||
self,
|
||||
self, ReferenceRelation, RepositoryError,
|
||||
dtos::{Delete, Upsert},
|
||||
},
|
||||
};
|
||||
@@ -49,11 +49,14 @@ async fn list_user_app_secrets(
|
||||
offset: query.offset,
|
||||
};
|
||||
|
||||
let secrets = repository::user_app_secrets::list(&pool, query)
|
||||
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))
|
||||
}
|
||||
@@ -64,9 +67,12 @@ 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::user_app_secrets::get_by_id(&pool, app_id, secret_id)
|
||||
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()))
|
||||
}
|
||||
@@ -78,8 +84,8 @@ async fn upsert_user_app_secret(
|
||||
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() {
|
||||
let now = chrono::Utc::now();
|
||||
if request.expires_at <= now {
|
||||
return Err(AppError::BadRequest(
|
||||
"expires_at must be in the future".to_owned(),
|
||||
));
|
||||
@@ -90,10 +96,14 @@ async fn upsert_user_app_secret(
|
||||
id: secret_id,
|
||||
app_id,
|
||||
secret_hash: hash_secret(&generated_secret),
|
||||
expires_at,
|
||||
created_at: chrono::Utc::now(),
|
||||
expires_at: request.expires_at,
|
||||
created_at: now,
|
||||
};
|
||||
let upsert = repository::user_app_secrets::upsert(&pool, secret).await?;
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
ensure_app_ownership(&mut tx, user_id, app_id).await?;
|
||||
let upsert = repository::user_app_secrets::upsert(&mut *tx, secret).await?;
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
|
||||
let (status, secret) = match upsert {
|
||||
Upsert::Created(secret) => (StatusCode::CREATED, secret),
|
||||
@@ -115,8 +125,27 @@ 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::user_app_secrets::delete_by_id(&pool, app_id, secret_id).await? {
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
ensure_app_ownership(&mut tx, user_id, app_id).await?;
|
||||
let result = repository::user_app_secrets::delete_by_id(&mut *tx, app_id, secret_id).await?;
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
|
||||
match result {
|
||||
Delete::Deleted => Ok(StatusCode::NO_CONTENT),
|
||||
Delete::NotFound => Err(AppError::NotFound),
|
||||
}
|
||||
}
|
||||
|
||||
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(RepositoryError::NotFound {
|
||||
relation: ReferenceRelation::UserApp,
|
||||
}
|
||||
.into()),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user