otp_offline 0.2.0

Library for offline verification of YubiKey OTPs.
Documentation
//! OTP store trait
//!
//! This module defines the `OtpStore` trait for validating `YubiKey` OTPs against a persistent store.

use std::{error::Error, time::SystemTime};

use crate::otp;

/// Trait for OTP storage and validation
///
/// Implementors of this trait provide methods to validate OTPs
/// against a persistent store.
pub trait OtpStore {
    /// Validate the given OTP received at the specified time.
    ///
    /// The OTP should be checked for validity, and the store should track
    /// state to prevent replay attacks.
    fn validate(&mut self, otp_str: &str, now: SystemTime) -> Result<(), StoreError>;
}

#[derive(Debug)]
pub enum StoreError {
    UnknownPublicId,

    Validation(otp::ValidationError),

    Otp(otp::Error),

    Other(Box<dyn Error>),
}

impl std::error::Error for StoreError {}

impl std::fmt::Display for StoreError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            StoreError::UnknownPublicId => write!(f, "Unknown PublicID"),
            StoreError::Validation(err) => write!(f, "{err}"),
            StoreError::Otp(err) => write!(f, "{err}"),
            StoreError::Other(err) => write!(f, "{err}"),
        }
    }
}