use crate::database::dtos::Upsert;
use super::{DatabaseError, dtos::RefreshTokenRow};
pub async fn get_by_hash<'e, E>(
executor: E,
secret_hash: &[u8],
) -> Result, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let row = sqlx::query_as!(
RefreshTokenRow,
r#"
SELECT
id,
access_token_id,
secret_hash,
expires_at,
issued_at
FROM refresh_tokens
WHERE secret_hash = $1
AND expires_at > now()
"#,
secret_hash,
)
.fetch_optional(executor)
.await
.map_err(DatabaseError::from)?;
Ok(row)
}
pub async fn upsert<'e, E>(
executor: E,
refresh_token: RefreshTokenRow,
) -> Result >, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let row = sqlx::query!(
r#"
INSERT INTO refresh_tokens (id, access_token_id, secret_hash, expires_at, issued_at)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (id) DO UPDATE SET
secret_hash = refresh_tokens.secret_hash,
expires_at = refresh_tokens.expires_at,
issued_at = refresh_tokens.issued_at
WHERE refresh_tokens.access_token_id = EXCLUDED.access_token_id
AND refresh_tokens.secret_hash <> EXCLUDED.secret_hash
RETURNING WITH (OLD AS old_row, NEW AS new_row)
old_row.id AS "old_id?",
COALESCE(old_row.access_token_id, new_row.access_token_id) AS "old_access_token_id!",
COALESCE(old_row.secret_hash, new_row.secret_hash) AS "old_secret_hash!",
COALESCE(old_row.expires_at, new_row.expires_at) AS "old_expires_at!",
COALESCE(old_row.issued_at, new_row.issued_at) AS "old_issued_at!",
new_row.id AS "id!",
new_row.access_token_id AS "access_token_id!",
new_row.secret_hash AS "secret_hash!",
new_row.expires_at AS "expires_at!",
new_row.issued_at AS "issued_at!"
"#,
refresh_token.id,
refresh_token.access_token_id,
refresh_token.secret_hash,
refresh_token.expires_at,
refresh_token.issued_at,
)
.fetch_optional(executor)
.await
.map_err(DatabaseError::from)?;
// Nothing is written when the new access token ID is different on the existing row, or the
// secret hash has not changed: the update path is intended for secret rotation, i.e., updating
// the secret hash while keeping the same access token ID.
let Some(row) = row else {
return Ok(None);
};
// `old_id` indicates whether there was a previous row before the upsert, other old fields are
// coalesced with the new row's values in case there was no previous row. This enables static
// typing on the previous row instead of having to check nullability for each old field in code.
let previous = row.old_id.map(|id| RefreshTokenRow {
id,
access_token_id: row.old_access_token_id,
secret_hash: row.old_secret_hash,
expires_at: row.old_expires_at,
issued_at: row.old_issued_at,
});
Ok(Some(Upsert {
previous,
current: RefreshTokenRow {
id: row.id,
access_token_id: row.access_token_id,
secret_hash: row.secret_hash,
expires_at: row.expires_at,
issued_at: row.issued_at,
},
}))
}
pub async fn delete_by_hash<'e, E>(
executor: E,
secret_hash: &[u8],
) -> Result , DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let row = sqlx::query_as!(
RefreshTokenRow,
r#"
DELETE FROM refresh_tokens
WHERE secret_hash = $1
RETURNING id, access_token_id, secret_hash, expires_at, issued_at
"#,
secret_hash,
)
.fetch_optional(executor)
.await
.map_err(DatabaseError::from)?;
Ok(row)
}