trifid/trifid-api/src/main.rs

101 lines
3.3 KiB
Rust
Raw Normal View History

2023-02-02 23:38:39 +00:00
use rocket::{routes, post};
use rocket::{serde::Serialize};
use rocket::http::{ContentType, Status};
use rocket::serde::Deserialize;
use rocket::serde::json::Json;
use crate::format::{validate_dh_pubkey_base64, validate_ed_pubkey_base64};
pub mod format;
pub mod util;
#[derive(Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct EnrollRequest {
pub code: String,
#[serde(rename = "dhPubkey")]
pub dh_pubkey: String,
#[serde(rename = "edPubkey")]
pub ed_pubkey: String,
pub timestamp: String,
}
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
pub struct EnrollResponseMetadata {}
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
pub struct EnrollResponseOrganization {
pub id: String,
pub name: String,
}
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
pub struct EnrollResponseData {
pub config: String,
pub host_id: String,
pub counter: i64,
pub trusted_keys: String,
pub organization: EnrollResponseOrganization,
}
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
pub struct EnrollResponse {
pub data: EnrollResponseData,
pub metadata: EnrollResponseMetadata,
}
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
pub struct APIError {
errors: Vec<APIErrorSingular>
}
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
pub struct APIErrorSingular {
code: String,
message: String
}
pub const ERR_MSG_MALFORMED_REQUEST: &str = "unable to parse the request body - is it valid JSON, using correct types?";
pub const ERR_MSG_MALFORMED_REQUEST_CODE: &str = "ERR_MALFORMED_REQUEST";
#[post("/v2/enroll", data = "<request>")]
fn enroll_endpoint(request: String) -> Result<(ContentType, Json<EnrollResponse>), (Status, Json<APIError>)> {
let request: EnrollRequest = match rocket::serde::json::from_str(request.as_str()) {
Ok(r) => r,
Err(e) => {
return Err((Status::BadRequest, Json(APIError { errors: vec![APIErrorSingular { code: ERR_MSG_MALFORMED_REQUEST_CODE.to_string(), message: format!("{} - {}", ERR_MSG_MALFORMED_REQUEST, e) }]})))
}
};
// validate request
if let Err(e) = validate_dh_pubkey_base64(request.dh_pubkey.as_str()) {
return Err((Status::BadRequest, Json(APIError { errors: vec![APIErrorSingular { code: ERR_MSG_MALFORMED_REQUEST_CODE.to_string(), message: format!("{} - invalid dhPubkey - {}", ERR_MSG_MALFORMED_REQUEST, e) }]})))
}
if let Err(e) = validate_ed_pubkey_base64(request.ed_pubkey.as_str()) {
return Err((Status::BadRequest, Json(APIError { errors: vec![APIErrorSingular { code: ERR_MSG_MALFORMED_REQUEST_CODE.to_string(), message: format!("{} - invalid edPubkey - {}", ERR_MSG_MALFORMED_REQUEST, e) }]})))
}
Ok((ContentType::JSON, Json(EnrollResponse {
data: EnrollResponseData {
config: "sdf".to_string(),
host_id: "sdf".to_string(),
counter: 0,
trusted_keys: "sdf".to_string(),
organization: EnrollResponseOrganization { id: "99s98d9878fds".to_string(), name: "e3team CA".to_string() },
},
metadata: EnrollResponseMetadata {},
})))
}
#[post("/v1/dnclient")]
fn dnclient_endpoint() -> &'static str {
"DNClient functionality is not yet implemented"
}
#[rocket::main]
async fn main() {
let _ = rocket::build().mount("/", routes![enroll_endpoint, dnclient_endpoint]).launch().await;
}