From a909d5789c7f40e2d8bf596fb8b03c3b246de35c Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Thu, 10 Aug 2023 14:30:31 +0200 Subject: [PATCH] client: convenience helper to get a serialized ticket Signed-off-by: Wolfgang Bumiller --- proxmox-client/src/client.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/proxmox-client/src/client.rs b/proxmox-client/src/client.rs index b9d5377f..a0fa4013 100644 --- a/proxmox-client/src/client.rs +++ b/proxmox-client/src/client.rs @@ -133,6 +133,20 @@ impl Client { self.auth.lock().unwrap().clone() } + /// Get a serialized version of the ticket if one is used. + /// + /// This returns `None` when using an API token and `Error::Unauthorized` if not logged in. + pub fn serialize_ticket(&self) -> Result>, Error> { + let auth = self.authentication().ok_or(Error::Unauthorized)?; + let auth = match &*auth { + AuthenticationKind::Token(_) => return Ok(None), + AuthenticationKind::Ticket(auth) => auth, + }; + Ok(Some(serde_json::to_vec(auth).map_err(|err| { + Error::internal("failed to serialize ticket", err) + })?)) + } + #[deprecated(note = "use set_authentication instead")] /// Replace the authentication information with an API token. pub fn use_api_token(&self, token: Token) {