lyceris 1.1.3

An open source Minecraft launcher library.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use oauth2::{AuthUrl, ClientId, CsrfToken, RedirectUrl, Scope, TokenUrl};
use reqwest::{Client, Method};
use serde::{Deserialize, Serialize};
use std::{
    collections::HashMap,
    time::{SystemTime, UNIX_EPOCH},
};

use crate::{error::Error, http::fetch::fetch_with_options, util::base64::decode_base64};

/// The client ID for Microsoft authentication.
pub static CLIENT_ID: &str = "00000000402b5328";
/// The redirect URI for Microsoft authentication.
pub static REDIRECT_URI: &str = "https://login.live.com/oauth20_desktop.srf";
/// The authorization URL for Microsoft authentication.
pub static AUTH_URL: &str = "https://login.live.com/oauth20_authorize.srf";
/// The token URL for Microsoft authentication.
pub static TOKEN_URL: &str = "https://login.live.com/oauth20_token.srf";

/// Represents the token received from Microsoft after authentication.
#[derive(Serialize, Deserialize, Debug, Clone)]
struct MSToken {
    access_token: String,
    refresh_token: String,
}

/// Represents the token received from Xbox Live after authentication.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "PascalCase")]
struct XboxToken {
    issue_instant: String,
    not_after: String,
    token: String,
}

/// Represents the response from the Minecraft API after successful authentication.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MinecraftResponse {
    pub username: String,
    pub access_token: String,
    pub expires_in: u32,
}

/// Represents the token received from Xbox Live's XSTS service.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "PascalCase")]
struct XstsToken {
    token: String,
    display_claims: DisplayClaims,
}

/// Represents the display claims returned by the XSTS token.
#[derive(Serialize, Deserialize, Debug, Clone)]
struct DisplayClaims {
    xui: Vec<Xui>,
}

/// Represents a user's Xbox Live identity.
#[derive(Serialize, Deserialize, Debug, Clone)]
struct Xui {
    uhs: String,
}

/// Represents a user's skin in Minecraft.
#[derive(Serialize, Deserialize, Debug, Clone)]
struct Skin {
    id: String,
    state: String,
    url: String,
    variant: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    alias: Option<String>,
}

/// Represents a user's cape in Minecraft.
#[derive(Serialize, Deserialize, Debug, Clone)]
struct Cape {
    id: String,
    state: String,
    url: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    alias: Option<String>,
}

/// Represents a user's profile in Minecraft, including skins and capes.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserProfile {
    pub id: Option<String>,
    pub name: Option<String>,
    skins: Option<Vec<Skin>>,
    capes: Option<Vec<Cape>>,
    path: Option<String>,
    error: Option<String>,
    #[serde(rename = "errorMessage")]
    error_message: Option<String>,
}

/// Represents the decoded JWT from Minecraft authentication.
#[derive(Debug, Deserialize, Clone)]
pub struct MCJWTDecoded {
    xuid: String,
    exp: u64,
}

/// Represents a Minecraft account with authentication details.
#[derive(Deserialize, Serialize, Default, Debug, Clone)]
pub struct MinecraftAccount {
    pub xuid: String,
    pub exp: u64,
    pub uuid: String,
    pub username: String,
    pub access_token: String,
    pub refresh_token: String,
    pub client_id: String,
}

/// Creates the authorization link for Microsoft authentication.
///
/// # Returns
/// A result containing the authorization URL as a string.
pub fn create_link() -> crate::Result<String> {
    let auth_url = AuthUrl::new(AUTH_URL.to_string())?;
    let token_url = TokenUrl::new(TOKEN_URL.to_string())?;

    let client = oauth2::basic::BasicClient::new(
        ClientId::new(CLIENT_ID.to_string()),
        None,
        auth_url,
        Some(token_url),
    )
    .set_redirect_uri(RedirectUrl::new(REDIRECT_URI.to_string())?);

    let (authorize_url, _) = client
        .authorize_url(CsrfToken::new_random)
        .add_scope(Scope::new(
            "service::user.auth.xboxlive.com::MBI_SSL".to_string(),
        ))
        .add_extra_param("prompt", "select_account")
        .url();

    Ok(authorize_url.to_string())
}

