iop_morpheus_proto/data/
auth.rs

1use super::*;
2
3use iop_keyvault::{multicipher, PublicKey};
4
5#[derive(Clone, Debug, Deserialize, Eq, Serialize)]
6#[serde(untagged)]
7pub enum Authentication {
8    #[serde(with = "serde_str")]
9    KeyId(multicipher::MKeyId),
10    #[serde(with = "serde_str")]
11    PublicKey(multicipher::MPublicKey),
12}
13
14impl PartialEq for Authentication {
15    fn eq(&self, other: &Self) -> bool {
16        match self {
17            Authentication::KeyId(id) => match other {
18                Authentication::KeyId(other_id) => *id == *other_id,
19                Authentication::PublicKey(other_key) => other_key.validate_id(id),
20            },
21            Authentication::PublicKey(key) => match other {
22                Authentication::KeyId(other_id) => key.validate_id(other_id),
23                Authentication::PublicKey(other_key) => *key == *other_key,
24            },
25        }
26    }
27}
28
29impl FromStr for Authentication {
30    type Err = anyhow::Error;
31    fn from_str(s: &str) -> Result<Self, Self::Err> {
32        let auth = serde_json::from_value(serde_json::Value::String(s.to_owned()))?;
33        Ok(auth)
34    }
35}
36
37impl fmt::Display for Authentication {
38    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39        match self {
40            Self::KeyId(id) => id.fmt(f),
41            Self::PublicKey(key) => key.fmt(f),
42        }
43    }
44}