feat: finish adding device name to login, register, and refresh endpoints

This commit is contained in:
Twig 2025-07-15 02:42:53 +02:00
parent 7872d2ec24
commit fc061738fa
No known key found for this signature in database
7 changed files with 21 additions and 29 deletions

View file

@ -7,7 +7,7 @@ use diesel_async::RunQueryDsl;
use serde::Deserialize; use serde::Deserialize;
use crate::{ 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; use super::Response;

View file

@ -7,7 +7,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
use crate::{ use crate::{
error::Error, schema::{ error::Error, schema::{
access_tokens::{self, dsl}, 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 }, utils::{generate_token, new_refresh_token_cookie}, Data
}; };
@ -50,7 +50,7 @@ pub async fn res(req: HttpRequest, data: web::Data<Data>) -> Result<HttpResponse
} }
let current_time = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() as i64; let current_time = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() as i64;
let existing_device_name: String; let mut device_name: String = String::new();
if lifetime > 1987200 { if lifetime > 1987200 {
let new_refresh_token = generate_token::<32>()?; let new_refresh_token = generate_token::<32>()?;
@ -65,9 +65,9 @@ pub async fn res(req: HttpRequest, data: web::Data<Data>) -> Result<HttpResponse
.get_result::<String>(&mut conn) .get_result::<String>(&mut conn)
.await .await
{ {
Ok(device_name) => { Ok(existing_device_name) => {
refresh_token = new_refresh_token; refresh_token = new_refresh_token;
existing_device_name = device_name.to_string(); device_name = existing_device_name;
} }
Err(error) => { Err(error) => {
error!("{error}"); error!("{error}");
@ -88,7 +88,7 @@ pub async fn res(req: HttpRequest, data: web::Data<Data>) -> Result<HttpResponse
return Ok(HttpResponse::Ok() return Ok(HttpResponse::Ok()
.cookie(new_refresh_token_cookie(&data.config, refresh_token)) .cookie(new_refresh_token_cookie(&data.config, refresh_token))
.json(Response { access_token, existing_device_name })); .json(Response { access_token, device_name }));
} }
refresh_token_cookie.make_removal(); refresh_token_cookie.make_removal();

View file

@ -17,8 +17,8 @@ use crate::{
refresh_tokens::{self, dsl as rdsl}, refresh_tokens::{self, dsl as rdsl},
users::{self, dsl as udsl}, users::{self, dsl as udsl},
}, utils::{ }, utils::{
generate_token, new_refresh_token_cookie, EMAIL_REGEX, PASSWORD_REGEX, USERNAME_REGEX generate_device_name, generate_token, new_refresh_token_cookie, EMAIL_REGEX, PASSWORD_REGEX, USERNAME_REGEX
}, generate_device_name::generate_device_name, Data }, Data
}; };
#[derive(Deserialize)] #[derive(Deserialize)]

View file

@ -9,7 +9,7 @@ use objects::MailClient;
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use std::time::SystemTime; use std::time::SystemTime;
mod config; mod config;
mod generate_device_name; mod wordlist;
use config::{Config, ConfigBuilder}; use config::{Config, ConfigBuilder};
use diesel_migrations::{EmbeddedMigrations, MigrationHarness, embed_migrations}; use diesel_migrations::{EmbeddedMigrations, MigrationHarness, embed_migrations};

View file

@ -103,7 +103,7 @@ diesel::table! {
token -> Varchar, token -> Varchar,
uuid -> Uuid, uuid -> Uuid,
created_at -> Int8, created_at -> Int8,
#[max_length = 16] #[max_length = 64]
device_name -> Varchar, device_name -> Varchar,
} }
} }

View file

@ -1,4 +1,5 @@
use std::sync::LazyLock; use std::sync::LazyLock;
use rand::Rng;
use actix_web::{ use actix_web::{
cookie::{Cookie, SameSite, time::Duration}, cookie::{Cookie, SameSite, time::Duration},
@ -16,11 +17,7 @@ use serde::Serialize;
use uuid::Uuid; use uuid::Uuid;
use crate::{ use crate::{
Conn, Data, config::Config, error::Error, objects::{HasIsAbove, HasUuid}, schema::users, wordlist::{ADJECTIVES, ANIMALS}, Conn, Data
config::Config,
error::Error,
objects::{HasIsAbove, HasUuid},
schema::users,
}; };
pub static EMAIL_REGEX: LazyLock<Regex> = LazyLock::new(|| { pub static EMAIL_REGEX: LazyLock<Regex> = LazyLock::new(|| {
@ -282,3 +279,10 @@ impl Data {
.await .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(" ")
}

View file

@ -1,16 +1,4 @@
use rand::Rng; pub const ANIMALS: [&'static str; 223] = [
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] = [
"Aardvark", "Aardvark",
"Albatross", "Albatross",
"Alligator", "Alligator",
@ -236,7 +224,7 @@ const ANIMALS: [&'static str; ANIMALS_LENGTH] = [
"Zebra", "Zebra",
]; ];
const ADJECTIVES: [&'static str; ADJECTIVES_LENGTH] = [ pub const ADJECTIVES: [&'static str; 765] = [
"other", "other",
"such", "such",
"first", "first",