Skip to content

Commit

Permalink
feat: check if session is authenticated
Browse files Browse the repository at this point in the history
  • Loading branch information
1zun4 committed Jun 12, 2024
1 parent c10cfb1 commit 1a60e66
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src-tauri/src/app/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ async fn run_client(build_id: u32, account_data: MinecraftAccount, options: Laun
let shareable_window: ShareableWindow = Arc::new(Mutex::new(window));

let (account_name, uuid, token, user_type) = match account_data {
MinecraftAccount::MsaAccount { msa: _, xbl: _, mca, profile } => (profile.name, profile.id.to_string(), mca.data.access_token, "msa".to_string()),
MinecraftAccount::MsaAccount { msa: _, xbl: _, mca, profile, .. } => (profile.name, profile.id.to_string(), mca.data.access_token, "msa".to_string()),
MinecraftAccount::LegacyMsaAccount { name, uuid, token, .. } => (name, uuid.to_string(), token, "msa".to_string()),
MinecraftAccount::OfflineAccount { name, id } => (name, id.to_string(), "-".to_string(), "legacy".to_string())
MinecraftAccount::OfflineAccount { name, id, .. } => (name, id.to_string(), "-".to_string(), "legacy".to_string())
};

// Random XUID
Expand Down Expand Up @@ -351,8 +351,10 @@ async fn terminate(app_state: tauri::State<'_, AppState>) -> Result<(), String>

#[tauri::command]
async fn refresh(account_data: MinecraftAccount) -> Result<MinecraftAccount, String> {
info!("Refreshing account...");
let account = account_data.refresh().await
.map_err(|e| format!("unable to refresh: {:?}", e))?;
info!("Account was refreshed - username {}", account.get_username());
Ok(account)
}

Expand Down
35 changes: 33 additions & 2 deletions src-tauri/src/minecraft/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,31 @@ pub enum MinecraftAccount {
/// The user's Minecraft profile (i.e. username, UUID, skin)
#[serde(flatten)]
profile: ProfileResponse,
#[serde(skip_deserializing)]
#[serde(default)]
#[serde(rename = "hasBeenAuthenticated")]
has_been_authenticated: bool,
},
#[serde(rename = "Microsoft")]
LegacyMsaAccount {
name: String,
uuid: Uuid,
token: String,
ms_auth: MsAuth,
#[serde(skip_deserializing)]
#[serde(default)]
#[serde(rename = "hasBeenAuthenticated")]
has_been_authenticated: bool,
},
#[serde(rename = "Offline")]
OfflineAccount {
name: String,
#[serde(alias = "uuid")]
id: Uuid
id: Uuid,
#[serde(skip_deserializing)]
#[serde(default)]
#[serde(rename = "hasBeenAuthenticated")]
has_been_authenticated: bool,
},
}

Expand Down Expand Up @@ -119,6 +131,7 @@ impl MinecraftAccount {
MinecraftAccount::OfflineAccount {
name: username,
id: uuid,
has_been_authenticated: true
}
}

Expand All @@ -130,6 +143,7 @@ impl MinecraftAccount {
xbl,
mca,
profile,
..
} => {
// Not necessary to refresh if the Minecraft auth token is not expired
if !mca.is_expired() {
Expand All @@ -138,6 +152,7 @@ impl MinecraftAccount {
xbl,
mca,
profile,
has_been_authenticated: true
});
}

Expand All @@ -163,14 +178,29 @@ impl MinecraftAccount {
&ms_auth.refresh_token).await?;
return Ok(login_msa(msa).await?);
}
MinecraftAccount::OfflineAccount { .. } => Ok(self),
MinecraftAccount::OfflineAccount { name, id, .. } => {
Ok(MinecraftAccount::OfflineAccount {
name,
id,
has_been_authenticated: true
})
},
};
}

/// Logout the account
pub async fn logout(&self) -> Result<()> {
Ok(())
}

pub fn get_username(&self) -> &str {
match self {
MinecraftAccount::MsaAccount { profile, .. } => &profile.name,
MinecraftAccount::LegacyMsaAccount { name, .. } => name,
MinecraftAccount::OfflineAccount { name, .. } => name,
}
}

}

async fn login_msa(msa: ExpiringValue<AccessTokenResponse>) -> Result<MinecraftAccount, AuthError> {
Expand All @@ -186,5 +216,6 @@ async fn login_msa(msa: ExpiringValue<AccessTokenResponse>) -> Result<MinecraftA
xbl: minecraft.xbl,
mca: minecraft.mca,
profile,
has_been_authenticated: true
})
}
23 changes: 17 additions & 6 deletions src/lib/main/MainScreen.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,22 @@
}
async function runClient() {
console.log("Client started");
log = [];
clientRunning = true;
let build = getBuild();
console.debug("Running build", build);
if (!build) {
return;
}
console.log(options.currentAccount);
if (!options.currentAccount.hasBeenAuthenticated) {
alert("Your session has not been authenticated yet - please wait a few seconds...\nIf there was an error shown about your session, please try to logout and login again.");
return;
}
console.info("Starting client build", build);
clientRunning = true;
await invoke("run_client", {
buildId: build.buildId,
accountData: options.currentAccount,
Expand Down Expand Up @@ -303,11 +312,13 @@
invoke("refresh", { accountData: options.currentAccount })
.then((account) => {
console.info("Account Refreshed", account);
options.currentAccount = account;
options.store();
})
.catch((e) => console.error(e));
.catch((e) => {
console.error("Failed to refresh account", e);
alert("Failed to refresh account session: " + e);
});
</script>
{#if clientLogShown}
Expand Down

0 comments on commit 1a60e66

Please sign in to comment.