public inbox for pdm-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Shannon Sterz <s.sterz@proxmox.com>
To: pdm-devel@lists.proxmox.com
Subject: [pdm-devel] [PATCH proxmox v3 12/21] login: add helpers to pass cookie values when parsing login responses
Date: Thu, 27 Feb 2025 15:07:03 +0100	[thread overview]
Message-ID: <20250227140712.209679-13-s.sterz@proxmox.com> (raw)
In-Reply-To: <20250227140712.209679-1-s.sterz@proxmox.com>

depending on the context a client may or may not have access to
HttpOnly cookies. this change allows them to pass such values to
`proxmox-login` to take them into account when parsing login
responses.

Signed-off-by: Shannon Sterz <s.sterz@proxmox.com>
---
 proxmox-login/src/lib.rs | 89 +++++++++++++++++++++++++++++++++-------
 1 file changed, 75 insertions(+), 14 deletions(-)

diff --git a/proxmox-login/src/lib.rs b/proxmox-login/src/lib.rs
index 3b3558d0..52282052 100644
--- a/proxmox-login/src/lib.rs
+++ b/proxmox-login/src/lib.rs
@@ -162,10 +162,27 @@ impl Login {
         &self,
         body: &T,
     ) -> Result<TicketResult, ResponseError> {
-        self.response_bytes(body.as_ref())
+        self.response_bytes(None, body.as_ref())
     }
 
