Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update xbox_security_token with less general error responses #12

Merged
merged 3 commits into from
Apr 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 45 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

use getset::{CopyGetters, Getters};
use nutype::nutype;
use reqwest::Client;
use reqwest::{Client, StatusCode};
use serde::{Deserialize, Serialize};
use serde_json::json;
use thiserror::Error;
Expand Down Expand Up @@ -92,6 +92,14 @@
#[error(transparent)]
Reqwest(#[from] reqwest::Error),

/// Account belongs to a minor who needs to be added to a microsoft family
#[error("Minor must be added to microsoft family")]
AddToFamily,

/// Account does not have xbox, user must create an xbox account to continue
#[error("Account does not have xbox")]
NoXbox,

/// Claims were missing from the response
#[error("missing claims from response")]
MissingClaims,
Expand Down Expand Up @@ -131,6 +139,25 @@
display_claims: HashMap<String, Vec<HashMap<String, String>>>,
}

/// The error response from Xbox when authenticating with a Microsoft token
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct XboxLiveAuthenticationResponseError {
/// Always zero
identity: String,

/// Error id
/// 2148916238 means <18 and needs to be added to microsoft family
/// 2148916233 means xbox account needs to be created
x_err: i64,

/// Message about error
message: String,

/// Where to go to fix the error as a user
redirect: String,
}

/// The flow for authenticating with a Microsoft access token and getting a
/// Minecraft access token.
pub struct MinecraftAuthorizationFlow {
Expand Down Expand Up @@ -188,9 +215,23 @@
}))
.send()
.await?;
response.error_for_status_ref()?;
let xbox_security_token_resp: XboxLiveAuthenticationResponse = response.json().await?;
Ok(xbox_security_token_resp)
if response.status() == StatusCode::UNAUTHORIZED {
let xbox_security_token_err_resp_res = response.json().await;
if xbox_security_token_err_resp_res.is_err() {
return Err(MinecraftAuthorizationError::MissingClaims);
}
let xbox_security_token_err_resp: XboxLiveAuthenticationResponseError =
xbox_security_token_err_resp_res.expect("This should succeed always");
match xbox_security_token_err_resp.x_err {
2148916238 => Err(MinecraftAuthorizationError::AddToFamily),
2148916233 => Err(MinecraftAuthorizationError::NoXbox),
_ => Err(MinecraftAuthorizationError::MissingClaims),
}
} else {
response.error_for_status_ref()?;
let xbox_security_token_resp: XboxLiveAuthenticationResponse = response.json().await?;
Ok(xbox_security_token_resp)
}
}

async fn xbox_token(
Expand All @@ -214,11 +255,11 @@
response.error_for_status_ref()?;
let xbox_resp: XboxLiveAuthenticationResponse = response.json().await?;
let xbox_token = xbox_resp.token;
let user_hash = xbox_resp
.display_claims
.get("xui")
.ok_or(MinecraftAuthorizationError::MissingClaims)?
.get(0)

Check warning on line 262 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Lint with Clippy

cargo-clippy: accessing first element with `xbox_resp .display_claims .get("xui") .ok_or(MinecraftAuthorizationError::MissingClaims)?.get(0)`

warning: accessing first element with `xbox_resp .display_claims .get("xui") .ok_or(MinecraftAuthorizationError::MissingClaims)?.get(0)` --> src/lib.rs:258:25 | 258 | let user_hash = xbox_resp | _________________________^ 259 | | .display_claims 260 | | .get("xui") 261 | | .ok_or(MinecraftAuthorizationError::MissingClaims)? 262 | | .get(0) | |___________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#get_first = note: `#[warn(clippy::get_first)]` on by default help: try | 258 ~ let user_hash = xbox_resp 259 + .display_claims 260 + .get("xui") 261 + .ok_or(MinecraftAuthorizationError::MissingClaims)?.first() |
.ok_or(MinecraftAuthorizationError::MissingClaims)?
.get("uhs")
.ok_or(MinecraftAuthorizationError::MissingClaims)?
Expand Down
Loading