use std::fmt;
use std::str::FromStr;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::did::DidNostr;
use crate::id::{InvitationId, Sha256Digest};
pub const ADMISSION_NOTICE_V1: &str =
"Before you join this Stratum node, please note three things. \
First, the operator of this node can read the data in your pod while it is stored on the node. \
Second, backups of this node hold your pod data, encrypted to recipients the operator chose; \
whoever holds the key for one of those recipients can read it. \
Third, once the node is served on its public origin, Cloudflare terminates TLS for it and \
can read your pod traffic in transit. \
By signing your admission request you confirm that you have read this notice.";
pub fn admission_notice_v1_sha256() -> Sha256Digest {
Sha256Digest::of(ADMISSION_NOTICE_V1.as_bytes())
}
pub const ADMISSION_CLAIM_PURPOSE: &str = "stratum.admission.v1";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AdmissionFields {
pub node: DidNostr,
pub subject: DidNostr,
pub invitation: InvitationId,
pub notice_sha256: Sha256Digest,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AdmissionClaim {
fields: AdmissionFields,
bytes: Vec<u8>,
}
impl AdmissionClaim {
pub fn new(fields: AdmissionFields) -> Self {
let wire = WireAdmission {
purpose: ADMISSION_CLAIM_PURPOSE.to_owned(),
node: fields.node,
subject: fields.subject,
invitation: fields.invitation,
notice_sha256: fields.notice_sha256,
};
let bytes =
serde_json::to_vec(&wire).expect("serde_json cannot fail on a struct of strings");
Self { fields, bytes }
}
pub fn parse(bytes: &[u8]) -> Result<Self, ParseAdmissionError> {
let probe: PurposeProbe =
serde_json::from_slice(bytes).map_err(|_| ParseAdmissionError::Malformed)?;
if probe.purpose != ADMISSION_CLAIM_PURPOSE {
return Err(ParseAdmissionError::WrongPurpose);
}
let wire: WireAdmission =
serde_json::from_slice(bytes).map_err(|_| ParseAdmissionError::Malformed)?;
Ok(Self {
fields: AdmissionFields {
node: wire.node,
subject: wire.subject,
invitation: wire.invitation,
notice_sha256: wire.notice_sha256,
},
bytes: bytes.to_vec(),
})
}
pub fn check(
&self,
expected_node: &DidNostr,
signer: &DidNostr,
expected_notice: &Sha256Digest,
) -> Result<(), CheckAdmissionError> {
if self.fields.node != *expected_node {
return Err(CheckAdmissionError::NodeMismatch);
}
if self.fields.subject != *signer {
return Err(CheckAdmissionError::SubjectMismatch);
}
if self.fields.notice_sha256 != *expected_notice {
return Err(CheckAdmissionError::NoticeMismatch);
}
Ok(())
}
pub fn as_bytes(&self) -> &[u8] {
&self.bytes
}
pub fn into_bytes(self) -> Vec<u8> {
self.bytes
}
pub fn fields(&self) -> &AdmissionFields {
&self.fields
}
pub fn node(&self) -> DidNostr {
self.fields.node
}
pub fn subject(&self) -> DidNostr {
self.fields.subject
}
pub fn invitation(&self) -> InvitationId {
self.fields.invitation
}
pub fn notice_sha256(&self) -> Sha256Digest {
self.fields.notice_sha256
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum ParseAdmissionError {
#[error("admission claim purpose is not {ADMISSION_CLAIM_PURPOSE}")]
WrongPurpose,
#[error("malformed admission claim")]
Malformed,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum CheckAdmissionError {
#[error("admission claim names a different node")]
NodeMismatch,
#[error("admission claim subject is not the request signer")]
SubjectMismatch,
#[error("admission claim notice digest does not match the node's notice")]
NoticeMismatch,
}
#[derive(Deserialize)]
struct PurposeProbe {
purpose: String,
}
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct WireAdmission {
purpose: String,
node: DidNostr,
subject: DidNostr,
invitation: InvitationId,
notice_sha256: Sha256Digest,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum MemberState {
Reserved,
Provisioned,
Active,
Cancelled,
}
impl MemberState {
pub fn as_str(self) -> &'static str {
match self {
Self::Reserved => "reserved",
Self::Provisioned => "provisioned",
Self::Active => "active",
Self::Cancelled => "cancelled",
}
}
pub fn holds_slot(self) -> bool {
matches!(self, Self::Reserved | Self::Provisioned | Self::Active)
}
}
impl fmt::Display for MemberState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for MemberState {
type Err = ParseMemberStateError;
fn from_str(text: &str) -> Result<Self, Self::Err> {
match text {
"reserved" => Ok(Self::Reserved),
"provisioned" => Ok(Self::Provisioned),
"active" => Ok(Self::Active),
"cancelled" => Ok(Self::Cancelled),
_ => Err(ParseMemberStateError::Unknown),
}
}
}
impl Serialize for MemberState {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}
impl<'de> Deserialize<'de> for MemberState {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let text = String::deserialize(deserializer)?;
text.parse().map_err(serde::de::Error::custom)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum ParseMemberStateError {
#[error("unknown member state")]
Unknown,
}