authnz-common 0.2.1

Authnz common library (types and utils).
Documentation
#[cfg(any(feature = "app-server-types", feature = "authnz-server-types"))]
use impulse_server_kit::salvo;
#[cfg(any(feature = "app-server-types", feature = "authnz-server-types"))]
use salvo::oapi::ToSchema;
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, PartialEq, Eq, Hash, Clone, Debug, Default)]
#[cfg_attr(any(feature = "app-server-types", feature = "authnz-server-types"), derive(ToSchema))]
#[serde(rename_all = "snake_case", tag = "type")]
/// TOTP algorithm.
pub enum TOTPAlgorithm {
  #[default]
  /// SHA1 algorithm.
  Sha1,
  /// SHA2-256 algorithm.
  Sha256,
  /// SHA2-512 algorithm.
  Sha512,
}

impl TOTPAlgorithm {
  /// Converts string to algorithm.
  /// Panics if `alg` is not in the list: `SHA1`, `SHA256`, `SHA512`.
  pub fn from_unchecked(alg: &str) -> Self {
    match alg {
      "SHA1" => Self::Sha1,
      "SHA256" => Self::Sha256,
      "SHA512" => Self::Sha512,
      _ => unreachable!(),
    }
  }

  /// Converts to `authnz_totp::Algorithm`.
  pub fn into_totp_rs(&self) -> authnz_totp::Algorithm {
    match self {
      Self::Sha1 => authnz_totp::Algorithm::SHA1,
      Self::Sha256 => authnz_totp::Algorithm::SHA256,
      Self::Sha512 => authnz_totp::Algorithm::SHA512,
    }
  }
}

impl std::fmt::Display for TOTPAlgorithm {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      Self::Sha1 => f.write_str("SHA1"),
      Self::Sha256 => f.write_str("SHA256"),
      Self::Sha512 => f.write_str("SHA512"),
    }
  }
}