pub struct KeyManager { /* private fields */ }Expand description
API Key Manager for handling all key operations
Implementations§
Source§impl KeyManager
impl KeyManager
Sourcepub fn new<R: KeyRepository + 'static>(repository: R) -> Self
pub fn new<R: KeyRepository + 'static>(repository: R) -> Self
Create a new KeyManager with the given repository
Sourcepub fn with_arc_repository(repository: Arc<dyn KeyRepository>) -> Self
pub fn with_arc_repository(repository: Arc<dyn KeyRepository>) -> Self
Create a new KeyManager with an Arc repository
Sourcepub fn with_hmac_secret(self, secret: Option<String>) -> Self
pub fn with_hmac_secret(self, secret: Option<String>) -> Self
Set the HMAC secret for key hashing
Sourcepub async fn generate_key(
&self,
config: CreateKeyConfig,
) -> Result<(Uuid, String)>
pub async fn generate_key( &self, config: CreateKeyConfig, ) -> Result<(Uuid, String)>
Generate a new API key
Returns a tuple of (key_id, raw_key). The raw_key should be shown to the user only once and never stored. Only the hash is stored.
Sourcepub async fn validate_key(&self, raw_key: &str) -> Result<VerifyKeyResult>
pub async fn validate_key(&self, raw_key: &str) -> Result<VerifyKeyResult>
Validate a raw API key
Returns verification result with key info if valid.
Sourcepub async fn revoke_key(&self, key_id: Uuid) -> Result<()>
pub async fn revoke_key(&self, key_id: Uuid) -> Result<()>
Revoke an API key
Sourcepub async fn rotate_key(&self, key_id: Uuid) -> Result<(Uuid, String)>
pub async fn rotate_key(&self, key_id: Uuid) -> Result<(Uuid, String)>
Rotate an API key (generate new key, revoke old one)
Returns a tuple of (new_key_id, new_raw_key). The new_raw_key should be shown to the user only once.
Sourcepub async fn update_key(
&self,
key_id: Uuid,
config: UpdateKeyConfig,
) -> Result<KeyInfo>
pub async fn update_key( &self, key_id: Uuid, config: UpdateKeyConfig, ) -> Result<KeyInfo>
Update an API key’s configuration
Sourcepub async fn get_usage_stats(&self, key_id: Uuid) -> Result<KeyUsageStats>
pub async fn get_usage_stats(&self, key_id: Uuid) -> Result<KeyUsageStats>
Get key usage statistics
Sourcepub async fn record_usage(
&self,
key_id: Uuid,
tokens: u64,
cost: f64,
) -> Result<()>
pub async fn record_usage( &self, key_id: Uuid, tokens: u64, cost: f64, ) -> Result<()>
Record usage for a key
Sourcepub async fn record_usage_record(
&self,
key_id: Uuid,
record: UsageRecord,
) -> Result<()>
pub async fn record_usage_record( &self, key_id: Uuid, record: UsageRecord, ) -> Result<()>
Record usage for a key from an explicit usage record. On Tokio, a write accepted on the first poll survives cancellation of its waiter. Other executors directly await the repository under the same capacity bound, preserving their existing execution and cancellation behavior. Capacity exhaustion and repository failures remain best-effort write errors; runtime shutdown does not provide durable retry.
Sourcepub async fn list_keys(
&self,
status: Option<KeyStatus>,
limit: Option<usize>,
offset: Option<usize>,
) -> Result<Vec<KeyInfo>>
pub async fn list_keys( &self, status: Option<KeyStatus>, limit: Option<usize>, offset: Option<usize>, ) -> Result<Vec<KeyInfo>>
List all keys with optional filtering
Sourcepub async fn delete_key(&self, key_id: Uuid) -> Result<()>
pub async fn delete_key(&self, key_id: Uuid) -> Result<()>
Delete a key permanently (use with caution)
Sourcepub async fn cleanup_expired_keys(&self) -> Result<u64>
pub async fn cleanup_expired_keys(&self) -> Result<u64>
Cleanup expired keys
Trait Implementations§
Source§impl Clone for KeyManager
impl Clone for KeyManager
Auto Trait Implementations§
impl !RefUnwindSafe for KeyManager
impl !UnwindSafe for KeyManager
impl Freeze for KeyManager
impl Send for KeyManager
impl Sync for KeyManager
impl Unpin for KeyManager
impl UnsafeUnpin for KeyManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> BudgetAwareRouting for T
impl<T> BudgetAwareRouting for T
Source§fn filter_by_budget(
&self,
providers: Vec<String>,
budget_router: &BudgetAwareRouter,
) -> Vec<String>
fn filter_by_budget( &self, providers: Vec<String>, budget_router: &BudgetAwareRouter, ) -> Vec<String>
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more