Added user apps and token resources.

This commit is contained in:
2026-10-07 22:45:25 +02:00
parent 1f1f027368
commit 69afcedae1
47 changed files with 1940 additions and 28 deletions
+6
View File
@@ -1,11 +1,17 @@
mod account;
mod recurring_transaction;
mod results;
mod token;
mod transaction;
mod user;
mod user_app;
mod user_app_secret;
pub use account::*;
pub use recurring_transaction::*;
pub use results::*;
pub use token::*;
pub use transaction::*;
pub use user::*;
pub use user_app::*;
pub use user_app_secret::*;
+13
View File
@@ -0,0 +1,13 @@
use chrono::{DateTime, Utc};
use uuid::Uuid;
pub struct TokenRow {
pub id: Uuid,
pub app_id: Uuid,
pub access_token_hash: Vec<u8>,
pub refresh_token_hash: Vec<u8>,
pub access_expires_at: DateTime<Utc>,
pub refresh_expires_at: DateTime<Utc>,
pub revoked_at: Option<DateTime<Utc>>,
pub created_at: DateTime<Utc>,
}
+6
View File
@@ -0,0 +1,6 @@
use uuid::Uuid;
pub struct UserAppRow {
pub id: Uuid,
pub user_id: Uuid,
}
@@ -0,0 +1,10 @@
use chrono::{DateTime, Utc};
use uuid::Uuid;
pub struct UserAppSecretRow {
pub id: Uuid,
pub app_id: Uuid,
pub secret_hash: Vec<u8>,
pub expires_at: DateTime<Utc>,
pub created_at: DateTime<Utc>,
}
+86
View File
@@ -0,0 +1,86 @@
use chrono::{DateTime, Utc};
use super::{DatabaseError, dtos::TokenRow};
pub async fn issue<'e, E>(executor: E, token: TokenRow) -> Result<(), DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
sqlx::query!(
r#"
INSERT INTO tokens (
id, app_id, access_token_hash, refresh_token_hash,
access_expires_at, refresh_expires_at, revoked_at, created_at
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
"#,
token.id,
token.app_id,
token.access_token_hash,
token.refresh_token_hash,
token.access_expires_at,
token.refresh_expires_at,
token.revoked_at,
token.created_at,
)
.execute(executor)
.await
.map_err(DatabaseError::from)?;
Ok(())
}
pub async fn rotate_refresh_token<'e, E>(
executor: E,
old_refresh_hash: &[u8],
access_token_hash: &[u8],
refresh_token_hash: &[u8],
access_expires_at: DateTime<Utc>,
refresh_expires_at: DateTime<Utc>,
) -> Result<bool, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let row = sqlx::query!(
r#"
UPDATE tokens
SET access_token_hash = $2,
refresh_token_hash = $3,
access_expires_at = $4,
refresh_expires_at = $5
WHERE refresh_token_hash = $1
AND revoked_at IS NULL
AND refresh_expires_at > now()
RETURNING id
"#,
old_refresh_hash,
access_token_hash,
refresh_token_hash,
access_expires_at,
refresh_expires_at,
)
.fetch_optional(executor)
.await
.map_err(DatabaseError::from)?;
Ok(row.is_some())
}
pub async fn revoke<'e, E>(executor: E, token_hash: &[u8]) -> Result<(), DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
sqlx::query!(
r#"
UPDATE tokens
SET revoked_at = COALESCE(revoked_at, now())
WHERE access_token_hash = $1 OR refresh_token_hash = $1
"#,
token_hash,
)
.execute(executor)
.await
.map_err(DatabaseError::from)?;
Ok(())
}
+163
View File
@@ -0,0 +1,163 @@
use uuid::Uuid;
use super::{
DatabaseError,
dtos::{Delete, Upsert, UserAppSecretRow},
};
pub struct Query {
pub app_id: Uuid,
pub limit: Option<i64>,
pub offset: Option<i64>,
}
impl Default for Query {
fn default() -> Self {
Self {
app_id: Uuid::nil(),
limit: None,
offset: None,
}
}
}
pub async fn query<'e, E>(executor: E, query: Query) -> Result<Vec<UserAppSecretRow>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
sqlx::query_as!(
UserAppSecretRow,
r#"
SELECT id, app_id, secret_hash, expires_at, created_at
FROM user_app_secrets
WHERE app_id = $1
ORDER BY created_at DESC, id DESC
LIMIT $2
OFFSET $3
"#,
query.app_id,
query.limit,
query.offset,
)
.fetch_all(executor)
.await
.map_err(DatabaseError::from)
}
pub async fn get_by_id<'e, E>(
executor: E,
app_id: Uuid,
id: Uuid,
) -> Result<Option<UserAppSecretRow>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
sqlx::query_as!(
UserAppSecretRow,
r#"
SELECT id, app_id, secret_hash, expires_at, created_at
FROM user_app_secrets
WHERE app_id = $1 AND id = $2
"#,
app_id,
id,
)
.fetch_optional(executor)
.await
.map_err(DatabaseError::from)
}
pub async fn upsert<'e, E>(
executor: E,
secret: UserAppSecretRow,
) -> Result<Upsert<UserAppSecretRow>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let row = sqlx::query!(
r#"
INSERT INTO user_app_secrets (id, app_id, secret_hash, expires_at)
VALUES ($1, $2, $3, $4)
ON CONFLICT (id) DO UPDATE SET
secret_hash = EXCLUDED.secret_hash,
expires_at = EXCLUDED.expires_at
WHERE user_app_secrets.app_id = EXCLUDED.app_id
RETURNING WITH (OLD AS old_row, NEW AS new_row)
old_row.id AS "old_id?",
new_row.id AS "id!",
new_row.app_id AS "app_id!",
new_row.secret_hash AS "secret_hash!",
new_row.expires_at AS "expires_at!",
new_row.created_at AS "created_at!"
"#,
secret.id,
secret.app_id,
secret.secret_hash,
secret.expires_at,
)
.fetch_one(executor)
.await
.map_err(DatabaseError::from)?;
let secret = UserAppSecretRow {
id: row.id,
app_id: row.app_id,
secret_hash: row.secret_hash,
expires_at: row.expires_at,
created_at: row.created_at,
};
Ok(if row.old_id.is_some() {
Upsert::Updated(secret)
} else {
Upsert::Created(secret)
})
}
pub async fn verify<'e, E>(
executor: E,
app_id: Uuid,
secret_hash: &[u8],
) -> Result<bool, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
sqlx::query_scalar!(
r#"
SELECT EXISTS (
SELECT 1
FROM user_app_secrets
WHERE app_id = $1 AND secret_hash = $2 AND expires_at > now()
) AS "is_valid!"
"#,
app_id,
secret_hash,
)
.fetch_one(executor)
.await
.map_err(DatabaseError::from)
}
pub async fn delete_by_id<'e, E>(
executor: E,
app_id: Uuid,
id: Uuid,
) -> Result<Delete, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let deleted = sqlx::query!(
"DELETE FROM user_app_secrets WHERE app_id = $1 AND id = $2 RETURNING id",
app_id,
id,
)
.fetch_optional(executor)
.await
.map_err(DatabaseError::from)?;
Ok(if deleted.is_some() {
Delete::Deleted
} else {
Delete::NotFound
})
}
+129
View File
@@ -0,0 +1,129 @@
use uuid::Uuid;
use super::{
DatabaseError,
dtos::{Delete, Upsert, UserAppRow},
};
pub struct Query {
pub user_id: Uuid,
pub limit: Option<i64>,
pub offset: Option<i64>,
}
impl Default for Query {
fn default() -> Self {
Self {
user_id: Uuid::nil(),
limit: None,
offset: None,
}
}
}
pub async fn query<'e, E>(executor: E, query: Query) -> Result<Vec<UserAppRow>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
sqlx::query_as!(
UserAppRow,
r#"
SELECT id, user_id
FROM user_apps
WHERE user_id = $1
ORDER BY id
LIMIT $2
OFFSET $3
"#,
query.user_id,
query.limit,
query.offset,
)
.fetch_all(executor)
.await
.map_err(DatabaseError::from)
}
pub async fn get_by_id<'e, E>(
executor: E,
user_id: Uuid,
id: Uuid,
) -> Result<Option<UserAppRow>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
sqlx::query_as!(
UserAppRow,
r#"
SELECT id, user_id
FROM user_apps
WHERE user_id = $1 AND id = $2
"#,
user_id,
id,
)
.fetch_optional(executor)
.await
.map_err(DatabaseError::from)
}
pub async fn upsert<'e, E>(
executor: E,
app: UserAppRow,
) -> Result<Upsert<UserAppRow>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let row = sqlx::query!(
r#"
INSERT INTO user_apps (id, user_id)
VALUES ($1, $2)
ON CONFLICT (id) DO UPDATE SET
id = EXCLUDED.id
RETURNING WITH (OLD AS old_row, NEW AS new_row)
old_row.id AS "old_id?",
new_row.id AS "id!",
new_row.user_id AS "user_id!"
"#,
app.id,
app.user_id,
)
.fetch_one(executor)
.await
.map_err(DatabaseError::from)?;
let app = UserAppRow {
id: row.id,
user_id: row.user_id,
};
Ok(if row.old_id.is_some() {
Upsert::Updated(app)
} else {
Upsert::Created(app)
})
}
pub async fn delete_by_id<'e, E>(
executor: E,
user_id: Uuid,
id: Uuid,
) -> Result<Delete, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let deleted = sqlx::query!(
"DELETE FROM user_apps WHERE user_id = $1 AND id = $2 RETURNING id",
user_id,
id,
)
.fetch_optional(executor)
.await
.map_err(DatabaseError::from)?;
Ok(if deleted.is_some() {
Delete::Deleted
} else {
Delete::NotFound
})
}