ordinary-api 0.6.0-pre.13

API server for Ordinary
Documentation
// Copyright (C) 2026 Ordinary Labs, LLC.
//
// SPDX-License-Identifier: AGPL-3.0-only

use crate::server::APPLICATION;
use axum::extract::{Query, State};
use axum::http::{HeaderMap, StatusCode};
use axum::response::IntoResponse;
use serde::Deserialize;
use std::sync::Arc;
use tracing::Instrument;
use utoipa::IntoParams;

#[derive(Deserialize, IntoParams)]
pub struct PublicKeyParams {
    /// project domain
    d: String,
}

#[utoipa::path(
    get,
    path = "/keys/dh/public",
    tag = APPLICATION,
    params(PublicKeyParams),
    responses(
        (status = 401, description = "unauthorized for operation"),
        (status = 200, description = "retrieved diffie-hellman public key", body = [u8]),
    ),
    security(
        ("access" = []),
    ),
)]
pub async fn dh_public_key(
    State(state): State<Arc<crate::server::OrdinaryApiServerState>>,
    Query(PublicKeyParams { d }): Query<PublicKeyParams>,
    headers: HeaderMap,
) -> impl IntoResponse {
    let domain = d;

    let span = tracing::info_span!("app", %domain);
    let span = span.in_scope(|| tracing::info_span!("keys"));
    let span = span.in_scope(|| tracing::info_span!("dh"));
    let span = span.in_scope(|| tracing::info_span!("public"));

    async {
        let account = match crate::server::check_ordinary_auth(&state, &headers, 0, &domain) {
            Ok(account) => account,
            Err(code) => return code.into_response(),
        };

        tracing::info!(account, "getting");

        let apps = state.apps.read().await;

        if let Some(wrapped_app) = apps.get(&domain) {
            return (StatusCode::OK, wrapped_app.dh_keypair.1.to_bytes()).into_response();
        }

        StatusCode::NOT_FOUND.into_response()
    }
    .instrument(span)
    .await
}