tfa: derive WebauthnConfigUpdater via api macro

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller
2021-02-03 10:42:29 +01:00
parent 51c80c5a52
commit 7f9d8438ab
2 changed files with 5 additions and 50 deletions

View File

@ -18,6 +18,7 @@ use webauthn_rs::Webauthn;
use webauthn_rs::proto::Credential as WebauthnCredential;
use proxmox::api::api;
use proxmox::api::schema::{Updatable, Updater};
use proxmox::sys::error::SysError;
use proxmox::tools::fs::CreateOptions;
use proxmox::tools::tfa::totp::Totp;
@ -87,7 +88,7 @@ pub struct U2fConfig {
}
#[api]
#[derive(Clone, Deserialize, Serialize)]
#[derive(Clone, Deserialize, Serialize, Updater)]
#[serde(deny_unknown_fields)]
/// Server side webauthn server configuration.
pub struct WebauthnConfig {
@ -115,53 +116,6 @@ impl WebauthnConfig {
}
}
// TODO: api macro should be able to generate this struct & impl automatically:
#[api]
#[derive(Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
/// Server side webauthn server configuration.
pub struct WebauthnConfigUpdater {
/// Relying party name. Any text identifier.
///
/// Changing this *may* break existing credentials.
rp: Option<String>,
/// Site origin. Must be a `https://` URL (or `http://localhost`). Should contain the address
/// users type in their browsers to access the web interface.
///
/// Changing this *may* break existing credentials.
origin: Option<String>,
/// Relying part ID. Must be the domain name without protocol, port or location.
///
/// Changing this *will* break existing credentials.
id: Option<String>,
}
impl WebauthnConfigUpdater {
pub fn apply_to(self, target: &mut WebauthnConfig) {
if let Some(val) = self.rp {
target.rp = val;
}
if let Some(val) = self.origin {
target.origin = val;
}
if let Some(val) = self.id {
target.id = val;
}
}
pub fn build(self) -> Result<WebauthnConfig, Error> {
Ok(WebauthnConfig {
rp: self.rp.ok_or_else(|| format_err!("missing required field: `rp`"))?,
origin: self.origin.ok_or_else(|| format_err!("missing required field: `origin`"))?,
id: self.id.ok_or_else(|| format_err!("missing required field: `origin`"))?,
})
}
}
/// For now we just implement this on the configuration this way.
///
/// Note that we may consider changing this so `get_origin` returns the `Host:` header provided by