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
//! XAL - Xbox Live Authentication Library for Rust
//!
//! This library aims at giving a high range of configurability to the user, so that authentication
//! can be targeted inidividually for each required scenario.
//!
//! Features:
//! - (Lower level) OAuth2 Authentication - see [`crate::XalAuthenticator`]
//! - (Higher level) Authentication flows - see [`crate::Flows`]
//! - (Standalone) HTTP Request Signing - see [`crate::RequestSigner`]
//! - Container for storing tokens / authentication parameters - see [`crate::TokenStore`]
//! - Extensions for Reqwest HTTP client library - see [`crate::extensions`]
//! - Verbose errors for JSON deserialization - see [`crate::extensions::JsonExDeserializeMiddleware`]
//! - Debug logging for HTTP requests - see [`crate::extensions::LoggingReqwestRequestHandler`]
//! - Debug logging for HTTP responses - see [`crate::extensions::LoggingReqwestResponseHandler`]
//! - Signing HTTP requests - see [`crate::extensions::SigningReqwestBuilder`]
//! - Adding `MS-CV` header to requests - see [`crate::extensions::CorrelationVectorReqwestBuilder`]
//!
//! # Quick Start
//!
//! Authenticate and save tokens to JSON file `tokens.json`
//!
//! ```no_run
//! use xal::{XalAuthenticator, Flows, CliCallbackHandler};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let mut authenticator = XalAuthenticator::default();
//!
//! // Do full SISU auth flow
//! let mut token_store = Flows::xbox_live_sisu_full_flow(
//! &mut authenticator,
//! CliCallbackHandler
//! ).await?;
//!
//! // User will be prompted on commandline to proceed with authentication
//!
//! token_store.update_timestamp();
//! token_store.save_to_file("tokens.json")?;
//!
//! Ok(())
//! }
//! ```
//!
//! Load tokens from file and refresh them
//!
//! ```no_run
//! use xal::{XalAuthenticator, Flows, CliCallbackHandler};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! println!("Trying to refresh tokens...");
//! let mut token_store = match Flows::try_refresh_live_tokens_from_file("tokens.json").await {
//! Ok((mut authenticator, ts)) => {
//! println!("Tokens refreshed succesfully, proceeding with Xbox Live Authorization");
//! Flows::xbox_live_sisu_authorization_flow(&mut authenticator, ts.live_token)
//! .await?
//! },
//! Err(err) => {
//! eprintln!("Refreshing tokens failed err={err}");
//! let mut authenticator = XalAuthenticator::default();
//! println!("Authentication via SISU");
//! Flows::xbox_live_sisu_full_flow(&mut authenticator, CliCallbackHandler)
//! .await?
//! }
//! };
//!
//! token_store.update_timestamp();
//! token_store.save_to_file("tokens.json")?;
//! Ok(())
//! }
//! ```
//!
//! Make use of acquired XSTS token
//!
//! ```no_run
//! use xal::{XalAuthenticator, Flows, CliCallbackHandler};
//! use xal::extensions::JsonExDeserializeMiddleware;
//! use xal::oauth2::TokenResponse;
//! use serde_json::json;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create an authenticator with minecraft client parameters
//! let mut authenticator = XalAuthenticator::new(
//! xal::app_params::MC_BEDROCK_SWITCH(),
//! xal::client_params::CLIENT_NINTENDO(),
//! "RETAIL".into(),
//! );
//!
//! // Do full SISU authentication flow
//! let mut token_store = Flows::xbox_live_sisu_full_flow(
//! &mut authenticator,
//! CliCallbackHandler
//! ).await?;
//!
//! // Authorize to XSTS endpoint via Minecraft RelyingParty
//! let xsts_mc_services = authenticator
//! .get_xsts_token(
//! token_store.device_token.as_ref(),
//! token_store.title_token.as_ref(),
//! token_store.user_token.as_ref(),
//! "rp://api.minecraftservices.com/"
//! )
//! .await?;
//!
//! let identity_token = xsts_mc_services.authorization_header_value();
//! println!("identityToken: {identity_token}");
//!
//! /* Minecraft stuff */
//! // Exchange XSTS Token against Minecraft Token
//! let mc_token = reqwest::Client::new()
//! .post("https://api.minecraftservices.com/authentication/login_with_xbox")
//! .json(&json!({"identityToken": identity_token}))
//! .send()
//! .await?
//! .json_ex::<xal::oauth2::basic::BasicTokenResponse>()
//! .await?;
//! println!("MC: {mc_token:?}");
//!
//! // Get minecraft profile, use Minecraft Token as Bearer Auth
//! let profile = reqwest::Client::new()
//! .get("https://api.minecraftservices.com/minecraft/profile")
//! .bearer_auth(mc_token.access_token().secret())
//! .send()
//! .await?
//! .text()
//! .await?;
//! println!("Profile: {profile}");
//! Ok(())
//! }
//! ```
//!
//! Loading tokens from file and sending a signed a request
//!
//! ```no_run
//! use xal::{
//! RequestSigner, TokenStore, Error,
//! extensions::{
//! SigningReqwestBuilder,
//! CorrelationVectorReqwestBuilder,
//!
//! },
//! cvlib::CorrelationVector,
//! };
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Load tokens from JSON
//! let token_store = TokenStore::load_from_file("tokens.json")?;
//!
//! // Create new instances of Correlation vector and request signer
//! let mut cv = CorrelationVector::new();
//! let mut signer = RequestSigner::new();
//!
//! // Check if XSTS token exists
//! let xsts_token = token_store.authorization_token
//! .ok_or(Error::GeneralError("No XSTS token was acquired".into()))?;
//!
//! // Send a http request
//! // Request will get signed and MS-CV header populated
//! let userpresence = reqwest::Client::new()
//! .get("https://userpresence.xboxlive.com/users/me?level=all")
//! .header("x-xbl-contract-version", "3")
//! .header("Authorization", xsts_token.authorization_header_value())
//! .add_cv(&mut cv)?
//! .sign(&mut signer, None)
//! .await?
//! .send()
//! .await?;
//!
//! println!("{:?}", userpresence);
//! Ok(())
//! }
//! ```
//!
//! # Examples
//!
//! Check out the [xal-examples](https://github.com/OpenXbox/xal-rs/tree/master/examples).
//!
//! # Advanced
//!
//! For advanced usage, see [`crate::XalAuthenticator`].
pub use cvlib;
pub use oauth2;
pub use url;
pub use *;
pub use Error;
pub use *;
pub use *;
pub use *;
pub use *;