elemental-core 0.3.0

Core library for Elemental, providing common utilities and abstractions used across the project.
Documentation
pub use nidhogg::types::meta::{Links, Meta, ServerMeta};
use nidhogg::types::{profile::Profile, user::User};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::auth::accounts::offline::offline_uuid;
use crate::auth::accounts::{Account, AccountId};
use crate::auth::credential::{Credential, CredentialKind};

pub const YGGDRASIL_AUTHORIZER_ID: &str = "yggdrasil";

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct YggdrasilProfile {
    pub id: String,
    pub name: String,
}

impl From<&Profile> for YggdrasilProfile {
    fn from(profile: &Profile) -> Self {
        Self {
            id: profile.id.clone(),
            name: profile.name.clone(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct YggdrasilAccount {
    pub account_id: AccountId,
    pub base_url: Option<String>,
    pub user_id: Option<String>,
    pub preferred_language: Option<String>,
    pub access_token: String,
    pub client_token: String,
    pub selected_profile: YggdrasilProfile,
}

impl YggdrasilAccount {
    /// Creates an offline Yggdrasil account without a persisted API root.
    ///
    /// The launcher supplies a loopback auth server when one is needed.
    pub fn offline(username: String) -> Self {
        let uuid = offline_uuid(&username);
        Self::offline_with_uuid(username, uuid)
    }

    /// Creates an offline account with an explicit player UUID.
    pub fn offline_with_uuid(username: String, uuid: Uuid) -> Self {
        Self {
            account_id: AccountId::derive(
                YGGDRASIL_AUTHORIZER_ID,
                &format!("offline:{}", uuid.simple()),
            ),
            base_url: None,
            user_id: None,
            preferred_language: None,
            access_token: String::new(),
            client_token: String::new(),
            selected_profile: YggdrasilProfile {
                id: uuid.to_string(),
                name: username,
            },
        }
    }
}

pub(crate) fn user_id(user: Option<&User>) -> Option<String> {
    user.map(|user| user.id.clone())
}

pub(crate) fn preferred_language(user: Option<&User>) -> Option<String> {
    user.and_then(User::get_preferred_language).cloned()
}

impl Account for YggdrasilAccount {
    fn account_id(&self) -> AccountId {
        self.account_id
    }

    fn authorizer_id(&self) -> &'static str {
        YGGDRASIL_AUTHORIZER_ID
    }

    fn display_name(&self) -> &str {
        &self.selected_profile.name
    }

    fn credential(&self) -> Credential {
        let (kind, auth_server) = match &self.base_url {
            Some(base_url) => (CredentialKind::Mojang, Some(base_url.clone())),
            None => (CredentialKind::Legacy, None),
        };
        Credential {
            username: self.selected_profile.name.clone(),
            uuid: self.selected_profile.id.clone(),
            access_token: self.access_token.clone(),
            kind,
            xuid: None,
            client_id: None,
            auth_server,
        }
    }
}