Skip to content

Commit

Permalink
src: lib: server: Add REST endpoints for onvif authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoantoniocardoso committed Nov 28, 2024
1 parent 1e07bfa commit 40d7a76
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/lib/server/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ pub async fn run(server_address: &str) -> Result<(), std::io::Error> {
)
.route("", web::get().to(pages::thumbnail)),
)
.route("/onvif/devices", web::get().to(pages::onvif_devices))
.route(
"/onvif/authentication",
web::post().to(pages::authenticate_onvif_device),
)
.route(
"/onvif/authentication",
web::delete().to(pages::unauthenticate_onvif_device),
)
.build()
})
.bind(server_address)
Expand Down
67 changes: 67 additions & 0 deletions src/lib/server/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,22 @@ impl Info {
}
}

#[derive(Apiv2Schema, Deserialize, Debug)]
pub struct AuthenticateOnvifDeviceRequest {
/// Onvif Device UUID, obtained via `/onvif/devices` get request
device_uuid: uuid::Uuid,
/// Username for the Onvif Device
username: String,
/// Password for the Onvif Device
password: String,
}

#[derive(Apiv2Schema, Deserialize, Debug)]
pub struct UnauthenticateOnvifDeviceRequest {
/// Onvif Device UUID, obtained via `/onvif/devices` get request
device_uuid: uuid::Uuid,
}

use std::{ffi::OsStr, path::Path};

use include_dir::{include_dir, Dir};
Expand Down Expand Up @@ -500,3 +516,54 @@ pub async fn log(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse,

Ok(response)
}

#[api_v2_operation]
pub async fn onvif_devices() -> HttpResponse {
let onvif_devices = crate::controls::onvif::manager::Manager::onvif_devices().await;

match serde_json::to_string_pretty(&onvif_devices) {
Ok(json) => HttpResponse::Ok()
.content_type("application/json")
.body(json),
Err(error) => HttpResponse::InternalServerError()
.content_type("text/plain")
.body(format!("{error:#?}")),
}
}

#[api_v2_operation]
pub async fn authenticate_onvif_device(
query: web::Query<AuthenticateOnvifDeviceRequest>,
) -> HttpResponse {
if let Err(error) = crate::controls::onvif::manager::Manager::register_credentials(
query.device_uuid,
Some(onvif::soap::client::Credentials {
username: query.username.clone(),
password: query.password.clone(),
}),
)
.await
{
return HttpResponse::InternalServerError()
.content_type("text/plain")
.body(format!("{error:#?}"));
}

HttpResponse::Ok().finish()
}

#[api_v2_operation]
pub async fn unauthenticate_onvif_device(
query: web::Query<UnauthenticateOnvifDeviceRequest>,
) -> HttpResponse {
if let Err(error) =
crate::controls::onvif::manager::Manager::register_credentials(query.device_uuid, None)
.await
{
return HttpResponse::InternalServerError()
.content_type("text/plain")
.body(format!("{error:#?}"));
}

HttpResponse::Ok().finish()
}

0 comments on commit 40d7a76

Please sign in to comment.