More nit picking.
This commit is contained in:
@@ -25,7 +25,7 @@ where
|
||||
SELECT id, account_id, user_id
|
||||
FROM account_members
|
||||
WHERE account_id IN (SELECT id FROM accessible_accounts($1))
|
||||
AND account_id = $2
|
||||
AND account_id = $2
|
||||
ORDER BY id
|
||||
LIMIT $3
|
||||
OFFSET $4
|
||||
@@ -55,8 +55,8 @@ where
|
||||
SELECT id, account_id, user_id
|
||||
FROM account_members
|
||||
WHERE account_id IN (SELECT id FROM accessible_accounts($1))
|
||||
AND account_id = $2
|
||||
AND id = $3
|
||||
AND account_id = $2
|
||||
AND id = $3
|
||||
"#,
|
||||
user_id,
|
||||
account_id,
|
||||
@@ -164,8 +164,8 @@ where
|
||||
r#"
|
||||
DELETE FROM account_members
|
||||
WHERE account_id IN (SELECT id FROM accessible_accounts($1))
|
||||
AND account_id = $2
|
||||
AND id = $3
|
||||
AND account_id = $2
|
||||
AND id = $3
|
||||
RETURNING id, account_id, user_id
|
||||
"#,
|
||||
user_id,
|
||||
|
||||
@@ -78,8 +78,8 @@ where
|
||||
r#"
|
||||
SELECT id, name
|
||||
FROM accounts
|
||||
WHERE id = $2
|
||||
AND id IN (SELECT id FROM accessible_accounts($1))
|
||||
WHERE id IN (SELECT id FROM accessible_accounts($1))
|
||||
AND id = $2
|
||||
FOR NO KEY UPDATE
|
||||
"#,
|
||||
user_id,
|
||||
@@ -178,7 +178,7 @@ where
|
||||
r#"
|
||||
DELETE FROM accounts
|
||||
WHERE id IN (SELECT id FROM accessible_accounts($1))
|
||||
AND id = $2
|
||||
AND id = $2
|
||||
RETURNING id, name
|
||||
"#,
|
||||
user_id,
|
||||
|
||||
@@ -70,7 +70,7 @@ where
|
||||
weekend_adjustment
|
||||
FROM recurring_transactions
|
||||
WHERE account_id IN (SELECT id FROM accessible_accounts($1))
|
||||
AND id = $2
|
||||
AND id = $2
|
||||
"#,
|
||||
user_id,
|
||||
id,
|
||||
@@ -207,7 +207,7 @@ where
|
||||
r#"
|
||||
DELETE FROM recurring_transactions
|
||||
WHERE account_id IN (SELECT id FROM accessible_accounts($1))
|
||||
AND id = $2
|
||||
AND id = $2
|
||||
RETURNING
|
||||
id,
|
||||
name,
|
||||
|
||||
@@ -44,8 +44,16 @@ where
|
||||
sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO tokens (
|
||||
id, user_id, user_app_id, access_token_hash, refresh_token_hash,
|
||||
access_expires_at, refresh_expires_at, refreshed_at, revoked_at, created_at
|
||||
id,
|
||||
user_id,
|
||||
user_app_id,
|
||||
access_token_hash,
|
||||
refresh_token_hash,
|
||||
access_expires_at,
|
||||
refresh_expires_at,
|
||||
refreshed_at,
|
||||
revoked_at,
|
||||
created_at
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
"#,
|
||||
|
||||
@@ -51,7 +51,7 @@ where
|
||||
SELECT id, account_id, amount_cents, direction, date, description
|
||||
FROM transactions
|
||||
WHERE account_id IN (SELECT id FROM accessible_accounts($1))
|
||||
AND id = $2
|
||||
AND id = $2
|
||||
"#,
|
||||
user_id,
|
||||
id,
|
||||
@@ -152,7 +152,7 @@ where
|
||||
r#"
|
||||
DELETE FROM transactions
|
||||
WHERE account_id IN (SELECT id FROM accessible_accounts($1))
|
||||
AND id = $2
|
||||
AND id = $2
|
||||
RETURNING id, account_id, amount_cents, direction, date, description
|
||||
"#,
|
||||
user_id,
|
||||
|
||||
@@ -25,7 +25,7 @@ where
|
||||
SELECT id, app_id, secret_hash, expires_at, created_at
|
||||
FROM user_app_secrets
|
||||
WHERE app_id IN (SELECT id FROM user_apps WHERE user_id = $1)
|
||||
AND app_id = $2
|
||||
AND app_id = $2
|
||||
ORDER BY created_at DESC, id DESC
|
||||
LIMIT $3
|
||||
OFFSET $4
|
||||
@@ -55,8 +55,8 @@ where
|
||||
SELECT id, app_id, secret_hash, expires_at, created_at
|
||||
FROM user_app_secrets
|
||||
WHERE app_id IN (SELECT id FROM user_apps WHERE user_id = $1)
|
||||
AND app_id = $2
|
||||
AND id = $3
|
||||
AND app_id = $2
|
||||
AND id = $3
|
||||
"#,
|
||||
user_id,
|
||||
app_id,
|
||||
@@ -143,7 +143,7 @@ pub async fn verify<'e, E>(
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
sqlx::query_scalar!(
|
||||
let is_valid = sqlx::query_scalar!(
|
||||
r#"
|
||||
SELECT EXISTS (
|
||||
SELECT 1
|
||||
@@ -156,7 +156,9 @@ where
|
||||
)
|
||||
.fetch_one(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
Ok(is_valid)
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(
|
||||
@@ -173,8 +175,8 @@ where
|
||||
r#"
|
||||
DELETE FROM user_app_secrets
|
||||
WHERE app_id IN (SELECT id FROM user_apps WHERE user_id = $1)
|
||||
AND app_id = $2
|
||||
AND id = $3
|
||||
AND app_id = $2
|
||||
AND id = $3
|
||||
RETURNING id, app_id, secret_hash, expires_at, created_at
|
||||
"#,
|
||||
user_id,
|
||||
|
||||
@@ -47,7 +47,11 @@ where
|
||||
{
|
||||
sqlx::query_as!(
|
||||
UserAppRow,
|
||||
"SELECT id, user_id FROM user_apps WHERE user_id = $1 AND id = $2",
|
||||
r#"
|
||||
SELECT id, user_id
|
||||
FROM user_apps
|
||||
WHERE user_id = $1 AND id = $2
|
||||
"#,
|
||||
user_id,
|
||||
id,
|
||||
)
|
||||
@@ -65,7 +69,11 @@ where
|
||||
{
|
||||
sqlx::query_as!(
|
||||
UserAppRow,
|
||||
"SELECT id, user_id FROM user_apps WHERE id = $1",
|
||||
r#"
|
||||
SELECT id, user_id
|
||||
FROM user_apps
|
||||
WHERE id = $1
|
||||
"#,
|
||||
id,
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
@@ -132,7 +140,11 @@ where
|
||||
{
|
||||
sqlx::query_as!(
|
||||
UserAppRow,
|
||||
"DELETE FROM user_apps WHERE user_id = $1 AND id = $2 RETURNING id, user_id",
|
||||
r#"
|
||||
DELETE FROM user_apps
|
||||
WHERE user_id = $1 AND id = $2
|
||||
RETURNING id, user_id
|
||||
"#,
|
||||
user_id,
|
||||
id,
|
||||
)
|
||||
|
||||
@@ -79,8 +79,16 @@ pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<UserRow
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
sqlx::query_as!(UserRow, "DELETE FROM users WHERE id = $1 RETURNING id", id)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)
|
||||
sqlx::query_as!(
|
||||
UserRow,
|
||||
r#"
|
||||
DELETE FROM users
|
||||
WHERE id = $1
|
||||
RETURNING id
|
||||
"#,
|
||||
id
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
@@ -25,7 +25,9 @@ pub async fn issue<'e, E>(executor: E, token: Token) -> Result<(), RepositoryErr
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
Ok(db::issue(executor, encode(token)).await?)
|
||||
db::issue(executor, encode(token)).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn rotate_refresh_token<'e, E>(
|
||||
@@ -39,7 +41,7 @@ pub async fn rotate_refresh_token<'e, E>(
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
Ok(db::rotate_refresh_token(
|
||||
let rotated = db::rotate_refresh_token(
|
||||
executor,
|
||||
old_refresh_hash,
|
||||
access_token_hash,
|
||||
@@ -47,14 +49,18 @@ where
|
||||
access_expires_at,
|
||||
refresh_expires_at,
|
||||
)
|
||||
.await?)
|
||||
.await?;
|
||||
|
||||
Ok(rotated)
|
||||
}
|
||||
|
||||
pub async fn revoke<'e, E>(executor: E, token_hash: &[u8]) -> Result<(), RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
Ok(db::revoke(executor, token_hash).await?)
|
||||
db::revoke(executor, token_hash).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decode(token: TokenRow) -> Result<Token, RepositoryError> {
|
||||
|
||||
@@ -100,9 +100,9 @@ pub async fn verify<'e, E>(
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let result = db::verify(executor, app_id, secret_hash).await?;
|
||||
let is_valid = db::verify(executor, app_id, secret_hash).await?;
|
||||
|
||||
Ok(result)
|
||||
Ok(is_valid)
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(
|
||||
|
||||
@@ -37,28 +37,15 @@ async fn issue_token(
|
||||
let secret_hash = hash_secret(&client_secret);
|
||||
let generated = generate_token_pair()?;
|
||||
|
||||
let mut tx = pool
|
||||
.begin()
|
||||
.await
|
||||
.map_err(repository::RepositoryError::from)?;
|
||||
|
||||
if !repository::user_app_secrets::verify(&mut *tx, client_id, &secret_hash).await? {
|
||||
if !repository::user_app_secrets::verify(&pool, client_id, &secret_hash).await? {
|
||||
return Err(AppError::InvalidClientCredentials);
|
||||
}
|
||||
|
||||
let user_app = repository::user_apps::get_by_id_unscoped(&mut *tx, client_id)
|
||||
let app = repository::user_apps::get_by_id_unscoped(&pool, client_id)
|
||||
.await?
|
||||
.ok_or(AppError::InvalidClientCredentials)?;
|
||||
|
||||
repository::tokens::issue(
|
||||
&mut *tx,
|
||||
token(user_app.user_id, Some(user_app.id), &generated),
|
||||
)
|
||||
.await?;
|
||||
|
||||
tx.commit()
|
||||
.await
|
||||
.map_err(repository::RepositoryError::from)?;
|
||||
repository::tokens::issue(&pool, token(app.user_id, Some(app.id), &generated)).await?;
|
||||
|
||||
Ok(Json(token_response(
|
||||
generated.access_token,
|
||||
@@ -66,27 +53,21 @@ async fn issue_token(
|
||||
)))
|
||||
}
|
||||
TokenRequest::RefreshToken { refresh_token } => {
|
||||
let generated = generate_token_pair()?;
|
||||
let old_refresh_hash = hash_secret(&refresh_token);
|
||||
let mut tx = pool
|
||||
.begin()
|
||||
.await
|
||||
.map_err(repository::RepositoryError::from)?;
|
||||
let rotated = repository::tokens::rotate_refresh_token(
|
||||
&mut *tx,
|
||||
let generated = generate_token_pair()?;
|
||||
|
||||
if !repository::tokens::rotate_refresh_token(
|
||||
&pool,
|
||||
&old_refresh_hash,
|
||||
&generated.access_token_hash,
|
||||
&generated.refresh_token_hash,
|
||||
generated.access_expires_at,
|
||||
generated.refresh_expires_at,
|
||||
)
|
||||
.await?;
|
||||
if !rotated {
|
||||
.await?
|
||||
{
|
||||
return Err(AppError::InvalidRefreshToken);
|
||||
}
|
||||
tx.commit()
|
||||
.await
|
||||
.map_err(repository::RepositoryError::from)?;
|
||||
|
||||
Ok(Json(token_response(
|
||||
generated.access_token,
|
||||
@@ -102,7 +83,11 @@ async fn revoke_token(
|
||||
State(pool): State<PgPool>,
|
||||
Form(request): Form<TokenRevocationRequest>,
|
||||
) -> Result<(), AppError> {
|
||||
Ok(repository::tokens::revoke(&pool, &hash_secret(&request.token)).await?)
|
||||
let token_hash = hash_secret(&request.token);
|
||||
|
||||
repository::tokens::revoke(&pool, &token_hash).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
struct TokenPair {
|
||||
|
||||
Reference in New Issue
Block a user