if access token expired, use refresh token, and get new access + refresh tokens

This commit is contained in:
Philippe Loctaux 2023-03-19 20:03:30 +01:00
parent 9172155893
commit 8db0bbb874
12 changed files with 462 additions and 41 deletions

View file

@ -0,0 +1,5 @@
update refresh_tokens
set used_at = CURRENT_TIMESTAMP
where token is ?

View file

@ -0,0 +1,16 @@
select u.id,
u.created_at as "created_at: DateTime<Utc>",
u.updated_at as "updated_at: DateTime<Utc>",
u.is_admin as "is_admin: bool",
u.username,
u.name,
u.email,
u.password,
u.password_recover,
u.paper_key,
u.is_archived as "is_archived: bool"
from users u
inner join refresh_tokens rt on u.id = rt.user
where rt.token is ?

View file

@ -20,6 +20,94 @@
},
"query": "update settings\n\nset business_name = ?\n\nwhere id is 0\n"
},
"3c8e31ffa5cbfd4dded8a272777cb320fb51fd2e53ed25054d24e9801df0c358": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Right": 1
}
},
"query": "update refresh_tokens\n\nset used_at = CURRENT_TIMESTAMP\n\nwhere token is ?"
},
"4f83a1908a1980ce4bf65eadf24eed2af6c6225972ef7f9f4cf0c702264033a7": {
"describe": {
"columns": [
{
"name": "id",
"ordinal": 0,
"type_info": "Text"
},
{
"name": "created_at: DateTime<Utc>",
"ordinal": 1,
"type_info": "Text"
},
{
"name": "updated_at: DateTime<Utc>",
"ordinal": 2,
"type_info": "Text"
},
{
"name": "is_admin: bool",
"ordinal": 3,
"type_info": "Int64"
},
{
"name": "username",
"ordinal": 4,
"type_info": "Text"
},
{
"name": "name",
"ordinal": 5,
"type_info": "Text"
},
{
"name": "email",
"ordinal": 6,
"type_info": "Text"
},
{
"name": "password",
"ordinal": 7,
"type_info": "Text"
},
{
"name": "password_recover",
"ordinal": 8,
"type_info": "Text"
},
{
"name": "paper_key",
"ordinal": 9,
"type_info": "Text"
},
{
"name": "is_archived: bool",
"ordinal": 10,
"type_info": "Int64"
}
],
"nullable": [
false,
false,
false,
false,
false,
true,
true,
true,
true,
true,
false
],
"parameters": {
"Right": 1
}
},
"query": "select u.id,\n u.created_at as \"created_at: DateTime<Utc>\",\n u.updated_at as \"updated_at: DateTime<Utc>\",\n u.is_admin as \"is_admin: bool\",\n u.username,\n u.name,\n u.email,\n u.password,\n u.password_recover,\n u.paper_key,\n u.is_archived as \"is_archived: bool\"\nfrom users u\n\n inner join refresh_tokens rt on u.id = rt.user\n\nwhere rt.token is ?\n"
},
"520fe30e21f6b6c4d9a47c457675eebd144cf020e9230d154e9e4d0c8d6e01ca": {
"describe": {
"columns": [],

View file

@ -71,4 +71,17 @@ impl RefreshTokens {
Ok((query.rows_affected() >= 1).then_some(()))
}
pub async fn use_token(
conn: impl SqliteExecutor<'_>,
token: &str,
) -> Result<Option<()>, Error> {
let query: SqliteQueryResult =
sqlx::query_file!("queries/refresh_tokens/use_token.sql", token)
.execute(conn)
.await
.map_err(handle_error)?;
Ok((query.rows_affected() == 1).then_some(()))
}
}

View file

@ -85,4 +85,14 @@ impl Users {
.await
.map_err(handle_error)
}
pub async fn get_one_from_refresh_token(
conn: impl SqliteExecutor<'_>,
token: &str,
) -> Result<Option<Self>, Error> {
sqlx::query_file_as!(Self, "queries/users/get_one_from_refresh_token.sql", token)
.fetch_optional(conn)
.await
.map_err(handle_error)
}
}