Skip to main content

ApiKeyRepository

Trait ApiKeyRepository 

Source
pub trait ApiKeyRepository: Send + Sync {
    // Required method
    fn find_by_key(&self, key: &str) -> Option<ApiKey>;
}
Expand description

Trait for loading API keys.

Implement this trait to provide custom storage backends for API keys (e.g., database, Redis, external service).

§Example

use actix_security::http::security::api_key::{ApiKey, ApiKeyRepository};

struct DatabaseApiKeyRepository {
    pool: DbPool,
}

impl ApiKeyRepository for DatabaseApiKeyRepository {
    fn find_by_key(&self, key: &str) -> Option<ApiKey> {
        // Query database for the API key
        self.pool.query("SELECT * FROM api_keys WHERE key = ?", &[key])
            .ok()
            .map(|row| ApiKey::new(row.key)
                .name(row.name)
                .owner(row.owner)
                .roles(row.roles)
                .authorities(row.authorities)
                .enabled(row.enabled))
    }
}

Required Methods§

Source

fn find_by_key(&self, key: &str) -> Option<ApiKey>

Finds an API key by its value.

Returns Some(ApiKey) if found, None otherwise.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§