use std::borrow::Cow;
use chrono::{DateTime, Utc};
use serde::Serialize;
use crate::types::InvitationStatus;
pub trait AuthUser: Clone + Send + Sync + Serialize + std::fmt::Debug + 'static {
fn id(&self) -> Cow<'_, str>;
fn email(&self) -> Option<&str>;
fn name(&self) -> Option<&str>;
fn email_verified(&self) -> bool;
fn image(&self) -> Option<&str>;
fn created_at(&self) -> DateTime<Utc>;
fn updated_at(&self) -> DateTime<Utc>;
fn username(&self) -> Option<&str>;
fn display_username(&self) -> Option<&str>;
fn two_factor_enabled(&self) -> bool;
fn role(&self) -> Option<&str>;
fn banned(&self) -> bool;
fn ban_reason(&self) -> Option<&str>;
fn ban_expires(&self) -> Option<DateTime<Utc>>;
fn metadata(&self) -> &serde_json::Value;
}
pub trait AuthSession: Clone + Send + Sync + Serialize + std::fmt::Debug + 'static {
fn id(&self) -> Cow<'_, str>;
fn expires_at(&self) -> DateTime<Utc>;
fn token(&self) -> &str;
fn created_at(&self) -> DateTime<Utc>;
fn updated_at(&self) -> DateTime<Utc>;
fn ip_address(&self) -> Option<&str>;
fn user_agent(&self) -> Option<&str>;
fn user_id(&self) -> Cow<'_, str>;
fn impersonated_by(&self) -> Option<&str>;
fn active_organization_id(&self) -> Option<&str>;
fn active(&self) -> bool;
}
pub trait AuthAccount: Clone + Send + Sync + Serialize + std::fmt::Debug + 'static {
fn id(&self) -> Cow<'_, str>;
fn account_id(&self) -> &str;
fn provider_id(&self) -> &str;
fn user_id(&self) -> Cow<'_, str>;
fn access_token(&self) -> Option<&str>;
fn refresh_token(&self) -> Option<&str>;
fn id_token(&self) -> Option<&str>;
fn access_token_expires_at(&self) -> Option<DateTime<Utc>>;
fn refresh_token_expires_at(&self) -> Option<DateTime<Utc>>;
fn scope(&self) -> Option<&str>;
fn password(&self) -> Option<&str>;
fn created_at(&self) -> DateTime<Utc>;
fn updated_at(&self) -> DateTime<Utc>;
}
pub trait AuthOrganization: Clone + Send + Sync + Serialize + std::fmt::Debug + 'static {
fn id(&self) -> Cow<'_, str>;
fn name(&self) -> &str;
fn slug(&self) -> &str;
fn logo(&self) -> Option<&str>;
fn metadata(&self) -> Option<&serde_json::Value>;
fn created_at(&self) -> DateTime<Utc>;
fn updated_at(&self) -> DateTime<Utc>;
}
pub trait AuthMember: Clone + Send + Sync + Serialize + std::fmt::Debug + 'static {
fn id(&self) -> Cow<'_, str>;
fn organization_id(&self) -> Cow<'_, str>;
fn user_id(&self) -> Cow<'_, str>;
fn role(&self) -> &str;
fn created_at(&self) -> DateTime<Utc>;
}
pub trait AuthInvitation: Clone + Send + Sync + Serialize + std::fmt::Debug + 'static {
fn id(&self) -> Cow<'_, str>;
fn organization_id(&self) -> Cow<'_, str>;
fn email(&self) -> &str;
fn role(&self) -> &str;
fn status(&self) -> &InvitationStatus;
fn inviter_id(&self) -> Cow<'_, str>;
fn expires_at(&self) -> DateTime<Utc>;
fn created_at(&self) -> DateTime<Utc>;
fn is_pending(&self) -> bool {
*self.status() == InvitationStatus::Pending
}
fn is_expired(&self) -> bool {
self.expires_at() < Utc::now()
}
}
pub trait AuthVerification: Clone + Send + Sync + Serialize + std::fmt::Debug + 'static {
fn id(&self) -> Cow<'_, str>;
fn identifier(&self) -> &str;
fn value(&self) -> &str;
fn expires_at(&self) -> DateTime<Utc>;
fn created_at(&self) -> DateTime<Utc>;
fn updated_at(&self) -> DateTime<Utc>;
}
pub trait AuthTwoFactor: Clone + Send + Sync + Serialize + std::fmt::Debug + 'static {
fn id(&self) -> Cow<'_, str>;
fn secret(&self) -> &str;
fn backup_codes(&self) -> &str;
fn user_id(&self) -> Cow<'_, str>;
fn created_at(&self) -> DateTime<Utc>;
fn updated_at(&self) -> DateTime<Utc>;
}
pub trait AuthApiKey: Clone + Send + Sync + Serialize + std::fmt::Debug + 'static {
fn id(&self) -> Cow<'_, str>;
fn name(&self) -> Option<&str>;
fn start(&self) -> Option<&str>;
fn prefix(&self) -> Option<&str>;
fn key_hash(&self) -> &str;
fn user_id(&self) -> Cow<'_, str>;
fn refill_interval(&self) -> Option<i64>;
fn refill_amount(&self) -> Option<i64>;
fn last_refill_at(&self) -> Option<&str>;
fn enabled(&self) -> bool;
fn rate_limit_enabled(&self) -> bool;
fn rate_limit_time_window(&self) -> Option<i64>;
fn rate_limit_max(&self) -> Option<i64>;
fn request_count(&self) -> Option<i64>;
fn remaining(&self) -> Option<i64>;
fn last_request(&self) -> Option<&str>;
fn expires_at(&self) -> Option<&str>;
fn created_at(&self) -> &str;
fn updated_at(&self) -> &str;
fn permissions(&self) -> Option<&str>;
fn metadata(&self) -> Option<&str>;
}
pub trait AuthPasskey: Clone + Send + Sync + Serialize + std::fmt::Debug + 'static {
fn id(&self) -> Cow<'_, str>;
fn name(&self) -> Option<&str>;
fn public_key(&self) -> &str;
fn user_id(&self) -> Cow<'_, str>;
fn credential_id(&self) -> &str;
fn counter(&self) -> u64;
fn device_type(&self) -> &str;
fn backed_up(&self) -> bool;
fn transports(&self) -> Option<&str>;
fn created_at(&self) -> DateTime<Utc>;
fn updated_at(&self) -> DateTime<Utc>;
fn aaguid(&self) -> Option<&str>;
fn credential(&self) -> &str;
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemberUserView {
pub id: String,
pub email: Option<String>,
pub name: Option<String>,
pub image: Option<String>,
}
impl MemberUserView {
pub fn from_user(user: &impl AuthUser) -> Self {
Self {
id: user.id().to_string(),
email: user.email().map(|s| s.to_string()),
name: user.name().map(|s| s.to_string()),
image: user.image().map(|s| s.to_string()),
}
}
}
use serde::Deserialize;