mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 17:02:32 +00:00
principal_type refactoring
This commit is contained in:
@@ -3,7 +3,10 @@ mod principal;
|
|||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use async_trait::async_trait;
|
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]
|
#[async_trait]
|
||||||
pub trait AuthenticationProvider: Send + Sync + 'static {
|
pub trait AuthenticationProvider: Send + Sync + 'static {
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use crate::{Secret, auth::PrincipalType};
|
||||||
use axum::{
|
use axum::{
|
||||||
body::Body,
|
body::Body,
|
||||||
extract::{FromRequestParts, OptionalFromRequestParts},
|
extract::{FromRequestParts, OptionalFromRequestParts},
|
||||||
@@ -6,67 +7,8 @@ use axum::{
|
|||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use derive_more::Display;
|
use derive_more::Display;
|
||||||
use http::{HeaderValue, StatusCode, header};
|
use http::{HeaderValue, StatusCode, header};
|
||||||
use rustical_xml::ValueSerialize;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{convert::Infallible, fmt::Display};
|
use std::convert::Infallible;
|
||||||
|
|
||||||
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<Self, Self::Error> {
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
pub struct AppToken {
|
pub struct AppToken {
|
||||||
|
|||||||
60
crates/store/src/auth/principal_type.rs
Normal file
60
crates/store/src/auth/principal_type.rs
Normal file
@@ -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<Self, Self::Error> {
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user