mod auth;
mod content;
pub mod models;
mod piston;
mod public;
mod services;
use crate::error::Result;
use crate::http::HttpClient;
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_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";
pub const DEFAULT_LAUNCHER_CONTENT_URL: &str = "https://launchercontent.mojang.com";
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum XstsErrorKind {
AccountBanned,
NoXboxAccount,
TermsOfServiceNotAccepted,
CountryNotSupported,
AdultVerificationRequired,
ChildAccountRequiresFamily,
Unknown(u64),
}
impl XstsErrorKind {
#[must_use]
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),
}
}
}
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct XstsError {
pub xerr: u64,
pub kind: XstsErrorKind,
pub message: String,
pub redirect: Option<String>,
}
impl XstsError {
#[must_use]
pub fn new(
xerr: u64,
kind: XstsErrorKind,
message: impl Into<String>,
redirect: Option<String>,
) -> Self {
Self {
xerr,
kind,
message: message.into(),
redirect,
}
}
}
#[derive(Clone, Debug)]
pub struct MojangClient {
pub(crate) http: HttpClient,
base_url: Option<String>,
}
impl MojangClient {
pub fn new() -> Result<Self> {
Ok(Self::from_http(HttpClient::new()?))
}
#[must_use]
pub const fn from_http(http: HttpClient) -> Self {
Self {
http,
base_url: None,
}
}
#[must_use]
pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
self.base_url = Some(trim_trailing_slash(base_url));
self
}
#[must_use]
pub fn authed<'a>(&'a self, mc_access_token: &'a str) -> Authed<'a> {
Authed::new(self, mc_access_token)
}
#[must_use]
pub fn http(&self) -> &HttpClient {
&self.http
}
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);