BasicOIDC/src/data/session_identity.rs

40 lines
924 B
Rust
Raw Normal View History

2022-04-01 16:59:17 +00:00
use std::fmt::Display;
use actix_identity::Identity;
use crate::data::user::User;
pub struct SessionIdentity {
pub id: String,
pub is_admin: bool,
}
impl SessionIdentity {
pub fn from_user(user: &User) -> Self {
Self {
id: user.uid.clone(),
is_admin: user.admin,
}
}
pub fn deserialize<D: Display>(input: D) -> Self {
let input = input.to_string();
let mut iter = input.split('-');
Self {
id: iter.next().unwrap_or_default().to_string(),
is_admin: iter.next().unwrap_or_default() == "true",
}
}
pub fn serialize(&self) -> String {
format!("{}-{}", self.id, self.is_admin)
}
pub fn is_authenticated(i: &Identity) -> bool {
i.identity()
.as_ref()
.map(Self::deserialize)
.map(|s| !s.id.is_empty())
.unwrap_or(false)
}
}