-    fn response_bytes(&self, body: &[u8]) -> Result<TicketResult, ResponseError> {
+    /// Parse the result body of a [`CreateTicket`](api::CreateTicket) API request taking into
+    /// account potential tickets obtained via a `Set-Cookie` header.
+    ///
+    /// On success, this will either yield an [`Authentication`] or a [`SecondFactorChallenge`] if
+    /// Two-Factor-Authentication is required.
+    pub fn response_with_cookie_ticket<T: ?Sized + AsRef<[u8]>>(
+        &self,
+        cookie_ticket: Option<Ticket>,
+        body: &T,
+    ) -> Result<TicketResult, ResponseError> {
+        self.response_bytes(cookie_ticket, body.as_ref())
+    }
+
+    fn response_bytes(
+        &self,
+        cookie_ticket: Option<Ticket>,
+        body: &[u8],
+    ) -> Result<TicketResult, ResponseError> {
         use ticket::TicketResponse;
 
         let response: api::ApiResponse<api::CreateTicketResponse> = serde_json::from_slice(body)?;
@@ -175,6 +192,14 @@ impl Login {
             return Err("ticket response contained unexpected userid".into());
         }
 
+        // if a ticket was provided via a cookie, use it like a normal ticket
+        if let Some(ticket) = cookie_ticket {
+            check_ticket_userid(ticket.userid(), &self.userid)?;
+            return Ok(TicketResult::Full(
+                self.authentication_for(ticket, response)?,
+            ));
+        }
+
         let ticket: TicketResponse = match response.ticket {
             Some(ticket) => ticket.parse()?,
             None => return Err("missing ticket".into()),
@@ -183,15 +208,7 @@ impl Login {
         Ok(match ticket {
             TicketResponse::Full(ticket) => {
                 check_ticket_userid(ticket.userid(), &self.userid)?;
-                TicketResult::Full(Authentication {
-                    csrfprevention_token: response
-                        .csrfprevention_token
-                        .ok_or("missing CSRFPreventionToken in ticket response")?,
-                    clustername: response.clustername,
-                    api_url: self.api_url.clone(),
-                    userid: response.username,
-                    ticket,
-                })
+                TicketResult::Full(self.authentication_for(ticket, response)?)
             }
 
             TicketResponse::Tfa(ticket, challenge) => {
@@ -205,6 +222,22 @@ impl Login {
             }
         })
     }
+
+    fn authentication_for(
+        &self,
+        ticket: Ticket,
+        response: api::CreateTicketResponse,
+    ) -> Result<Authentication, ResponseError> {
+        Ok(Authentication {
+            csrfprevention_token: response
+                .csrfprevention_token
+                .ok_or("missing CSRFPreventionToken in ticket response")?,
+            clustername: response.clustername,
+            api_url: self.api_url.clone(),
+            userid: response.username,
+            ticket,
+        })
+    }
 }
 
 /// This is the result of a ticket call. It will either yield a final ticket, or a TFA challenge.
@@ -310,10 +343,24 @@ impl SecondFactorChallenge {
         &self,
         body: &T,
     ) -> Result<Authentication, ResponseError> {
-        self.response_bytes(body.as_ref())
+        self.response_bytes(None, body.as_ref())
     }
 
-    fn response_bytes(&self, body: &[u8]) -> Result<Authentication, ResponseError> {
+    /// Deal with the API's response object to extract the ticket either from a cookie or the
+    /// response itself.
+    pub fn response_with_cookie_ticket<T: ?Sized + AsRef<[u8]>>(
+        &self,
+        cookie_ticket: Option<Ticket>,
+        body: &T,
+    ) -> Result<Authentication, ResponseError> {
+        self.response_bytes(cookie_ticket, body.as_ref())
+    }
+
+    fn response_bytes(
+        &self,
+        cookie_ticket: Option<Ticket>,
+        body: &[u8],
+    ) -> Result<Authentication, ResponseError> {
         let response: api::ApiResponse<api::CreateTicketResponse> = serde_json::from_slice(body)?;
         let response = response.data.ok_or("missing response data")?;
 
@@ -321,7 +368,21 @@ impl SecondFactorChallenge {
             return Err("ticket response contained unexpected userid".into());
         }
 
-        let ticket: Ticket = response.ticket.ok_or("no ticket in response")?.parse()?;
+        // get the ticket from:
+        // 1. the cookie if possible -> new HttpOnly authentication outside of the browser
+        // 2. just the `ticket_info` -> new HttpOnly authentication inside a browser context or
+        //    similar, assume the ticket is handle by that
+        // 3. the `ticket` field -> old authentication flow where we handle the ticket ourselves
+        let ticket: Ticket = cookie_ticket
+            .ok_or(ResponseError::from("no ticket in response"))
+            .or_else(|e| {
+                response
+                    .ticket_info
+                    .or(response.ticket)
+                    .ok_or(e)
+                    .and_then(|t| t.parse().map_err(|e: TicketError| e.into()))
+            })?;
+
         check_ticket_userid(ticket.userid(), &self.userid)?;
 
         Ok(Authentication {
-- 
2.39.5



_______________________________________________
pdm-devel mailing list
pdm-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel


  parent reply	other threads:[~2025-02-27 14:08 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-27 14:06 [pdm-devel] [PATCH datacenter-manager/proxmox/yew-comp v3 00/21] use HttpOnly cookies in new projects Shannon Sterz
2025-02-27 14:06 ` [pdm-devel] [PATCH proxmox v3 01/21] time: add new `epoch_to_http_date` helper Shannon Sterz
2025-02-27 14:06 ` [pdm-devel] [PATCH proxmox v3 02/21] rest-server: borrow parts parameter in `get_request_parameter` Shannon Sterz
2025-02-27 14:06 ` [pdm-devel] [PATCH proxmox v3 03/21] router/rest-server: add new `AsyncHttpBodyParameters` api handler type Shannon Sterz
2025-02-27 14:06 ` [pdm-devel] [PATCH proxmox v3 04/21] auth-api: extend `AuthContext` with prefixed cookie name Shannon Sterz
2025-02-27 14:06 ` [pdm-devel] [PATCH proxmox v3 05/21] auth-api: check for new prefixed cookies as well Shannon Sterz
2025-02-27 14:06 ` [pdm-devel] [PATCH proxmox v3 06/21] auth-api: introduce new CreateTicket and CreateTickeReponse api types Shannon Sterz
2025-02-27 14:06 ` [pdm-devel] [PATCH proxmox v3 07/21] auth-api: add endpoint for issuing tickets as HttpOnly tickets Shannon Sterz
2025-02-27 14:06 ` [pdm-devel] [PATCH proxmox v3 08/21] auth-api: make regular ticket endpoint use the new types and handler Shannon Sterz
2025-02-27 14:07 ` [pdm-devel] [PATCH proxmox v3 09/21] auth-api: add logout method Shannon Sterz
2025-02-27 14:07 ` [pdm-devel] [PATCH proxmox v3 10/21] login: add optional field for ticket_info and make password optional Shannon Sterz
2025-02-27 14:07 ` [pdm-devel] [PATCH proxmox v3 11/21] login: make password optional when creating Login requests Shannon Sterz
2025-02-27 14:07 ` Shannon Sterz [this message]
2025-02-27 14:07 ` [pdm-devel] [PATCH proxmox v3 13/21] login: add `TicketResult::HttpOnly` member Shannon Sterz
2025-02-27 14:07 ` [pdm-devel] [PATCH proxmox v3 14/21] login: add helper to check whether a ticket is just informational Shannon Sterz
2025-02-27 14:07 ` [pdm-devel] [PATCH proxmox v3 15/21] login: add functions to specify full cookie names Shannon Sterz
2025-02-27 14:07 ` [pdm-devel] [PATCH proxmox v3 16/21] client: add compatibility with HttpOnly cookies Shannon Sterz
2025-02-27 14:07 ` [pdm-devel] [PATCH proxmox v3 17/21] client: specify cookie names for authentication headers where possible Shannon Sterz
2025-02-27 14:07 ` [pdm-devel] [PATCH yew-comp v3 18/21] HttpClient: add helpers to refresh HttpOnly cookies and remove them Shannon Sterz
2025-02-27 14:07 ` [pdm-devel] [PATCH yew-comp v3 19/21] LoginPanel/http helpers: add support for handling HttpOnly cookies Shannon Sterz
2025-02-27 14:07 ` [pdm-devel] [PATCH yew-comp v3 20/21] http helpers: ask server to remove `__Host-` prefixed cookie on logout Shannon Sterz
2025-02-27 14:07 ` [pdm-devel] [PATCH datacenter-manager v3 21/21] api: switch ticket endpoint over to new http only endpoint Shannon Sterz
2025-02-27 14:08 ` [pdm-devel] [PATCH datacenter-manager/proxmox/yew-comp v3 00/21] use HttpOnly cookies in new projects Shannon Sterz
2025-03-04 12:08 ` Shannon Sterz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250227140712.209679-13-s.sterz@proxmox.com \
    --to=s.sterz@proxmox.com \
    --cc=pdm-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal