use murgamu::prelude::*;
use std::collections::HashSet;
pub trait HasRole {
fn role_name(&self) -> &'static str;
}
#[guard]
pub struct AuthGuard<R: HasRole + Send + Sync + 'static> {
_marker: std::marker::PhantomData<R>,
}
impl<R: HasRole + Send + Sync + 'static> AuthGuard<R> {
pub fn new() -> Self {
Self {
_marker: std::marker::PhantomData,
}
}
pub async fn check(&self, ctx: &MurRequestContext) -> bool {
if ctx.is_public_route() {
return true;
}
let token = match ctx.bearer_token() {
Some(t) => t,
None => return false,
};
let user_roles = match self.validate_token_and_get_roles(token).await {
Some(roles) => roles,
None => return false,
};
if let Some(allowed_roles) = ctx.allowed_roles() {
if allowed_roles.is_empty() {
return true; }
return user_roles.iter().any(|r| allowed_roles.contains(r));
}
true }
async fn validate_token_and_get_roles(&self, token: &str) -> Option<HashSet<String>> {
todo!()
}
}