Skip to content

Commit 86dc824

Browse files
committed
store credentials only in sqlite
1 parent ccc49a8 commit 86dc824

File tree

1 file changed

+1
-85
lines changed
  • crates/chat-cli/src/database

1 file changed

+1
-85
lines changed

crates/chat-cli/src/database/mod.rs

Lines changed: 1 addition & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,6 @@ pub enum DatabaseError {
134134
DbOpenError(#[from] DbOpenError),
135135
#[error("{}", .0)]
136136
PoisonError(String),
137-
#[cfg(target_os = "macos")]
138-
#[error("Security error: {}", .0)]
139-
Security(String),
140137
#[error(transparent)]
141138
StringFromUtf8(#[from] std::string::FromUtf8Error),
142139
#[error(transparent)]
@@ -157,8 +154,7 @@ pub enum Table {
157154
State,
158155
/// The conversations tables contains user chat conversations.
159156
Conversations,
160-
#[cfg(not(target_os = "macos"))]
161-
/// The auth table contains
157+
/// The auth table contains SSO and Builder ID credentials.
162158
Auth,
163159
}
164160

@@ -167,7 +163,6 @@ impl std::fmt::Display for Table {
167163
match self {
168164
Table::State => write!(f, "state"),
169165
Table::Conversations => write!(f, "conversations"),
170-
#[cfg(not(target_os = "macos"))]
171166
Table::Auth => write!(f, "auth_kv"),
172167
}
173168
}
@@ -433,86 +428,7 @@ impl Database {
433428

434429
Ok(map)
435430
}
436-
}
437-
438-
#[cfg(target_os = "macos")]
439-
impl Database {
440-
/// The account name is not used.
441-
const ACCOUNT: &str = "";
442-
/// Path to the `security` binary
443-
const SECURITY_BIN: &str = "/usr/bin/security";
444-
445-
/// Sets the `key` to `password` on the keychain, this will override any existing value
446-
pub async fn set_secret(&self, key: &str, password: &str) -> Result<(), DatabaseError> {
447-
let output = tokio::process::Command::new(Self::SECURITY_BIN)
448-
.args([
449-
"add-generic-password",
450-
"-U",
451-
"-s",
452-
key,
453-
"-a",
454-
Self::ACCOUNT,
455-
"-w",
456-
password,
457-
])
458-
.output()
459-
.await?;
460-
461-
if !output.status.success() {
462-
let stderr = std::str::from_utf8(&output.stderr)?;
463-
return Err(DatabaseError::Security(stderr.into()));
464-
}
465-
466-
Ok(())
467-
}
468-
469-
/// Returns the password for the `key`
470-
///
471-
/// If not found the result will be `Ok(None)`, other errors will be returned
472-
pub async fn get_secret(&self, key: &str) -> Result<Option<Secret>, DatabaseError> {
473-
let output = tokio::process::Command::new(Self::SECURITY_BIN)
474-
.args(["find-generic-password", "-s", key, "-a", Self::ACCOUNT, "-w"])
475-
.output()
476-
.await?;
477-
478-
if !output.status.success() {
479-
let stderr = std::str::from_utf8(&output.stderr)?;
480-
if stderr.contains("could not be found") {
481-
return Ok(None);
482-
} else {
483-
return Err(DatabaseError::Security(stderr.into()));
484-
}
485-
}
486-
487-
let stdout = std::str::from_utf8(&output.stdout)?;
488431

489-
// strip newline
490-
let stdout = match stdout.strip_suffix('\n') {
491-
Some(stdout) => stdout,
492-
None => stdout,
493-
};
494-
495-
Ok(Some(stdout.into()))
496-
}
497-
498-
/// Deletes the `key` from the keychain
499-
pub async fn delete_secret(&self, key: &str) -> Result<(), DatabaseError> {
500-
let output = tokio::process::Command::new(Self::SECURITY_BIN)
501-
.args(["delete-generic-password", "-s", key, "-a", Self::ACCOUNT])
502-
.output()
503-
.await?;
504-
505-
if !output.status.success() {
506-
let stderr = std::str::from_utf8(&output.stderr)?;
507-
return Err(DatabaseError::Security(stderr.into()));
508-
}
509-
510-
Ok(())
511-
}
512-
}
513-
514-
#[cfg(any(target_os = "linux", windows))]
515-
impl Database {
516432
pub async fn get_secret(&self, key: &str) -> Result<Option<Secret>, DatabaseError> {
517433
Ok(self.get_entry::<String>(Table::Auth, key)?.map(Into::into))
518434
}

0 commit comments

Comments
 (0)