Skip to content

Commit

Permalink
Update xbox_security_token with less general error responses
Browse files Browse the repository at this point in the history
  • Loading branch information
supercoolspy committed Apr 13, 2024
1 parent a5b97ab commit dafe660
Showing 1 changed file with 59 additions and 6 deletions.
65 changes: 59 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use std::fmt::Debug;

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 All @@ -67,8 +67,8 @@ const XBOX_XSTS_AUTHORIZE: &str = "https://xsts.auth.xboxlive.com/xsts/authorize

/// Represents a Minecraft access token
#[nutype(
validate(not_empty),
derive(Clone, PartialEq, Eq, Hash, Deserialize, Serialize, AsRef, Into)
validate(not_empty),
derive(Clone, PartialEq, Eq, Hash, Deserialize, Serialize, AsRef, Into)
)]
pub struct MinecraftAccessToken(String);

Expand All @@ -92,6 +92,14 @@ pub enum MinecraftAuthorizationError {
#[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 @@ struct XboxLiveAuthenticationResponse {
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,35 @@ impl MinecraftAuthorizationFlow {
}))
.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::Reqwest(
response
.error_for_status_ref()
.err()
.expect("This error should always happen"),
));
}
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),
_ => {
return Err(MinecraftAuthorizationError::Reqwest(
response

Check failure on line 235 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Lint with Clippy

cargo-clippy: borrow of moved value: `response`

error[E0382]: borrow of moved value: `response` --> src/lib.rs:235:25 | 205 | let response = self | -------- move occurs because `response` has type `reqwest::Response`, which does not implement the `Copy` trait ... 219 | let xbox_security_token_err_resp_res = response.json().await; | ------ `response` moved due to this method call ... 235 | response | ^^^^^^^^ value borrowed here after move | note: `reqwest::Response::json` takes ownership of the receiver `self`, which moves `response` --> /home/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/reqwest-0.12.3/src/async_impl/response.rs:265:44 | 265 | pub async fn json<T: DeserializeOwned>(self) -> crate::Result<T> { | ^^^^
.error_for_status_ref()
.err()
.expect("This error should always happen"),
))
},
}
} 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 Down

0 comments on commit dafe660

Please sign in to comment.