2020-12-01 19:43:15 +00:00
|
|
|
use serde::{Serialize, de::DeserializeOwned};
|
2020-11-16 06:13:16 +00:00
|
|
|
|
2020-12-17 00:20:54 +00:00
|
|
|
use std::convert::TryInto;
|
|
|
|
|
2020-11-16 06:13:16 +00:00
|
|
|
use crate::user_management::{User, Result};
|
2020-11-19 22:00:32 +00:00
|
|
|
use crate::user_management::user::{RegisteredUser, NotSignedInUser, PartialUser};
|
2020-11-16 06:13:16 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
/// A struct containing information for managing users.
|
|
|
|
///
|
|
|
|
/// Wraps a [`sled::Db`]
|
|
|
|
pub struct UserManager {
|
2020-12-08 16:01:07 +00:00
|
|
|
/// Allows access to the [`sled::Db`] used by the UserManager to store user data.
|
|
|
|
///
|
|
|
|
/// Do not try to use this database to access user information, and instead prefer
|
|
|
|
/// methods such as [`lookup_user()`] and [`lookup_certificate()`].
|
|
|
|
///
|
|
|
|
/// However, you're welcome to use the database to store you own data without needing
|
|
|
|
/// to run parallel sled databases. It's recommended that any trees you open be
|
|
|
|
/// namespaced like `tld.yourdomain.projectname.treename` in order to prevent
|
|
|
|
/// conflict.
|
|
|
|
///
|
|
|
|
/// [`lookup_user()`]: Self::lookup_user
|
|
|
|
/// [`lookup_certificate()`]: Self::lookup_certificate
|
2020-11-27 02:04:02 +00:00
|
|
|
pub db: sled::Db,
|
2020-12-17 00:20:54 +00:00
|
|
|
pub (crate) users: sled::Tree, // user_id:u64 maps to data:PartialUser
|
|
|
|
pub (crate) certificates: sled::Tree, // fingerprint:[u8; 32] maps to uid:u64
|
|
|
|
pub (crate) usernames: sled::Tree, // username:String maps to uid:u64
|
2020-11-16 06:13:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UserManager {
|
|
|
|
|
|
|
|
/// Create or open a new UserManager
|
|
|
|
///
|
|
|
|
/// The `dir` argument is the path to a data directory, to be populated using sled.
|
|
|
|
/// This will be created if it does not exist.
|
2020-11-27 22:24:16 +00:00
|
|
|
pub fn new(db: sled::Db) -> Result<Self> {
|
2020-11-16 06:13:16 +00:00
|
|
|
Ok(Self {
|
2020-11-27 02:04:02 +00:00
|
|
|
users: db.open_tree("gay.emii.kochab.users")?,
|
|
|
|
certificates: db.open_tree("gay.emii.kochab.certificates")?,
|
2020-12-17 00:20:54 +00:00
|
|
|
usernames: db.open_tree("gay.emii.kochab.usernames")?,
|
2020-11-16 06:13:16 +00:00
|
|
|
db,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-01 19:43:15 +00:00
|
|
|
/// Lookup the owner of a certificate based on it's fingerprint
|
2020-11-16 06:13:16 +00:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// An error is thrown if there is an error reading from the database or if data
|
|
|
|
/// recieved from the database is corrupt
|
2020-12-17 00:20:54 +00:00
|
|
|
///
|
|
|
|
/// [`None`] can be returned if their is no user with this certificate.
|
|
|
|
pub fn lookup_certificate<UserData: Serialize + DeserializeOwned>(
|
|
|
|
&self,
|
|
|
|
cert: [u8; 32]
|
|
|
|
) -> Result<Option<RegisteredUser<UserData>>> {
|
2020-12-01 19:43:15 +00:00
|
|
|
if let Some(bytes) = self.certificates.get(cert)? {
|
2020-12-17 00:20:54 +00:00
|
|
|
let id = u64::from_le_bytes(bytes.as_ref().try_into()?);
|
|
|
|
Ok(Some(
|
|
|
|
self.lookup_user(id)?
|
|
|
|
.ok_or(super::DeserializeError::InvalidReference(id))?
|
|
|
|
))
|
2020-11-16 06:13:16 +00:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:20:54 +00:00
|
|
|
/// Get the user with the specified username
|
2020-11-16 06:13:16 +00:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// An error is thrown if there is an error reading from the database or if data
|
|
|
|
/// recieved from the database is corrupt
|
2020-12-17 00:20:54 +00:00
|
|
|
///
|
|
|
|
/// [`None`] can be returned if their is no user with this username.
|
|
|
|
pub fn lookup_username<UserData: Serialize + DeserializeOwned>(
|
2020-11-16 06:13:16 +00:00
|
|
|
&self,
|
2020-11-19 21:54:29 +00:00
|
|
|
username: impl AsRef<str>
|
2020-12-17 00:20:54 +00:00
|
|
|
) -> Result<Option<RegisteredUser<UserData>>> {
|
|
|
|
if let Some(bytes) = self.usernames.get(username.as_ref())? {
|
|
|
|
let id = u64::from_le_bytes(bytes.as_ref().try_into()?);
|
|
|
|
Ok(Some(
|
|
|
|
self.lookup_user(id)?
|
|
|
|
.ok_or(super::DeserializeError::InvalidReference(id))?
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Lookup a user by uid
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// An error is thrown if there is an error reading from the database or if data
|
|
|
|
/// recieved from the database is corrupt
|
|
|
|
pub fn lookup_user<UserData>(
|
|
|
|
&self,
|
|
|
|
uid: u64,
|
2020-11-19 22:00:32 +00:00
|
|
|
) -> Result<Option<RegisteredUser<UserData>>>
|
2020-11-16 06:13:16 +00:00
|
|
|
where
|
|
|
|
UserData: Serialize + DeserializeOwned
|
|
|
|
{
|
2020-12-17 00:20:54 +00:00
|
|
|
if let Some(bytes) = self.users.get(uid.to_le_bytes())? {
|
2020-11-19 21:54:29 +00:00
|
|
|
let inner: PartialUser<UserData> = bincode::deserialize_from(bytes.as_ref())?;
|
2020-12-17 00:20:54 +00:00
|
|
|
Ok(Some(RegisteredUser::new(uid, None, self.clone(), inner)))
|
2020-11-16 06:13:16 +00:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-27 23:01:49 +00:00
|
|
|
/// Produce a list of all users in the database
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
/// An panics if there is an error reading from the database or if data recieved from
|
|
|
|
/// the database is corrupt
|
|
|
|
pub fn all_users<UserData>(
|
|
|
|
&self,
|
2020-12-17 00:20:54 +00:00
|
|
|
) -> impl Iterator<Item = Result<RegisteredUser<UserData>>>
|
2020-11-27 23:01:49 +00:00
|
|
|
where
|
|
|
|
UserData: Serialize + DeserializeOwned
|
|
|
|
{
|
2020-12-17 00:20:54 +00:00
|
|
|
let this = self.clone();
|
2020-11-27 23:01:49 +00:00
|
|
|
self.users.iter()
|
2020-12-17 00:20:54 +00:00
|
|
|
.map(move|result| {
|
|
|
|
let (uid, bytes) = result?;
|
|
|
|
let inner: PartialUser<UserData> = bincode::deserialize_from(bytes.as_ref())?;
|
|
|
|
let uid = u64::from_le_bytes(uid.as_ref().try_into()?);
|
|
|
|
Ok(RegisteredUser::new(uid, None, this.clone(), inner))
|
2020-11-27 23:01:49 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-16 06:13:16 +00:00
|
|
|
/// Attempt to determine the user who sent a request based on the certificate.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// An error is thrown if there is an error reading from the database or if data
|
|
|
|
/// recieved from the database is corrupt
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
/// Pancis if the database is corrupt
|
2020-12-17 00:20:54 +00:00
|
|
|
pub fn get_user_by_cert<UserData>(
|
2020-11-16 06:13:16 +00:00
|
|
|
&self,
|
2020-12-01 19:43:15 +00:00
|
|
|
cert: Option<&[u8; 32]>
|
2020-11-16 06:13:16 +00:00
|
|
|
) -> Result<User<UserData>>
|
|
|
|
where
|
|
|
|
UserData: Serialize + DeserializeOwned
|
|
|
|
{
|
|
|
|
if let Some(certificate) = cert {
|
2020-12-17 00:20:54 +00:00
|
|
|
if let Some(user_inner) = self.lookup_certificate(*certificate)? {
|
2020-12-01 19:43:15 +00:00
|
|
|
Ok(User::SignedIn(user_inner.with_cert(*certificate)))
|
2020-11-16 06:13:16 +00:00
|
|
|
} else {
|
|
|
|
Ok(User::NotSignedIn(NotSignedInUser {
|
2020-12-01 19:43:15 +00:00
|
|
|
certificate: *certificate,
|
2020-11-16 06:13:16 +00:00
|
|
|
manager: self.clone(),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(User::Unauthenticated)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|