feat: token extractor middleware

This commit is contained in:
Ahmet Kaan Gümüş 2025-04-16 06:27:08 +03:00
parent 96199f71ef
commit 4f874d8789
9 changed files with 247 additions and 32 deletions

View file

@ -1,11 +1,28 @@
use std::fmt::Display;
use std::{fmt::Display, str::FromStr};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserAndSignal {
pub user: User,
pub signal: Signal,
}
impl UserAndSignal {
pub async fn new(user: User, signal: Signal) -> Self {
UserAndSignal { user, signal }
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
pub username: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Error {
SignalType(SignalType),
UnexpectedSignalType(SignalType),
InvalidSignalType(String),
}
impl std::error::Error for Error {
@ -17,8 +34,11 @@ impl std::error::Error for Error {
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::SignalType(signal_type) => {
write!(f, "Not Expected Signal Type: {}", signal_type)
Error::UnexpectedSignalType(signal_type) => {
write!(f, "Unexpected Signal Type: {}", signal_type)
}
Error::InvalidSignalType(invalid_signal_type) => {
write!(f, "Invalid Signal Type: {}", invalid_signal_type)
}
}
}
@ -29,9 +49,23 @@ pub enum SignalType {
Auth,
Offer,
Answer,
ICECandidate,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
impl FromStr for SignalType {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"Auth" => Ok(SignalType::Auth),
"Offer" => Ok(SignalType::Offer),
"Answer" => Ok(SignalType::Answer),
_ => Err(Error::InvalidSignalType(s.to_owned())),
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Signal {
signal_type: SignalType,
data: String,
@ -44,6 +78,7 @@ impl Display for SignalType {
SignalType::Auth => write!(f, "Auth"),
SignalType::Offer => write!(f, "Offer"),
SignalType::Answer => write!(f, "Answer"),
SignalType::ICECandidate => write!(f, "ICE Candidate"),
}
}
}