Added user apps and token resources.

This commit is contained in:
2026-10-07 22:50:18 +02:00
parent 1f1f027368
commit 9038120734
47 changed files with 1940 additions and 29 deletions
+194
View File
@@ -0,0 +1,194 @@
use uuid::Uuid;
use super::{
QueryConversionError, ReferenceRelation, ReferenceViolationKind, RepositoryError, dtos::Upsert,
};
use crate::{
database::{Constraint, DatabaseError, dtos::UserAppSecretRow, user_app_secrets as db},
domain::UserAppSecret,
};
pub struct Query {
pub app_id: Uuid,
pub limit: Option<u64>,
pub offset: Option<u64>,
}
impl Default for Query {
fn default() -> Self {
Self {
app_id: Uuid::nil(),
limit: None,
offset: None,
}
}
}
impl TryFrom<Query> for db::Query {
type Error = QueryConversionError;
fn try_from(query: Query) -> Result<Self, Self::Error> {
Ok(db::Query {
app_id: query.app_id,
limit: query
.limit
.map(|limit| i64::try_from(limit).map_err(|_| QueryConversionError::Limit(limit)))
.transpose()?,
offset: query
.offset
.map(|offset| {
i64::try_from(offset).map_err(|_| QueryConversionError::Offset(offset))
})
.transpose()?,
})
}
}
pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<UserAppSecret>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
Ok(db::query(executor, query.try_into()?)
.await?
.into_iter()
.map(decode)
.collect())
}
pub async fn get<'e, E>(
executor: E,
app_id: Uuid,
id: Uuid,
) -> Result<Option<UserAppSecret>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
Ok(db::get_by_id(executor, app_id, id).await?.map(decode))
}
pub async fn upsert<'e, E>(
executor: E,
secret: UserAppSecret,
) -> Result<Upsert<UserAppSecret>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let upsert = db::upsert(executor, encode(secret))
.await
.map_err(classify_upsert_error)?;
Ok(match upsert {
crate::database::dtos::Upsert::Created(row) => Upsert::Created(decode(row)),
crate::database::dtos::Upsert::Updated(row) => Upsert::Updated(decode(row)),
})
}
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
match &error {
DatabaseError::ForeignKeyViolation {
constraint: Constraint::UserAppSecretApp,
..
} => RepositoryError::ReferenceViolation {
relation: ReferenceRelation::UserApp,
kind: ReferenceViolationKind::Missing,
source: error,
},
_ => RepositoryError::Database(error),
}
}
pub async fn verify<'e, E>(
executor: E,
app_id: Uuid,
secret_hash: &[u8],
) -> Result<bool, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
Ok(db::verify(executor, app_id, secret_hash).await?)
}
pub async fn delete<'e, E>(
executor: E,
app_id: Uuid,
id: Uuid,
) -> Result<super::dtos::Delete, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
Ok(db::delete_by_id(executor, app_id, id).await?.into())
}
fn decode(row: UserAppSecretRow) -> UserAppSecret {
UserAppSecret {
id: row.id,
app_id: row.app_id,
secret_hash: row.secret_hash,
expires_at: row.expires_at,
created_at: row.created_at,
}
}
fn encode(secret: UserAppSecret) -> UserAppSecretRow {
UserAppSecretRow {
id: secret.id,
app_id: secret.app_id,
secret_hash: secret.secret_hash,
expires_at: secret.expires_at,
created_at: secret.created_at,
}
}
#[cfg(test)]
mod tests {
use chrono::Utc;
use super::*;
fn foreign_key_violation(constraint: Constraint) -> DatabaseError {
DatabaseError::ForeignKeyViolation {
constraint,
source: sqlx::Error::Protocol("test database cause".to_owned()),
}
}
#[test]
fn classifies_user_app_secret_upsert_foreign_key_violation() {
let error = classify_upsert_error(foreign_key_violation(Constraint::UserAppSecretApp));
assert!(matches!(
error,
RepositoryError::ReferenceViolation {
relation: ReferenceRelation::UserApp,
kind: ReferenceViolationKind::Missing,
..
}
));
}
#[test]
fn keeps_unknown_upsert_foreign_keys_generic() {
let error = classify_upsert_error(foreign_key_violation(Constraint::Unnamed));
assert!(matches!(error, RepositoryError::Database(_)));
}
fn row() -> UserAppSecretRow {
UserAppSecretRow {
id: Uuid::new_v4(),
app_id: Uuid::new_v4(),
secret_hash: vec![7; 32],
expires_at: Utc::now(),
created_at: Utc::now(),
}
}
#[test]
fn round_trips_through_rows() {
let secret = decode(row());
let round_trip = decode(encode(secret.clone()));
assert_eq!(round_trip.id, secret.id);
assert_eq!(round_trip.app_id, secret.app_id);
assert_eq!(round_trip.secret_hash, secret.secret_hash);
assert_eq!(round_trip.expires_at, secret.expires_at);
}
}