use super::{Endpoint, EndpointBuilder};
use hmac::{digest::InvalidLength, Hmac, Mac};
use sha1::Sha1;
pub type HmacSha1 = Hmac<Sha1>;
#[derive(Default, Clone)]
pub enum Security {
#[default]
Unsafe,
Hmac(HmacSha1),
}
impl TryFrom<String> for Security {
type Error = InvalidLength;
fn try_from(value: String) -> Result<Self, Self::Error> {
let hmac = HmacSha1::new_from_slice(value.as_bytes())?;
Ok(Security::Hmac(hmac))
}
}
#[derive(Default, Clone)]
pub struct Server {
pub origin: String,
pub security: Security,
}
impl Server {
pub fn new(origin: impl Into<String>, key: impl Into<String>) -> Result<Self, InvalidLength> {
Ok(Server {
origin: origin.into(),
security: key.into().try_into()?,
})
}
pub fn new_unsafe(origin: impl Into<String>) -> Self {
Server {
origin: origin.into(),
security: Security::Unsafe,
}
}
pub fn endpoint_builder(&self) -> EndpointBuilder {
Endpoint::with_server(self.clone())
}
}