More verbose, but also more consistent.
This commit is contained in:
+120
@@ -0,0 +1,120 @@
|
|||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "\n INSERT INTO refresh_tokens (id, access_token_id, secret_hash, expires_at, issued_at)\n VALUES ($1, $2, $3, $4, $5)\n ON CONFLICT (id) DO UPDATE SET\n secret_hash = refresh_tokens.secret_hash,\n expires_at = refresh_tokens.expires_at,\n issued_at = refresh_tokens.issued_at\n WHERE refresh_tokens.access_token_id = EXCLUDED.access_token_id\n AND refresh_tokens.secret_hash <> EXCLUDED.secret_hash\n RETURNING WITH (OLD AS old_row, NEW AS new_row)\n old_row.id AS \"old_id?\",\n COALESCE(old_row.access_token_id, new_row.access_token_id) AS \"old_access_token_id!\",\n COALESCE(old_row.secret_hash, new_row.secret_hash) AS \"old_secret_hash!\",\n COALESCE(old_row.expires_at, new_row.expires_at) AS \"old_expires_at!\",\n COALESCE(old_row.issued_at, new_row.issued_at) AS \"old_issued_at!\",\n new_row.id AS \"id!\",\n new_row.access_token_id AS \"access_token_id!\",\n new_row.secret_hash AS \"secret_hash!\",\n new_row.expires_at AS \"expires_at!\",\n new_row.issued_at AS \"issued_at!\"\n ",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "old_id?",
|
||||||
|
"type_info": "Uuid",
|
||||||
|
"origin": {
|
||||||
|
"Table": {
|
||||||
|
"table": "refresh_tokens",
|
||||||
|
"name": "id"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 1,
|
||||||
|
"name": "old_access_token_id!",
|
||||||
|
"type_info": "Uuid",
|
||||||
|
"origin": "Expression"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 2,
|
||||||
|
"name": "old_secret_hash!",
|
||||||
|
"type_info": "Bytea",
|
||||||
|
"origin": "Expression"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 3,
|
||||||
|
"name": "old_expires_at!",
|
||||||
|
"type_info": "Timestamptz",
|
||||||
|
"origin": "Expression"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 4,
|
||||||
|
"name": "old_issued_at!",
|
||||||
|
"type_info": "Timestamptz",
|
||||||
|
"origin": "Expression"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 5,
|
||||||
|
"name": "id!",
|
||||||
|
"type_info": "Uuid",
|
||||||
|
"origin": {
|
||||||
|
"Table": {
|
||||||
|
"table": "refresh_tokens",
|
||||||
|
"name": "id"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 6,
|
||||||
|
"name": "access_token_id!",
|
||||||
|
"type_info": "Uuid",
|
||||||
|
"origin": {
|
||||||
|
"Table": {
|
||||||
|
"table": "refresh_tokens",
|
||||||
|
"name": "access_token_id"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 7,
|
||||||
|
"name": "secret_hash!",
|
||||||
|
"type_info": "Bytea",
|
||||||
|
"origin": {
|
||||||
|
"Table": {
|
||||||
|
"table": "refresh_tokens",
|
||||||
|
"name": "secret_hash"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 8,
|
||||||
|
"name": "expires_at!",
|
||||||
|
"type_info": "Timestamptz",
|
||||||
|
"origin": {
|
||||||
|
"Table": {
|
||||||
|
"table": "refresh_tokens",
|
||||||
|
"name": "expires_at"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 9,
|
||||||
|
"name": "issued_at!",
|
||||||
|
"type_info": "Timestamptz",
|
||||||
|
"origin": {
|
||||||
|
"Table": {
|
||||||
|
"table": "refresh_tokens",
|
||||||
|
"name": "issued_at"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid",
|
||||||
|
"Uuid",
|
||||||
|
"Bytea",
|
||||||
|
"Timestamptz",
|
||||||
|
"Timestamptz"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "5320efb1916efecdeb9f0b5d199d7e7d13d6df53399de9c062d0fab3ea17d224"
|
||||||
|
}
|
||||||
+145
@@ -0,0 +1,145 @@
|
|||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "\n INSERT INTO access_tokens (id, user_id, user_app_id, secret_hash, expires_at, issued_at)\n VALUES ($1, $2, $3, $4, $5, $6)\n ON CONFLICT (id) DO UPDATE SET\n secret_hash = access_tokens.secret_hash,\n expires_at = access_tokens.expires_at,\n issued_at = access_tokens.issued_at\n WHERE access_tokens.user_id = EXCLUDED.user_id\n AND access_tokens.user_app_id IS NOT DISTINCT FROM EXCLUDED.user_app_id\n AND access_tokens.secret_hash <> EXCLUDED.secret_hash\n RETURNING WITH (OLD AS old_row, NEW AS new_row)\n old_row.id AS \"old_id?\",\n COALESCE(old_row.user_id, new_row.user_id) AS \"old_user_id!\",\n old_row.user_app_id AS \"old_user_app_id?\",\n COALESCE(old_row.secret_hash, new_row.secret_hash) AS \"old_secret_hash!\",\n COALESCE(old_row.expires_at, new_row.expires_at) AS \"old_expires_at!\",\n COALESCE(old_row.issued_at, new_row.issued_at) AS \"old_issued_at!\",\n new_row.id AS \"id!\",\n new_row.user_id AS \"user_id!\",\n new_row.user_app_id AS \"user_app_id?\",\n new_row.secret_hash AS \"secret_hash!\",\n new_row.expires_at AS \"expires_at!\",\n new_row.issued_at AS \"issued_at!\"\n ",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "old_id?",
|
||||||
|
"type_info": "Uuid",
|
||||||
|
"origin": {
|
||||||
|
"Table": {
|
||||||
|
"table": "access_tokens",
|
||||||
|
"name": "id"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 1,
|
||||||
|
"name": "old_user_id!",
|
||||||
|
"type_info": "Uuid",
|
||||||
|
"origin": "Expression"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 2,
|
||||||
|
"name": "old_user_app_id?",
|
||||||
|
"type_info": "Uuid",
|
||||||
|
"origin": {
|
||||||
|
"Table": {
|
||||||
|
"table": "access_tokens",
|
||||||
|
"name": "user_app_id"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 3,
|
||||||
|
"name": "old_secret_hash!",
|
||||||
|
"type_info": "Bytea",
|
||||||
|
"origin": "Expression"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 4,
|
||||||
|
"name": "old_expires_at!",
|
||||||
|
"type_info": "Timestamptz",
|
||||||
|
"origin": "Expression"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 5,
|
||||||
|
"name": "old_issued_at!",
|
||||||
|
"type_info": "Timestamptz",
|
||||||
|
"origin": "Expression"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 6,
|
||||||
|
"name": "id!",
|
||||||
|
"type_info": "Uuid",
|
||||||
|
"origin": {
|
||||||
|
"Table": {
|
||||||
|
"table": "access_tokens",
|
||||||
|
"name": "id"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 7,
|
||||||
|
"name": "user_id!",
|
||||||
|
"type_info": "Uuid",
|
||||||
|
"origin": {
|
||||||
|
"Table": {
|
||||||
|
"table": "access_tokens",
|
||||||
|
"name": "user_id"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 8,
|
||||||
|
"name": "user_app_id?",
|
||||||
|
"type_info": "Uuid",
|
||||||
|
"origin": {
|
||||||
|
"Table": {
|
||||||
|
"table": "access_tokens",
|
||||||
|
"name": "user_app_id"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 9,
|
||||||
|
"name": "secret_hash!",
|
||||||
|
"type_info": "Bytea",
|
||||||
|
"origin": {
|
||||||
|
"Table": {
|
||||||
|
"table": "access_tokens",
|
||||||
|
"name": "secret_hash"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 10,
|
||||||
|
"name": "expires_at!",
|
||||||
|
"type_info": "Timestamptz",
|
||||||
|
"origin": {
|
||||||
|
"Table": {
|
||||||
|
"table": "access_tokens",
|
||||||
|
"name": "expires_at"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 11,
|
||||||
|
"name": "issued_at!",
|
||||||
|
"type_info": "Timestamptz",
|
||||||
|
"origin": {
|
||||||
|
"Table": {
|
||||||
|
"table": "access_tokens",
|
||||||
|
"name": "issued_at"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid",
|
||||||
|
"Uuid",
|
||||||
|
"Uuid",
|
||||||
|
"Bytea",
|
||||||
|
"Timestamptz",
|
||||||
|
"Timestamptz"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
null,
|
||||||
|
true,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "952c2e15628f93a5d08cd2ebd0510443d21644fdbc878069eda6959677b220ec"
|
||||||
|
}
|
||||||
-80
@@ -1,80 +0,0 @@
|
|||||||
{
|
|
||||||
"db_name": "PostgreSQL",
|
|
||||||
"query": "\n\t\tINSERT INTO refresh_tokens (id, access_token_id, secret_hash, expires_at, issued_at)\n\t\tVALUES ($1, $2, $3, $4, $5)\n\t\tRETURNING id, access_token_id, secret_hash, expires_at, issued_at\n\t\t",
|
|
||||||
"describe": {
|
|
||||||
"columns": [
|
|
||||||
{
|
|
||||||
"ordinal": 0,
|
|
||||||
"name": "id",
|
|
||||||
"type_info": "Uuid",
|
|
||||||
"origin": {
|
|
||||||
"Table": {
|
|
||||||
"table": "refresh_tokens",
|
|
||||||
"name": "id"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"ordinal": 1,
|
|
||||||
"name": "access_token_id",
|
|
||||||
"type_info": "Uuid",
|
|
||||||
"origin": {
|
|
||||||
"Table": {
|
|
||||||
"table": "refresh_tokens",
|
|
||||||
"name": "access_token_id"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"ordinal": 2,
|
|
||||||
"name": "secret_hash",
|
|
||||||
"type_info": "Bytea",
|
|
||||||
"origin": {
|
|
||||||
"Table": {
|
|
||||||
"table": "refresh_tokens",
|
|
||||||
"name": "secret_hash"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"ordinal": 3,
|
|
||||||
"name": "expires_at",
|
|
||||||
"type_info": "Timestamptz",
|
|
||||||
"origin": {
|
|
||||||
"Table": {
|
|
||||||
"table": "refresh_tokens",
|
|
||||||
"name": "expires_at"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"ordinal": 4,
|
|
||||||
"name": "issued_at",
|
|
||||||
"type_info": "Timestamptz",
|
|
||||||
"origin": {
|
|
||||||
"Table": {
|
|
||||||
"table": "refresh_tokens",
|
|
||||||
"name": "issued_at"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"parameters": {
|
|
||||||
"Left": [
|
|
||||||
"Uuid",
|
|
||||||
"Uuid",
|
|
||||||
"Bytea",
|
|
||||||
"Timestamptz",
|
|
||||||
"Timestamptz"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"nullable": [
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
false
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"hash": "a1d81c17935a785a1001ee0673e1bbb02a64b5743406428dd18f8199ad8fb77b"
|
|
||||||
}
|
|
||||||
+112
-98
@@ -1,4 +1,4 @@
|
|||||||
use chrono::{DateTime, Utc};
|
use crate::database::dtos::Upsert;
|
||||||
|
|
||||||
use super::{DatabaseError, dtos::AccessTokenRow, dtos::RefreshTokenRow};
|
use super::{DatabaseError, dtos::AccessTokenRow, dtos::RefreshTokenRow};
|
||||||
|
|
||||||
@@ -63,19 +63,38 @@ where
|
|||||||
Ok(row)
|
Ok(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn insert_access_token<'e, E>(
|
pub async fn upsert_access_token<'e, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
access_token: AccessTokenRow,
|
access_token: AccessTokenRow,
|
||||||
) -> Result<AccessTokenRow, DatabaseError>
|
) -> Result<Option<Upsert<AccessTokenRow>>, DatabaseError>
|
||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
let row = sqlx::query!(
|
let row = sqlx::query!(
|
||||||
r#"
|
r#"
|
||||||
INSERT INTO access_tokens (id, user_id, user_app_id, secret_hash, expires_at, issued_at)
|
INSERT INTO access_tokens (id, user_id, user_app_id, secret_hash, expires_at, issued_at)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6)
|
VALUES ($1, $2, $3, $4, $5, $6)
|
||||||
RETURNING id, user_id, user_app_id, secret_hash, expires_at, issued_at
|
ON CONFLICT (id) DO UPDATE SET
|
||||||
"#,
|
secret_hash = access_tokens.secret_hash,
|
||||||
|
expires_at = access_tokens.expires_at,
|
||||||
|
issued_at = access_tokens.issued_at
|
||||||
|
WHERE access_tokens.user_id = EXCLUDED.user_id
|
||||||
|
AND access_tokens.user_app_id IS NOT DISTINCT FROM EXCLUDED.user_app_id
|
||||||
|
AND access_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.user_id, new_row.user_id) AS "old_user_id!",
|
||||||
|
old_row.user_app_id AS "old_user_app_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.user_id AS "user_id!",
|
||||||
|
new_row.user_app_id AS "user_app_id?",
|
||||||
|
new_row.secret_hash AS "secret_hash!",
|
||||||
|
new_row.expires_at AS "expires_at!",
|
||||||
|
new_row.issued_at AS "issued_at!"
|
||||||
|
"#,
|
||||||
access_token.id,
|
access_token.id,
|
||||||
access_token.user_id,
|
access_token.user_id,
|
||||||
access_token.user_app_id,
|
access_token.user_app_id,
|
||||||
@@ -83,50 +102,40 @@ where
|
|||||||
access_token.expires_at,
|
access_token.expires_at,
|
||||||
access_token.issued_at,
|
access_token.issued_at,
|
||||||
)
|
)
|
||||||
.fetch_one(executor)
|
|
||||||
.await
|
|
||||||
.map_err(DatabaseError::from)?;
|
|
||||||
|
|
||||||
Ok(AccessTokenRow {
|
|
||||||
id: row.id,
|
|
||||||
user_id: row.user_id,
|
|
||||||
user_app_id: row.user_app_id,
|
|
||||||
secret_hash: row.secret_hash,
|
|
||||||
expires_at: row.expires_at,
|
|
||||||
issued_at: row.issued_at,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn update_access_token_by_hash<'e, E>(
|
|
||||||
executor: E,
|
|
||||||
secret_hash: &[u8],
|
|
||||||
new_secret_hash: &[u8],
|
|
||||||
new_expires_at: DateTime<Utc>,
|
|
||||||
new_issued_at: DateTime<Utc>,
|
|
||||||
) -> Result<Option<AccessTokenRow>, DatabaseError>
|
|
||||||
where
|
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
||||||
{
|
|
||||||
let row = sqlx::query_as!(
|
|
||||||
AccessTokenRow,
|
|
||||||
r#"
|
|
||||||
UPDATE access_tokens
|
|
||||||
SET secret_hash = $2,
|
|
||||||
expires_at = $3,
|
|
||||||
issued_at = $4
|
|
||||||
WHERE secret_hash = $1
|
|
||||||
RETURNING id, user_id, user_app_id, secret_hash, expires_at, issued_at
|
|
||||||
"#,
|
|
||||||
secret_hash,
|
|
||||||
new_secret_hash,
|
|
||||||
new_expires_at,
|
|
||||||
new_issued_at,
|
|
||||||
)
|
|
||||||
.fetch_optional(executor)
|
.fetch_optional(executor)
|
||||||
.await
|
.await
|
||||||
.map_err(DatabaseError::from)?;
|
.map_err(DatabaseError::from)?;
|
||||||
|
|
||||||
Ok(row)
|
// Nothing is written when the access token already exists, but it's new user ID and user app ID
|
||||||
|
// are 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 user ID and user app 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| AccessTokenRow {
|
||||||
|
id,
|
||||||
|
user_id: row.old_user_id,
|
||||||
|
user_app_id: row.old_user_app_id,
|
||||||
|
secret_hash: row.old_secret_hash,
|
||||||
|
expires_at: row.old_expires_at,
|
||||||
|
issued_at: row.old_issued_at,
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(Some(Upsert {
|
||||||
|
previous,
|
||||||
|
current: AccessTokenRow {
|
||||||
|
id: row.id,
|
||||||
|
user_id: row.user_id,
|
||||||
|
user_app_id: row.user_app_id,
|
||||||
|
secret_hash: row.secret_hash,
|
||||||
|
expires_at: row.expires_at,
|
||||||
|
issued_at: row.issued_at,
|
||||||
|
},
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete_access_token_by_hash<'e, E>(
|
pub async fn delete_access_token_by_hash<'e, E>(
|
||||||
@@ -181,68 +190,73 @@ where
|
|||||||
Ok(row)
|
Ok(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub async fn insert_refresh_token<'e, E>(
|
pub async fn upsert_refresh_token<'e, E>(
|
||||||
// executor: E,
|
|
||||||
// refresh_token: RefreshTokenRow,
|
|
||||||
// ) -> Result<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)
|
|
||||||
// RETURNING id, access_token_id, secret_hash, expires_at, issued_at
|
|
||||||
// "#,
|
|
||||||
// refresh_token.id,
|
|
||||||
// refresh_token.access_token_id,
|
|
||||||
// refresh_token.secret_hash,
|
|
||||||
// refresh_token.expires_at,
|
|
||||||
// refresh_token.issued_at,
|
|
||||||
// )
|
|
||||||
// .fetch_one(executor)
|
|
||||||
// .await
|
|
||||||
// .map_err(DatabaseError::from)?;
|
|
||||||
|
|
||||||
// Ok(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 update_refresh_token_by_hash<'e, E>(
|
|
||||||
executor: E,
|
executor: E,
|
||||||
secret_hash: &[u8],
|
refresh_token: RefreshTokenRow,
|
||||||
new_secret_hash: &[u8],
|
) -> Result<Option<Upsert<RefreshTokenRow>>, DatabaseError>
|
||||||
new_expires_at: DateTime<Utc>,
|
|
||||||
new_issued_at: DateTime<Utc>,
|
|
||||||
) -> Result<Option<RefreshTokenRow>, DatabaseError>
|
|
||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
let row = sqlx::query_as!(
|
let row = sqlx::query!(
|
||||||
RefreshTokenRow,
|
|
||||||
r#"
|
r#"
|
||||||
UPDATE refresh_tokens
|
INSERT INTO refresh_tokens (id, access_token_id, secret_hash, expires_at, issued_at)
|
||||||
SET secret_hash = $2,
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
expires_at = $3,
|
ON CONFLICT (id) DO UPDATE SET
|
||||||
issued_at = $4
|
secret_hash = refresh_tokens.secret_hash,
|
||||||
WHERE secret_hash = $1
|
expires_at = refresh_tokens.expires_at,
|
||||||
RETURNING id, access_token_id, secret_hash, expires_at, issued_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!"
|
||||||
"#,
|
"#,
|
||||||
secret_hash,
|
refresh_token.id,
|
||||||
new_secret_hash,
|
refresh_token.access_token_id,
|
||||||
new_expires_at,
|
refresh_token.secret_hash,
|
||||||
new_issued_at,
|
refresh_token.expires_at,
|
||||||
|
refresh_token.issued_at,
|
||||||
)
|
)
|
||||||
.fetch_optional(executor)
|
.fetch_optional(executor)
|
||||||
.await
|
.await
|
||||||
.map_err(DatabaseError::from)?;
|
.map_err(DatabaseError::from)?;
|
||||||
|
|
||||||
Ok(row)
|
// 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>(
|
pub async fn delete_refresh_token_by_hash<'e, E>(
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
use chrono::{DateTime, Utc};
|
|
||||||
|
|
||||||
use super::{ReferenceRelation, ReferenceViolationKind, RepositoryError};
|
use super::{ReferenceRelation, ReferenceViolationKind, RepositoryError};
|
||||||
use crate::{
|
use crate::{
|
||||||
database::{
|
database::{
|
||||||
@@ -7,6 +5,7 @@ use crate::{
|
|||||||
dtos::{AccessTokenRow, RefreshTokenRow},
|
dtos::{AccessTokenRow, RefreshTokenRow},
|
||||||
},
|
},
|
||||||
domain::{HashedAccessToken, HashedRefreshToken, HashedSecret},
|
domain::{HashedAccessToken, HashedRefreshToken, HashedSecret},
|
||||||
|
repository::dtos::Upsert,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[tracing::instrument(skip(executor, secret_hash))]
|
#[tracing::instrument(skip(executor, secret_hash))]
|
||||||
@@ -19,7 +18,8 @@ where
|
|||||||
{
|
{
|
||||||
let token = database::tokens::get_access_token_by_hash(executor, secret_hash.hash())
|
let token = database::tokens::get_access_token_by_hash(executor, secret_hash.hash())
|
||||||
.await?
|
.await?
|
||||||
.map(decode_access_token);
|
.map(decode_access_token)
|
||||||
|
.transpose()?;
|
||||||
|
|
||||||
Ok(token)
|
Ok(token)
|
||||||
}
|
}
|
||||||
@@ -35,46 +35,26 @@ where
|
|||||||
let token =
|
let token =
|
||||||
database::tokens::get_access_token_by_refresh_secret_hash(executor, secret_hash.hash())
|
database::tokens::get_access_token_by_refresh_secret_hash(executor, secret_hash.hash())
|
||||||
.await?
|
.await?
|
||||||
.map(decode_access_token);
|
.map(decode_access_token)
|
||||||
|
.transpose()?;
|
||||||
|
|
||||||
Ok(token)
|
Ok(token)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument(skip(executor, access_token))]
|
#[tracing::instrument(skip(executor, access_token))]
|
||||||
pub async fn insert_access_token<'e, E>(
|
pub async fn upsert_access_token<'e, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
access_token: HashedAccessToken,
|
access_token: HashedAccessToken,
|
||||||
) -> Result<HashedAccessToken, RepositoryError>
|
) -> Result<Option<Upsert<HashedAccessToken>>, RepositoryError>
|
||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
let token = database::tokens::insert_access_token(executor, encode_access_token(access_token))
|
let token = database::tokens::upsert_access_token(executor, encode_access_token(access_token))
|
||||||
.await
|
.await
|
||||||
.map_err(classify_insert_error)?;
|
.map_err(classify_upsert_error)?
|
||||||
|
.map(|upserted| upserted.try_map(decode_access_token))
|
||||||
Ok(decode_access_token(token))
|
.transpose()?
|
||||||
}
|
.map(Into::into);
|
||||||
|
|
||||||
#[tracing::instrument(skip(executor, secret_hash, new_secret_hash))]
|
|
||||||
pub async fn update_access_token_by_hash<'e, E>(
|
|
||||||
executor: E,
|
|
||||||
secret_hash: &HashedSecret,
|
|
||||||
new_secret_hash: &HashedSecret,
|
|
||||||
new_expires_at: DateTime<Utc>,
|
|
||||||
new_issued_at: DateTime<Utc>,
|
|
||||||
) -> Result<Option<HashedAccessToken>, RepositoryError>
|
|
||||||
where
|
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
||||||
{
|
|
||||||
let token = database::tokens::update_access_token_by_hash(
|
|
||||||
executor,
|
|
||||||
secret_hash.hash(),
|
|
||||||
new_secret_hash.hash(),
|
|
||||||
new_expires_at,
|
|
||||||
new_issued_at,
|
|
||||||
)
|
|
||||||
.await?
|
|
||||||
.map(decode_access_token);
|
|
||||||
|
|
||||||
Ok(token)
|
Ok(token)
|
||||||
}
|
}
|
||||||
@@ -89,7 +69,8 @@ where
|
|||||||
{
|
{
|
||||||
let token = database::tokens::delete_access_token_by_hash(executor, secret_hash.hash())
|
let token = database::tokens::delete_access_token_by_hash(executor, secret_hash.hash())
|
||||||
.await?
|
.await?
|
||||||
.map(decode_access_token);
|
.map(decode_access_token)
|
||||||
|
.transpose()?;
|
||||||
|
|
||||||
Ok(token)
|
Ok(token)
|
||||||
}
|
}
|
||||||
@@ -104,46 +85,27 @@ where
|
|||||||
{
|
{
|
||||||
let token = database::tokens::get_refresh_token_by_hash(executor, secret_hash.hash())
|
let token = database::tokens::get_refresh_token_by_hash(executor, secret_hash.hash())
|
||||||
.await?
|
.await?
|
||||||
.map(decode_refresh_token);
|
.map(decode_refresh_token)
|
||||||
|
.transpose()?;
|
||||||
|
|
||||||
Ok(token)
|
Ok(token)
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub async fn insert_refresh_token<'e, E>(
|
#[tracing::instrument(skip(executor, refresh_token))]
|
||||||
// executor: E,
|
pub async fn upsert_refresh_token<'e, E>(
|
||||||
// refresh_token: HashedRefreshToken,
|
|
||||||
// ) -> Result<HashedRefreshToken, RepositoryError>
|
|
||||||
// where
|
|
||||||
// E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
||||||
// {
|
|
||||||
// let token =
|
|
||||||
// database::tokens::insert_refresh_token(executor, encode_refresh_token(refresh_token))
|
|
||||||
// .await
|
|
||||||
// .map_err(classify_insert_error)?;
|
|
||||||
|
|
||||||
// Ok(decode_refresh_token(token))
|
|
||||||
// }
|
|
||||||
|
|
||||||
#[tracing::instrument(skip(executor, secret_hash, new_secret_hash))]
|
|
||||||
pub async fn update_refresh_token_by_hash<'e, E>(
|
|
||||||
executor: E,
|
executor: E,
|
||||||
secret_hash: &HashedSecret,
|
refresh_token: HashedRefreshToken,
|
||||||
new_secret_hash: &HashedSecret,
|
) -> Result<Option<Upsert<HashedRefreshToken>>, RepositoryError>
|
||||||
new_expires_at: DateTime<Utc>,
|
|
||||||
new_issued_at: DateTime<Utc>,
|
|
||||||
) -> Result<Option<HashedRefreshToken>, RepositoryError>
|
|
||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
let token = database::tokens::update_refresh_token_by_hash(
|
let token =
|
||||||
executor,
|
database::tokens::upsert_refresh_token(executor, encode_refresh_token(refresh_token))
|
||||||
secret_hash.hash(),
|
.await
|
||||||
new_secret_hash.hash(),
|
.map_err(classify_upsert_error)?
|
||||||
new_expires_at,
|
.map(|upserted| upserted.try_map(decode_refresh_token))
|
||||||
new_issued_at,
|
.transpose()?
|
||||||
)
|
.map(Into::into);
|
||||||
.await?
|
|
||||||
.map(decode_refresh_token);
|
|
||||||
|
|
||||||
Ok(token)
|
Ok(token)
|
||||||
}
|
}
|
||||||
@@ -158,12 +120,13 @@ where
|
|||||||
{
|
{
|
||||||
let token = database::tokens::delete_refresh_token_by_hash(executor, secret_hash.hash())
|
let token = database::tokens::delete_refresh_token_by_hash(executor, secret_hash.hash())
|
||||||
.await?
|
.await?
|
||||||
.map(decode_refresh_token);
|
.map(decode_refresh_token)
|
||||||
|
.transpose()?;
|
||||||
|
|
||||||
Ok(token)
|
Ok(token)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn classify_insert_error(error: DatabaseError) -> RepositoryError {
|
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||||
let relation = match &error {
|
let relation = match &error {
|
||||||
DatabaseError::ForeignKeyViolation {
|
DatabaseError::ForeignKeyViolation {
|
||||||
constraint: Constraint::AccessTokenUser,
|
constraint: Constraint::AccessTokenUser,
|
||||||
@@ -187,15 +150,15 @@ fn classify_insert_error(error: DatabaseError) -> RepositoryError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decode_access_token(row: AccessTokenRow) -> HashedAccessToken {
|
fn decode_access_token(row: AccessTokenRow) -> Result<HashedAccessToken, RepositoryError> {
|
||||||
HashedAccessToken {
|
Ok(HashedAccessToken {
|
||||||
id: row.id,
|
id: row.id,
|
||||||
user_id: row.user_id,
|
user_id: row.user_id,
|
||||||
user_app_id: row.user_app_id,
|
user_app_id: row.user_app_id,
|
||||||
secret_hash: HashedSecret::from(row.secret_hash),
|
secret_hash: HashedSecret::from(row.secret_hash),
|
||||||
expires_at: row.expires_at,
|
expires_at: row.expires_at,
|
||||||
issued_at: row.issued_at,
|
issued_at: row.issued_at,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn encode_access_token(token: HashedAccessToken) -> AccessTokenRow {
|
fn encode_access_token(token: HashedAccessToken) -> AccessTokenRow {
|
||||||
@@ -209,22 +172,22 @@ fn encode_access_token(token: HashedAccessToken) -> AccessTokenRow {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decode_refresh_token(row: RefreshTokenRow) -> HashedRefreshToken {
|
fn decode_refresh_token(row: RefreshTokenRow) -> Result<HashedRefreshToken, RepositoryError> {
|
||||||
HashedRefreshToken {
|
Ok(HashedRefreshToken {
|
||||||
id: row.id,
|
id: row.id,
|
||||||
token_id: row.access_token_id,
|
token_id: row.access_token_id,
|
||||||
secret_hash: HashedSecret::from(row.secret_hash),
|
secret_hash: HashedSecret::from(row.secret_hash),
|
||||||
expires_at: row.expires_at,
|
expires_at: row.expires_at,
|
||||||
issued_at: row.issued_at,
|
issued_at: row.issued_at,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// fn encode_refresh_token(token: HashedRefreshToken) -> RefreshTokenRow {
|
fn encode_refresh_token(token: HashedRefreshToken) -> RefreshTokenRow {
|
||||||
// RefreshTokenRow {
|
RefreshTokenRow {
|
||||||
// id: token.id,
|
id: token.id,
|
||||||
// access_token_id: token.token_id,
|
access_token_id: token.token_id,
|
||||||
// secret_hash: token.secret_hash.hash().to_vec(),
|
secret_hash: token.secret_hash.hash().to_vec(),
|
||||||
// expires_at: token.expires_at,
|
expires_at: token.expires_at,
|
||||||
// issued_at: token.issued_at,
|
issued_at: token.issued_at,
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ async fn issue_token(
|
|||||||
token_config.access_token_lifetime,
|
token_config.access_token_lifetime,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
repository::tokens::insert_access_token(&pool, access_token.clone().into()).await?;
|
repository::tokens::upsert_access_token(&pool, access_token.clone().into()).await?;
|
||||||
|
|
||||||
Ok(Json(TokenResponse::new(access_token, None)))
|
Ok(Json(TokenResponse::new(access_token, None)))
|
||||||
}
|
}
|
||||||
@@ -57,30 +57,13 @@ async fn issue_token(
|
|||||||
return Err(AppError::InvalidRefreshToken);
|
return Err(AppError::InvalidRefreshToken);
|
||||||
};
|
};
|
||||||
|
|
||||||
let new_access_token = access_token.refresh(token_config.access_token_lifetime)?;
|
let new_access = access_token.refresh(token_config.access_token_lifetime)?;
|
||||||
repository::tokens::update_access_token_by_hash(
|
repository::tokens::upsert_access_token(&pool, new_access.clone().into()).await?;
|
||||||
&pool,
|
|
||||||
&token_hash,
|
|
||||||
&new_access_token.secret.hash(),
|
|
||||||
new_access_token.expires_at,
|
|
||||||
new_access_token.issued_at,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let new_refresh_token = refresh_token.refresh(token_config.refresh_token_lifetime)?;
|
let new_refresh = refresh_token.refresh(token_config.refresh_token_lifetime)?;
|
||||||
repository::tokens::update_refresh_token_by_hash(
|
repository::tokens::upsert_refresh_token(&pool, new_refresh.clone().into()).await?;
|
||||||
&pool,
|
|
||||||
&token_hash,
|
|
||||||
&new_refresh_token.secret.hash(),
|
|
||||||
new_refresh_token.expires_at,
|
|
||||||
new_refresh_token.issued_at,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(Json(TokenResponse::new(
|
Ok(Json(TokenResponse::new(new_access, Some(new_refresh))))
|
||||||
new_access_token,
|
|
||||||
Some(new_refresh_token),
|
|
||||||
)))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user