1
Fork 0

webauthn: Define code structure

This commit is contained in:
Steffo 2025-02-15 12:52:40 +01:00
parent 9e0439b2a3
commit 3c42f974da
Signed by: steffo
GPG key ID: 6B8E18743E7E1F86
9 changed files with 68 additions and 0 deletions

View file

@ -22,6 +22,7 @@ serde = { version = "1.0.215", features = ["derive"] }
serde_json = "1.0.132"
tokio = { version = "1.41.1", features = ["macros", "net", "rt-multi-thread"] }
mediatype = { version = "0.19.18", features = ["serde"] }
webauthn-rs = "0.5.1"
[lints.clippy]
tabs-in-doc-comments = "allow"

View file

@ -1,3 +1,6 @@
micronfig::config!(
ACRATE_WEBAUTHN_BIND_ADDRESS: String > std::net::SocketAddr,
ACRATE_WEBAUTHN_RELYING_PARTY_ID: String,
ACRATE_WEBAUTHN_RELYING_PARTY_ORIGIN: String > webauthn_rs::prelude::Url,
ACRATE_WEBAUTHN_RELYING_PARTY_NAME: String,
);

View file

@ -0,0 +1,32 @@
use std::sync::Arc;
use axum::Extension;
use webauthn_rs::{Webauthn, WebauthnBuilder};
pub type ExtWebAuthn = Extension<Arc<Webauthn>>;
pub fn ext_webauthn() -> ExtWebAuthn {
log::debug!("Creating ExtWebAuthn...");
let rp_id = crate::config::ACRATE_WEBAUTHN_RELYING_PARTY_ID().as_str();
log::trace!("Relying party ID is set to: {rp_id:?}");
let rp_origin = crate::config::ACRATE_WEBAUTHN_RELYING_PARTY_ORIGIN();
log::trace!("Relying party origin is set to: {rp_origin:?}");
let rp_name = crate::config::ACRATE_WEBAUTHN_RELYING_PARTY_NAME();
log::trace!("Relying party name is set to: {rp_name:?}");
log::trace!("Creating WebAuthnBuilder with the given parameters...");
let builder = WebauthnBuilder::new(rp_id, rp_origin)
.expect("Relying party origin must be a subdomain of relying party ID")
.rp_name(rp_name);
log::trace!("Running builder...");
let webauthn = builder.build().unwrap();
log::trace!("Wrapping in an Arc...");
let arc = Arc::new(webauthn);
log::trace!("Wrapping in an Extension...");
Extension(arc)
}

View file

@ -3,6 +3,7 @@ use acrate_utils::web_server;
mod config;
mod routes;
mod ext;
#[tokio::main]
async fn main() {
@ -11,6 +12,9 @@ async fn main() {
templates: [],
routes: {
"/.healthcheck" => get(routes::healthcheck::handler)
},
layers: {
ext::ext_webauthn()
}
);
}

View file

@ -1 +1,5 @@
pub mod healthcheck;
mod registration_start;
mod registration_finish;
mod authentication_start;
mod authentication_finish;

View file

@ -0,0 +1,24 @@
use axum::Extension;
use axum::http::StatusCode;
use acrate_database::connect::connect_async;
use crate::ext::ExtWebAuthn;
pub async fn handler(
Extension(webauthn): ExtWebAuthn,
) -> Result<StatusCode, StatusCode> {
log::debug!("Handling a registration start request!");
log::trace!("Connecting to the database...");
let _conn = connect_async()
.await
.map_err(|_| StatusCode::BAD_GATEWAY)?;
let result = webauthn.start_passkey_registration(
user_id,
user_name,
user_display_name,
exclude_credentials,
);
Ok(StatusCode::NO_CONTENT)
}