diff --git a/crates/store/src/auth/mod.rs b/crates/store/src/auth/mod.rs index 1b99c4d..202aa9b 100644 --- a/crates/store/src/auth/mod.rs +++ b/crates/store/src/auth/mod.rs @@ -3,7 +3,10 @@ mod principal; use crate::error::Error; use async_trait::async_trait; -pub use principal::{AppToken, Principal, PrincipalType}; +mod principal_type; +pub use principal_type::*; + +pub use principal::{AppToken, Principal}; #[async_trait] pub trait AuthenticationProvider: Send + Sync + 'static { diff --git a/crates/store/src/auth/principal.rs b/crates/store/src/auth/principal.rs index de15a1b..a022c42 100644 --- a/crates/store/src/auth/principal.rs +++ b/crates/store/src/auth/principal.rs @@ -1,3 +1,4 @@ +use crate::{Secret, auth::PrincipalType}; use axum::{ body::Body, extract::{FromRequestParts, OptionalFromRequestParts}, @@ -6,67 +7,8 @@ use axum::{ use chrono::{DateTime, Utc}; use derive_more::Display; use http::{HeaderValue, StatusCode, header}; -use rustical_xml::ValueSerialize; use serde::{Deserialize, Serialize}; -use std::{convert::Infallible, fmt::Display}; - -use crate::Secret; - -/// https://datatracker.ietf.org/doc/html/rfc5545#section-3.2.3 -#[derive(Debug, Clone, Deserialize, Serialize, Default, PartialEq, clap::ValueEnum)] -#[serde(rename_all = "lowercase")] -pub enum PrincipalType { - #[default] - Individual, - Group, - Resource, - Room, - Unknown, - // TODO: X-Name, IANA-token -} - -impl TryFrom<&str> for PrincipalType { - type Error = crate::Error; - - fn try_from(value: &str) -> Result { - Ok(match value { - "INDIVIDUAL" => Self::Individual, - "GROUP" => Self::Group, - "RESOURCE" => Self::Resource, - "ROOM" => Self::Room, - "UNKNOWN" => Self::Unknown, - _ => { - return Err(crate::Error::InvalidPrincipalType( - "Invalid principal type".to_owned(), - )); - } - }) - } -} - -impl PrincipalType { - pub fn as_str(&self) -> &'static str { - match self { - PrincipalType::Individual => "INDIVIDUAL", - PrincipalType::Group => "GROUP", - PrincipalType::Resource => "RESOURCE", - PrincipalType::Room => "ROOM", - PrincipalType::Unknown => "UNKNOWN", - } - } -} - -impl Display for PrincipalType { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str(self.as_str()) - } -} - -impl ValueSerialize for PrincipalType { - fn serialize(&self) -> String { - self.to_string() - } -} +use std::convert::Infallible; #[derive(Debug, Clone, Deserialize, Serialize)] pub struct AppToken { diff --git a/crates/store/src/auth/principal_type.rs b/crates/store/src/auth/principal_type.rs new file mode 100644 index 0000000..271f239 --- /dev/null +++ b/crates/store/src/auth/principal_type.rs @@ -0,0 +1,60 @@ +use std::fmt::Display; + +use rustical_xml::ValueSerialize; +use serde::{Deserialize, Serialize}; + +/// https://datatracker.ietf.org/doc/html/rfc5545#section-3.2.3 +#[derive(Debug, Clone, Deserialize, Serialize, Default, PartialEq, clap::ValueEnum)] +#[serde(rename_all = "lowercase")] +pub enum PrincipalType { + #[default] + Individual, + Group, + Resource, + Room, + Unknown, + // TODO: X-Name, IANA-token +} + +impl TryFrom<&str> for PrincipalType { + type Error = crate::Error; + + fn try_from(value: &str) -> Result { + Ok(match value { + "INDIVIDUAL" => Self::Individual, + "GROUP" => Self::Group, + "RESOURCE" => Self::Resource, + "ROOM" => Self::Room, + "UNKNOWN" => Self::Unknown, + _ => { + return Err(crate::Error::InvalidPrincipalType( + "Invalid principal type".to_owned(), + )); + } + }) + } +} + +impl PrincipalType { + pub fn as_str(&self) -> &'static str { + match self { + PrincipalType::Individual => "INDIVIDUAL", + PrincipalType::Group => "GROUP", + PrincipalType::Resource => "RESOURCE", + PrincipalType::Room => "ROOM", + PrincipalType::Unknown => "UNKNOWN", + } + } +} + +impl Display for PrincipalType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.as_str()) + } +} + +impl ValueSerialize for PrincipalType { + fn serialize(&self) -> String { + self.to_string() + } +}