[pdm-devel] [PATCH proxmox 1/3] proxmox-login: refactor PVE TFA compat mode

Christian Ebner c.ebner at proxmox.com
Mon Sep 29 17:48:15 CEST 2025


In preparation for extending the compat mode by Proxmox Backup Server
version 3 API compatibility for login ticket parsing.

Instead of storing the compat mode as simple boolean flag and only
considering PVE, use a `CompatMode` enum instead. Further, make the
variable and method names more generic.

Signed-off-by: Christian Ebner <c.ebner at proxmox.com>
---
 proxmox-login/src/lib.rs | 34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)

diff --git a/proxmox-login/src/lib.rs b/proxmox-login/src/lib.rs
index 4482f2e4..e7b8e023 100644
--- a/proxmox-login/src/lib.rs
+++ b/proxmox-login/src/lib.rs
@@ -49,7 +49,15 @@ pub struct Login {
     api_url: String,
     userid: String,
     password: Option<String>,
-    pve_compat: bool,
+    compat_mode: CompatMode,
+}
+
+/// Compatibility mode to use for login or ticket renewal.
+#[derive(Copy, Clone, Debug, Default, PartialEq)]
+pub enum CompatMode {
+    #[default]
+    Generic,
+    PveTfa,
 }
 
 fn normalize_url(mut api_url: String) -> String {
@@ -90,9 +98,14 @@ impl Login {
 
     /// Prepare a request given an already parsed ticket.
     pub fn renew_ticket(api_url: impl Into<String>, ticket: Ticket) -> Self {
+        let compat_mode = if ticket.product() == "PVE" {
+            CompatMode::PveTfa
+        } else {
+            CompatMode::default()
+        };
         Self {
             api_url: normalize_url(api_url.into()),
-            pve_compat: ticket.product() == "PVE",
+            compat_mode,
             userid: ticket.userid().to_string(),
             password: Some(ticket.into()),
         }
@@ -103,7 +116,7 @@ impl Login {
     pub fn renew_with_cookie(api_url: impl Into<String>, userid: impl Into<String>) -> Self {
         Self {
             api_url: normalize_url(api_url.into()),
-            pve_compat: false,
+            compat_mode: CompatMode::default(),
             userid: userid.into(),
             password: None,
         }
@@ -119,13 +132,13 @@ impl Login {
             api_url: normalize_url(api_url.into()),
             userid: userid.into(),
             password: Some(password.into()),
-            pve_compat: false,
+            compat_mode: CompatMode::default(),
         }
     }
 
     /// Set the Proxmox VE compatibility parameter for Two-Factor-Authentication support.
-    pub fn pve_compatibility(mut self, compatibility: bool) -> Self {
-        self.pve_compat = compatibility;
+    pub fn set_compatibility(mut self, compat_mode: CompatMode) -> Self {
+        self.compat_mode = compat_mode;
         self
     }
 
@@ -135,8 +148,13 @@ impl Login {
     /// [`response`](Login::response) method in order to extract the validated ticket or
     /// Two-Factor-Authentication challenge.
     pub fn request(&self) -> Request {
+        let new_format = if self.compat_mode == CompatMode::PveTfa {
+            Some(true)
+        } else {
+            None
+        };
         let request = api::CreateTicket {
-            new_format: self.pve_compat.then_some(true),
+            new_format,
             username: self.userid.clone(),
             password: self.password.clone(),
             ..Default::default()
@@ -225,7 +243,7 @@ impl Login {
             TicketResponse::Tfa(ticket, challenge) => {
                 TicketResult::TfaRequired(SecondFactorChallenge {
                     api_url: self.api_url.clone(),
-                    pve_compat: self.pve_compat,
+                    pve_compat: self.compat_mode == CompatMode::PveTfa,
                     userid: response.username,
                     ticket,
                     challenge,
-- 
2.47.3





More information about the pdm-devel mailing list