tfa: add webauthn configuration API entry points
Currently there's not yet a node config and the WA config is somewhat "tightly coupled" to the user entries in that changing it can lock them all out, so for now I opted for fewer reorganization and just use a digest of the canonicalized config here, and keep it all in the tfa.json file. Experimentally using the flatten feature on the methods with an`Updater` struct similar to what the api macro is supposed to be able to derive on its own in the future. Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
committed by
Thomas Lamprecht
parent
aefd74197a
commit
a670b99db1
10
src/api2/config/access/mod.rs
Normal file
10
src/api2/config/access/mod.rs
Normal file
@ -0,0 +1,10 @@
|
||||
use proxmox::api::{Router, SubdirMap};
|
||||
use proxmox::list_subdirs_api_method;
|
||||
|
||||
pub mod tfa;
|
||||
|
||||
const SUBDIRS: SubdirMap = &[("tfa", &tfa::ROUTER)];
|
||||
|
||||
pub const ROUTER: Router = Router::new()
|
||||
.get(&list_subdirs_api_method!(SUBDIRS))
|
||||
.subdirs(SUBDIRS);
|
84
src/api2/config/access/tfa/mod.rs
Normal file
84
src/api2/config/access/tfa/mod.rs
Normal file
@ -0,0 +1,84 @@
|
||||
//! For now this only has the TFA subdir, which is in this file.
|
||||
//! If we add more, it should be moved into a sub module.
|
||||
|
||||
use anyhow::Error;
|
||||
|
||||
use crate::api2::types::PROXMOX_CONFIG_DIGEST_SCHEMA;
|
||||
use proxmox::api::{api, Permission, Router, RpcEnvironment, SubdirMap};
|
||||
use proxmox::list_subdirs_api_method;
|
||||
|
||||
use crate::config::tfa::{self, WebauthnConfig, WebauthnConfigUpdater};
|
||||
|
||||
pub const ROUTER: Router = Router::new()
|
||||
.get(&list_subdirs_api_method!(SUBDIRS))
|
||||
.subdirs(SUBDIRS);
|
||||
|
||||
const SUBDIRS: SubdirMap = &[("webauthn", &WEBAUTHN_ROUTER)];
|
||||
|
||||
const WEBAUTHN_ROUTER: Router = Router::new()
|
||||
.get(&API_METHOD_GET_WEBAUTHN_CONFIG)
|
||||
.put(&API_METHOD_UPDATE_WEBAUTHN_CONFIG);
|
||||
|
||||
#[api(
|
||||
protected: true,
|
||||
input: {
|
||||
properties: {},
|
||||
},
|
||||
returns: {
|
||||
type: WebauthnConfig,
|
||||
optional: true,
|
||||
},
|
||||
access: {
|
||||
permission: &Permission::Anybody,
|
||||
},
|
||||
)]
|
||||
/// Get the TFA configuration.
|
||||
pub fn get_webauthn_config(
|
||||
mut rpcenv: &mut dyn RpcEnvironment,
|
||||
) -> Result<Option<WebauthnConfig>, Error> {
|
||||
let (config, digest) = match tfa::webauthn_config()? {
|
||||
Some(c) => c,
|
||||
None => return Ok(None),
|
||||
};
|
||||
rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into();
|
||||
Ok(Some(config))
|
||||
}
|
||||
|
||||
#[api(
|
||||
protected: true,
|
||||
input: {
|
||||
properties: {
|
||||
webauthn: {
|
||||
flatten: true,
|
||||
type: WebauthnConfigUpdater,
|
||||
},
|
||||
digest: {
|
||||
optional: true,
|
||||
schema: PROXMOX_CONFIG_DIGEST_SCHEMA,
|
||||
},
|
||||
},
|
||||
},
|
||||
)]
|
||||
/// Update the TFA configuration.
|
||||
pub fn update_webauthn_config(
|
||||
webauthn: WebauthnConfigUpdater,
|
||||
digest: Option<String>,
|
||||
) -> Result<(), Error> {
|
||||
let _lock = tfa::write_lock();
|
||||
|
||||
let mut tfa = tfa::read()?;
|
||||
|
||||
if let Some(wa) = &mut tfa.webauthn {
|
||||
if let Some(ref digest) = digest {
|
||||
let digest = proxmox::tools::hex_to_digest(digest)?;
|
||||
crate::tools::detect_modified_configuration_file(&digest, &wa.digest()?)?;
|
||||
}
|
||||
webauthn.apply_to(wa);
|
||||
} else {
|
||||
tfa.webauthn = Some(webauthn.build()?);
|
||||
}
|
||||
|
||||
tfa::write(&tfa)?;
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user