WIP: Registration

This commit is contained in:
Daniel Lundin 2025-01-18 12:15:59 +01:00
commit a3328e0714
Signed by: dln
SSH key fingerprint: SHA256:dQy1Xj3UiqJYpKR5ggQ2bxgz4jCH8IF+k3AB8o0kmdI
10 changed files with 294 additions and 1 deletions

View file

@ -7,12 +7,17 @@ version.workspace = true
[dependencies]
anyhow.workspace = true
chrono.workspace = true
clap.workspace = true
dropshot.workspace = true
ed25519-dalek.workspace = true
hex.workspace = true
http.workspace = true
instrumentation = { path = "../instrumentation" }
rand.workspace = true
schemars.workspace = true
serde.workspace = true
serde_with.workspace = true
slog-async.workspace = true
slog.workspace = true
sqlx = { version = "0.8.3", default-features = false, features = [

View file

@ -4,6 +4,7 @@ use dropshot::ApiDescription;
use std::sync::Arc;
use crate::context::ControllerContext;
use crate::node;
use crate::user;
use crate::version;
@ -11,6 +12,7 @@ type ControllerApiDescription = ApiDescription<Arc<ControllerContext>>;
pub fn api() -> Result<ControllerApiDescription> {
let mut api = ControllerApiDescription::new();
node::register_api(&mut api)?;
user::register_api(&mut api)?;
api.register(version::version)?;
Ok(api)

View file

@ -1,5 +1,6 @@
pub mod api;
pub mod context;
mod node;
mod user;
mod version;

View file

@ -0,0 +1,70 @@
use chrono::DateTime;
use dropshot::{endpoint, HttpError, HttpResponseOk, RequestContext};
use dropshot::{ApiDescription, ApiDescriptionRegisterError};
use rand::RngCore;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_with::base64::Base64;
use serde_with::base64::Crypt;
use serde_with::serde_as;
use trace_request::trace_request;
use uuid::Uuid;
use std::sync::Arc;
use crate::context::ControllerContext;
pub fn register_api(
api: &mut ApiDescription<Arc<ControllerContext>>,
) -> Result<(), ApiDescriptionRegisterError> {
api.register(get_registration_info)
// api.register(register_node)
}
#[serde_as]
#[derive(Deserialize, JsonSchema, Serialize)]
struct RegistrationInfo {
node_id: Uuid,
expiration: DateTime<chrono::Utc>,
// #[serde_as(as = "Base64<serde_with::base64::BinHex, serde_with::formats::Unpadded>")]
// #[serde_as(as = "Base64<serde_with::base64::UrlSafe, serde_with::formats::Unpadded>")]
#[serde_as(as = "serde_with::base64::Base64")]
// #[serde_as(as = "serde_with::hex::Hex<serde_with::formats::Uppercase>")]
#[schemars(with = "String")]
challenge: [u8; 60],
// #[serde_as(as = "serde_with::base64::Base64")]
#[serde_as(as = "serde_with::hex::Hex")]
// #[serde_as(as = "Base64<serde_with::base64::UrlSafe, serde_with::formats::Unpadded>")]
// #[serde_as(as = "serde_with::hex::Hex<serde_with::formats::Uppercase>")]
#[schemars(with = "String")]
public_key: [u8; 32],
}
/// Get registration info
#[endpoint {
method = GET,
path = "/nodes/register",
tags = [ "node" ],
}]
#[trace_request]
async fn get_registration_info(
rqctx: RequestContext<Arc<ControllerContext>>,
) -> Result<HttpResponseOk<RegistrationInfo>, HttpError> {
tracing::debug!("Registration info");
let rng = &mut rand::rngs::OsRng;
let mut challenge = [0u8; 60];
rng.fill_bytes(&mut challenge);
let key = ed25519_dalek::SigningKey::generate(rng);
let info = RegistrationInfo {
node_id: Uuid::new_v4(),
expiration: chrono::Utc::now(),
challenge,
public_key: key.verifying_key().to_bytes(),
};
Ok(HttpResponseOk(info))
}

View file

@ -0,0 +1,3 @@
mod api;
pub use self::api::register_api;