use crate::cache::Caches;
use crate::errors::{CoreError, CoreResult};
use crate::time::SharedClock;
use sui_id_shared::ids::UserId;
use sui_id_store::Database;
use zeroize::Zeroizing;
use super::{audit_with_note, require_admin};
pub async fn list_signing_keys(
db: &Database,
actor: UserId,
) -> CoreResult<Vec<sui_id_store::models::SigningKeyRow>> {
require_admin(db, actor).await?;
Ok(sui_id_store::repos::signing_keys::list_published(db).await?)
}
pub async fn rotate_signing_key(
db: &Database,
clock: &SharedClock,
keyring_path: &str,
actor: UserId,
reason: Option<String>,
caches: &Caches,
) -> CoreResult<sui_id_shared::ids::SigningKeyId> {
use ed25519_dalek::SigningKey;
use sui_id_shared::ids::SigningKeyId;
use sui_id_store::repos::signing_keys;
require_admin(db, actor).await?;
let mut secret = Zeroizing::new([0u8; 32]);
getrandom::fill(secret.as_mut()).expect("system RNG unavailable");
let sk = SigningKey::from_bytes(&secret);
let pk = sk.verifying_key();
let new_id = SigningKeyId::new();
signing_keys::rotate_atomic(
db,
new_id,
"EdDSA",
sk.to_bytes().as_ref(),
pk.to_bytes().as_ref(),
).await?;
if let Err(e) = caches.jwks.rebuild(db).await {
tracing::warn!(error = %e, "cache rebuild failed after rotate_signing_key");
}
let _ = clock;
let _ = keyring_path;
audit_with_note(db, actor, "signing_key.rotate", Some(new_id.to_string()), reason).await;
Ok(new_id)
}
pub async fn delete_signing_key(
db: &Database,
clock: &SharedClock,
actor: UserId,
target: sui_id_shared::ids::SigningKeyId,
reason: Option<String>,
caches: &Caches,
) -> CoreResult<()> {
require_admin(db, actor).await?;
sui_id_store::repos::signing_keys::delete(db, target).await.map_err(|e| match e {
sui_id_store::StoreError::NotFound => CoreError::NotFound,
sui_id_store::StoreError::Conflict => CoreError::Conflict(
"cannot delete the active signing key; rotate first".into(),
),
other => CoreError::from(other),
})?;
let _ = clock;
audit_with_note(db, actor, "signing_key.delete", Some(target.to_string()), reason).await;
if let Err(e) = caches.jwks.rebuild(db).await {
tracing::warn!(error = %e, "cache rebuild failed after delete_signing_key");
}
Ok(())
}