✨ encrypted msg + online reporting + refactor
This commit is contained in:
parent
125d50530d
commit
a567214f58
19 changed files with 318 additions and 304 deletions
58
src/herd_core/route.rs
Normal file
58
src/herd_core/route.rs
Normal file
|
@ -0,0 +1,58 @@
|
|||
use crate::api;
|
||||
use crate::api::JoinResponse;
|
||||
use crate::herd_core::model::Machine;
|
||||
use axum::Json;
|
||||
use axum::http::StatusCode;
|
||||
use axum_client_ip::ClientIp;
|
||||
use based::auth::Sessions;
|
||||
use based::auth::User;
|
||||
use owl::save;
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct LoginParam {
|
||||
username: String,
|
||||
password: String,
|
||||
}
|
||||
|
||||
pub async fn login_user(Json(payload): Json<LoginParam>) -> (StatusCode, Json<serde_json::Value>) {
|
||||
log::info!("Login attempt for {}", payload.username);
|
||||
let u = User::find(&payload.username).await.unwrap();
|
||||
if u.read().verify_pw(&payload.password) {
|
||||
let ses = u.read().session().await;
|
||||
(StatusCode::OK, Json(json!({"token": ses.read().token})))
|
||||
} else {
|
||||
(StatusCode::FORBIDDEN, Json(json!({"error": "invalid"})))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn join_device(
|
||||
ClientIp(ip): ClientIp,
|
||||
Json(payload): Json<api::JoinParams>,
|
||||
) -> (StatusCode, Json<serde_json::Value>) {
|
||||
// TODO : check if exists already
|
||||
|
||||
// TODO : validate join token
|
||||
log::info!(
|
||||
"New device joined: {} [{}]",
|
||||
payload.hostname.trim(),
|
||||
payload.machine_id
|
||||
);
|
||||
|
||||
let machine = Machine::from_join_param(payload);
|
||||
let new_token = machine.token.clone();
|
||||
|
||||
save!(machine);
|
||||
|
||||
let i = crate::IDENTITY.get().unwrap();
|
||||
|
||||
(
|
||||
StatusCode::OK,
|
||||
Json(json!(JoinResponse {
|
||||
token: new_token,
|
||||
identity: i.public(),
|
||||
mqtt: crate::CONFIG.get().unwrap().mqtt.clone()
|
||||
})),
|
||||
)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue