elemental-core 0.3.0

Core library for Elemental, providing common utilities and abstractions used across the project.
Documentation
#[cfg(feature = "microsoft-auth")]
pub mod microsoft;
pub mod offline;
#[cfg(feature = "yggdrasil-auth")]
pub mod yggdrasil;

use std::any::Any;

use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::auth::credential::Credential;

/// Stable identity of a persisted account.
///
/// This is independent from the Minecraft profile UUID because Yggdrasil
/// profiles from different API roots may share the same profile ID.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct AccountId(Uuid);

impl AccountId {
    pub fn new() -> Self {
        Self(Uuid::new_v4())
    }

    pub fn from_uuid(uuid: Uuid) -> Self {
        Self(uuid)
    }

    /// Derives a stable account identity from its authorizer and provider-specific identity.
    pub fn derive(authorizer_id: &str, identity: &str) -> Self {
        Self(Uuid::new_v5(
            &Uuid::NAMESPACE_URL,
            format!("elemental:{authorizer_id}:{identity}").as_bytes(),
        ))
    }
}

impl Default for AccountId {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Display for AccountId {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(formatter)
    }
}

/// Persisted authentication state owned by one [`crate::auth::authorizers::Authorizer`].
pub trait Account: Send + Sync + erased_serde::Serialize + Any {
    fn account_id(&self) -> AccountId;

    fn authorizer_id(&self) -> &'static str;

    fn display_name(&self) -> &str;

    fn needs_refresh(&self) -> bool {
        false
    }

    fn credential(&self) -> Credential;
}