Add User models
This commit is contained in:
@@ -1 +1 @@
|
||||
|
||||
pub mod user;
|
||||
|
||||
112
src/models/user.rs
Normal file
112
src/models/user.rs
Normal file
@@ -0,0 +1,112 @@
|
||||
//! Various user types. Some types marked server-only to help prevent
|
||||
//! leaking passwords to the frontend
|
||||
|
||||
/// Standard informational user type, contains no password information
|
||||
#[derive(Clone, Debug)]
|
||||
#[cfg_attr(feature = "server", derive(Queryable, Selectable, Identifiable))]
|
||||
#[cfg_attr(feature = "server", diesel(table_name = crate::schema::users,
|
||||
check_for_backend(diesel::pg::Pg)))]
|
||||
pub struct User {
|
||||
pub id: i32,
|
||||
pub username: String,
|
||||
pub created_at: chrono::DateTime<chrono::Local>,
|
||||
}
|
||||
|
||||
/// Plaintext user credentials, used for login/signup form
|
||||
pub struct UserCredentials {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(feature = "server")] {
|
||||
|
||||
use diesel::{
|
||||
deserialize::{FromSql, FromSqlRow},
|
||||
expression::AsExpression,
|
||||
prelude::*,
|
||||
serialize::ToSql,
|
||||
sql_types,
|
||||
};
|
||||
use pbkdf2::{PasswordHasher, Pbkdf2};
|
||||
|
||||
/// Newtype for a `String`-represented hashed password
|
||||
#[derive(Clone, Debug, AsExpression, FromSqlRow)]
|
||||
#[diesel(sql_type = sql_types::Text)]
|
||||
pub struct HashedPassword(String);
|
||||
|
||||
impl<DB> FromSql<diesel::sql_types::Text, DB> for HashedPassword
|
||||
where
|
||||
DB: diesel::backend::Backend,
|
||||
String: FromSql<sql_types::Text, DB>,
|
||||
{
|
||||
fn from_sql(bytes: DB::RawValue<'_>) -> diesel::deserialize::Result<Self> {
|
||||
Ok(Self(String::from_sql(bytes)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl<DB> ToSql<diesel::sql_types::Text, DB> for HashedPassword
|
||||
where
|
||||
DB: diesel::backend::Backend,
|
||||
String: ToSql<sql_types::Text, DB>,
|
||||
{
|
||||
fn to_sql<'b>(
|
||||
&'b self,
|
||||
out: &mut diesel::serialize::Output<'b, '_, DB>,
|
||||
) -> diesel::serialize::Result {
|
||||
self.0.to_sql(out)
|
||||
}
|
||||
}
|
||||
|
||||
/// User as it appears in the database, with hashed password
|
||||
#[derive(Clone, Debug, Identifiable, Queryable, Selectable)]
|
||||
#[diesel(table_name = crate::schema::users, check_for_backend(diesel::pg::Pg))]
|
||||
pub struct DbUser {
|
||||
pub id: i32,
|
||||
pub username: String,
|
||||
pub hashed_password: HashedPassword,
|
||||
pub created_at: chrono::DateTime<chrono::Local>,
|
||||
}
|
||||
|
||||
impl From<DbUser> for User {
|
||||
fn from(db_user: DbUser) -> Self {
|
||||
User {
|
||||
id: db_user.id,
|
||||
username: db_user.username,
|
||||
created_at: db_user.created_at,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// User credentials with hashed password
|
||||
#[derive(Clone, Debug, Insertable, Queryable, Selectable)]
|
||||
#[diesel(table_name = crate::schema::users, check_for_backend(diesel::pg::Pg))]
|
||||
pub struct HashedUserCredentials {
|
||||
username: String,
|
||||
hashed_password: HashedPassword,
|
||||
}
|
||||
|
||||
impl From<DbUser> for HashedUserCredentials {
|
||||
fn from(db_user: DbUser) -> Self {
|
||||
HashedUserCredentials {
|
||||
username: db_user.username,
|
||||
hashed_password: db_user.hashed_password,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl UserCredentials {
|
||||
/// Attempt to convert into `HashedUserCredentials` by hashing the password. Yields a PBKDF2
|
||||
/// error on failure.
|
||||
pub fn try_hash(self) -> Result<HashedUserCredentials, pbkdf2::password_hash::Error> {
|
||||
let hashed_password = Pbkdf2::default().hash_password(self.password.as_bytes())?;
|
||||
|
||||
Ok(HashedUserCredentials {
|
||||
username: self.username,
|
||||
hashed_password: HashedPassword(hashed_password.to_string()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user