client: specify cookie names for authentication headers where possible

if the client knows the auth cookie's name, it now passes it on to the
relevant parts of `proxmox-login` so that the ticket is send the
correct cookie

Signed-off-by: Shannon Sterz <s.sterz@proxmox.com>
This commit is contained in:
Shannon Sterz 2025-03-04 15:42:43 +01:00 committed by Wolfgang Bumiller
parent bedf92c0df
commit 0c9ed7daa4

View File

@ -222,8 +222,12 @@ impl Client {
json_body: Option<String>, json_body: Option<String>,
// send an `Accept: application/json-seq` header. // send an `Accept: application/json-seq` header.
streaming: bool, streaming: bool,
cookie_name: &Option<String>,
) -> Result<(http::response::Parts, hyper::Body), Error> { ) -> Result<(http::response::Parts, hyper::Body), Error> {
let mut request = auth.set_auth_headers(Request::builder().method(method).uri(uri)); let mut request = auth.set_auth_headers_with_cookie_name(
Request::builder().method(method).uri(uri),
cookie_name,
);
if streaming { if streaming {
request = request.header(http::header::ACCEPT, "application/json-seq"); request = request.header(http::header::ACCEPT, "application/json-seq");
} }
@ -270,9 +274,18 @@ impl Client {
method: Method, method: Method,
uri: Uri, uri: Uri,
json_body: Option<String>, json_body: Option<String>,
cookie_name: &Option<String>,
) -> Result<HttpApiResponse, Error> { ) -> Result<HttpApiResponse, Error> {
let (response, body) = let (response, body) = Self::send_authenticated_request(
Self::send_authenticated_request(client, auth, method, uri, json_body, false).await?; client,
auth,
method,
uri,
json_body,
false,
cookie_name,
)
.await?;
let body = read_body(body).await?; let body = read_body(body).await?;
let content_type = match response.headers.get(http::header::CONTENT_TYPE) { let content_type = match response.headers.get(http::header::CONTENT_TYPE) {
@ -475,7 +488,7 @@ impl HttpApiClient for Client {
let auth = self.login_auth()?; let auth = self.login_auth()?;
let uri = self.build_uri(path_and_query)?; let uri = self.build_uri(path_and_query)?;
let client = Arc::clone(&self.client); let client = Arc::clone(&self.client);
Self::authenticated_request(client, auth, method, uri, params).await Self::authenticated_request(client, auth, method, uri, params, &self.cookie_name).await
}) })
} }
@ -500,8 +513,16 @@ impl HttpApiClient for Client {
let auth = self.login_auth()?; let auth = self.login_auth()?;
let uri = self.build_uri(path_and_query)?; let uri = self.build_uri(path_and_query)?;
let client = Arc::clone(&self.client); let client = Arc::clone(&self.client);
let (response, body) = let (response, body) = Self::send_authenticated_request(
Self::send_authenticated_request(client, auth, method, uri, params, true).await?; client,
auth,
method,
uri,
params,
true,
&self.cookie_name,
)
.await?;
let content_type = match response.headers.get(http::header::CONTENT_TYPE) { let content_type = match response.headers.get(http::header::CONTENT_TYPE) {
None => None, None => None,
@ -575,6 +596,23 @@ impl AuthenticationKind {
} }
} }
pub fn set_auth_headers_with_cookie_name(
&self,
request: http::request::Builder,
cookie_name: &Option<String>,
) -> http::request::Builder {
match self {
AuthenticationKind::Ticket(auth) => {
if let Some(name) = cookie_name {
auth.set_auth_headers_with_cookie_name(request, name)
} else {
auth.set_auth_headers(request)
}
}
AuthenticationKind::Token(auth) => auth.set_auth_headers(request),
}
}
pub fn userid(&self) -> &str { pub fn userid(&self) -> &str {
match self { match self {
AuthenticationKind::Ticket(auth) => &auth.userid, AuthenticationKind::Ticket(auth) => &auth.userid,