public inbox for yew-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Shannon Sterz <s.sterz@proxmox.com>
To: yew-devel@lists.proxmox.com
Subject: [yew-devel] [PATCH yew-comp 2/2] http wasm client: refactor extract_auth_from_cookie to return a Ticket
Date: Wed, 17 Dec 2025 15:26:14 +0100	[thread overview]
Message-ID: <20251217142613.277559-4-s.sterz@proxmox.com> (raw)
In-Reply-To: <20251217142613.277559-2-s.sterz@proxmox.com>

instead of a `String`. this saves us the parsing step in the only
caller of this function `authentication_from_cookie`. also refactors
this function to be more efficient (by e.g., not percent decoding
every cookie until we find the ticket cookie) and legible.

Signed-off-by: Shannon Sterz <s.sterz@proxmox.com>
---
 src/http_client_wasm.rs | 49 ++++++++++++++++++++---------------------
 1 file changed, 24 insertions(+), 25 deletions(-)

diff --git a/src/http_client_wasm.rs b/src/http_client_wasm.rs
index 51933bb..5bf67a0 100644
--- a/src/http_client_wasm.rs
+++ b/src/http_client_wasm.rs
@@ -23,42 +23,41 @@ fn convert_js_error(js_err: ::wasm_bindgen::JsValue) -> Error {
 pub fn authentication_from_cookie(project: &dyn ProjectInfo) -> Option<Authentication> {
     if let Some(ticket) = extract_auth_from_cookie(project) {
         if let Some(csrfprevention_token) = current_csrf_token() {
-            let ticket: Result<Ticket, _> = ticket.parse();
-            if let Ok(ticket) = ticket {
-                return Some(Authentication {
-                    api_url: String::new(),
-                    userid: ticket.userid().to_string(),
-                    ticket,
-                    clustername: None,
-                    csrfprevention_token,
-                });
-            }
+            return Some(Authentication {
+                api_url: String::new(),
+                userid: ticket.userid().to_string(),
+                ticket,
+                clustername: None,
+                csrfprevention_token,
+            });
         }
     }
 
     None
 }
 
-fn extract_auth_from_cookie(project: &dyn ProjectInfo) -> Option<String> {
+fn extract_auth_from_cookie(project: &dyn ProjectInfo) -> Option<Ticket> {
     let cookie = crate::get_cookie();
-    //log::info!("COOKIE: {}", cookie);
-
     let name = project.auth_cookie_name();
-    let prefixes = project.auth_cookie_prefixes();
+    let prefixes = project
+        .auth_cookie_prefixes()
+        .iter()
+        .map(|p| format!("{p}:"))
+        .collect::<Vec<String>>();
 
     for part in cookie.split(';') {
-        let part = part.trim();
-        if let Some((key, value)) = part.split_once('=') {
-            // cookie value can be percent encoded
-            let value = match percent_decode_str(value).decode_utf8() {
-                Ok(value) => value,
-                Err(_) => continue,
-            };
-
+        if let Some((key, value)) = part.trim().split_once('=') {
+            // check if current cookie is the ticket cookie
             if key == name {
-                let items: Vec<&str> = value.split(':').take(2).collect();
-                if prefixes.contains(&items[0]) {
-                    return Some(value.to_string());
+                // cookie value can be percent encoded
+                if let Ok(value) = percent_decode_str(value).decode_utf8() {
+                    // check that the ticket prefix matches the ones defined by the current project
+                    if prefixes.iter().any(|p| value.starts_with(p)) {
+                        // parse ticket and return; if this fails keep looking for a valid ticket
+                        if let Ok(ticket) = value.to_string().parse() {
+                            return Some(ticket);
+                        }
+                    }
                 }
             }
         }
-- 
2.47.3



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


      parent reply	other threads:[~2025-12-17 14:25 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-17 14:26 [yew-devel] [PATCH yew-comp 0/2] make http wasm client get fresh csrf tokens from index document Shannon Sterz
2025-12-17 14:26 ` [yew-devel] [PATCH yew-comp 1/2] http wasm client: load csrf token from global Proxmox object Shannon Sterz
2025-12-17 14:26 ` Shannon Sterz [this message]

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=20251217142613.277559-4-s.sterz@proxmox.com \
    --to=s.sterz@proxmox.com \
    --cc=yew-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