Complete enroll route

This commit is contained in:
2024-07-02 22:55:51 +02:00
parent e64a444bd0
commit 01ffe085d7
8 changed files with 121 additions and 23 deletions

View File

@ -1,4 +1,3 @@
use crate::crypto::pki;
use crate::devices::device::{DeviceId, DeviceInfo};
use crate::energy::energy_actor;
use crate::server::custom_error::HttpResult;
@ -51,18 +50,24 @@ pub async fn enroll(req: web::Json<EnrollRequest>, actor: WebEnergyActor) -> Htt
}
let device_id = DeviceId(cn);
log::info!("Received enrollment request for device with ID {device_id:?}",);
log::info!(
"Received enrollment request for device with ID {device_id:?} - {:#?}",
req.info
);
if actor
.send(energy_actor::CheckDeviceExists(device_id.clone()))
.await?
{
log::error!("Device could not be enrolled: it already exists!");
return Ok(HttpResponse::Conflict().json("Device "));
return Ok(
HttpResponse::Conflict().json("A device with the same ID has already been enrolled!")
);
}
log::info!("Issue certificate for device...");
let cert = pki::gen_certificate_for_device(&csr)?;
actor
.send(energy_actor::EnrollDevice(device_id, req.0.info, csr))
.await??;
Ok(HttpResponse::Ok().body(cert))
Ok(HttpResponse::Accepted().json("Device successfully enrolled"))
}