use std::collections::HashMap;
use crate::auth::jwt::Claims;
#[derive(Debug, Clone)]
pub struct AuthToken(pub Claims);
#[derive(Debug, Clone)]
pub enum AuthState {
Authorised(AuthToken),
InvalidToken,
NoToken,
}
impl AuthState {
pub fn has_claims(&self, test_claims: HashMap<&str, &str>) -> bool {
if let Self::Authorised(AuthToken(Claims { claims, .. })) = self {
for (key, val) in &test_claims {
if claims.get(&key.to_string()) != Some(&val.to_string()) {
return false;
}
}
true } else {
false
}
}
pub fn is_valid(&self) -> bool {
matches!(self, Self::Authorised(_))
}
pub fn is_invalid(&self) -> bool {
matches!(self, Self::InvalidToken)
}
pub fn has_no_token(&self) -> bool {
matches!(self, Self::NoToken)
}
}