cleanup username and apikey structs

master
Edward Shen 2021-02-08 19:50:52 -05:00
parent 8264ced7a6
commit bca934ec50
Signed by: edward
GPG Key ID: 19182661E818369F
4 changed files with 21 additions and 13 deletions

View File

@ -1,14 +1,24 @@
use serde::Deserialize; use serde::Deserialize;
use uuid::Uuid; use uuid::Uuid;
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default, Deserialize, sqlx::Type)] #[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default, Deserialize)]
#[sqlx(transparent)] pub struct ApiKey(Uuid);
pub struct ApiKey(pub Uuid);
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default, Deserialize, sqlx::Type)] impl ApiKey {
#[sqlx(transparent)] pub fn inner(&self) -> &Uuid {
&self.0
}
}
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default, Deserialize)]
pub struct Username(String); pub struct Username(String);
impl Username {
pub fn inner(&self) -> &str {
&self.0
}
}
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default, Deserialize)] #[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default, Deserialize)]
pub struct Password(String); pub struct Password(String);

View File

@ -1,6 +1,5 @@
use serde::Deserialize; use serde::Deserialize;
use vtse_common::stock::StockName; use vtse_common::stock::StockName;
use vtse_common::user::{ApiKey, Password, Username}; use vtse_common::user::{ApiKey, Password, Username};
#[derive(Deserialize, Debug, PartialEq)] #[derive(Deserialize, Debug, PartialEq)]

View File

@ -72,7 +72,7 @@ impl AppState {
"SELECT (user_id) FROM user_api_keys "SELECT (user_id) FROM user_api_keys
JOIN api_keys ON user_api_keys.key_id = api_keys.key_id JOIN api_keys ON user_api_keys.key_id = api_keys.key_id
WHERE key = $1", WHERE key = $1",
api_key.0 api_key.inner()
) )
.fetch_optional(pool) .fetch_optional(pool)
.await?; .await?;
@ -98,7 +98,7 @@ impl AppState {
"INSERT INTO users "INSERT INTO users
(username, pwhash_data) (username, pwhash_data)
VALUES ($1, $2)", VALUES ($1, $2)",
username as Username, username.inner(),
salted_password.as_ref(), salted_password.as_ref(),
) )
.execute(pool) .execute(pool)
@ -156,7 +156,7 @@ impl AppState {
let result = query!( let result = query!(
"SELECT user_id, pwhash_data FROM users "SELECT user_id, pwhash_data FROM users
WHERE username = $1", WHERE username = $1",
username as &Username username.inner()
) )
.fetch_one(pool) .fetch_one(pool)
.await?; .await?;

View File

@ -5,19 +5,18 @@ use sodiumoxide::crypto::pwhash::{
use std::convert::TryFrom; use std::convert::TryFrom;
use vtse_common::user::Password; use vtse_common::user::Password;
#[derive(sqlx::Type)]
#[sqlx(transparent)]
pub(crate) struct SaltedPassword(HashedPassword); pub(crate) struct SaltedPassword(HashedPassword);
impl TryFrom<Password> for SaltedPassword { impl TryFrom<Password> for SaltedPassword {
type Error = (); type Error = ();
fn try_from(password: Password) -> Result<Self, Self::Error> { fn try_from(password: Password) -> Result<Self, Self::Error> {
Ok(Self(pwhash( pwhash(
password.as_bytes(), password.as_bytes(),
OPSLIMIT_INTERACTIVE, OPSLIMIT_INTERACTIVE,
MEMLIMIT_INTERACTIVE, MEMLIMIT_INTERACTIVE,
)?)) )
.map(Self)
} }
} }