mcapi 0.2.0

Unified async Rust client for the Minecraft ecosystem (Mojang, Modrinth, CurseForge, Yggdrasil, Blessing Skin, Loaders, Java Runtimes, and SLP)
Documentation
//! Mojang / Microsoft authentication chain, public session/UUID lookups,
//! Minecraft Services profile/skin/cape management, and Piston Meta manifests.

mod auth;
pub mod models;
mod piston;
mod public;
mod services;

use crate::error::Result;
use crate::http::HttpCore;
use crate::mirror::Mirror;
use crate::util::{define_authed, trim_trailing_slash};

pub const DEFAULT_MS_OAUTH_URL: &str = "https://login.microsoftonline.com";
pub const DEFAULT_XBL_AUTH_URL: &str = "https://user.auth.xboxlive.com";
pub const DEFAULT_XSTS_AUTH_URL: &str = "https://xsts.auth.xboxlive.com";
pub const DEFAULT_API_MOJANG_URL: &str = "https://api.mojang.com";
pub const DEFAULT_SESSIONSERVER_URL: &str = "https://sessionserver.mojang.com";
pub const DEFAULT_MINECRAFT_SERVICES_URL: &str = "https://api.minecraftservices.com";
pub const DEFAULT_PISTON_META_URL: &str = "https://piston-meta.mojang.com";

/// Known Xbox Live XSTS rejection reason codes (`XErr`) per
/// <https://minecraft.wiki/w/Microsoft_authentication#Authenticate_with_XSTS>.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum XstsErrorKind {
    /// `2148916227`: Xbox account is banned.
    AccountBanned,
    /// `2148916233`: Microsoft account does not have an Xbox profile yet.
    NoXboxAccount,
    /// `2148916234`: Xbox Live Terms of Service have not been accepted.
    TermsOfServiceNotAccepted,
    /// `2148916235`: Xbox Live is unavailable in the account's country/region.
    CountryNotSupported,
    /// `2148916236` / `2148916237`: Adult verification required (e.g. South Korea).
    AdultVerificationRequired,
    /// `2148916238`: Child account must be added to a Microsoft Family.
    ChildAccountRequiresFamily,
    /// Any other numeric `XErr` code.
    Unknown(u64),
}

impl XstsErrorKind {
    /// Classifies a raw Xbox `XErr` code.
    pub const fn from_xerr(code: u64) -> Self {
        match code {
            2148916227 => XstsErrorKind::AccountBanned,
            2148916233 => XstsErrorKind::NoXboxAccount,
            2148916234 => XstsErrorKind::TermsOfServiceNotAccepted,
            2148916235 => XstsErrorKind::CountryNotSupported,
            2148916236 | 2148916237 => XstsErrorKind::AdultVerificationRequired,
            2148916238 => XstsErrorKind::ChildAccountRequiresFamily,
            other => XstsErrorKind::Unknown(other),
        }
    }
}

/// Detailed XSTS authorization failure returned by Xbox Live.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct XstsError {
    pub xerr: u64,
    pub kind: XstsErrorKind,
    pub message: String,
    pub redirect: Option<String>,
}

/// Async client covering Microsoft-to-Minecraft authentication, Mojang public endpoints,
/// Minecraft Services account/skin/cape management, and Piston Meta manifests.
#[derive(Clone, Debug)]
pub struct MojangClient {
    pub(crate) http: HttpCore,
    base_url: Option<String>,
}

impl MojangClient {
    /// Creates a [`MojangClient`] with a default [`HttpCore`].
    pub fn new() -> Result<Self> {
        Ok(Self::from_http(HttpCore::new()?))
    }

    /// Creates a [`MojangClient`] sharing an existing [`HttpCore`].
    pub const fn from_http(http: HttpCore) -> Self {
        Self {
            http,
            base_url: None,
        }
    }

    /// Routes all Mojang/Microsoft endpoints to a custom base URL (for mock tests or proxies).
    pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
        self.base_url = Some(trim_trailing_slash(base_url));
        self
    }

    /// Returns an authenticated view bound to a Minecraft Services Bearer access token.
    pub fn authed<'a>(&'a self, mc_access_token: &'a str) -> Authed<'a> {
        Authed::new(self, mc_access_token)
    }

    /// Returns the underlying shared [`HttpCore`].
    pub fn http(&self) -> &HttpCore {
        &self.http
    }

    /// Returns the configured [`Mirror`].
    pub fn mirror(&self) -> &Mirror {
        self.http.mirror()
    }

    pub(crate) fn url(&self, official_base: &str, path: &str) -> String {
        let base = self.base_url.as_deref().unwrap_or(official_base);
        format!("{base}{path}")
    }
}

define_authed!(MojangClient);