use super::*;
use crate::{prelude::TryFrom, *};
use serde::{Deserialize, Serialize};
use std::option;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct PlainClientCreds {
pub(crate) username: String,
pub(crate) password: String,
}
impl PlainClientCreds {
pub fn new<U, P>(username: U, password: P) -> Self
where
U: Into<String>,
P: Into<String>,
{
Self {
username: username.into(),
password: password.into(),
}
}
pub fn username(&self) -> &str {
&self.username
}
pub fn password(&self) -> &str {
&self.password
}
}
impl<'a> From<&'a PlainClientCreds> for PlainClientCreds {
fn from(creds: &'a PlainClientCreds) -> Self {
creds.to_owned()
}
}
impl<'a> From<&'a PlainClientCreds> for Mechanism {
fn from(creds: &'a PlainClientCreds) -> Self {
Self::from(creds.to_owned())
}
}
impl From<PlainClientCreds> for Mechanism {
fn from(creds: PlainClientCreds) -> Self {
Mechanism::PlainClient(creds)
}
}
impl IntoIterator for PlainClientCreds {
type Item = Self;
type IntoIter = option::IntoIter<Self>;
fn into_iter(self) -> Self::IntoIter {
Some(self).into_iter()
}
}
impl<'a> IntoIterator for &'a PlainClientCreds {
type Item = Self;
type IntoIter = option::IntoIter<Self>;
fn into_iter(self) -> Self::IntoIter {
Some(self).into_iter()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct CurveClientCreds {
pub(crate) client: Option<CurveCert>,
pub(crate) server: CurvePublicKey,
}
impl CurveClientCreds {
pub fn new<S>(server: S) -> Self
where
S: Into<CurvePublicKey>,
{
Self {
client: None,
server: server.into(),
}
}
pub fn add_cert<C>(mut self, client: C) -> Self
where
C: Into<CurveCert>,
{
self.client = Some(client.into());
self
}
pub fn cert(&self) -> Option<&CurveCert> {
self.client.as_ref()
}
pub fn server(&self) -> &CurvePublicKey {
&self.server
}
}
impl<'a> From<&'a CurveClientCreds> for CurveClientCreds {
fn from(creds: &'a CurveClientCreds) -> Self {
creds.to_owned()
}
}
impl<'a> From<&'a CurveClientCreds> for Mechanism {
fn from(creds: &'a CurveClientCreds) -> Self {
Mechanism::CurveClient(creds.to_owned())
}
}
impl From<CurveClientCreds> for Mechanism {
fn from(creds: CurveClientCreds) -> Self {
Mechanism::CurveClient(creds)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct CurveServerCreds {
pub(crate) secret: CurveSecretKey,
}
impl CurveServerCreds {
pub fn new<S>(secret: S) -> Self
where
S: Into<CurveSecretKey>,
{
Self {
secret: secret.into(),
}
}
pub fn secret(&self) -> &CurveSecretKey {
&self.secret
}
}
impl<'a> From<&'a CurveServerCreds> for CurveServerCreds {
fn from(creds: &'a CurveServerCreds) -> Self {
creds.to_owned()
}
}
impl<'a> From<&'a CurveServerCreds> for Mechanism {
fn from(creds: &'a CurveServerCreds) -> Self {
Mechanism::CurveServer(creds.to_owned())
}
}
impl From<CurveServerCreds> for Mechanism {
fn from(creds: CurveServerCreds) -> Self {
Mechanism::CurveServer(creds)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Mechanism {
Null,
PlainClient(PlainClientCreds),
PlainServer,
CurveClient(CurveClientCreds),
CurveServer(CurveServerCreds),
}
impl<'a> From<&'a Mechanism> for Mechanism {
fn from(mechanism: &'a Mechanism) -> Self {
mechanism.to_owned()
}
}
impl Default for Mechanism {
fn default() -> Self {
Mechanism::Null
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub(crate) enum MechanismName {
Null,
Plain,
Curve,
}
#[derive(Debug, Error)]
#[error("unsupported mechanism")]
pub(crate) struct InvalidMechanismName;
impl<'a> TryFrom<&'a str> for MechanismName {
type Error = InvalidMechanismName;
fn try_from(s: &'a str) -> Result<MechanismName, InvalidMechanismName> {
match s {
"NULL" => Ok(MechanismName::Null),
"PLAIN" => Ok(MechanismName::Plain),
"CURVE" => Ok(MechanismName::Curve),
_ => Err(InvalidMechanismName),
}
}
}