tfa api: return types and 'pub' structs/methods

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2020-12-18 13:59:35 +01:00
parent f58e5132aa
commit 759af9f00c

View File

@ -1,6 +1,5 @@
use anyhow::{bail, format_err, Error}; use anyhow::{bail, format_err, Error};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value;
use proxmox::api::{api, Permission, Router, RpcEnvironment}; use proxmox::api::{api, Permission, Router, RpcEnvironment};
use proxmox::tools::tfa::totp::Totp; use proxmox::tools::tfa::totp::Totp;
@ -45,7 +44,7 @@ fn tfa_update_auth(
/// A TFA entry type. /// A TFA entry type.
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
#[serde(rename_all = "lowercase")] #[serde(rename_all = "lowercase")]
pub enum TfaType { enum TfaType {
/// A TOTP entry type. /// A TOTP entry type.
Totp, Totp,
/// A U2F token entry. /// A U2F token entry.
@ -65,7 +64,7 @@ pub enum TfaType {
/// A TFA entry for a user. /// A TFA entry for a user.
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
pub struct TypedTfaInfo { struct TypedTfaInfo {
#[serde(rename = "type")] #[serde(rename = "type")]
pub ty: TfaType, pub ty: TfaType,
@ -145,7 +144,7 @@ fn tfa_id_iter(data: &TfaUserData) -> impl Iterator<Item = (TfaType, usize, &str
}, },
)] )]
/// Add a TOTP secret to the user. /// Add a TOTP secret to the user.
pub fn list_user_tfa(userid: Userid) -> Result<Vec<TypedTfaInfo>, Error> { fn list_user_tfa(userid: Userid) -> Result<Vec<TypedTfaInfo>, Error> {
let _lock = crate::config::tfa::read_lock()?; let _lock = crate::config::tfa::read_lock()?;
Ok(match crate::config::tfa::read()?.users.remove(&userid) { Ok(match crate::config::tfa::read()?.users.remove(&userid) {
@ -170,7 +169,7 @@ pub fn list_user_tfa(userid: Userid) -> Result<Vec<TypedTfaInfo>, Error> {
}, },
)] )]
/// Get a single TFA entry. /// Get a single TFA entry.
pub fn get_tfa_entry(userid: Userid, id: String) -> Result<TypedTfaInfo, Error> { fn get_tfa_entry(userid: Userid, id: String) -> Result<TypedTfaInfo, Error> {
let _lock = crate::config::tfa::read_lock()?; let _lock = crate::config::tfa::read_lock()?;
if let Some(user_data) = crate::config::tfa::read()?.users.remove(&userid) { if let Some(user_data) = crate::config::tfa::read()?.users.remove(&userid) {
@ -233,7 +232,7 @@ pub fn get_tfa_entry(userid: Userid, id: String) -> Result<TypedTfaInfo, Error>
}, },
)] )]
/// Get a single TFA entry. /// Get a single TFA entry.
pub fn delete_tfa( fn delete_tfa(
userid: Userid, userid: Userid,
id: String, id: String,
password: Option<String>, password: Option<String>,
@ -283,7 +282,7 @@ pub fn delete_tfa(
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
/// Over the API we only provide the descriptions for TFA data. /// Over the API we only provide the descriptions for TFA data.
pub struct TfaUser { struct TfaUser {
/// The user this entry belongs to. /// The user this entry belongs to.
userid: Userid, userid: Userid,
@ -300,9 +299,14 @@ pub struct TfaUser {
permission: &Permission::Anybody, permission: &Permission::Anybody,
description: "Returns all or just the logged-in user, depending on privileges.", description: "Returns all or just the logged-in user, depending on privileges.",
}, },
returns: {
description: "The list tuples of user and TFA entries.",
type: Array,
items: { type: TfaUser }
},
)] )]
/// List user TFA configuration. /// List user TFA configuration.
pub fn list_tfa(rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> { fn list_tfa(rpcenv: &mut dyn RpcEnvironment) -> Result<Vec<TfaUser>, Error> {
let authid: Authid = rpcenv.get_auth_id().unwrap().parse()?; let authid: Authid = rpcenv.get_auth_id().unwrap().parse()?;
let user_info = CachedUserInfo::new()?; let user_info = CachedUserInfo::new()?;
@ -329,7 +333,7 @@ pub fn list_tfa(rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> {
} }
} }
Ok(serde_json::to_value(out)?) Ok(out)
} }
#[api( #[api(
@ -535,7 +539,7 @@ fn add_tfa_entry(
}, },
)] )]
/// Update user's TFA entry description. /// Update user's TFA entry description.
pub fn update_tfa_entry( fn update_tfa_entry(
userid: Userid, userid: Userid,
id: String, id: String,
description: Option<String>, description: Option<String>,