From bca934ec50cce0488a1bcff8e1ddb40e27aef658 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Mon, 8 Feb 2021 19:50:52 -0500 Subject: [PATCH] cleanup username and apikey structs --- vtse-common/src/user.rs | 20 +++++++++++++++----- vtse-server/src/operations.rs | 1 - vtse-server/src/state.rs | 6 +++--- vtse-server/src/user.rs | 7 +++---- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/vtse-common/src/user.rs b/vtse-common/src/user.rs index 2893a28..139c178 100644 --- a/vtse-common/src/user.rs +++ b/vtse-common/src/user.rs @@ -1,14 +1,24 @@ use serde::Deserialize; use uuid::Uuid; -#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default, Deserialize, sqlx::Type)] -#[sqlx(transparent)] -pub struct ApiKey(pub Uuid); +#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default, Deserialize)] +pub struct ApiKey(Uuid); -#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default, Deserialize, sqlx::Type)] -#[sqlx(transparent)] +impl ApiKey { + pub fn inner(&self) -> &Uuid { + &self.0 + } +} + +#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default, Deserialize)] pub struct Username(String); +impl Username { + pub fn inner(&self) -> &str { + &self.0 + } +} + #[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default, Deserialize)] pub struct Password(String); diff --git a/vtse-server/src/operations.rs b/vtse-server/src/operations.rs index d6f2747..d80c52e 100644 --- a/vtse-server/src/operations.rs +++ b/vtse-server/src/operations.rs @@ -1,6 +1,5 @@ use serde::Deserialize; use vtse_common::stock::StockName; - use vtse_common::user::{ApiKey, Password, Username}; #[derive(Deserialize, Debug, PartialEq)] diff --git a/vtse-server/src/state.rs b/vtse-server/src/state.rs index 08e4d8e..bbd59c8 100644 --- a/vtse-server/src/state.rs +++ b/vtse-server/src/state.rs @@ -72,7 +72,7 @@ impl AppState { "SELECT (user_id) FROM user_api_keys JOIN api_keys ON user_api_keys.key_id = api_keys.key_id WHERE key = $1", - api_key.0 + api_key.inner() ) .fetch_optional(pool) .await?; @@ -98,7 +98,7 @@ impl AppState { "INSERT INTO users (username, pwhash_data) VALUES ($1, $2)", - username as Username, + username.inner(), salted_password.as_ref(), ) .execute(pool) @@ -156,7 +156,7 @@ impl AppState { let result = query!( "SELECT user_id, pwhash_data FROM users WHERE username = $1", - username as &Username + username.inner() ) .fetch_one(pool) .await?; diff --git a/vtse-server/src/user.rs b/vtse-server/src/user.rs index 021cf51..ae9de55 100644 --- a/vtse-server/src/user.rs +++ b/vtse-server/src/user.rs @@ -5,19 +5,18 @@ use sodiumoxide::crypto::pwhash::{ use std::convert::TryFrom; use vtse_common::user::Password; -#[derive(sqlx::Type)] -#[sqlx(transparent)] pub(crate) struct SaltedPassword(HashedPassword); impl TryFrom for SaltedPassword { type Error = (); fn try_from(password: Password) -> Result { - Ok(Self(pwhash( + pwhash( password.as_bytes(), OPSLIMIT_INTERACTIVE, MEMLIMIT_INTERACTIVE, - )?)) + ) + .map(Self) } }