Split up files and did some consistency work.
This commit is contained in:
-10
@@ -7,14 +7,4 @@ CREATE TABLE access_tokens (
|
|||||||
),
|
),
|
||||||
expires_at TIMESTAMPTZ NOT NULL,
|
expires_at TIMESTAMPTZ NOT NULL,
|
||||||
issued_at TIMESTAMPTZ NOT NULL
|
issued_at TIMESTAMPTZ NOT NULL
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE refresh_tokens (
|
|
||||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
|
|
||||||
access_token_id UUID NOT NULL UNIQUE CONSTRAINT fk_refresh_tokens_access_token REFERENCES access_tokens (id) ON DELETE CASCADE,
|
|
||||||
secret_hash BYTEA NOT NULL UNIQUE CHECK (
|
|
||||||
octet_length(secret_hash) = 32
|
|
||||||
),
|
|
||||||
expires_at TIMESTAMPTZ NOT NULL,
|
|
||||||
issued_at TIMESTAMPTZ NOT NULL
|
|
||||||
);
|
);
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
CREATE TABLE refresh_tokens (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
|
||||||
|
access_token_id UUID NOT NULL UNIQUE CONSTRAINT fk_refresh_tokens_access_token REFERENCES access_tokens (id) ON DELETE CASCADE,
|
||||||
|
secret_hash BYTEA NOT NULL UNIQUE CHECK (
|
||||||
|
octet_length(secret_hash) = 32
|
||||||
|
),
|
||||||
|
expires_at TIMESTAMPTZ NOT NULL,
|
||||||
|
issued_at TIMESTAMPTZ NOT NULL
|
||||||
|
);
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
|
pub mod access_tokens;
|
||||||
pub mod account_members;
|
pub mod account_members;
|
||||||
pub mod accounts;
|
pub mod accounts;
|
||||||
pub mod dtos;
|
pub mod dtos;
|
||||||
mod error_codes;
|
mod error_codes;
|
||||||
pub mod recurring_transactions;
|
pub mod recurring_transactions;
|
||||||
pub mod tokens;
|
pub mod refresh_tokens;
|
||||||
pub mod transactions;
|
pub mod transactions;
|
||||||
pub mod user_app_secrets;
|
pub mod user_app_secrets;
|
||||||
pub mod user_apps;
|
pub mod user_apps;
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
use crate::database::dtos::Upsert;
|
use crate::database::dtos::Upsert;
|
||||||
|
|
||||||
use super::{DatabaseError, dtos::AccessTokenRow, dtos::RefreshTokenRow};
|
use super::{DatabaseError, dtos::AccessTokenRow};
|
||||||
|
|
||||||
pub async fn get_access_token_by_hash<'e, E>(
|
pub async fn get_by_hash<'e, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
secret_hash: &[u8],
|
secret_hash: &[u8],
|
||||||
) -> Result<Option<AccessTokenRow>, DatabaseError>
|
) -> Result<Option<AccessTokenRow>, DatabaseError>
|
||||||
@@ -32,7 +32,7 @@ where
|
|||||||
Ok(row)
|
Ok(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_access_token_by_refresh_secret_hash<'e, E>(
|
pub async fn get_by_refresh_secret_hash<'e, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
refresh_secret_hash: &[u8],
|
refresh_secret_hash: &[u8],
|
||||||
) -> Result<Option<AccessTokenRow>, DatabaseError>
|
) -> Result<Option<AccessTokenRow>, DatabaseError>
|
||||||
@@ -63,7 +63,7 @@ where
|
|||||||
Ok(row)
|
Ok(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn upsert_access_token<'e, E>(
|
pub async fn upsert<'e, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
access_token: AccessTokenRow,
|
access_token: AccessTokenRow,
|
||||||
) -> Result<Option<Upsert<AccessTokenRow>>, DatabaseError>
|
) -> Result<Option<Upsert<AccessTokenRow>>, DatabaseError>
|
||||||
@@ -138,7 +138,7 @@ where
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete_access_token_by_hash<'e, E>(
|
pub async fn delete_by_hash<'e, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
secret_hash: &[u8],
|
secret_hash: &[u8],
|
||||||
) -> Result<Option<AccessTokenRow>, DatabaseError>
|
) -> Result<Option<AccessTokenRow>, DatabaseError>
|
||||||
@@ -160,124 +160,3 @@ where
|
|||||||
|
|
||||||
Ok(row)
|
Ok(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_refresh_token_by_hash<'e, E>(
|
|
||||||
executor: E,
|
|
||||||
secret_hash: &[u8],
|
|
||||||
) -> Result<Option<RefreshTokenRow>, 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_refresh_token<'e, E>(
|
|
||||||
executor: E,
|
|
||||||
refresh_token: RefreshTokenRow,
|
|
||||||
) -> Result<Option<Upsert<RefreshTokenRow>>, 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 refresh token already exists, but it's new access token ID
|
|
||||||
// is different, 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_refresh_token_by_hash<'e, E>(
|
|
||||||
executor: E,
|
|
||||||
secret_hash: &[u8],
|
|
||||||
) -> Result<Option<RefreshTokenRow>, 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)
|
|
||||||
}
|
|
||||||
@@ -1,18 +1,20 @@
|
|||||||
|
mod access_token;
|
||||||
mod account;
|
mod account;
|
||||||
mod account_member;
|
mod account_member;
|
||||||
mod recurring_transaction;
|
mod recurring_transaction;
|
||||||
|
mod refresh_token;
|
||||||
mod results;
|
mod results;
|
||||||
mod token;
|
|
||||||
mod transaction;
|
mod transaction;
|
||||||
mod user;
|
mod user;
|
||||||
mod user_app;
|
mod user_app;
|
||||||
mod user_app_secret;
|
mod user_app_secret;
|
||||||
|
|
||||||
|
pub use access_token::*;
|
||||||
pub use account::*;
|
pub use account::*;
|
||||||
pub use account_member::*;
|
pub use account_member::*;
|
||||||
pub use recurring_transaction::*;
|
pub use recurring_transaction::*;
|
||||||
|
pub use refresh_token::*;
|
||||||
pub use results::*;
|
pub use results::*;
|
||||||
pub use token::*;
|
|
||||||
pub use transaction::*;
|
pub use transaction::*;
|
||||||
pub use user::*;
|
pub use user::*;
|
||||||
pub use user_app::*;
|
pub use user_app::*;
|
||||||
|
|||||||
@@ -9,11 +9,3 @@ pub struct AccessTokenRow {
|
|||||||
pub expires_at: DateTime<Utc>,
|
pub expires_at: DateTime<Utc>,
|
||||||
pub issued_at: DateTime<Utc>,
|
pub issued_at: DateTime<Utc>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RefreshTokenRow {
|
|
||||||
pub id: Uuid,
|
|
||||||
pub access_token_id: Uuid,
|
|
||||||
pub secret_hash: Vec<u8>,
|
|
||||||
pub expires_at: DateTime<Utc>,
|
|
||||||
pub issued_at: DateTime<Utc>,
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub struct RefreshTokenRow {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub access_token_id: Uuid,
|
||||||
|
pub secret_hash: Vec<u8>,
|
||||||
|
pub expires_at: DateTime<Utc>,
|
||||||
|
pub issued_at: DateTime<Utc>,
|
||||||
|
}
|
||||||
@@ -0,0 +1,124 @@
|
|||||||
|
use crate::database::dtos::Upsert;
|
||||||
|
|
||||||
|
use super::{DatabaseError, dtos::RefreshTokenRow};
|
||||||
|
|
||||||
|
pub async fn get_by_hash<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
secret_hash: &[u8],
|
||||||
|
) -> Result<Option<RefreshTokenRow>, 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<Option<Upsert<RefreshTokenRow>>, 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 refresh token already exists, but it's new access token ID
|
||||||
|
// is different, 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<Option<RefreshTokenRow>, 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)
|
||||||
|
}
|
||||||
@@ -1,24 +1,26 @@
|
|||||||
|
mod access_token;
|
||||||
mod account;
|
mod account;
|
||||||
mod account_member;
|
mod account_member;
|
||||||
mod cents;
|
mod cents;
|
||||||
mod direction;
|
mod direction;
|
||||||
mod recurring_transaction;
|
mod recurring_transaction;
|
||||||
|
mod refresh_token;
|
||||||
mod schedule;
|
mod schedule;
|
||||||
mod secret;
|
mod secret;
|
||||||
mod token;
|
|
||||||
mod transaction;
|
mod transaction;
|
||||||
mod user;
|
mod user;
|
||||||
mod user_app;
|
mod user_app;
|
||||||
mod user_app_secret;
|
mod user_app_secret;
|
||||||
|
|
||||||
|
pub use access_token::*;
|
||||||
pub use account::*;
|
pub use account::*;
|
||||||
pub use account_member::*;
|
pub use account_member::*;
|
||||||
pub use cents::*;
|
pub use cents::*;
|
||||||
pub use direction::*;
|
pub use direction::*;
|
||||||
pub use recurring_transaction::*;
|
pub use recurring_transaction::*;
|
||||||
|
pub use refresh_token::*;
|
||||||
pub use schedule::*;
|
pub use schedule::*;
|
||||||
pub use secret::*;
|
pub use secret::*;
|
||||||
pub use token::*;
|
|
||||||
pub use transaction::*;
|
pub use transaction::*;
|
||||||
pub use user::*;
|
pub use user::*;
|
||||||
pub use user_app::*;
|
pub use user_app::*;
|
||||||
|
|||||||
@@ -67,58 +67,3 @@ impl From<AccessToken> for HashedAccessToken {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq)]
|
|
||||||
pub struct RefreshToken {
|
|
||||||
pub id: Uuid,
|
|
||||||
pub token_id: Uuid,
|
|
||||||
pub secret: Secret,
|
|
||||||
pub expires_at: DateTime<Utc>,
|
|
||||||
pub issued_at: DateTime<Utc>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RefreshToken {
|
|
||||||
// pub fn generate(token_id: Uuid, lifetime: Duration) -> Result<Self, DomainError> {
|
|
||||||
// let now = Utc::now();
|
|
||||||
// Ok(Self {
|
|
||||||
// id: Uuid::new_v4(),
|
|
||||||
// token_id,
|
|
||||||
// secret: Secret::generate()?,
|
|
||||||
// expires_at: now + lifetime,
|
|
||||||
// issued_at: now,
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq)]
|
|
||||||
pub struct HashedRefreshToken {
|
|
||||||
pub id: Uuid,
|
|
||||||
pub token_id: Uuid,
|
|
||||||
pub secret_hash: HashedSecret,
|
|
||||||
pub expires_at: DateTime<Utc>,
|
|
||||||
pub issued_at: DateTime<Utc>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HashedRefreshToken {
|
|
||||||
pub fn refresh(self, lifetime: Duration) -> Result<RefreshToken, DomainError> {
|
|
||||||
Ok(RefreshToken {
|
|
||||||
id: self.id,
|
|
||||||
token_id: self.token_id,
|
|
||||||
secret: Secret::generate()?,
|
|
||||||
expires_at: Utc::now() + lifetime,
|
|
||||||
issued_at: Utc::now(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<RefreshToken> for HashedRefreshToken {
|
|
||||||
fn from(value: RefreshToken) -> Self {
|
|
||||||
Self {
|
|
||||||
id: value.id,
|
|
||||||
token_id: value.token_id,
|
|
||||||
secret_hash: value.secret.hash(),
|
|
||||||
expires_at: value.expires_at,
|
|
||||||
issued_at: value.issued_at,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
use chrono::{DateTime, Duration, Utc};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::DomainError;
|
||||||
|
use crate::domain::{HashedSecret, Secret};
|
||||||
|
|
||||||
|
#[derive(Clone, PartialEq, Eq)]
|
||||||
|
pub struct RefreshToken {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub token_id: Uuid,
|
||||||
|
pub secret: Secret,
|
||||||
|
pub expires_at: DateTime<Utc>,
|
||||||
|
pub issued_at: DateTime<Utc>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RefreshToken {
|
||||||
|
// pub fn generate(token_id: Uuid, lifetime: Duration) -> Result<Self, DomainError> {
|
||||||
|
// let now = Utc::now();
|
||||||
|
// Ok(Self {
|
||||||
|
// id: Uuid::new_v4(),
|
||||||
|
// token_id,
|
||||||
|
// secret: Secret::generate()?,
|
||||||
|
// expires_at: now + lifetime,
|
||||||
|
// issued_at: now,
|
||||||
|
// })
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, PartialEq, Eq)]
|
||||||
|
pub struct HashedRefreshToken {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub token_id: Uuid,
|
||||||
|
pub secret_hash: HashedSecret,
|
||||||
|
pub expires_at: DateTime<Utc>,
|
||||||
|
pub issued_at: DateTime<Utc>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HashedRefreshToken {
|
||||||
|
pub fn refresh(self, lifetime: Duration) -> Result<RefreshToken, DomainError> {
|
||||||
|
Ok(RefreshToken {
|
||||||
|
id: self.id,
|
||||||
|
token_id: self.token_id,
|
||||||
|
secret: Secret::generate()?,
|
||||||
|
expires_at: Utc::now() + lifetime,
|
||||||
|
issued_at: Utc::now(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<RefreshToken> for HashedRefreshToken {
|
||||||
|
fn from(value: RefreshToken) -> Self {
|
||||||
|
Self {
|
||||||
|
id: value.id,
|
||||||
|
token_id: value.token_id,
|
||||||
|
secret_hash: value.secret.hash(),
|
||||||
|
expires_at: value.expires_at,
|
||||||
|
issued_at: value.issued_at,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,12 +3,13 @@ use crate::{
|
|||||||
domain::{CentsError, ScheduleError, UserAppSecretError},
|
domain::{CentsError, ScheduleError, UserAppSecretError},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub mod access_tokens;
|
||||||
pub mod account_members;
|
pub mod account_members;
|
||||||
pub mod accounts;
|
pub mod accounts;
|
||||||
pub mod dtos;
|
pub mod dtos;
|
||||||
mod encoding;
|
mod encoding;
|
||||||
pub mod recurring_transactions;
|
pub mod recurring_transactions;
|
||||||
pub mod tokens;
|
pub mod refresh_tokens;
|
||||||
pub mod transactions;
|
pub mod transactions;
|
||||||
pub mod user_app_secrets;
|
pub mod user_app_secrets;
|
||||||
pub mod user_apps;
|
pub mod user_apps;
|
||||||
|
|||||||
@@ -0,0 +1,110 @@
|
|||||||
|
use super::{ReferenceRelation, ReferenceViolationKind, RepositoryError};
|
||||||
|
use crate::{
|
||||||
|
database::{self, Constraint, DatabaseError, dtos::AccessTokenRow},
|
||||||
|
domain::{HashedAccessToken, HashedSecret},
|
||||||
|
repository::dtos::Upsert,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub async fn get_by_hash<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
secret_hash: &HashedSecret,
|
||||||
|
) -> Result<Option<HashedAccessToken>, RepositoryError>
|
||||||
|
where
|
||||||
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
|
{
|
||||||
|
let token = database::access_tokens::get_by_hash(executor, secret_hash.hash())
|
||||||
|
.await?
|
||||||
|
.map(decode_access_token)
|
||||||
|
.transpose()?;
|
||||||
|
|
||||||
|
Ok(token)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_by_refresh_hash<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
secret_hash: &HashedSecret,
|
||||||
|
) -> Result<Option<HashedAccessToken>, RepositoryError>
|
||||||
|
where
|
||||||
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
|
{
|
||||||
|
let token = database::access_tokens::get_by_refresh_secret_hash(executor, secret_hash.hash())
|
||||||
|
.await?
|
||||||
|
.map(decode_access_token)
|
||||||
|
.transpose()?;
|
||||||
|
|
||||||
|
Ok(token)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn upsert<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
access_token: HashedAccessToken,
|
||||||
|
) -> Result<Option<Upsert<HashedAccessToken>>, RepositoryError>
|
||||||
|
where
|
||||||
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
|
{
|
||||||
|
let token = database::access_tokens::upsert(executor, encode_access_token(access_token))
|
||||||
|
.await
|
||||||
|
.map_err(classify_upsert_error)?
|
||||||
|
.map(|upserted| upserted.try_map(decode_access_token))
|
||||||
|
.transpose()?
|
||||||
|
.map(Into::into);
|
||||||
|
|
||||||
|
Ok(token)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete_by_hash<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
secret_hash: &HashedSecret,
|
||||||
|
) -> Result<Option<HashedAccessToken>, RepositoryError>
|
||||||
|
where
|
||||||
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
|
{
|
||||||
|
let token = database::access_tokens::delete_by_hash(executor, secret_hash.hash())
|
||||||
|
.await?
|
||||||
|
.map(decode_access_token)
|
||||||
|
.transpose()?;
|
||||||
|
|
||||||
|
Ok(token)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||||
|
let relation = match &error {
|
||||||
|
DatabaseError::ForeignKeyViolation {
|
||||||
|
constraint: Constraint::AccessTokenUser,
|
||||||
|
..
|
||||||
|
} => ReferenceRelation::User,
|
||||||
|
DatabaseError::ForeignKeyViolation {
|
||||||
|
constraint: Constraint::AccessTokenUserApp,
|
||||||
|
..
|
||||||
|
} => ReferenceRelation::UserApp,
|
||||||
|
_ => return RepositoryError::Database(error),
|
||||||
|
};
|
||||||
|
|
||||||
|
RepositoryError::ReferenceViolation {
|
||||||
|
relation,
|
||||||
|
kind: ReferenceViolationKind::Missing,
|
||||||
|
source: error,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decode_access_token(row: AccessTokenRow) -> Result<HashedAccessToken, RepositoryError> {
|
||||||
|
Ok(HashedAccessToken {
|
||||||
|
id: row.id,
|
||||||
|
user_id: row.user_id,
|
||||||
|
user_app_id: row.user_app_id,
|
||||||
|
secret_hash: HashedSecret::from(row.secret_hash),
|
||||||
|
expires_at: row.expires_at,
|
||||||
|
issued_at: row.issued_at,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn encode_access_token(token: HashedAccessToken) -> AccessTokenRow {
|
||||||
|
AccessTokenRow {
|
||||||
|
id: token.id,
|
||||||
|
user_id: token.user_id,
|
||||||
|
user_app_id: token.user_app_id,
|
||||||
|
secret_hash: token.secret_hash.hash().to_vec(),
|
||||||
|
expires_at: token.expires_at,
|
||||||
|
issued_at: token.issued_at,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -95,6 +95,24 @@ where
|
|||||||
Ok(decode(row)?)
|
Ok(decode(row)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn delete_by_id<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
user_id: Uuid,
|
||||||
|
account_id: Uuid,
|
||||||
|
id: Uuid,
|
||||||
|
) -> Result<Option<AccountMember>, RepositoryError>
|
||||||
|
where
|
||||||
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
|
{
|
||||||
|
let member = database::account_members::delete_by_id(executor, user_id, account_id, id)
|
||||||
|
.await
|
||||||
|
.map_err(classify_delete_error)?
|
||||||
|
.map(decode)
|
||||||
|
.transpose()?;
|
||||||
|
|
||||||
|
Ok(member)
|
||||||
|
}
|
||||||
|
|
||||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||||
match &error {
|
match &error {
|
||||||
DatabaseError::ForeignKeyViolation {
|
DatabaseError::ForeignKeyViolation {
|
||||||
@@ -124,24 +142,6 @@ fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete_by_id<'e, E>(
|
|
||||||
executor: E,
|
|
||||||
user_id: Uuid,
|
|
||||||
account_id: Uuid,
|
|
||||||
id: Uuid,
|
|
||||||
) -> Result<Option<AccountMember>, RepositoryError>
|
|
||||||
where
|
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
||||||
{
|
|
||||||
let member = database::account_members::delete_by_id(executor, user_id, account_id, id)
|
|
||||||
.await
|
|
||||||
.map_err(classify_delete_error)?
|
|
||||||
.map(decode)
|
|
||||||
.transpose()?;
|
|
||||||
|
|
||||||
Ok(member)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn classify_delete_error(error: DatabaseError) -> RepositoryError {
|
fn classify_delete_error(error: DatabaseError) -> RepositoryError {
|
||||||
match &error {
|
match &error {
|
||||||
DatabaseError::CheckViolation {
|
DatabaseError::CheckViolation {
|
||||||
|
|||||||
@@ -80,6 +80,22 @@ where
|
|||||||
Ok(upserted)
|
Ok(upserted)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn delete_by_id<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
user_id: Uuid,
|
||||||
|
id: Uuid,
|
||||||
|
) -> Result<Option<RecurringTransaction>, RepositoryError>
|
||||||
|
where
|
||||||
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
|
{
|
||||||
|
let transaction = database::recurring_transactions::delete_by_id(executor, user_id, id)
|
||||||
|
.await?
|
||||||
|
.map(decode)
|
||||||
|
.transpose()?;
|
||||||
|
|
||||||
|
Ok(transaction)
|
||||||
|
}
|
||||||
|
|
||||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||||
let relation = match &error {
|
let relation = match &error {
|
||||||
DatabaseError::ForeignKeyViolation {
|
DatabaseError::ForeignKeyViolation {
|
||||||
@@ -99,22 +115,6 @@ fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete_by_id<'e, E>(
|
|
||||||
executor: E,
|
|
||||||
user_id: Uuid,
|
|
||||||
id: Uuid,
|
|
||||||
) -> Result<Option<RecurringTransaction>, RepositoryError>
|
|
||||||
where
|
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
||||||
{
|
|
||||||
let transaction = database::recurring_transactions::delete_by_id(executor, user_id, id)
|
|
||||||
.await?
|
|
||||||
.map(decode)
|
|
||||||
.transpose()?;
|
|
||||||
|
|
||||||
Ok(transaction)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn decode(row: RecurringTransactionRow) -> Result<RecurringTransaction, DecodeError> {
|
fn decode(row: RecurringTransactionRow) -> Result<RecurringTransaction, DecodeError> {
|
||||||
let recurrence = Recurrence {
|
let recurrence = Recurrence {
|
||||||
every: RecurrenceInterval::try_from(row.recurrence_every)?,
|
every: RecurrenceInterval::try_from(row.recurrence_every)?,
|
||||||
|
|||||||
@@ -0,0 +1,89 @@
|
|||||||
|
use super::{ReferenceRelation, ReferenceViolationKind, RepositoryError};
|
||||||
|
use crate::{
|
||||||
|
database::{self, Constraint, DatabaseError, dtos::RefreshTokenRow},
|
||||||
|
domain::{HashedRefreshToken, HashedSecret},
|
||||||
|
repository::dtos::Upsert,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub async fn get_by_hash<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
secret_hash: &HashedSecret,
|
||||||
|
) -> Result<Option<HashedRefreshToken>, RepositoryError>
|
||||||
|
where
|
||||||
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
|
{
|
||||||
|
let token = database::refresh_tokens::get_by_hash(executor, secret_hash.hash())
|
||||||
|
.await?
|
||||||
|
.map(decode_refresh_token)
|
||||||
|
.transpose()?;
|
||||||
|
|
||||||
|
Ok(token)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn upsert<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
refresh_token: HashedRefreshToken,
|
||||||
|
) -> Result<Option<Upsert<HashedRefreshToken>>, RepositoryError>
|
||||||
|
where
|
||||||
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
|
{
|
||||||
|
let token = database::refresh_tokens::upsert(executor, encode_refresh_token(refresh_token))
|
||||||
|
.await
|
||||||
|
.map_err(classify_upsert_error)?
|
||||||
|
.map(|upserted| upserted.try_map(decode_refresh_token))
|
||||||
|
.transpose()?
|
||||||
|
.map(Into::into);
|
||||||
|
|
||||||
|
Ok(token)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete_by_hash<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
secret_hash: &HashedSecret,
|
||||||
|
) -> Result<Option<HashedRefreshToken>, RepositoryError>
|
||||||
|
where
|
||||||
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
|
{
|
||||||
|
let token = database::refresh_tokens::delete_by_hash(executor, secret_hash.hash())
|
||||||
|
.await?
|
||||||
|
.map(decode_refresh_token)
|
||||||
|
.transpose()?;
|
||||||
|
|
||||||
|
Ok(token)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||||
|
let relation = match &error {
|
||||||
|
DatabaseError::ForeignKeyViolation {
|
||||||
|
constraint: Constraint::RefreshTokenAccessToken,
|
||||||
|
..
|
||||||
|
} => ReferenceRelation::AccessToken,
|
||||||
|
_ => return RepositoryError::Database(error),
|
||||||
|
};
|
||||||
|
|
||||||
|
RepositoryError::ReferenceViolation {
|
||||||
|
relation,
|
||||||
|
kind: ReferenceViolationKind::Missing,
|
||||||
|
source: error,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decode_refresh_token(row: RefreshTokenRow) -> Result<HashedRefreshToken, RepositoryError> {
|
||||||
|
Ok(HashedRefreshToken {
|
||||||
|
id: row.id,
|
||||||
|
token_id: row.access_token_id,
|
||||||
|
secret_hash: HashedSecret::from(row.secret_hash),
|
||||||
|
expires_at: row.expires_at,
|
||||||
|
issued_at: row.issued_at,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn encode_refresh_token(token: HashedRefreshToken) -> RefreshTokenRow {
|
||||||
|
RefreshTokenRow {
|
||||||
|
id: token.id,
|
||||||
|
access_token_id: token.token_id,
|
||||||
|
secret_hash: token.secret_hash.hash().to_vec(),
|
||||||
|
expires_at: token.expires_at,
|
||||||
|
issued_at: token.issued_at,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,193 +0,0 @@
|
|||||||
use super::{ReferenceRelation, ReferenceViolationKind, RepositoryError};
|
|
||||||
use crate::{
|
|
||||||
database::{
|
|
||||||
self, Constraint, DatabaseError,
|
|
||||||
dtos::{AccessTokenRow, RefreshTokenRow},
|
|
||||||
},
|
|
||||||
domain::{HashedAccessToken, HashedRefreshToken, HashedSecret},
|
|
||||||
repository::dtos::Upsert,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[tracing::instrument(skip(executor, secret_hash))]
|
|
||||||
pub async fn get_access_token_by_hash<'e, E>(
|
|
||||||
executor: E,
|
|
||||||
secret_hash: &HashedSecret,
|
|
||||||
) -> Result<Option<HashedAccessToken>, RepositoryError>
|
|
||||||
where
|
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
||||||
{
|
|
||||||
let token = database::tokens::get_access_token_by_hash(executor, secret_hash.hash())
|
|
||||||
.await?
|
|
||||||
.map(decode_access_token)
|
|
||||||
.transpose()?;
|
|
||||||
|
|
||||||
Ok(token)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tracing::instrument(skip(executor, secret_hash))]
|
|
||||||
pub async fn get_access_token_by_refresh_hash<'e, E>(
|
|
||||||
executor: E,
|
|
||||||
secret_hash: &HashedSecret,
|
|
||||||
) -> Result<Option<HashedAccessToken>, RepositoryError>
|
|
||||||
where
|
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
||||||
{
|
|
||||||
let token =
|
|
||||||
database::tokens::get_access_token_by_refresh_secret_hash(executor, secret_hash.hash())
|
|
||||||
.await?
|
|
||||||
.map(decode_access_token)
|
|
||||||
.transpose()?;
|
|
||||||
|
|
||||||
Ok(token)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tracing::instrument(skip(executor, access_token))]
|
|
||||||
pub async fn upsert_access_token<'e, E>(
|
|
||||||
executor: E,
|
|
||||||
access_token: HashedAccessToken,
|
|
||||||
) -> Result<Option<Upsert<HashedAccessToken>>, RepositoryError>
|
|
||||||
where
|
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
||||||
{
|
|
||||||
let token = database::tokens::upsert_access_token(executor, encode_access_token(access_token))
|
|
||||||
.await
|
|
||||||
.map_err(classify_upsert_error)?
|
|
||||||
.map(|upserted| upserted.try_map(decode_access_token))
|
|
||||||
.transpose()?
|
|
||||||
.map(Into::into);
|
|
||||||
|
|
||||||
Ok(token)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tracing::instrument(skip(executor, secret_hash))]
|
|
||||||
pub async fn delete_access_token_by_hash<'e, E>(
|
|
||||||
executor: E,
|
|
||||||
secret_hash: &HashedSecret,
|
|
||||||
) -> Result<Option<HashedAccessToken>, RepositoryError>
|
|
||||||
where
|
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
||||||
{
|
|
||||||
let token = database::tokens::delete_access_token_by_hash(executor, secret_hash.hash())
|
|
||||||
.await?
|
|
||||||
.map(decode_access_token)
|
|
||||||
.transpose()?;
|
|
||||||
|
|
||||||
Ok(token)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tracing::instrument(skip(executor, secret_hash))]
|
|
||||||
pub async fn get_refresh_token_by_hash<'e, E>(
|
|
||||||
executor: E,
|
|
||||||
secret_hash: &HashedSecret,
|
|
||||||
) -> Result<Option<HashedRefreshToken>, RepositoryError>
|
|
||||||
where
|
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
||||||
{
|
|
||||||
let token = database::tokens::get_refresh_token_by_hash(executor, secret_hash.hash())
|
|
||||||
.await?
|
|
||||||
.map(decode_refresh_token)
|
|
||||||
.transpose()?;
|
|
||||||
|
|
||||||
Ok(token)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tracing::instrument(skip(executor, refresh_token))]
|
|
||||||
pub async fn upsert_refresh_token<'e, E>(
|
|
||||||
executor: E,
|
|
||||||
refresh_token: HashedRefreshToken,
|
|
||||||
) -> Result<Option<Upsert<HashedRefreshToken>>, RepositoryError>
|
|
||||||
where
|
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
||||||
{
|
|
||||||
let token =
|
|
||||||
database::tokens::upsert_refresh_token(executor, encode_refresh_token(refresh_token))
|
|
||||||
.await
|
|
||||||
.map_err(classify_upsert_error)?
|
|
||||||
.map(|upserted| upserted.try_map(decode_refresh_token))
|
|
||||||
.transpose()?
|
|
||||||
.map(Into::into);
|
|
||||||
|
|
||||||
Ok(token)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tracing::instrument(skip(executor, secret_hash))]
|
|
||||||
pub async fn delete_refresh_token_by_hash<'e, E>(
|
|
||||||
executor: E,
|
|
||||||
secret_hash: &HashedSecret,
|
|
||||||
) -> Result<Option<HashedRefreshToken>, RepositoryError>
|
|
||||||
where
|
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
||||||
{
|
|
||||||
let token = database::tokens::delete_refresh_token_by_hash(executor, secret_hash.hash())
|
|
||||||
.await?
|
|
||||||
.map(decode_refresh_token)
|
|
||||||
.transpose()?;
|
|
||||||
|
|
||||||
Ok(token)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
|
||||||
let relation = match &error {
|
|
||||||
DatabaseError::ForeignKeyViolation {
|
|
||||||
constraint: Constraint::AccessTokenUser,
|
|
||||||
..
|
|
||||||
} => ReferenceRelation::User,
|
|
||||||
DatabaseError::ForeignKeyViolation {
|
|
||||||
constraint: Constraint::AccessTokenUserApp,
|
|
||||||
..
|
|
||||||
} => ReferenceRelation::UserApp,
|
|
||||||
DatabaseError::ForeignKeyViolation {
|
|
||||||
constraint: Constraint::RefreshTokenAccessToken,
|
|
||||||
..
|
|
||||||
} => ReferenceRelation::AccessToken,
|
|
||||||
_ => return RepositoryError::Database(error),
|
|
||||||
};
|
|
||||||
|
|
||||||
RepositoryError::ReferenceViolation {
|
|
||||||
relation,
|
|
||||||
kind: ReferenceViolationKind::Missing,
|
|
||||||
source: error,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn decode_access_token(row: AccessTokenRow) -> Result<HashedAccessToken, RepositoryError> {
|
|
||||||
Ok(HashedAccessToken {
|
|
||||||
id: row.id,
|
|
||||||
user_id: row.user_id,
|
|
||||||
user_app_id: row.user_app_id,
|
|
||||||
secret_hash: HashedSecret::from(row.secret_hash),
|
|
||||||
expires_at: row.expires_at,
|
|
||||||
issued_at: row.issued_at,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn encode_access_token(token: HashedAccessToken) -> AccessTokenRow {
|
|
||||||
AccessTokenRow {
|
|
||||||
id: token.id,
|
|
||||||
user_id: token.user_id,
|
|
||||||
user_app_id: token.user_app_id,
|
|
||||||
secret_hash: token.secret_hash.hash().to_vec(),
|
|
||||||
expires_at: token.expires_at,
|
|
||||||
issued_at: token.issued_at,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn decode_refresh_token(row: RefreshTokenRow) -> Result<HashedRefreshToken, RepositoryError> {
|
|
||||||
Ok(HashedRefreshToken {
|
|
||||||
id: row.id,
|
|
||||||
token_id: row.access_token_id,
|
|
||||||
secret_hash: HashedSecret::from(row.secret_hash),
|
|
||||||
expires_at: row.expires_at,
|
|
||||||
issued_at: row.issued_at,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn encode_refresh_token(token: HashedRefreshToken) -> RefreshTokenRow {
|
|
||||||
RefreshTokenRow {
|
|
||||||
id: token.id,
|
|
||||||
access_token_id: token.token_id,
|
|
||||||
secret_hash: token.secret_hash.hash().to_vec(),
|
|
||||||
expires_at: token.expires_at,
|
|
||||||
issued_at: token.issued_at,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -77,20 +77,6 @@ where
|
|||||||
Ok(upserted)
|
Ok(upserted)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
|
||||||
match &error {
|
|
||||||
DatabaseError::ForeignKeyViolation {
|
|
||||||
constraint: Constraint::TransactionAccount,
|
|
||||||
..
|
|
||||||
} => RepositoryError::ReferenceViolation {
|
|
||||||
relation: ReferenceRelation::Account,
|
|
||||||
kind: ReferenceViolationKind::Missing,
|
|
||||||
source: error,
|
|
||||||
},
|
|
||||||
_ => RepositoryError::Database(error),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn delete_by_id<'e, E>(
|
pub async fn delete_by_id<'e, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
user_id: Uuid,
|
user_id: Uuid,
|
||||||
@@ -107,6 +93,20 @@ where
|
|||||||
Ok(transaction)
|
Ok(transaction)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||||
|
match &error {
|
||||||
|
DatabaseError::ForeignKeyViolation {
|
||||||
|
constraint: Constraint::TransactionAccount,
|
||||||
|
..
|
||||||
|
} => RepositoryError::ReferenceViolation {
|
||||||
|
relation: ReferenceRelation::Account,
|
||||||
|
kind: ReferenceViolationKind::Missing,
|
||||||
|
source: error,
|
||||||
|
},
|
||||||
|
_ => RepositoryError::Database(error),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn decode(row: TransactionRow) -> Result<Transaction, DecodeError> {
|
fn decode(row: TransactionRow) -> Result<Transaction, DecodeError> {
|
||||||
Ok(Transaction {
|
Ok(Transaction {
|
||||||
id: row.id,
|
id: row.id,
|
||||||
|
|||||||
@@ -78,20 +78,6 @@ where
|
|||||||
Ok(secret)
|
Ok(secret)
|
||||||
}
|
}
|
||||||
|
|
||||||
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>(
|
pub async fn verify<'e, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
app_id: Uuid,
|
app_id: Uuid,
|
||||||
@@ -122,6 +108,20 @@ where
|
|||||||
Ok(secret)
|
Ok(secret)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn decode(row: UserAppSecretRow) -> Result<UserAppSecret, DecodeError> {
|
fn decode(row: UserAppSecretRow) -> Result<UserAppSecret, DecodeError> {
|
||||||
Ok(UserAppSecret::new(
|
Ok(UserAppSecret::new(
|
||||||
row.id,
|
row.id,
|
||||||
|
|||||||
@@ -88,20 +88,6 @@ where
|
|||||||
Ok(app)
|
Ok(app)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
|
||||||
match &error {
|
|
||||||
DatabaseError::ForeignKeyViolation {
|
|
||||||
constraint: Constraint::UserAppUser,
|
|
||||||
..
|
|
||||||
} => RepositoryError::ReferenceViolation {
|
|
||||||
relation: super::ReferenceRelation::User,
|
|
||||||
kind: super::ReferenceViolationKind::Missing,
|
|
||||||
source: error,
|
|
||||||
},
|
|
||||||
_ => RepositoryError::Database(error),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn delete_by_id<'e, E>(
|
pub async fn delete_by_id<'e, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
user_id: Uuid,
|
user_id: Uuid,
|
||||||
@@ -118,6 +104,20 @@ where
|
|||||||
Ok(app)
|
Ok(app)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||||
|
match &error {
|
||||||
|
DatabaseError::ForeignKeyViolation {
|
||||||
|
constraint: Constraint::UserAppUser,
|
||||||
|
..
|
||||||
|
} => RepositoryError::ReferenceViolation {
|
||||||
|
relation: super::ReferenceRelation::User,
|
||||||
|
kind: super::ReferenceViolationKind::Missing,
|
||||||
|
source: error,
|
||||||
|
},
|
||||||
|
_ => RepositoryError::Database(error),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn decode(row: UserAppRow) -> Result<UserApp, DecodeError> {
|
fn decode(row: UserAppRow) -> Result<UserApp, DecodeError> {
|
||||||
Ok(UserApp {
|
Ok(UserApp {
|
||||||
id: row.id,
|
id: row.id,
|
||||||
|
|||||||
@@ -44,26 +44,26 @@ async fn issue_token(
|
|||||||
token_config.access_token_lifetime,
|
token_config.access_token_lifetime,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
repository::tokens::upsert_access_token(&pool, access_token.clone().into()).await?;
|
repository::access_tokens::upsert(&pool, access_token.clone().into()).await?;
|
||||||
|
|
||||||
Ok(Json(TokenResponse::new(access_token, None)))
|
Ok(Json(TokenResponse::new(access_token, None)))
|
||||||
}
|
}
|
||||||
TokenRequest::RefreshToken { refresh_token } => {
|
TokenRequest::RefreshToken { refresh_token } => {
|
||||||
let token_hash = Secret::from(refresh_token).hash();
|
let token_hash = Secret::from(refresh_token).hash();
|
||||||
let (Some(access_token), Some(refresh_token)) = (
|
let (Some(access_token), Some(refresh_token)) = (
|
||||||
repository::tokens::get_access_token_by_refresh_hash(&pool, &token_hash).await?,
|
repository::access_tokens::get_by_refresh_hash(&pool, &token_hash).await?,
|
||||||
repository::tokens::get_refresh_token_by_hash(&pool, &token_hash).await?,
|
repository::refresh_tokens::get_by_hash(&pool, &token_hash).await?,
|
||||||
) else {
|
) else {
|
||||||
return Err(AppError::InvalidRefreshToken);
|
return Err(AppError::InvalidRefreshToken);
|
||||||
};
|
};
|
||||||
|
|
||||||
let new_access = access_token.refresh(token_config.access_token_lifetime)?;
|
let access_token = access_token.refresh(token_config.access_token_lifetime)?;
|
||||||
repository::tokens::upsert_access_token(&pool, new_access.clone().into()).await?;
|
repository::access_tokens::upsert(&pool, access_token.clone().into()).await?;
|
||||||
|
|
||||||
let new_refresh = refresh_token.refresh(token_config.refresh_token_lifetime)?;
|
let refresh_token = refresh_token.refresh(token_config.refresh_token_lifetime)?;
|
||||||
repository::tokens::upsert_refresh_token(&pool, new_refresh.clone().into()).await?;
|
repository::refresh_tokens::upsert(&pool, refresh_token.clone().into()).await?;
|
||||||
|
|
||||||
Ok(Json(TokenResponse::new(new_access, Some(new_refresh))))
|
Ok(Json(TokenResponse::new(access_token, Some(refresh_token))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -76,10 +76,10 @@ async fn revoke_token(
|
|||||||
) -> Result<(), AppError> {
|
) -> Result<(), AppError> {
|
||||||
let token_hash = Secret::from(request.token).hash();
|
let token_hash = Secret::from(request.token).hash();
|
||||||
|
|
||||||
repository::tokens::delete_access_token_by_hash(&pool, &token_hash)
|
repository::access_tokens::delete_by_hash(&pool, &token_hash)
|
||||||
.await
|
.await
|
||||||
.ok();
|
.ok();
|
||||||
repository::tokens::delete_refresh_token_by_hash(&pool, &token_hash)
|
repository::refresh_tokens::delete_by_hash(&pool, &token_hash)
|
||||||
.await
|
.await
|
||||||
.ok();
|
.ok();
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ where
|
|||||||
let pool = PgPool::from_ref(state);
|
let pool = PgPool::from_ref(state);
|
||||||
|
|
||||||
let token_hash = Secret::from(credentials.0).hash();
|
let token_hash = Secret::from(credentials.0).hash();
|
||||||
let token = repository::tokens::get_access_token_by_hash(&pool, &token_hash)
|
let token = repository::access_tokens::get_by_hash(&pool, &token_hash)
|
||||||
.await
|
.await
|
||||||
.map_err(BearerError::Repository)?
|
.map_err(BearerError::Repository)?
|
||||||
.ok_or(BearerError::InvalidCredentials)?;
|
.ok_or(BearerError::InvalidCredentials)?;
|
||||||
|
|||||||
Reference in New Issue
Block a user