|
| 1 | +use { |
| 2 | + self::{header::Header, payload::Payload, signature::Signature}, |
| 3 | + core::fmt::Debug, |
| 4 | + serde::{Deserialize, Serialize}, |
| 5 | + std::fmt::{Display, Write as _}, |
| 6 | + thiserror::Error as ThisError, |
| 7 | +}; |
| 8 | + |
| 9 | +pub mod header; |
| 10 | +pub mod payload; |
| 11 | +pub mod signature; |
| 12 | + |
| 13 | +/// Errors that can occur during JWT verification |
| 14 | +#[derive(Debug, ThisError)] |
| 15 | +pub enum CacaoError { |
| 16 | + #[error("Invalid header")] |
| 17 | + Header, |
| 18 | + |
| 19 | + #[error("Invalid or missing identity key in payload resources")] |
| 20 | + PayloadIdentityKey, |
| 21 | + |
| 22 | + #[error("Invalid payload resources")] |
| 23 | + PayloadResources, |
| 24 | + |
| 25 | + #[error("Unsupported signature type")] |
| 26 | + UnsupportedSignature, |
| 27 | + |
| 28 | + #[error("Unable to verify")] |
| 29 | + Verification, |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] |
| 33 | +pub enum Version { |
| 34 | + V1 = 1, |
| 35 | +} |
| 36 | + |
| 37 | +impl<'de> Deserialize<'de> for Version { |
| 38 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 39 | + where |
| 40 | + D: serde::Deserializer<'de>, |
| 41 | + { |
| 42 | + let version = String::deserialize(deserializer)?; |
| 43 | + match version.as_str() { |
| 44 | + "1" => Ok(Version::V1), |
| 45 | + _ => Err(serde::de::Error::custom("Invalid version")), |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl Serialize for Version { |
| 51 | + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 52 | + where |
| 53 | + S: serde::Serializer, |
| 54 | + { |
| 55 | + serializer.serialize_str(&format!("{}", *self as u8)) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl Display for Version { |
| 60 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 61 | + write!(f, "{}", *self as u8) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Hash)] |
| 66 | +pub struct Cacao { |
| 67 | + pub h: Header, |
| 68 | + pub p: Payload, |
| 69 | + pub s: Signature, |
| 70 | +} |
| 71 | + |
| 72 | +impl Cacao { |
| 73 | + const ETHEREUM: &'static str = "Ethereum"; |
| 74 | + |
| 75 | + pub fn verify(&self) -> Result<bool, CacaoError> { |
| 76 | + self.p.is_valid()?; |
| 77 | + self.h.is_valid()?; |
| 78 | + self.s.verify(self) |
| 79 | + } |
| 80 | + |
| 81 | + pub fn siwe_message(&self) -> Result<String, CacaoError> { |
| 82 | + self.caip122_message(Self::ETHEREUM) |
| 83 | + } |
| 84 | + |
| 85 | + pub fn caip122_message(&self, chain_name: &str) -> Result<String, CacaoError> { |
| 86 | + let mut message = format!( |
| 87 | + "{} wants you to sign in with your {} account:\n{}\n", |
| 88 | + self.p.domain, |
| 89 | + chain_name, |
| 90 | + self.p.address()? |
| 91 | + ); |
| 92 | + |
| 93 | + if let Some(statement) = &self.p.statement { |
| 94 | + let _ = write!(message, "\n{}\n", statement); |
| 95 | + } |
| 96 | + |
| 97 | + let _ = write!( |
| 98 | + message, |
| 99 | + "\nURI: {}\nVersion: {}\nChain ID: {}\nNonce: {}\nIssued At: {}", |
| 100 | + self.p.aud, |
| 101 | + self.p.version, |
| 102 | + self.p.chain_id()?, |
| 103 | + self.p.nonce, |
| 104 | + self.p.iat |
| 105 | + ); |
| 106 | + |
| 107 | + if let Some(exp) = &self.p.exp { |
| 108 | + let _ = write!(message, "\nExpiration Time: {}", exp); |
| 109 | + } |
| 110 | + |
| 111 | + if let Some(nbf) = &self.p.nbf { |
| 112 | + let _ = write!(message, "\nNot Before: {}", nbf); |
| 113 | + } |
| 114 | + |
| 115 | + if let Some(request_id) = &self.p.request_id { |
| 116 | + let _ = write!(message, "\nRequest ID: {}", request_id); |
| 117 | + } |
| 118 | + |
| 119 | + if let Some(resources) = &self.p.resources { |
| 120 | + if !resources.is_empty() { |
| 121 | + let _ = write!(message, "\nResources:"); |
| 122 | + resources.iter().for_each(|resource| { |
| 123 | + let _ = write!(message, "\n- {}", resource); |
| 124 | + }); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + Ok(message) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +#[cfg(test)] |
| 133 | +mod tests; |
0 commit comments