Skip to main content

ApiKeyProvider

Trait ApiKeyProvider 

Source
pub trait ApiKeyProvider:
    Send
    + Sync
    + 'static {
    // Required method
    fn verify_key<'life0, 'life1, 'async_trait>(
        &'life0 self,
        raw_key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<ApiKeyInfo, ()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
}
Expand description

Storage-agnostic API key verification.

Implement this trait to connect the middleware to your key store (database, cache, etc.).

§Example

use ferro_rs::{async_trait, ApiKeyProvider, ApiKeyInfo};

pub struct DbApiKeyProvider;

#[async_trait]
impl ApiKeyProvider for DbApiKeyProvider {
    async fn verify_key(&self, raw_key: &str) -> Result<ApiKeyInfo, ()> {
        let prefix = &raw_key[..16.min(raw_key.len())];
        let record = api_key::Entity::find()
            .filter(api_key::Column::Prefix.eq(prefix))
            .one(&db())
            .await
            .map_err(|_| ())?
            .ok_or(())?;

        if verify_api_key_hash(raw_key, &record.hashed_key) {
            Ok(ApiKeyInfo {
                id: record.id,
                name: record.name,
                scopes: serde_json::from_str(&record.scopes).unwrap_or_default(),
            })
        } else {
            Err(())
        }
    }
}

Required Methods§

Source

fn verify_key<'life0, 'life1, 'async_trait>( &'life0 self, raw_key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<ApiKeyInfo, ()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Look up and verify the raw key, returning key metadata on success.

Implementors§