/// Authenticates the user using the provided authorization code.
///
/// # Parameters
/// - `code`: The authorization code received from the Microsoft authentication process.
/// - `client`: The HTTP client used for making requests.
///
/// # Returns
/// A result containing the authenticated `MinecraftAccount`.
pub async fn authenticate(
    code: String,
    client: &Client,
) -> crate::Result<MinecraftAccount> {
    let ms_token = get_ms_token(&code, client).await?;
    let xbox_token = get_xbox_token(&ms_token.access_token, client).await?;
    let xsts_token = get_xsts_token(&xbox_token.token, client).await?;
    let userhash = xsts_token
        .display_claims
        .xui
        .first()
        .ok_or(Error::Authentication("No XUI claims found.".to_string()))?
        .uhs
        .clone();

    obtain_minecraft_account(&xsts_token.token, &userhash, ms_token.refresh_token, client).await
}

/// Refreshes the access token using the provided refresh token.
///
/// # Parameters
/// - `refresh_token`: The refresh token used to obtain a new access token.
/// - `client`: The HTTP client used for making requests.
///
/// # Returns
/// A result containing the refreshed `MinecraftAccount`.
pub async fn refresh(
    refresh_token: String,
    client: &Client,
) -> crate::Result<MinecraftAccount> {
    let token_response = client
        .post(TOKEN_URL)
        .form(&[ 
            ("client_id", CLIENT_ID),
            ("scope", "service::user.auth.xboxlive.com::MBI_SSL"),
            ("grant_type", "refresh_token"),
            ("redirect_uri", REDIRECT_URI),
            ("refresh_token", &refresh_token),
        ])
        .send()
        .await?;

    let ms_token: MSToken = token_response.json().await?;
    let xbox_token = get_xbox_token(&ms_token.access_token, client).await?;
    let xsts_token = get_xsts_token(&xbox_token.token, client).await?;
    let userhash = xsts_token
        .display_claims
        .xui
        .first()
        .ok_or(Error::Authentication("No XUI claims found.".to_string()))?
        .uhs
        .clone();

    obtain_minecraft_account(&xsts_token.token, &userhash, ms_token.refresh_token, client).await
}

/// Obtains the Minecraft account details using the provided tokens.
///
/// # Parameters
/// - `xsts_token`: The XSTS token for authentication.
/// - `userhash`: The user hash obtained from the XSTS token.
/// - `refresh_token`: The refresh token for obtaining new access tokens.
/// - `client`: The HTTP client used for making requests.
///
/// # Returns
/// A result containing the authenticated `MinecraftAccount`.
async fn obtain_minecraft_account(
    xsts_token: &str,
    userhash: &str,
    refresh_token: String,
    client: &Client,
) -> crate::Result<MinecraftAccount> {
    let token = get_minecraft_token(xsts_token, userhash, client).await?;
    let profile = get_profile(token.access_token.clone()).await?;
    let jwt = parse_login_token(&token.access_token)?;

    Ok(MinecraftAccount {
        xuid: jwt.xuid,
        exp: jwt.exp,
        uuid: profile.id.unwrap_or_default(),
        username: profile.name.unwrap_or_default(),
        access_token: token.access_token,
        refresh_token,
        client_id: CLIENT_ID.to_string(),
    })
}

/// Retrieves the Microsoft token using the provided authorization code.
///
/// # Parameters
/// - `code`: The authorization code received from the Microsoft authentication process.
/// - `client`: The HTTP client used for making requests.
///
/// # Returns
/// A result containing the `MSToken`.
async fn get_ms_token(code: &str, client: &Client) -> crate::Result<MSToken> {
    let token_response = client
        .post(TOKEN_URL)
        .form(&[
            ("client_id", CLIENT_ID),
            ("scope", "service::user.auth.xboxlive.com::MBI_SSL"),
            ("code", code),
            ("grant_type", "authorization_code"),
            ("redirect_uri", REDIRECT_URI),
        ])
        .send()
        .await?;

    let ms_token: MSToken = token_response.json().await?;
    Ok(ms_token)
}

/// Retrieves the Xbox token using the provided Microsoft token.
///
/// # Parameters
/// - `ms_token`: The Microsoft access token.
/// - `client`: The HTTP client used for making requests.
///
/// # Returns
/// A result containing the `XboxToken`.
async fn get_xbox_token(ms_token: &str, client: &Client) -> crate::Result<XboxToken> {
    let body = serde_json::json!( {
        "Properties": {
            "AuthMethod": "RPS",
            "SiteName": "user.auth.xboxlive.com",
            "RpsTicket": ms_token
        },
        "RelyingParty": "http://auth.xboxlive.com",
        "TokenType": "JWT"
    });

    fetch_token(
        "https://user.auth.xboxlive.com/user/authenticate",
        body,
        client,
    )
    .await
}

