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";
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum XstsErrorKind {
AccountBanned,
NoXboxAccount,
TermsOfServiceNotAccepted,
CountryNotSupported,
AdultVerificationRequired,
ChildAccountRequiresFamily,
Unknown(u64),
}
impl XstsErrorKind {
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),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct XstsError {
pub xerr: u64,
pub kind: XstsErrorKind,
pub message: String,
pub redirect: Option<String>,
}
#[derive(Clone, Debug)]
pub struct MojangClient {
pub(crate) http: HttpCore,
base_url: Option<String>,
}
impl MojangClient {
pub fn new() -> Result<Self> {
Ok(Self::from_http(HttpCore::new()?))
}
pub const fn from_http(http: HttpCore) -> Self {
Self {
http,
base_url: None,
}
}
pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
self.base_url = Some(trim_trailing_slash(base_url));
self
}
pub fn authed<'a>(&'a self, mc_access_token: &'a str) -> Authed<'a> {
Authed::new(self, mc_access_token)
}
pub fn http(&self) -> &HttpCore {
&self.http
}
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);