diff --git a/migrations/20230426221940_init.up.sql b/migrations/20230426221940_init.up.sql index 02df49a..7d53573 100644 --- a/migrations/20230426221940_init.up.sql +++ b/migrations/20230426221940_init.up.sql @@ -5,7 +5,7 @@ -- users create table if not exists witches ( - id int not null primary key, + id blob not null primary key, username text not null unique, displayname text, email text, diff --git a/src/lib.rs b/src/lib.rs index b0ff663..6c16c13 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,13 +6,3 @@ pub mod generic_handlers; pub mod session_store; pub(crate) mod templates; pub mod users; - -pub trait ToBlob { - fn blob(&self) -> &[u8]; -} - -impl ToBlob for uuid::Uuid { - fn blob(&self) -> &[u8] { - self.as_bytes().as_slice() - } -} diff --git a/src/users.rs b/src/users.rs index c729ccc..63c46e7 100644 --- a/src/users.rs +++ b/src/users.rs @@ -11,15 +11,11 @@ use axum::{ response::{IntoResponse, Response}, }; use axum_login::{secrecy::SecretVec, AuthUser, SqliteStore}; -use rand_core::CryptoRngCore; use sqlx::{query_as, SqlitePool}; use unicode_segmentation::UnicodeSegmentation; use uuid::Uuid; -use crate::{ - templates::{CreateUser, LoginGet}, - ToBlob, -}; +use crate::templates::{CreateUser, LoginGet}; const CREATE_QUERY: &str = "insert into witches (id, username, displayname, email, pwhash) values ($1, $2, $3, $4, $5)"; @@ -28,7 +24,7 @@ const ID_QUERY: &str = "select * from witches where id = $1"; #[derive(Debug, Default, Clone, PartialEq, Eq, sqlx::FromRow)] pub struct User { - pub id: i64, + pub id: Uuid, pub username: String, pub displayname: Option, pub email: Option, @@ -49,10 +45,10 @@ impl Display for User { } } -pub type AuthContext = axum_login::extractors::AuthContext>; +pub type AuthContext = axum_login::extractors::AuthContext>; -impl AuthUser for User { - fn get_id(&self) -> i64 { +impl AuthUser for User { + fn get_id(&self) -> Uuid { self.id } @@ -170,7 +166,7 @@ pub async fn post_create_user( let user = create_user(username, displayname, email, password, &pool).await?; tracing::debug!("created {user:?}"); - let id = user.id; + let id = user.id.as_simple().to_string(); let location = format!("/signup_success/{id}"); let resp = axum::response::Redirect::temporary(&location).into_response(); @@ -187,7 +183,7 @@ pub async fn handle_signup_success( let id = id.trim(); let id = Uuid::try_parse(id).unwrap_or_default(); query_as(ID_QUERY) - .bind(id.blob()) + .bind(id) .fetch_one(&pool) .await .unwrap_or_default() @@ -266,8 +262,7 @@ async fn create_user( .unwrap() // safe to unwrap, we know the salt is valid .to_string(); - let mut rng = OsRng; - let id: i64 = rng.as_rngcore().next_u64() as i64; + let id = Uuid::new_v4(); let res = sqlx::query(CREATE_QUERY) .bind(id) .bind(username)