reginleif/auth/
minecraft.rs1use 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#[derive(Serialize, Deserialize,Debug,Clone, Expirable, NoRefresh)]
14pub struct MinecraftAuth {
15
16 pub username: String,
18 pub access_token: String,
20
21 #[serde(deserialize_with = "sec_to_duration", serialize_with = "duration_to_sec")]
23 #[dur]
24 pub expires_in: Duration,
25 pub token_type: String,
27}
28
29impl MinecraftAuth{
30
31 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#[derive(Debug, Serialize, Deserialize, Clone)]
61#[serde(rename_all = "camelCase")]
62pub struct Profile {
63
64 pub id: String,
66 pub name: String,
68 pub skins: Vec<Skin>,
70 pub capes: Vec<Caps>,
72}
73
74impl Profile{
75 pub async fn fetch(client: &Client, microsoft_auth: &MinecraftAuth) -> anyhow::Result<Profile>{
84 let res = client
85 .get(MINECRAFT_PROFILE)
86 .bearer_auth(µsoft_auth.access_token)
87 .send()
88 .await?
89 .error_for_status()?
90 .json()
91 .await?;
92
93 Ok(res)
94 }
95}
96
97
98#[derive(Debug, Serialize, Deserialize, Clone)]
100#[serde(rename_all = "camelCase")]
101pub struct Skin {
102 pub id: String,
104 pub state: String,
106 pub url: String,
108 pub texture_key: String,
109 pub variant: String,
111}
112
113#[derive(Debug, Serialize, Deserialize, Clone)]
115#[serde(rename_all = "camelCase")]
116pub struct Caps {
117 pub id: String,
119 pub state: String,
121 pub url: String,
123 pub alias: String,
125}