/// Retrieves the XSTS token using the provided Xbox token.
///
/// # Parameters
/// - `xbox_token`: The Xbox token for authentication.
/// - `client`: The HTTP client used for making requests.
///
/// # Returns
/// A result containing the `XstsToken`.
async fn get_xsts_token(xbox_token: &str, client: &Client) -> crate::Result<XstsToken> {
    let body = serde_json::json!( {
        "Properties": {
            "SandboxId": "RETAIL",
            "UserTokens": [xbox_token]
        },
        "RelyingParty": "rp://api.minecraftservices.com/",
        "TokenType": "JWT"
    });

    fetch_token(
        "https://xsts.auth.xboxlive.com/xsts/authorize",
        body,
        client,
    )
    .await
}

/// Fetches a token from the specified URL using the provided body.
///
/// # Parameters
/// - `url`: The URL to fetch the token from.
/// - `body`: The body of the request containing necessary parameters.
/// - `client`: The HTTP client used for making requests.
///
/// # Returns
/// A result containing the deserialized response of type `T`.
async fn fetch_token<T: for<'de> Deserialize<'de>>(
    url: &str,
    body: serde_json::Value,
    client: &Client,
) -> crate::Result<T> {
    let token_response: T = fetch_with_options(
        url,
        Some(crate::http::fetch::FetchOptions {
            method: Method::POST,
            headers: HashMap::default(),
            query_params: HashMap::default(),
            body: Some(body),
        }),
        client,
    )
    .await?;

    Ok(token_response)
}

/// Returns player's Minecraft data.
///
/// # Parameters
/// - `xsts_token`: Xbox token.
/// - `userhash`: Hash value.
/// - `client`: Reqwest client.
/// 
/// # Returns
/// A result containing the `MinecraftResponse`.
async fn get_minecraft_token(
    xsts_token: &str,
    userhash: &str,
    client: &Client,
) -> crate::Result<MinecraftResponse> {
    let body = serde_json::json!({
        "identityToken": format!("XBL3.0 x={};{}", userhash, xsts_token)
    });

    fetch_with_options(
        "https://api.minecraftservices.com/authentication/login_with_xbox",
        Some(crate::http::fetch::FetchOptions {
            method: Method::POST,
            headers: HashMap::default(),
            query_params: HashMap::default(),
            body: Some(body),
        }),
        client,
    )
    .await
}

/// Parses login token.
///
/// # Parameters
/// - `mc_token`: Token to be parsed.
///
/// # Returns
/// A result containing the `MCJWTDecoded`.
fn parse_login_token(mc_token: &str) -> crate::Result<MCJWTDecoded> {
    let base64_url = mc_token
        .split('.')
        .nth(1)
        .ok_or(Error::MalformedToken(mc_token.to_string()))?;

    let decoded_bytes = decode_base64(base64_url)?;
    let json_payload = String::from_utf8(decoded_bytes)?;

    let decoded: MCJWTDecoded = serde_json::from_str(&json_payload)?;

    Ok(decoded)
}

/// Retrieves the Minecraft profile using the provided access token.
///
/// # Parameters
/// - `access_token`: The access token for authentication.
///
/// # Returns
/// A result containing the `UserProfile`.
async fn get_profile(access_token: String) -> crate::Result<UserProfile> {
    let api_url = "https://api.minecraftservices.com/minecraft/profile";
    let client = Client::new();

    let response = client
        .get(api_url)
        .header("Authorization", format!("Bearer {}", access_token))
        .send()
        .await?;

    let profile = response.json::<UserProfile>().await?;

    if let Some(error) = profile.error {
        match error.as_str() {
            "NOT_FOUND" => Err(Error::Authentication(
                "Account does not own Minecraft.".to_string(),
            )),
            _ => Err(Error::Authentication(error)),
        }
    } else {
        Ok(profile)
    }
}

/// Validates the expiration time of the token.
///
/// # Parameters
/// - `exp`: The expiration time of the token.
///
/// # Returns
/// A boolean indicating whether the token is still valid.
pub fn validate(exp: u64) -> bool {
    exp > SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_err(|_| "System time error")
        .unwrap()
        .as_secs()
}