implement principal types

This commit is contained in:
Lennart
2025-02-02 15:12:15 +01:00
parent bb8f2bb370
commit aa6bd1cbc0
9 changed files with 75 additions and 36 deletions

View File

@@ -1,6 +1,7 @@
pub mod middleware;
pub mod static_user_store;
pub mod user;
mod user_store;
use crate::error::Error;
use async_trait::async_trait;
@@ -12,3 +13,4 @@ pub trait AuthenticationProvider: 'static {
pub use middleware::AuthenticationMiddleware;
pub use static_user_store::{StaticUserStore, StaticUserStoreConfig};
pub use user::User;
pub use user_store::UserStore;

View File

@@ -3,7 +3,7 @@ use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use super::AuthenticationProvider;
use super::{AuthenticationProvider, UserStore};
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub struct StaticUserStoreConfig {
@@ -23,11 +23,18 @@ impl StaticUserStore {
}
}
#[async_trait]
impl UserStore for StaticUserStore {
async fn get_user(&self, id: &str) -> Result<Option<User>, crate::Error> {
Ok(self.users.get(id).cloned())
}
}
#[async_trait]
impl AuthenticationProvider for StaticUserStore {
async fn validate_user_token(&self, user_id: &str, token: &str) -> Result<Option<User>, Error> {
let user: User = match self.users.get(user_id) {
Some(user) => user.clone(),
let user: User = match self.get_user(user_id).await? {
Some(user) => user,
None => return Ok(None),
};

View File

@@ -0,0 +1,7 @@
use super::User;
use async_trait::async_trait;
#[async_trait]
pub trait UserStore: 'static {
async fn get_user(&self, id: &str) -> Result<Option<User>, crate::Error>;
}