mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 22:52:22 +00:00
implement principal types
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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),
|
||||
};
|
||||
|
||||
|
||||
7
crates/store/src/auth/user_store.rs
Normal file
7
crates/store/src/auth/user_store.rs
Normal 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>;
|
||||
}
|
||||
Reference in New Issue
Block a user