Add CLI for most basic user management

This commit is contained in:
Lennart
2025-04-17 23:27:43 +02:00
parent 39b1da3a8f
commit 626eff0373
9 changed files with 140 additions and 5 deletions

View File

@@ -29,6 +29,7 @@ toml.workspace = true
tokio.workspace = true
rand.workspace = true
uuid.workspace = true
clap.workspace = true
[dev-dependencies]
rstest = { workspace = true }

View File

@@ -6,7 +6,9 @@ use async_trait::async_trait;
#[async_trait]
pub trait AuthenticationProvider: 'static {
async fn get_principals(&self) -> Result<Vec<User>, crate::Error>;
async fn get_principal(&self, id: &str) -> Result<Option<User>, crate::Error>;
async fn remove_principal(&self, id: &str) -> Result<(), crate::Error>;
async fn insert_principal(&self, user: User) -> Result<(), crate::Error>;
async fn validate_password(&self, user_id: &str, password: &str)
-> Result<Option<User>, Error>;

View File

@@ -61,6 +61,10 @@ impl TomlPrincipalStore {
#[async_trait]
impl AuthenticationProvider for TomlPrincipalStore {
async fn get_principals(&self) -> Result<Vec<User>, crate::Error> {
Ok(self.principals.read().await.values().cloned().collect())
}
async fn get_principal(&self, id: &str) -> Result<Option<User>, crate::Error> {
Ok(self.principals.read().await.get(id).cloned())
}
@@ -75,6 +79,13 @@ impl AuthenticationProvider for TomlPrincipalStore {
Ok(())
}
async fn remove_principal(&self, id: &str) -> Result<(), crate::Error> {
let mut principals = self.principals.write().await;
principals.remove(id);
self.save(principals.deref())?;
Ok(())
}
async fn validate_password(
&self,
user_id: &str,

View File

@@ -12,7 +12,7 @@ use std::future::{Ready, ready};
use crate::Secret;
/// https://datatracker.ietf.org/doc/html/rfc5545#section-3.2.3
#[derive(Debug, Clone, Deserialize, Serialize, Default, PartialEq)]
#[derive(Debug, Clone, Deserialize, Serialize, Default, PartialEq, Display, clap::ValueEnum)]
#[serde(rename_all = "lowercase")]
pub enum PrincipalType {
#[default]