From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id 99BC21FF28C for ; Wed, 17 Dec 2025 15:25:44 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 352D853D1; Wed, 17 Dec 2025 15:26:32 +0100 (CET) From: Shannon Sterz To: yew-devel@lists.proxmox.com Date: Wed, 17 Dec 2025 15:26:14 +0100 Message-ID: <20251217142613.277559-4-s.sterz@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20251217142613.277559-2-s.sterz@proxmox.com> References: <20251217142613.277559-2-s.sterz@proxmox.com> MIME-Version: 1.0 X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1765981579148 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.090 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [yew-devel] [PATCH yew-comp 2/2] http wasm client: refactor extract_auth_from_cookie to return a Ticket X-BeenThere: yew-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Yew framework devel list at Proxmox List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Yew framework devel list at Proxmox Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: yew-devel-bounces@lists.proxmox.com Sender: "yew-devel" 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 --- 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 { if let Some(ticket) = extract_auth_from_cookie(project) { if let Some(csrfprevention_token) = current_csrf_token() { - let ticket: Result = 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 { +fn extract_auth_from_cookie(project: &dyn ProjectInfo) -> Option { 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::>(); 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