mcapi 0.3.1

Unified async Rust client for the Minecraft ecosystem (Mojang, Modrinth, CurseForge, Yggdrasil, Blessing Skin, Loaders, BMCLAPI, and Java Runtimes)
Documentation
use serde::{Deserialize, Serialize};

use super::profile::GameProfile;
use crate::util::impl_redacted_debug;

/// Target game agent descriptor sent in `/authserver/authenticate`.
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct Agent {
    pub name: String,
    pub version: u32,
}

impl Agent {
    /// Returns the standard Minecraft Java agent (`{"name": "Minecraft", "version": 1}`).
    #[must_use]
    pub fn minecraft() -> Self {
        Self {
            name: "Minecraft".to_string(),
            version: 1,
        }
    }
}

impl Default for Agent {
    fn default() -> Self {
        Self::minecraft()
    }
}

/// Request payload for `POST /authserver/authenticate`.
#[non_exhaustive]
#[derive(Clone, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AuthenticateRequest {
    pub agent: Agent,
    pub username: String,
    pub password: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub client_token: Option<String>,
    #[serde(default)]
    pub request_user: bool,
}

impl_redacted_debug!(AuthenticateRequest {
    agent, username, request_user;
    redacted: password;
    optional_redacted: client_token,
});

impl AuthenticateRequest {
    /// Constructs an authentication request targeting Minecraft.
    #[must_use]
    pub fn new(username: impl Into<String>, password: impl Into<String>) -> Self {
        Self {
            agent: Agent::minecraft(),
            username: username.into(),
            password: password.into(),
            client_token: None,
            request_user: false,
        }
    }

    /// Sets the target game [`Agent`] (defaults to [`Agent::minecraft()`]).
    #[must_use]
    pub fn agent(mut self, agent: Agent) -> Self {
        self.agent = agent;
        self
    }

    /// Sets the client-chosen correlation token (`clientToken`).
    #[must_use]
    pub fn client_token(mut self, client_token: impl Into<String>) -> Self {
        self.client_token = Some(client_token.into());
        self
    }

    /// Sets whether the response should include the [`UserObject`] (`requestUser`).
    #[must_use]
    pub fn request_user(mut self, request_user: bool) -> Self {
        self.request_user = request_user;
        self
    }
}

/// Response payload from `POST /authserver/authenticate`.
#[derive(Clone, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AuthenticateResponse {
    pub access_token: String,
    pub client_token: String,
    #[serde(default)]
    pub available_profiles: Vec<GameProfile>,
    #[serde(default)]
    pub selected_profile: Option<GameProfile>,
    #[serde(default)]
    pub user: Option<UserObject>,
}

impl_redacted_debug!(AuthenticateResponse {
    available_profiles, selected_profile, user;
    redacted: access_token, client_token,
});

/// Request payload for `POST /authserver/refresh`.
#[non_exhaustive]
#[derive(Clone, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RefreshRequest {
    pub access_token: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub client_token: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub selected_profile: Option<GameProfile>,
    #[serde(default)]
    pub request_user: bool,
}

impl_redacted_debug!(RefreshRequest {
    selected_profile, request_user;
    redacted: access_token;
    optional_redacted: client_token,
});

impl RefreshRequest {
    /// Constructs a refresh request for `access_token`.
    #[must_use]
    pub fn new(access_token: impl Into<String>) -> Self {
        Self {
            access_token: access_token.into(),
            client_token: None,
            selected_profile: None,
            request_user: false,
        }
    }

    /// Sets the `clientToken` associated with `access_token`.
    #[must_use]
    pub fn client_token(mut self, client_token: impl Into<String>) -> Self {
        self.client_token = Some(client_token.into());
        self
    }

    /// Selects a profile when refreshing an unbound token.
    #[must_use]
    pub fn selected_profile(mut self, profile: GameProfile) -> Self {
        self.selected_profile = Some(profile);
        self
    }

    /// Sets whether the response should include the [`UserObject`] (`requestUser`).
    #[must_use]
    pub fn request_user(mut self, request_user: bool) -> Self {
        self.request_user = request_user;
        self
    }
}

/// Response payload from `POST /authserver/refresh`.
#[derive(Clone, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RefreshResponse {
    pub access_token: String,
    pub client_token: String,
    #[serde(default)]
    pub selected_profile: Option<GameProfile>,
    #[serde(default)]
    pub user: Option<UserObject>,
}

impl_redacted_debug!(RefreshResponse {
    selected_profile, user;
    redacted: access_token, client_token,
});

/// Request payload for `/authserver/validate` and `/authserver/invalidate`.
#[non_exhaustive]
#[derive(Clone, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TokenPairRequest {
    pub access_token: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub client_token: Option<String>,
}

impl_redacted_debug!(TokenPairRequest {
    ;
    redacted: access_token;
    optional_redacted: client_token,
});

impl TokenPairRequest {
    /// Constructs a validate/invalidate request for `access_token`.
    #[must_use]
    pub fn new(access_token: impl Into<String>) -> Self {
        Self {
            access_token: access_token.into(),
            client_token: None,
        }
    }

    /// Sets the optional `clientToken` matching `access_token`.
    #[must_use]
    pub fn client_token(mut self, client_token: impl Into<String>) -> Self {
        self.client_token = Some(client_token.into());
        self
    }
}

/// Request payload for `POST /authserver/signout`.
#[non_exhaustive]
#[derive(Clone, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SignoutRequest {
    pub username: String,
    pub password: String,
}

impl_redacted_debug!(SignoutRequest {
    username;
    redacted: password,
});

impl SignoutRequest {
    /// Constructs a signout request for `username` and `password`.
    #[must_use]
    pub fn new(username: impl Into<String>, password: impl Into<String>) -> Self {
        Self {
            username: username.into(),
            password: password.into(),
        }
    }
}

/// Optional user account details returned when `request_user = true`.
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct UserObject {
    pub id: String,
    #[serde(default)]
    pub properties: Vec<UserProperty>,
}

/// Key-value property on a [`UserObject`].
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct UserProperty {
    pub name: String,
    pub value: String,
}