proxmox-rest-server: make check_auth async

This commit is contained in:
Dietmar Maurer 2021-10-01 07:29:11 +02:00
parent a6c0ec35a3
commit 2b023101f7
3 changed files with 22 additions and 16 deletions

View File

@ -1,5 +1,7 @@
use std::sync::{Arc, Mutex};
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use anyhow::{bail, format_err, Error};
use lazy_static::lazy_static;
@ -26,15 +28,17 @@ impl UserInformation for DummyUserInfo {
struct DummyAuth;
impl ApiAuth for DummyAuth {
fn check_auth(
&self,
_headers: &http::HeaderMap,
_method: &hyper::Method,
) -> Result<(String, Box<dyn UserInformation + Sync + Send>), AuthError> {
fn check_auth<'a>(
&'a self,
_headers: &'a http::HeaderMap,
_method: &'a hyper::Method,
) -> Pin<Box<dyn Future<Output = Result<(String, Box<dyn UserInformation + Sync + Send>), AuthError>> + Send + 'a>> {
Box::pin(async move {
// get some global/cached userinfo
let userinfo = DummyUserInfo;
let userinfo: Box<dyn UserInformation + Sync + Send> = Box::new(DummyUserInfo);
// Do some user checks, e.g. cookie/csrf
Ok(("User".to_string(), Box::new(userinfo)))
Ok(("User".to_string(), userinfo))
})
}
}

View File

@ -16,6 +16,8 @@
//! * generic interface to authenticate user
use std::sync::atomic::{Ordering, AtomicBool};
use std::future::Future;
use std::pin::Pin;
use anyhow::{bail, format_err, Error};
use nix::unistd::Pid;
@ -74,11 +76,11 @@ pub trait ApiAuth {
///
/// If credenthials are valid, returns the username and a
/// [UserInformation] object to query additional user data.
fn check_auth(
&self,
headers: &http::HeaderMap,
method: &hyper::Method,
) -> Result<(String, Box<dyn UserInformation + Sync + Send>), AuthError>;
fn check_auth<'a>(
&'a self,
headers: &'a http::HeaderMap,
method: &'a hyper::Method,
) -> Pin<Box<dyn Future<Output = Result<(String, Box<dyn UserInformation + Sync + Send>), AuthError>> + Send + 'a>>;
}
lazy_static::lazy_static!{

View File

@ -654,7 +654,7 @@ async fn handle_request(
let mut user_info: Box<dyn UserInformation + Send + Sync> = Box::new(EmptyUserInformation {});
if auth_required {
match auth.check_auth(&parts.headers, &method) {
match auth.check_auth(&parts.headers, &method).await {
Ok((authid, info)) => {
rpcenv.set_auth_id(Some(authid));
user_info = info;
@ -726,7 +726,7 @@ async fn handle_request(
if comp_len == 0 {
let language = extract_lang_header(&parts.headers);
match auth.check_auth(&parts.headers, &method) {
match auth.check_auth(&parts.headers, &method).await {
Ok((auth_id, _user_info)) => {
return Ok(api.get_index(Some(auth_id), language, parts));
}