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
|
use std::borrow::Cow;
use std::collections::HashMap;
use chrono::{DateTime, Utc};
use log::debug;
use oauth2::AccessToken;
use reqwest::{Method};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::auth::AuthError;
use crate::auth::types::Token;
const XBOX_LIVE_AUTH: &str = "https://user.auth.xboxlive.com/user/authenticate";
const XBOX_LIVE_XSTS: &str = "https://xsts.auth.xboxlive.com/xsts/authorize";
#[derive(Debug, Serialize)]
#[serde(rename_all = "PascalCase")]
struct XboxLiveAuthRequestProperties<'a> {
auth_method: &'a str,
site_name: &'a str,
rps_ticket: &'a str
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "PascalCase")]
struct XboxLiveAuthRequest<'a> {
properties: XboxLiveAuthRequestProperties<'a>,
relying_party: &'a str,
token_type: &'a str
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
struct XboxLiveAuthResponse {
token: String,
not_after: DateTime<Utc>
}
pub async fn xbox_live_login(client: &reqwest::Client, access_token: &AccessToken, azure: bool) -> Result<Token, AuthError> {
debug!("MSA performing xbox live login ({azure})");
let ticket = match azure {
true => Cow::Owned(format!("d={}", access_token.secret())),
_ => Cow::Borrowed(access_token.secret().as_str())
};
let request = XboxLiveAuthRequest {
properties: XboxLiveAuthRequestProperties {
auth_method: "RPS",
site_name: "user.auth.xboxlive.com",
rps_ticket: ticket.as_ref()
},
relying_party: "http://auth.xboxlive.com",
token_type: "JWT"
};
let res: XboxLiveAuthResponse = super::build_json_request(client, XBOX_LIVE_AUTH, Method::POST).json(&request).send().await
.and_then(|r| r.error_for_status())
.map_err(|e| AuthError::Request { what: "xbox live auth", error: e })?
.json().await.map_err(|e| AuthError::Request { what: "xbox live auth (decode)", error: e })?;
Ok(Token {
value: res.token,
expire: Some(res.not_after)
})
}
#[derive(Serialize, Debug)]
#[serde(rename_all = "PascalCase")]
struct XSTSAuthRequest<'a> {
properties: XSTSAuthRequestProperties<'a>,
relying_party: &'a str,
token_type: &'a str
}
#[derive(Serialize, Debug)]
#[serde(rename_all = "PascalCase")]
struct XSTSAuthRequestProperties<'a> {
sandbox_id: &'a str,
user_tokens: &'a[&'a str]
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub(super) struct XSTSAuthSuccessResponse {
token: String,
#[serde(default)]
display_claims: XSTSAuthResponseDisplayClaims
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub(super) struct XSTSAuthErrorResponse {
x_err: u64,
message: Option<String>
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase", untagged)]
pub(super) enum XSTSAuthResponse {
Success(XSTSAuthSuccessResponse),
Error(XSTSAuthErrorResponse)
}
#[derive(Deserialize, Debug, Default)]
pub(super) struct XSTSAuthResponseDisplayClaims {
xui: Vec<HashMap<String, String>>
}
impl XSTSAuthSuccessResponse {
pub(super) fn into_token(self) -> String {
self.token
}
fn get_display_claim(&self, name: &str) -> Option<&str> {
self.display_claims.xui.iter().find(|m| m.contains_key(name)).and_then(|f| f.get(name).map(|s| s.as_str()))
}
pub(super) fn get_user_hash(&self) -> Option<&str> {
self.get_display_claim("uhs")
}
pub(super) fn get_xuid(&self) -> Option<Uuid> {
self.get_display_claim("xid").and_then(|s| s.parse().ok()).map(|n| Uuid::from_u64_pair(0, n))
}
pub(super) fn get_gamertag(&self) -> Option<&str> {
self.get_display_claim("gtg")
}
}
#[allow(clippy::from_over_into)]
impl Into<AuthError> for XSTSAuthErrorResponse {
fn into(self) -> AuthError {
AuthError::AuthXError {
// some well-known error values
what: match self.x_err {
2148916238u64 => "Microsoft account held by a minor outside of a family.",
2148916233u64 => "Account is not on Xbox.",
_ => "Unknown error."
},
x_error: self.x_err,
message: self.message
}
}
}
pub(super) const XSTS_RP_MINECRAFT_SERVICES: &str = "rp://api.minecraftservices.com/";
pub(super) const XSTS_RP_XBOX_LIVE: &str = "http://xboxlive.com";
pub async fn xsts_request(client: &reqwest::Client, xbl_token: &str, relying_party: &str) -> Result<XSTSAuthResponse, AuthError> {
debug!("Performing XSTS auth {relying_party}");
let token_array = [xbl_token];
let req = XSTSAuthRequest {
properties: XSTSAuthRequestProperties {
sandbox_id: "RETAIL",
user_tokens: token_array.as_slice()
},
relying_party,
token_type: "JWT"
};
let res: XSTSAuthResponse = super::build_json_request(client, XBOX_LIVE_XSTS, Method::POST).json(&req).send().await
.and_then(|r| r.error_for_status())
.map_err(|e| AuthError::Request { what: "xsts", error: e })?
.json().await
.map_err(|e| AuthError::Request { what: "xsts (decode)", error: e })?;
Ok(res)
}
|