Add token authentication for controllers
This commit is contained in:
parent
f90be43bd2
commit
ebac452a86
3 changed files with 21 additions and 1 deletions
|
|
@ -5,10 +5,11 @@ use actix_web::{get, web, HttpRequest, HttpResponse};
|
|||
use actix_web_actors::ws;
|
||||
use emgauwa_common::errors::EmgauwaError;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
|
||||
use emgauwa_common::constants;
|
||||
use crate::app_state::AppState;
|
||||
use crate::handlers::v1::ws::controllers::ControllersWs;
|
||||
use crate::handlers::v1::ws::relays::RelaysWs;
|
||||
use crate::settings::Settings;
|
||||
|
||||
pub mod controllers;
|
||||
pub mod relays;
|
||||
|
|
@ -17,9 +18,23 @@ pub mod relays;
|
|||
pub async fn ws_controllers(
|
||||
pool: web::Data<Pool<Sqlite>>,
|
||||
app_state: web::Data<Addr<AppState>>,
|
||||
settings: web::Data<Settings>,
|
||||
req: HttpRequest,
|
||||
stream: web::Payload,
|
||||
) -> Result<HttpResponse, EmgauwaError> {
|
||||
let token = req
|
||||
.headers()
|
||||
.get(constants::CONTROLLER_WS_TOKEN_HEADER)
|
||||
.ok_or(EmgauwaError::Unauthorized(
|
||||
String::from("Missing or invalid token header"),
|
||||
))?
|
||||
.to_str()
|
||||
.map_err(|_| EmgauwaError::Unauthorized(String::from("Invalid token header")))?;
|
||||
|
||||
if token != settings.server.token {
|
||||
return Err(EmgauwaError::Unauthorized(String::from("Wrong token header")));
|
||||
}
|
||||
|
||||
let resp = ws::start(
|
||||
ControllersWs {
|
||||
pool: pool.get_ref().clone(),
|
||||
|
|
|
|||
Loading…
Reference in a new issue