问题描述: 我正在用 Rust 开发一个数据库客户端,并使用密钥环箱将凭据存储在 Apple 钥匙串中。我的代码在存储凭据时返回成功消息,但我在钥匙串中找不到它们。
相关代码如下:
use crate::structure::database::StoredConnectionParams;
use keyring::Entry;
use serde::{Deserialize, Serialize}; // Import the StoredConnectionParams struct
#[derive(Debug, Serialize, Deserialize)]
pub struct CredentialStorageResult {
pub success: bool,
pub message: String,
}
pub fn store_database_credentials(
params: StoredConnectionParams,
) -> Result<CredentialStorageResult, String> {
let connection_id = params.connection_id.to_string();
let keyring_service = format!("datamizu-db-{}", connection_id);
let entry = Entry::new(&keyring_service, &connection_id)
.map_err(|e| format!("Failed to create keyring entry: {}", e))?;
let credentials = serde_json::to_string(¶ms.credentials)
.map_err(|e| e.to_string())?;
match entry.set_password(&credentials) {
Ok(_) => Ok(CredentialStorageResult {
success: true,
message: format!(
"Credentials for {} connection {} stored successfully",
params.data_source_type, connection_id
),
}),
Err(e) => Err(format!(
"Failed to store credentials for {} connection {}: {}",
params.data_source_type, connection_id, e
)),
}
}
pub fn fetch_database_credentials(
connection_id: String,
) -> Result<StoredConnectionParams, String> {
let keyring_service = format!("datamizu-db-{}", connection_id);
let entry = Entry::new(&keyring_service, &connection_id)
.map_err(|e| format!("Failed to create keyring entry: {}", e))?;
match entry.get_password() {
Ok(credentials) => serde_json::from_str(&credentials)
.map_err(|e| format!("Failed to parse stored credentials: {}", e)),
Err(e) => Err(format!("Failed to retrieve credentials: {}", e)),
}
}
采取的步骤:
我已通过 Keychain Access 手动检查了 macOS 钥匙串,但未找到格式为 datamizu-db-
问题: 即使密钥环箱返回成功,为什么凭据可能不会出现在 macOS 钥匙串中?是否与 macOS 上的应用程序权限或沙箱有关?
其他详细信息: macOS 版本:15.0 Rust 版本:cargo 1.81.0 钥匙圈箱版本:3.3.0
谢谢你
密钥环箱要求您指定要支持的平台。
如果您想要支持 MacOS 和 Windows,您可以在
Cargo.toml
中指定,如下所示:
keyring = { version = "3", features = ["apple-native", "windows-native"] }