cleanup username and apikey structs

This commit is contained in:
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 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);

View file

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

View file

@ -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?;

View file

@ -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<Password> for SaltedPassword {
type Error = ();
fn try_from(password: Password) -> Result<Self, Self::Error> {
Ok(Self(pwhash(
pwhash(
password.as_bytes(),
OPSLIMIT_INTERACTIVE,
MEMLIMIT_INTERACTIVE,
)?))
)
.map(Self)
}
}