pub struct ApiKeyManager { /* private fields */ }Expand description
API Key Manager
Manages API key lifecycle with injected storage.
Implementations§
Source§impl ApiKeyManager
impl ApiKeyManager
Sourcepub fn new(store: Arc<dyn ApiKeyStore>) -> Self
pub fn new(store: Arc<dyn ApiKeyStore>) -> Self
Create new API key manager with injected store
§Examples
use armature_auth::api_key::*;
use chrono::{DateTime, Utc};
use std::sync::Arc;
let store: Arc<dyn ApiKeyStore> = Arc::new(MyStore);
let manager = ApiKeyManager::new(store);Sourcepub fn with_prefix(self, prefix: impl Into<String>) -> Self
pub fn with_prefix(self, prefix: impl Into<String>) -> Self
Set key prefix (default: “ak”)
Sourcepub fn with_expiration(self, duration: Option<Duration>) -> Self
pub fn with_expiration(self, duration: Option<Duration>) -> Self
Set default expiration duration
Sourcepub async fn generate(
&self,
user_id: impl Into<String>,
scopes: Vec<String>,
) -> Result<ApiKey, ApiKeyError>
pub async fn generate( &self, user_id: impl Into<String>, scopes: Vec<String>, ) -> Result<ApiKey, ApiKeyError>
Generate a new API key
§Examples
let key = manager.generate(
"user_123",
vec!["read".to_string(), "write".to_string()]
).await?;
println!("API Key: {}", key.key);
println!("Key ID: {}", key.id);Sourcepub async fn validate(&self, key: &str) -> Result<Option<ApiKey>, ApiKeyError>
pub async fn validate(&self, key: &str) -> Result<Option<ApiKey>, ApiKeyError>
Validate an API key
Returns the API key if valid, None if invalid, or an error.
Sourcepub fn has_scope(&self, api_key: &ApiKey, required_scope: &str) -> bool
pub fn has_scope(&self, api_key: &ApiKey, required_scope: &str) -> bool
Check if key has required scope
Sourcepub async fn list_user_keys(
&self,
user_id: &str,
) -> Result<Vec<ApiKey>, ApiKeyError>
pub async fn list_user_keys( &self, user_id: &str, ) -> Result<Vec<ApiKey>, ApiKeyError>
List all keys for a user
Auto Trait Implementations§
impl Freeze for ApiKeyManager
impl !RefUnwindSafe for ApiKeyManager
impl Send for ApiKeyManager
impl Sync for ApiKeyManager
impl Unpin for ApiKeyManager
impl !UnwindSafe for ApiKeyManager
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
Mutably borrows from an owned value. Read more
Source§impl<T> Injectable for T
impl<T> Injectable for T
Source§fn type_id_of() -> TypeIdwhere
Self: Sized,
fn type_id_of() -> TypeIdwhere
Self: Sized,
Returns the TypeId of this type (for internal use)
Source§fn type_name_of() -> &'static strwhere
Self: Sized,
fn type_name_of() -> &'static strwhere
Self: Sized,
Returns the type name for debugging