Add token authentication for controllers

This commit is contained in:
Tobias Reisinger 2025-05-31 23:18:45 +02:00
parent faacdc8c83
commit e2dc5f8857
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
3 changed files with 9 additions and 0 deletions

View file

@ -1,6 +1,10 @@
use std::time::Duration; use std::time::Duration;
pub const DEFAULT_PORT: u16 = 4419; pub const DEFAULT_PORT: u16 = 4419;
pub const DEFAULT_TOKEN: &str = "emgauwa_token";
pub const CONTROLLER_WS_TOKEN_HEADER: &str = "X-Emgauwa-Token";
pub const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5); pub const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
pub const HEARTBEAT_TIMEOUT: Duration = Duration::from_secs(15); pub const HEARTBEAT_TIMEOUT: Duration = Duration::from_secs(15);

View file

@ -15,6 +15,7 @@ use crate::types::EmgauwaUid;
#[derive(Debug)] #[derive(Debug)]
pub enum EmgauwaError { pub enum EmgauwaError {
Api(ApiError), Api(ApiError),
Unauthorized(String),
Uid(uuid::Error), Uid(uuid::Error),
Serialization(serde_json::Error), Serialization(serde_json::Error),
Database(DatabaseError), Database(DatabaseError),
@ -28,6 +29,7 @@ impl EmgauwaError {
fn get_code(&self) -> StatusCode { fn get_code(&self) -> StatusCode {
match self { match self {
EmgauwaError::Api(err) => err.get_code(), EmgauwaError::Api(err) => err.get_code(),
EmgauwaError::Unauthorized(_) => StatusCode::UNAUTHORIZED,
EmgauwaError::Serialization(_) => StatusCode::INTERNAL_SERVER_ERROR, EmgauwaError::Serialization(_) => StatusCode::INTERNAL_SERVER_ERROR,
EmgauwaError::Database(err) => err.get_code(), EmgauwaError::Database(err) => err.get_code(),
EmgauwaError::Uid(_) => StatusCode::BAD_REQUEST, EmgauwaError::Uid(_) => StatusCode::BAD_REQUEST,
@ -43,6 +45,7 @@ impl From<&EmgauwaError> for String {
fn from(err: &EmgauwaError) -> Self { fn from(err: &EmgauwaError) -> Self {
match err { match err {
EmgauwaError::Api(err) => String::from(err), EmgauwaError::Api(err) => String::from(err),
EmgauwaError::Unauthorized(_) => format!("unauthorized request: {}", err),
EmgauwaError::Serialization(_) => String::from("error during (de-)serialization"), EmgauwaError::Serialization(_) => String::from("error during (de-)serialization"),
EmgauwaError::Database(err) => String::from(err), EmgauwaError::Database(err) => String::from(err),
EmgauwaError::Uid(_) => String::from("the uid is in a bad format"), EmgauwaError::Uid(_) => String::from("the uid is in a bad format"),

View file

@ -9,6 +9,7 @@ use crate::errors::EmgauwaError;
pub struct Server { pub struct Server {
pub host: String, pub host: String,
pub port: u16, pub port: u16,
pub token: String,
} }
#[derive(Clone, Debug, Deserialize)] #[derive(Clone, Debug, Deserialize)]
@ -31,6 +32,7 @@ impl Default for Server {
Server { Server {
host: String::from("127.0.0.1"), host: String::from("127.0.0.1"),
port: constants::DEFAULT_PORT, port: constants::DEFAULT_PORT,
token: String::from(constants::DEFAULT_TOKEN),
} }
} }
} }