reginleif/auth/
minecraft.rs

1use std::time::Duration;
2use reqwest::Client;
3use serde::{Deserialize, Serialize};
4use serde_json::json;
5use reginleif_macro::{Expirable, NoRefresh};
6use crate::auth::constant::{MINECRAFT_LOGIN_WITH_XBOX, MINECRAFT_PROFILE};
7use crate::auth::xbox::XboxSecurityToken;
8use reginleif_utils::serde_convert::{duration_to_sec, sec_to_duration};
9
10/// Minecraft Auth
11/// 
12/// This struct is used to authenticate the user with Minecraft Auth Server.
13#[derive(Serialize, Deserialize,Debug,Clone, Expirable, NoRefresh)]
14pub struct MinecraftAuth {
15    
16    /// UUID of MinecraftAuth, note this is **NOT** the UUID of the player.
17    pub username: String,
18    /// The access token you can use to access Minecraft.
19    pub access_token: String,
20    
21    /// The expires time of the access token.
22    #[serde(deserialize_with = "sec_to_duration", serialize_with = "duration_to_sec")]
23    #[dur]
24    pub expires_in: Duration,
25    /// The token type of the access token. Always being "bearer"
26    pub token_type: String,
27}
28
29impl MinecraftAuth{
30    
31    /// Fetch Minecraft Auth
32    /// 
33    /// This function will fetch Minecraft Auth from the given Xbox Security Token.
34    /// 
35    /// # Arguments
36    /// * `client` - The reqwest client
37    /// * `xbox_security_token` - The Xbox Security Token you get from [XboxSecurityToken::fetch](crate::auth::xbox::XboxSecurityToken::fetch)
38    pub async fn fetch(client: &Client, xbox_security_token: XboxSecurityToken) -> anyhow::Result<Self>{
39
40        let res = client
41            .post(MINECRAFT_LOGIN_WITH_XBOX)
42            .header("Content-Type", "application/json")
43            .json(&json!({
44                "identityToken": format!("XBL3.0 x={};{}",xbox_security_token.uhs,xbox_security_token.token)
45            }))
46            .send()
47            .await?
48            .error_for_status()?
49            .json()
50            .await?;
51
52        Ok(res)
53    }
54    
55}
56
57/// Minecraft Profile
58/// 
59/// This struct is used to store Minecraft Profile.If user doesn't have game, the profile won't exist too!
60#[derive(Debug, Serialize, Deserialize, Clone)]
61#[serde(rename_all = "camelCase")]
62pub struct Profile {
63    
64    /// uuid of the player
65    pub id: String,
66    /// name of the player
67    pub name: String,
68    /// the skins of the player
69    pub skins: Vec<Skin>,
70    /// the capes of the player have
71    pub capes: Vec<Caps>,
72}
73
74impl Profile{
75    /// Fetch Minecraft Profile
76    /// 
77    /// This function will fetch Minecraft Profile from the given Minecraft Auth.
78    /// If the user doesn't have game, the profile won't exist too!
79    /// 
80    /// # Arguments
81    /// * `client` - The reqwest client
82    /// * `microsoft_auth` - The Microsoft Auth you get from [MinecraftAuth::fetch](crate::auth::minecraft::MinecraftAuth::fetch)
83    pub async fn fetch(client: &Client, microsoft_auth: &MinecraftAuth) -> anyhow::Result<Profile>{
84        let res = client
85            .get(MINECRAFT_PROFILE)
86            .bearer_auth(&microsoft_auth.access_token)
87            .send()
88            .await?
89            .error_for_status()?
90            .json()
91            .await?;
92
93        Ok(res)
94    }
95}
96
97
98/// Minecraft Skin
99#[derive(Debug, Serialize, Deserialize, Clone)]
100#[serde(rename_all = "camelCase")]
101pub struct Skin {
102    /// the id of the skin
103    pub id: String,
104    /// ACTIVE or INACTIVE
105    pub state: String,
106    /// the url of the skin you can get.
107    pub url: String,
108    pub texture_key: String,
109    /// Classic or Slim
110    pub variant: String,
111}
112
113/// Minecraft Capes
114#[derive(Debug, Serialize, Deserialize, Clone)]
115#[serde(rename_all = "camelCase")]
116pub struct Caps {
117    /// the id of the cape
118    pub id: String,
119    /// ACTIVE or INACTIVE
120    pub state: String,
121    /// the url of the cape you can get.
122    pub url: String,
123    /// the alias of the cape
124    pub alias: String,
125}