Start to implement devices enrollment

This commit is contained in:
2024-07-01 21:10:45 +02:00
parent 378c296e71
commit 9ba4aa5194
21 changed files with 267 additions and 16 deletions

View File

@ -0,0 +1,32 @@
use crate::devices::device::DeviceInfo;
use crate::server::custom_error::HttpResult;
use actix_web::{web, HttpResponse};
use openssl::x509::X509Req;
#[derive(Debug, serde::Deserialize)]
pub struct EnrollRequest {
/// Device CSR
csr: String,
/// Associated device information
info: DeviceInfo,
}
/// Enroll a new device
pub async fn enroll(req: web::Json<EnrollRequest>) -> HttpResult {
let csr = match X509Req::from_pem(req.csr.as_bytes()) {
Ok(r) => r,
Err(e) => {
log::error!("Failed to parse given CSR! {e}");
return Ok(HttpResponse::BadRequest().json("Failed to parse given CSR!"));
}
};
if !csr.verify(csr.public_key()?.as_ref())? {
log::error!("Invalid CSR signature!");
return Ok(HttpResponse::BadRequest().json("Could not verify CSR signature!"));
}
println!("{:#?}", &req);
Ok(HttpResponse::Ok().json("go on"))
}