From fc061738fa74d1d5ead22ec034d7ac01e8119f3b Mon Sep 17 00:00:00 2001 From: JustTemmie <47639983+JustTemmie@users.noreply.github.com> Date: Tue, 15 Jul 2025 02:42:53 +0200 Subject: [PATCH] feat: finish adding device name to login, register, and refresh endpoints --- src/api/v1/auth/login.rs | 2 +- src/api/v1/auth/refresh.rs | 10 +++++----- src/api/v1/auth/register.rs | 4 ++-- src/main.rs | 2 +- src/schema.rs | 2 +- src/utils.rs | 14 +++++++++----- src/{generate_device_name.rs => wordlist.rs} | 16 ++-------------- 7 files changed, 21 insertions(+), 29 deletions(-) rename src/{generate_device_name.rs => wordlist.rs} (96%) diff --git a/src/api/v1/auth/login.rs b/src/api/v1/auth/login.rs index cef4726..b2f3180 100644 --- a/src/api/v1/auth/login.rs +++ b/src/api/v1/auth/login.rs @@ -7,7 +7,7 @@ use diesel_async::RunQueryDsl; use serde::Deserialize; use crate::{ - error::Error, schema::*, utils::{generate_token, new_refresh_token_cookie, user_uuid_from_identifier, PASSWORD_REGEX}, generate_device_name::generate_device_name, Data + error::Error, schema::*, utils::{generate_device_name, generate_token, new_refresh_token_cookie, user_uuid_from_identifier, PASSWORD_REGEX}, Data }; use super::Response; diff --git a/src/api/v1/auth/refresh.rs b/src/api/v1/auth/refresh.rs index a89efbc..90728de 100644 --- a/src/api/v1/auth/refresh.rs +++ b/src/api/v1/auth/refresh.rs @@ -7,7 +7,7 @@ use std::time::{SystemTime, UNIX_EPOCH}; use crate::{ error::Error, schema::{ access_tokens::{self, dsl}, - refresh_tokens::{self, device_name, dsl as rdsl}, + refresh_tokens::{self, dsl as rdsl}, }, utils::{generate_token, new_refresh_token_cookie}, Data }; @@ -50,7 +50,7 @@ pub async fn res(req: HttpRequest, data: web::Data) -> Result 1987200 { let new_refresh_token = generate_token::<32>()?; @@ -65,9 +65,9 @@ pub async fn res(req: HttpRequest, data: web::Data) -> Result(&mut conn) .await { - Ok(device_name) => { + Ok(existing_device_name) => { refresh_token = new_refresh_token; - existing_device_name = device_name.to_string(); + device_name = existing_device_name; } Err(error) => { error!("{error}"); @@ -88,7 +88,7 @@ pub async fn res(req: HttpRequest, data: web::Data) -> Result Varchar, uuid -> Uuid, created_at -> Int8, - #[max_length = 16] + #[max_length = 64] device_name -> Varchar, } } diff --git a/src/utils.rs b/src/utils.rs index 072143f..d058db3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,5 @@ use std::sync::LazyLock; +use rand::Rng; use actix_web::{ cookie::{Cookie, SameSite, time::Duration}, @@ -16,11 +17,7 @@ use serde::Serialize; use uuid::Uuid; use crate::{ - Conn, Data, - config::Config, - error::Error, - objects::{HasIsAbove, HasUuid}, - schema::users, + config::Config, error::Error, objects::{HasIsAbove, HasUuid}, schema::users, wordlist::{ADJECTIVES, ANIMALS}, Conn, Data }; pub static EMAIL_REGEX: LazyLock = LazyLock::new(|| { @@ -282,3 +279,10 @@ impl Data { .await } } + +pub fn generate_device_name() -> String { + let adjective_index = rand::rng().random_range(0..ADJECTIVES.len()-1); + let animal_index = rand::rng().random_range(0..ANIMALS.len()-1); + + return [ADJECTIVES[adjective_index], ANIMALS[animal_index]].join(" ") +} \ No newline at end of file diff --git a/src/generate_device_name.rs b/src/wordlist.rs similarity index 96% rename from src/generate_device_name.rs rename to src/wordlist.rs index 1992f95..3ca3c3c 100644 --- a/src/generate_device_name.rs +++ b/src/wordlist.rs @@ -1,16 +1,4 @@ -use rand::Rng; - -pub fn generate_device_name() -> String { - let adjective_index = rand::rng().random_range(0..ADJECTIVES_LENGTH-1); - let animal_index = rand::rng().random_range(0..ANIMALS_LENGTH-1); - - return [ADJECTIVES[adjective_index], ANIMALS[animal_index]].join(" ") -} - -const ANIMALS_LENGTH: usize = 223; -const ADJECTIVES_LENGTH: usize = 765; - -const ANIMALS: [&'static str; ANIMALS_LENGTH] = [ +pub const ANIMALS: [&'static str; 223] = [ "Aardvark", "Albatross", "Alligator", @@ -236,7 +224,7 @@ const ANIMALS: [&'static str; ANIMALS_LENGTH] = [ "Zebra", ]; -const ADJECTIVES: [&'static str; ADJECTIVES_LENGTH] = [ +pub const ADJECTIVES: [&'static str; 765] = [ "other", "such", "first",