Add token authentication for controllers
This commit is contained in:
parent
f90be43bd2
commit
ebac452a86
3 changed files with 21 additions and 1 deletions
4
core.pkl
4
core.pkl
|
|
@ -1,5 +1,9 @@
|
||||||
amends "package://emgauwa.app/pkl/emgauwa@0.2.1#/core.pkl"
|
amends "package://emgauwa.app/pkl/emgauwa@0.2.1#/core.pkl"
|
||||||
|
|
||||||
|
server {
|
||||||
|
token = "dev tokenx"
|
||||||
|
}
|
||||||
|
|
||||||
logging {
|
logging {
|
||||||
level = "DEBUG"
|
level = "DEBUG"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,11 @@ use actix_web::{get, web, HttpRequest, HttpResponse};
|
||||||
use actix_web_actors::ws;
|
use actix_web_actors::ws;
|
||||||
use emgauwa_common::errors::EmgauwaError;
|
use emgauwa_common::errors::EmgauwaError;
|
||||||
use sqlx::{Pool, Sqlite};
|
use sqlx::{Pool, Sqlite};
|
||||||
|
use emgauwa_common::constants;
|
||||||
use crate::app_state::AppState;
|
use crate::app_state::AppState;
|
||||||
use crate::handlers::v1::ws::controllers::ControllersWs;
|
use crate::handlers::v1::ws::controllers::ControllersWs;
|
||||||
use crate::handlers::v1::ws::relays::RelaysWs;
|
use crate::handlers::v1::ws::relays::RelaysWs;
|
||||||
|
use crate::settings::Settings;
|
||||||
|
|
||||||
pub mod controllers;
|
pub mod controllers;
|
||||||
pub mod relays;
|
pub mod relays;
|
||||||
|
|
@ -17,9 +18,23 @@ pub mod relays;
|
||||||
pub async fn ws_controllers(
|
pub async fn ws_controllers(
|
||||||
pool: web::Data<Pool<Sqlite>>,
|
pool: web::Data<Pool<Sqlite>>,
|
||||||
app_state: web::Data<Addr<AppState>>,
|
app_state: web::Data<Addr<AppState>>,
|
||||||
|
settings: web::Data<Settings>,
|
||||||
req: HttpRequest,
|
req: HttpRequest,
|
||||||
stream: web::Payload,
|
stream: web::Payload,
|
||||||
) -> Result<HttpResponse, EmgauwaError> {
|
) -> 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(
|
let resp = ws::start(
|
||||||
ControllersWs {
|
ControllersWs {
|
||||||
pool: pool.get_ref().clone(),
|
pool: pool.get_ref().clone(),
|
||||||
|
|
|
||||||
|
|
@ -72,6 +72,7 @@ async fn main() -> Result<(), std::io::Error> {
|
||||||
.app_data(web::JsonConfig::default().error_handler(handlers::json_error_handler))
|
.app_data(web::JsonConfig::default().error_handler(handlers::json_error_handler))
|
||||||
.app_data(web::Data::new(pool.clone()))
|
.app_data(web::Data::new(pool.clone()))
|
||||||
.app_data(web::Data::new(app_state.clone()))
|
.app_data(web::Data::new(app_state.clone()))
|
||||||
|
.app_data(web::Data::new(settings.clone()))
|
||||||
.service(
|
.service(
|
||||||
SwaggerUi::new("/api/docs/{_:.*}")
|
SwaggerUi::new("/api/docs/{_:.*}")
|
||||||
.external_urls_from_iter_unchecked([("/api/v1.json", api_v1_json.clone())]),
|
.external_urls_from_iter_unchecked([("/api/v1.json", api_v1_json.clone())]),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue