Skip to content

Commit

Permalink
flows: Work on mutable references of XalAuthenticator, only return To…
Browse files Browse the repository at this point in the history
…kenStore / not a tuple
  • Loading branch information
tuxuser committed Dec 18, 2023
1 parent 0fd94da commit 9b3c8c3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 34 deletions.
23 changes: 10 additions & 13 deletions examples/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,46 +79,44 @@ pub async fn auth_main(
let args = handle_args();

let mut ts = match flows::try_refresh_tokens_from_file(&args.token_filepath).await {
Ok((authenticator, ts)) => {
Ok((mut authenticator, ts)) => {
info!("Tokens refreshed succesfully, proceeding with Xbox Live Authorization");
match args.flow {
AuthFlow::Sisu => {
info!("Authorize and gather rest of xbox live tokens via sisu");
flows::xbox_live_sisu_authorization_flow(
authenticator, ts.live_token
&mut authenticator, ts.live_token
)
.await?
.1
},
_ => {
info!("Authorize Xbox Live the traditional way, via individual requests");
flows::xbox_live_authorization_traditional_flow(
authenticator,
&mut authenticator,
ts.live_token,
xsts_relying_party,
access_token_prefix,
false
)
.await?
.1
}
}
}
Err(err) => {
log::error!("Refreshing tokens failed err={err}");
let authenticator = XalAuthenticator::new(app_params, client_params, sandbox_id);
let mut authenticator = XalAuthenticator::new(app_params, client_params, sandbox_id);

info!("Authentication via flow={:?}", args.flow);
let (authenticator, ts) = match args.flow {
AuthFlow::Sisu => flows::xbox_live_sisu_full_flow(authenticator, auth_cb).await?,
let ts = match args.flow {
AuthFlow::Sisu => flows::xbox_live_sisu_full_flow(&mut authenticator, auth_cb).await?,
AuthFlow::DeviceCode => {
flows::ms_device_code_flow(authenticator, auth_cb, tokio::time::sleep).await?
flows::ms_device_code_flow(&mut authenticator, auth_cb, tokio::time::sleep).await?
}
AuthFlow::Implicit => {
flows::ms_authorization_flow(authenticator, auth_cb, true).await?
flows::ms_authorization_flow(&mut authenticator, auth_cb, true).await?
}
AuthFlow::AuthorizationCode => {
flows::ms_authorization_flow(authenticator, auth_cb, false).await?
flows::ms_authorization_flow(&mut authenticator, auth_cb, false).await?
}
};

Expand All @@ -129,14 +127,13 @@ pub async fn auth_main(
// Only required for non-sisu authentication, as
// sisu already gathers all the tokens at once
flows::xbox_live_authorization_traditional_flow(
authenticator,
&mut authenticator,
ts.live_token,
xsts_relying_party,
access_token_prefix,
false,
)
.await?
.1
},
}
}
Expand Down
1 change: 0 additions & 1 deletion src/authenticator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,6 @@ impl XalAuthenticator {
/// redirect_uri: Some(RedirectUrl::new("https://login.live.com/oauth20_desktop.srf".into()).unwrap())
/// },
/// client_params::CLIENT_ANDROID(),
/// None,
/// "RETAIL".into()
/// );
///
Expand Down
41 changes: 21 additions & 20 deletions src/flows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,9 @@ impl AuthPromptCallback for CliCallbackHandler {
pub async fn try_refresh_tokens_from_file(
filepath: &str,
) -> Result<(XalAuthenticator, TokenStore), Error> {
let ts = TokenStore::load_from_file(filepath)?;
try_refresh_live_tokens_from_tokenstore(ts).await
let mut ts = TokenStore::load_from_file(filepath)?;
let authenticator = try_refresh_live_tokens_from_tokenstore(&mut ts).await?;
Ok((authenticator, ts))
}

/// Try to read tokens from the token store and refresh the Windows Live tokens if needed.
Expand All @@ -241,8 +242,8 @@ pub async fn try_refresh_tokens_from_file(
/// If successful, a tuple of [`crate::XalAuthenticator`] and [`crate::tokenstore::TokenStore`]
/// is returned. TokenStore will contain the refreshed `live_tokens`.
pub async fn try_refresh_live_tokens_from_tokenstore(
mut ts: TokenStore,
) -> Result<(XalAuthenticator, TokenStore), Error> {
ts: &mut TokenStore,
) -> Result<XalAuthenticator, Error> {
let mut authenticator = Into::<XalAuthenticator>::into(ts.clone());

info!("Refreshing windows live tokens");
Expand All @@ -254,15 +255,15 @@ pub async fn try_refresh_live_tokens_from_tokenstore(
debug!("Windows Live tokens: {:?}", refreshed_wl_tokens);
ts.live_token = refreshed_wl_tokens.clone();

Ok((authenticator, ts))
Ok(authenticator)
}

/// Shorthand for Windows Live device code flow
pub async fn ms_device_code_flow<S, SF>(
mut authenticator: XalAuthenticator,
authenticator: &mut XalAuthenticator,
cb: impl AuthPromptCallback,
sleep_fn: S,
) -> Result<(XalAuthenticator, TokenStore), Error>
) -> Result<TokenStore, Error>
where
S: Fn(std::time::Duration) -> SF,
SF: std::future::Future<Output = ()>,
Expand Down Expand Up @@ -295,17 +296,17 @@ where
updated: None,
};

Ok((authenticator, ts))
Ok(ts)
}

/// Shorthand for Windows Live authorization flow
/// - Depending on the argument `implicit` the
/// methods `implicit grant` or `authorization code` are chosen
pub async fn ms_authorization_flow(
mut authenticator: XalAuthenticator,
authenticator: &mut XalAuthenticator,
cb: impl AuthPromptCallback,
implicit: bool,
) -> Result<(XalAuthenticator, TokenStore), Error> {
) -> Result<TokenStore, Error> {
trace!("Starting implicit authorization flow");

let (url, state) =
Expand Down Expand Up @@ -349,14 +350,14 @@ pub async fn ms_authorization_flow(
updated: None,
};

Ok((authenticator, ts))
Ok(ts)
}

/// Shorthand for sisu authentication flow
pub async fn xbox_live_sisu_full_flow(
mut authenticator: XalAuthenticator,
authenticator: &mut XalAuthenticator,
callback: impl AuthPromptCallback,
) -> Result<(XalAuthenticator, TokenStore), Error> {
) -> Result<TokenStore, Error> {
trace!("Getting device token");
let device_token = authenticator.get_device_token().await?;
debug!("Device token={:?}", device_token);
Expand Down Expand Up @@ -417,7 +418,7 @@ pub async fn xbox_live_sisu_full_flow(
updated: None,
};

Ok((authenticator, ts))
Ok(ts)
}

/// Implements the traditional Xbox Live authorization flow.
Expand Down Expand Up @@ -475,12 +476,12 @@ pub async fn xbox_live_sisu_full_flow(
/// - Depending on the client an AccessToken prefix is necessary to have the User Token (XASU) request succeed
/// - Success of authorizing (device, user, ?title?) tokens for XSTS relies on the target relying party
pub async fn xbox_live_authorization_traditional_flow(
mut authenticator: XalAuthenticator,
authenticator: &mut XalAuthenticator,
live_tokens: WindowsLiveTokens,
xsts_relying_party: String,
access_token_prefix: AccessTokenPrefix,
request_title_token: bool,
) -> Result<(XalAuthenticator, TokenStore), Error> {
) -> Result<TokenStore, Error> {
debug!("Windows live tokens={:?}", &live_tokens);
trace!("Getting device token");
let device_token = authenticator.get_device_token().await?;
Expand Down Expand Up @@ -530,14 +531,14 @@ pub async fn xbox_live_authorization_traditional_flow(
updated: None,
};

Ok((authenticator, ts))
Ok(ts)
}

/// bla
pub async fn xbox_live_sisu_authorization_flow(
mut authenticator: XalAuthenticator,
authenticator: &mut XalAuthenticator,
live_tokens: WindowsLiveTokens,
) -> Result<(XalAuthenticator, TokenStore), Error> {
) -> Result<TokenStore, Error> {
debug!("Windows live tokens={:?}", &live_tokens);
trace!("Getting device token");
let device_token = authenticator.get_device_token().await?;
Expand All @@ -560,5 +561,5 @@ pub async fn xbox_live_sisu_authorization_flow(
updated: None,
};

Ok((authenticator, ts))
Ok(ts)
}

0 comments on commit 9b3c8c3

Please sign in to comment.