Merge branch 'generate-device-name' into staging
Some checks failed
ci/woodpecker/push/build-and-publish Pipeline failed

This commit is contained in:
Radical 2025-07-20 16:28:02 +02:00
commit 969b517e18
11 changed files with 1034 additions and 18 deletions

View file

@ -21,7 +21,7 @@ use crate::{
schema::*,
utils::{
PASSWORD_REGEX, generate_token, new_refresh_token_cookie,
user_uuid_from_identifier,
user_uuid_from_identifier, generate_device_name
},
};
@ -29,7 +29,6 @@ use crate::{
pub struct LoginInformation {
username: String,
password: String,
device_name: String,
}
pub async fn response(
@ -72,12 +71,14 @@ pub async fn response(
use refresh_tokens::dsl as rdsl;
let device_name = generate_device_name();
insert_into(refresh_tokens::table)
.values((
rdsl::token.eq(&refresh_token),
rdsl::uuid.eq(uuid),
rdsl::created_at.eq(current_time),
rdsl::device_name.eq(&login_information.device_name),
rdsl::device_name.eq(&device_name),
))
.execute(&mut conn)
.await?;
@ -94,7 +95,7 @@ pub async fn response(
.execute(&mut conn)
.await?;
let mut response = (StatusCode::OK, Json(Response { access_token })).into_response();
let mut response = (StatusCode::OK, Json(Response { access_token, device_name })).into_response();
response.headers_mut().append(
"Set-Cookie",

View file

@ -27,6 +27,7 @@ mod verify_email;
#[derive(Serialize)]
pub struct Response {
access_token: String,
device_name: String,
}

View file

@ -19,8 +19,7 @@ use crate::{
schema::{
access_tokens::{self, dsl},
refresh_tokens::{self, dsl as rdsl},
},
utils::{generate_token, new_refresh_token_cookie},
}, utils::{generate_token, new_refresh_token_cookie}
};
pub async fn post(
@ -69,6 +68,7 @@ pub async fn post(
}
let current_time = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() as i64;
let mut device_name: String = String::new();
if lifetime > 1987200 {
let new_refresh_token = generate_token::<32>()?;
@ -79,11 +79,13 @@ pub async fn post(
rdsl::token.eq(&new_refresh_token),
rdsl::created_at.eq(current_time),
))
.execute(&mut conn)
.returning(rdsl::device_name)
.get_result::<String>(&mut conn)
.await
{
Ok(_) => {
Ok(existing_device_name) => {
refresh_token = new_refresh_token;
device_name = existing_device_name;
}
Err(error) => {
error!("{error}");
@ -102,7 +104,7 @@ pub async fn post(
.execute(&mut conn)
.await?;
let mut response = (StatusCode::OK, Json(Response { access_token })).into_response();
let mut response = (StatusCode::OK, Json(Response { access_token, device_name })).into_response();
// TODO: Dont set this when refresh token is unchanged
response.headers_mut().append(

View file

@ -30,7 +30,7 @@ use crate::{
},
utils::{
EMAIL_REGEX, PASSWORD_REGEX, USERNAME_REGEX, generate_token,
new_refresh_token_cookie,
new_refresh_token_cookie, generate_device_name
},
};
@ -39,7 +39,6 @@ pub struct AccountInformation {
identifier: String,
email: String,
password: String,
device_name: String,
}
#[derive(Serialize)]
@ -137,12 +136,14 @@ pub async fn post(
let current_time = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() as i64;
let device_name = generate_device_name();
insert_into(refresh_tokens::table)
.values((
rdsl::token.eq(&refresh_token),
rdsl::uuid.eq(uuid),
.values((
rdsl::token.eq(&refresh_token),
rdsl::uuid.eq(uuid),
rdsl::created_at.eq(current_time),
rdsl::device_name.eq(&account_information.device_name),
rdsl::device_name.eq(&device_name),
))
.execute(&mut conn)
.await?;
@ -161,7 +162,7 @@ pub async fn post(
Member::new(&app_state, uuid, initial_guild).await?;
}
let mut response = (StatusCode::OK, Json(Response {access_token})).into_response();
let mut response = (StatusCode::OK, Json(Response {access_token, device_name})).into_response();
response.headers_mut().append(
"Set-Cookie",