Skip to content

Commit 4ea36a9

Browse files
committed
fix: use current username for keyring RW
1 parent 1aaa65d commit 4ea36a9

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

src-tauri/Cargo.lock

Lines changed: 30 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ fix-path-env = { git = "https://github.com/tauri-apps/fix-path-env-rs" }
3636
keyring = "2.3.3"
3737
totp-rs = "5.6.0"
3838
base32 = "0.5.1"
39+
whoami = "1.5.2"
3940

4041
[target.'cfg(target_os = "windows")'.dependencies]
4142
native-dialog = "0.7.0"

src-tauri/src/main.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ use totp_rs::{Algorithm, Secret, TOTP};
5151
use std::time::{SystemTime, UNIX_EPOCH};
5252
use serde_json::json;
5353
use base32::{Alphabet, encode as base32_encode};
54+
use whoami;
5455

5556
#[derive(Clone, serde::Serialize)]
5657
struct Payload {
@@ -305,6 +306,14 @@ fn get_mac_deep_link_requests() -> Vec<String> {
305306

306307
const PHOENIX_CRED_PREFIX: &str = "phcode_";
307308

309+
fn get_username() -> String {
310+
// Ensure a fallback username in case retrieval fails
311+
match whoami::username().as_str() {
312+
"" => "unknown_user".to_string(), // Fallback if username is empty
313+
username => username.to_string(), // Otherwise, use the retrieved username
314+
}
315+
}
316+
308317
// Stores or updates the sessionID and OTP seed securely. The otp_seed can never be read from js and only
309318
// the 30-second valid t-otp can be read. this helps improving security posture with auth flows as
310319
// unsecure extensions are not being able to get long term session tokens.
@@ -313,11 +322,12 @@ const PHOENIX_CRED_PREFIX: &str = "phcode_";
313322
#[tauri::command]
314323
fn store_credential(scope_name: String, session_id: String, otp_seed: String) -> Result<(), String> {
315324
let service = format!("{}{}", PHOENIX_CRED_PREFIX, scope_name); // Unique service name per scope
325+
let user = get_username();
316326

317327
// Combine sessionID and OTP seed into one stored value
318328
let credential_data = format!("{}|{}", session_id, otp_seed);
319329

320-
let entry = Entry::new(&service, "default_user").map_err(|e| e.to_string())?;
330+
let entry = Entry::new(&service, &user).map_err(|e| e.to_string())?;
321331
entry.set_password(&credential_data).map_err(|e| e.to_string())?;
322332

323333
Ok(())
@@ -327,8 +337,9 @@ fn store_credential(scope_name: String, session_id: String, otp_seed: String) ->
327337
#[tauri::command]
328338
fn delete_credential(scope_name: String) -> Result<(), String> {
329339
let service = format!("{}{}", PHOENIX_CRED_PREFIX, scope_name);
340+
let user = get_username();
330341

331-
let entry = Entry::new(&service, "default_user").map_err(|e| e.to_string())?;
342+
let entry = Entry::new(&service, &user).map_err(|e| e.to_string())?;
332343
entry.delete_password().map_err(|e| e.to_string())?;
333344

334345
Ok(())
@@ -338,7 +349,8 @@ fn delete_credential(scope_name: String) -> Result<(), String> {
338349
#[tauri::command]
339350
fn get_credential_otp(scope_name: String) -> serde_json::Value {
340351
let service = format!("{}{}", PHOENIX_CRED_PREFIX, scope_name);
341-
let entry = match Entry::new(&service, "default_user") {
352+
let user = get_username();
353+
let entry = match Entry::new(&service, &user) {
342354
Ok(entry) => entry,
343355
Err(_) => return json!({ "err_code": "CREDENTIAL_ERROR" }), // Error creating keyring entry
344356
};

0 commit comments

Comments
 (0)