mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
device-key: clean up unused crate (#21487)
This commit is contained in:
@@ -1,66 +0,0 @@
|
||||
use super::*;
|
||||
|
||||
/// Persisted account/client binding for a generated device key.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct DeviceKeyBindingRecord {
|
||||
pub key_id: String,
|
||||
pub account_user_id: String,
|
||||
pub client_id: String,
|
||||
}
|
||||
|
||||
impl StateRuntime {
|
||||
pub async fn get_device_key_binding(
|
||||
&self,
|
||||
key_id: &str,
|
||||
) -> anyhow::Result<Option<DeviceKeyBindingRecord>> {
|
||||
let row = sqlx::query(
|
||||
r#"
|
||||
SELECT key_id, account_user_id, client_id
|
||||
FROM device_key_bindings
|
||||
WHERE key_id = ?
|
||||
"#,
|
||||
)
|
||||
.bind(key_id)
|
||||
.fetch_optional(self.pool.as_ref())
|
||||
.await?;
|
||||
|
||||
row.map(|row| {
|
||||
Ok(DeviceKeyBindingRecord {
|
||||
key_id: row.try_get("key_id")?,
|
||||
account_user_id: row.try_get("account_user_id")?,
|
||||
client_id: row.try_get("client_id")?,
|
||||
})
|
||||
})
|
||||
.transpose()
|
||||
}
|
||||
|
||||
pub async fn upsert_device_key_binding(
|
||||
&self,
|
||||
binding: &DeviceKeyBindingRecord,
|
||||
) -> anyhow::Result<()> {
|
||||
let now = Utc::now().timestamp();
|
||||
sqlx::query(
|
||||
r#"
|
||||
INSERT INTO device_key_bindings (
|
||||
key_id,
|
||||
account_user_id,
|
||||
client_id,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (?, ?, ?, ?, ?)
|
||||
ON CONFLICT(key_id) DO UPDATE SET
|
||||
account_user_id = excluded.account_user_id,
|
||||
client_id = excluded.client_id,
|
||||
updated_at = excluded.updated_at
|
||||
"#,
|
||||
)
|
||||
.bind(&binding.key_id)
|
||||
.bind(&binding.account_user_id)
|
||||
.bind(&binding.client_id)
|
||||
.bind(now)
|
||||
.bind(now)
|
||||
.execute(self.pool.as_ref())
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
use super::DeviceKeyBindingRecord;
|
||||
use super::StateRuntime;
|
||||
use super::test_support::unique_temp_dir;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[tokio::test]
|
||||
async fn device_key_binding_round_trips_by_key_id() {
|
||||
let codex_home = unique_temp_dir();
|
||||
let runtime = StateRuntime::init(codex_home.clone(), "test-provider".to_string())
|
||||
.await
|
||||
.expect("initialize runtime");
|
||||
|
||||
let first = DeviceKeyBindingRecord {
|
||||
key_id: "dk_tpm_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".to_string(),
|
||||
account_user_id: "account-user-a".to_string(),
|
||||
client_id: "cli_a".to_string(),
|
||||
};
|
||||
let second = DeviceKeyBindingRecord {
|
||||
key_id: "dk_tpm_BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB".to_string(),
|
||||
account_user_id: "account-user-b".to_string(),
|
||||
client_id: "cli_b".to_string(),
|
||||
};
|
||||
|
||||
runtime
|
||||
.upsert_device_key_binding(&first)
|
||||
.await
|
||||
.expect("insert first binding");
|
||||
runtime
|
||||
.upsert_device_key_binding(&second)
|
||||
.await
|
||||
.expect("insert second binding");
|
||||
|
||||
assert_eq!(
|
||||
runtime
|
||||
.get_device_key_binding(&first.key_id)
|
||||
.await
|
||||
.expect("load first binding"),
|
||||
Some(first)
|
||||
);
|
||||
assert_eq!(
|
||||
runtime
|
||||
.get_device_key_binding("dk_tpm_missing")
|
||||
.await
|
||||
.expect("load missing binding"),
|
||||
None
|
||||
);
|
||||
|
||||
let _ = tokio::fs::remove_dir_all(codex_home).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn device_key_binding_upsert_updates_existing_binding() {
|
||||
let codex_home = unique_temp_dir();
|
||||
let runtime = StateRuntime::init(codex_home.clone(), "test-provider".to_string())
|
||||
.await
|
||||
.expect("initialize runtime");
|
||||
|
||||
let key_id = "dk_tpm_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".to_string();
|
||||
runtime
|
||||
.upsert_device_key_binding(&DeviceKeyBindingRecord {
|
||||
key_id: key_id.clone(),
|
||||
account_user_id: "account-user-a".to_string(),
|
||||
client_id: "cli_a".to_string(),
|
||||
})
|
||||
.await
|
||||
.expect("insert binding");
|
||||
runtime
|
||||
.upsert_device_key_binding(&DeviceKeyBindingRecord {
|
||||
key_id: key_id.clone(),
|
||||
account_user_id: "account-user-b".to_string(),
|
||||
client_id: "cli_b".to_string(),
|
||||
})
|
||||
.await
|
||||
.expect("update binding");
|
||||
|
||||
assert_eq!(
|
||||
runtime
|
||||
.get_device_key_binding(&key_id)
|
||||
.await
|
||||
.expect("load updated binding"),
|
||||
Some(DeviceKeyBindingRecord {
|
||||
key_id,
|
||||
account_user_id: "account-user-b".to_string(),
|
||||
client_id: "cli_b".to_string(),
|
||||
})
|
||||
);
|
||||
|
||||
let _ = tokio::fs::remove_dir_all(codex_home).await;
|
||||
}
|
||||
Reference in New Issue
Block a user