-use crate::tools;
-use crate::api_schema::*;
-use crate::api_schema::router::*;
-use crate::api_schema::config::*;
-use crate::auth_helpers::*;
-use super::environment::RestEnvironment;
-use super::formatter::*;
-
+use std::collections::HashMap;
+use std::future::Future;
+use std::hash::BuildHasher;
use std::path::{Path, PathBuf};
+use std::pin::Pin;
use std::sync::Arc;
-use std::collections::HashMap;
+use std::task::{Context, Poll};
-use failure::*;
+use anyhow::{bail, format_err, Error};
+use futures::future::{self, FutureExt, TryFutureExt};
+use futures::stream::TryStreamExt;
+use hyper::header;
+use hyper::http::request::Parts;
+use hyper::{Body, Request, Response, StatusCode};
use serde_json::{json, Value};
+use tokio::fs::File;
+use tokio::time::Instant;
use url::form_urlencoded;
-use futures::future::{self, Either};
-//use tokio::prelude::*;
-//use tokio::timer::Delay;
-use tokio::fs::File;
-//use bytes::{BytesMut, BufMut};
+use proxmox::http_err;
+use proxmox::api::{ApiHandler, ApiMethod, HttpError};
+use proxmox::api::{RpcEnvironment, RpcEnvironmentType, check_api_permission};
+use proxmox::api::schema::{ObjectSchema, parse_simple_value, verify_json_object, parse_parameter_strings};
-//use hyper::body::Payload;
-use hyper::http::request::Parts;
-use hyper::{Body, Request, Response, StatusCode};
-use hyper::service::{Service, NewService};
-use hyper::rt::{Future, Stream};
-use hyper::header;
+use super::environment::RestEnvironment;
+use super::formatter::*;
+use super::ApiConfig;
+
+use crate::auth_helpers::*;
+use crate::tools;
+use crate::config::cached_user_info::CachedUserInfo;
extern "C" { fn tzset(); }
}
}
-impl NewService for RestServer
-{
- type ReqBody = Body;
- type ResBody = Body;
- type Error = hyper::Error;
- type InitError = hyper::Error;
- type Service = ApiService;
- type Future = Box<Future<Item = Self::Service, Error = Self::InitError> + Send>;
- fn new_service(&self) -> Self::Future {
- Box::new(future::ok(ApiService { api_config: self.api_config.clone() }))
+impl tower_service::Service<&tokio_openssl::SslStream<tokio::net::TcpStream>> for RestServer {
+ type Response = ApiService;
+ type Error = Error;
+ type Future = Pin<Box<dyn Future<Output = Result<ApiService, Error>> + Send>>;
+
+ fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
+ Poll::Ready(Ok(()))
+ }
+
+ fn call(&mut self, ctx: &tokio_openssl::SslStream<tokio::net::TcpStream>) -> Self::Future {
+ match ctx.get_ref().peer_addr() {
+ Err(err) => {
+ future::err(format_err!("unable to get peer address - {}", err)).boxed()
+ }
+ Ok(peer) => {
+ future::ok(ApiService { peer, api_config: self.api_config.clone() }).boxed()
+ }
+ }
+ }
+}
+
+impl tower_service::Service<&tokio::net::TcpStream> for RestServer {
+ type Response = ApiService;
+ type Error = Error;
+ type Future = Pin<Box<dyn Future<Output = Result<ApiService, Error>> + Send>>;
+
+ fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
+ Poll::Ready(Ok(()))
+ }
+
+ fn call(&mut self, ctx: &tokio::net::TcpStream) -> Self::Future {
+ match ctx.peer_addr() {
+ Err(err) => {
+ future::err(format_err!("unable to get peer address - {}", err)).boxed()
+ }
+ Ok(peer) => {
+ future::ok(ApiService { peer, api_config: self.api_config.clone() }).boxed()
+ }
+ }
}
}
pub struct ApiService {
+ pub peer: std::net::SocketAddr,
pub api_config: Arc<ApiConfig>,
}
-fn log_response(method: hyper::Method, path: &str, resp: &Response<Body>) {
+fn log_response(
+ peer: &std::net::SocketAddr,
+ method: hyper::Method,
+ path: &str,
+ resp: &Response<Body>,
+) {
if resp.extensions().get::<NoLogExtension>().is_some() { return; };
let status = resp.status();
- if !status.is_success() {
+ if !(status.is_success() || status.is_informational()) {
let reason = status.canonical_reason().unwrap_or("unknown reason");
- let client = "unknown"; // fixme: howto get peer_addr ?
let mut message = "request failed";
if let Some(data) = resp.extensions().get::<ErrorMessageExtension>() {
message = &data.0;
}
- log::error!("{} {}: {} {}: [client {}] {}", method.as_str(), path, status.as_str(), reason, client, message);
+ log::error!("{} {}: {} {}: [client {}] {}", method.as_str(), path, status.as_str(), reason, peer, message);
}
}
-impl Service for ApiService {
- type ReqBody = Body;
- type ResBody = Body;
- type Error = hyper::Error;
- type Future = Box<Future<Item = Response<Body>, Error = Self::Error> + Send>;
+impl tower_service::Service<Request<Body>> for ApiService {
+ type Response = Response<Body>;
+ type Error = Error;
+ type Future = Pin<Box<dyn Future<Output = Result<Response<Body>, Self::Error>> + Send>>;
- fn call(&mut self, req: Request<Self::ReqBody>) -> Self::Future {
+ fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
+ Poll::Ready(Ok(()))
+ }
+
+ fn call(&mut self, req: Request<Body>) -> Self::Future {
let path = req.uri().path().to_owned();
let method = req.method().clone();
- Box::new(handle_request(self.api_config.clone(), req).then(move |result| {
- match result {
+ let peer = self.peer;
+ handle_request(self.api_config.clone(), req)
+ .map(move |result| match result {
Ok(res) => {
- log_response(method, &path, &res);
- Ok::<_, hyper::Error>(res)
+ log_response(&peer, method, &path, &res);
+ Ok::<_, Self::Error>(res)
}
Err(err) => {
if let Some(apierr) = err.downcast_ref::<HttpError>() {
let mut resp = Response::new(Body::from(apierr.message.clone()));
*resp.status_mut() = apierr.code;
- log_response(method, &path, &resp);
+ log_response(&peer, method, &path, &resp);
Ok(resp)
} else {
let mut resp = Response::new(Body::from(err.to_string()));
*resp.status_mut() = StatusCode::BAD_REQUEST;
- log_response(method, &path, &resp);
+ log_response(&peer, method, &path, &resp);
Ok(resp)
}
}
- }
- }))
+ })
+ .boxed()
}
}
-fn get_request_parameters_async(
- info: &'static ApiMethod,
- parts: Parts,
- req_body: Body,
- uri_param: HashMap<String, String>,
-) -> Box<Future<Item = Value, Error = failure::Error> + Send>
-{
- let mut is_json = false;
+fn parse_query_parameters<S: 'static + BuildHasher + Send>(
+ param_schema: &ObjectSchema,
+ form: &str, // x-www-form-urlencoded body data
+ parts: &Parts,
+ uri_param: &HashMap<String, String, S>,
+) -> Result<Value, Error> {
- if let Some(value) = parts.headers.get(header::CONTENT_TYPE) {
- if value == "application/x-www-form-urlencoded" {
- is_json = false;
- } else if value == "application/json" {
- is_json = true;
- } else {
- return Box::new(future::err(http_err!(BAD_REQUEST, format!("unsupported content type"))));
+ let mut param_list: Vec<(String, String)> = vec![];
+
+ if !form.is_empty() {
+ for (k, v) in form_urlencoded::parse(form.as_bytes()).into_owned() {
+ param_list.push((k, v));
}
}
- let resp = req_body
- .map_err(|err| http_err!(BAD_REQUEST, format!("Promlems reading request body: {}", err)))
- .fold(Vec::new(), |mut acc, chunk| {
- if acc.len() + chunk.len() < 64*1024 { //fimxe: max request body size?
- acc.extend_from_slice(&*chunk);
- Ok(acc)
- }
- else { Err(http_err!(BAD_REQUEST, format!("Request body too large"))) }
- })
- .and_then(move |body| {
+ if let Some(query_str) = parts.uri.query() {
+ for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
+ if k == "_dc" { continue; } // skip extjs "disable cache" parameter
+ param_list.push((k, v));
+ }
+ }
- let utf8 = std::str::from_utf8(&body)?;
+ for (k, v) in uri_param {
+ param_list.push((k.clone(), v.clone()));
+ }
- let obj_schema = &info.parameters;
+ let params = parse_parameter_strings(¶m_list, param_schema, true)?;
- if is_json {
- let mut params: Value = serde_json::from_str(utf8)?;
- for (k, v) in uri_param {
- if let Some((_optional, prop_schema)) = obj_schema.properties.get::<str>(&k) {
- params[&k] = parse_simple_value(&v, prop_schema)?;
- }
- }
- return Ok(params);
- }
+ Ok(params)
+}
- let mut param_list: Vec<(String, String)> = vec![];
+async fn get_request_parameters<S: 'static + BuildHasher + Send>(
+ param_schema: &ObjectSchema,
+ parts: Parts,
+ req_body: Body,
+ uri_param: HashMap<String, String, S>,
+) -> Result<Value, Error> {
- if utf8.len() > 0 {
- for (k, v) in form_urlencoded::parse(utf8.as_bytes()).into_owned() {
- param_list.push((k, v));
- }
+ let mut is_json = false;
+ if let Some(value) = parts.headers.get(header::CONTENT_TYPE) {
+ match value.to_str().map(|v| v.split(';').next()) {
+ Ok(Some("application/x-www-form-urlencoded")) => {
+ is_json = false;
}
-
- if let Some(query_str) = parts.uri.query() {
- for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
- if k == "_dc" { continue; } // skip extjs "disable cache" parameter
- param_list.push((k, v));
- }
+ Ok(Some("application/json")) => {
+ is_json = true;
}
+ _ => bail!("unsupported content type {:?}", value.to_str()),
+ }
+ }
- for (k, v) in uri_param {
- param_list.push((k.clone(), v.clone()));
+ let body = req_body
+ .map_err(|err| http_err!(BAD_REQUEST, format!("Promlems reading request body: {}", err)))
+ .try_fold(Vec::new(), |mut acc, chunk| async move {
+ if acc.len() + chunk.len() < 64*1024 { //fimxe: max request body size?
+ acc.extend_from_slice(&*chunk);
+ Ok(acc)
+ } else {
+ Err(http_err!(BAD_REQUEST, "Request body too large".to_string()))
}
+ }).await?;
- let params = parse_parameter_strings(¶m_list, obj_schema, true)?;
-
- Ok(params)
- });
+ let utf8_data = std::str::from_utf8(&body)
+ .map_err(|err| format_err!("Request body not uft8: {}", err))?;
- Box::new(resp)
+ if is_json {
+ let mut params: Value = serde_json::from_str(utf8_data)?;
+ for (k, v) in uri_param {
+ if let Some((_optional, prop_schema)) = param_schema.lookup(&k) {
+ params[&k] = parse_simple_value(&v, prop_schema)?;
+ }
+ }
+ verify_json_object(¶ms, param_schema)?;
+ return Ok(params);
+ } else {
+ parse_query_parameters(param_schema, utf8_data, &parts, &uri_param)
+ }
}
struct NoLogExtension();
-fn proxy_protected_request(
+async fn proxy_protected_request(
info: &'static ApiMethod,
mut parts: Parts,
req_body: Body,
-) -> BoxFut
-{
+) -> Result<Response<Body>, Error> {
let mut uri_parts = parts.uri.clone().into_parts();
let request = Request::from_parts(parts, req_body);
+ let reload_timezone = info.reload_timezone;
+
let resp = hyper::client::Client::new()
.request(request)
.map_err(Error::from)
- .map(|mut resp| {
+ .map_ok(|mut resp| {
resp.extensions_mut().insert(NoLogExtension());
resp
- });
+ })
+ .await?;
+ if reload_timezone { unsafe { tzset(); } }
- let resp = if info.reload_timezone {
- Either::A(resp.then(|resp| {unsafe { tzset() }; resp }))
- } else {
- Either::B(resp)
- };
-
- return Box::new(resp);
+ Ok(resp)
}
-fn handle_sync_api_request(
- mut rpcenv: RestEnvironment,
+pub async fn handle_api_request<Env: RpcEnvironment, S: 'static + BuildHasher + Send>(
+ mut rpcenv: Env,
info: &'static ApiMethod,
formatter: &'static OutputFormatter,
parts: Parts,
req_body: Body,
- uri_param: HashMap<String, String>,
-) -> BoxFut
-{
- let params = get_request_parameters_async(info, parts, req_body, uri_param);
+ uri_param: HashMap<String, String, S>,
+) -> Result<Response<Body>, Error> {
let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
- let resp = params
- .and_then(move |params| {
- let mut delay = false;
- let resp = match (info.handler)(params, info, &mut rpcenv) {
- Ok(data) => (formatter.format_result)(data, &rpcenv),
- Err(err) => {
- if let Some(httperr) = err.downcast_ref::<HttpError>() {
- if httperr.code == StatusCode::UNAUTHORIZED { delay = true; }
- }
- (formatter.format_error)(err)
- }
- };
+ let result = match info.handler {
+ ApiHandler::AsyncHttp(handler) => {
+ let params = parse_query_parameters(info.parameters, "", &parts, &uri_param)?;
+ (handler)(parts, req_body, params, info, Box::new(rpcenv)).await
+ }
+ ApiHandler::Sync(handler) => {
+ let params = get_request_parameters(info.parameters, parts, req_body, uri_param).await?;
+ (handler)(params, info, &mut rpcenv)
+ .map(|data| (formatter.format_data)(data, &rpcenv))
+ }
+ ApiHandler::Async(handler) => {
+ let params = get_request_parameters(info.parameters, parts, req_body, uri_param).await?;
+ (handler)(params, info, &mut rpcenv)
+ .await
+ .map(|data| (formatter.format_data)(data, &rpcenv))
+ }
+ };
- if info.reload_timezone {
- unsafe { tzset() };
+ let resp = match result {
+ Ok(resp) => resp,
+ Err(err) => {
+ if let Some(httperr) = err.downcast_ref::<HttpError>() {
+ if httperr.code == StatusCode::UNAUTHORIZED {
+ tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
+ }
}
+ (formatter.format_error)(err)
+ }
+ };
- if delay {
- Either::A(delayed_response(resp, delay_unauth_time))
- } else {
- Either::B(future::ok(resp))
- }
- });
+ if info.reload_timezone { unsafe { tzset(); } }
- Box::new(resp)
+ Ok(resp)
}
-fn handle_async_api_request(
- mut rpcenv: RestEnvironment,
- info: &'static ApiAsyncMethod,
- formatter: &'static OutputFormatter,
- parts: Parts,
- req_body: Body,
- uri_param: HashMap<String, String>,
-) -> BoxFut
-{
- // fixme: convert parameters to Json
- let mut param_list: Vec<(String, String)> = vec![];
+fn get_index(username: Option<String>, token: Option<String>, api: &Arc<ApiConfig>, parts: Parts) -> Response<Body> {
- if let Some(query_str) = parts.uri.query() {
- for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
- if k == "_dc" { continue; } // skip extjs "disable cache" parameter
- param_list.push((k, v));
- }
- }
+ let nodename = proxmox::tools::nodename();
+ let username = username.unwrap_or_else(|| String::from(""));
- for (k, v) in uri_param {
- param_list.push((k.clone(), v.clone()));
- }
+ let token = token.unwrap_or_else(|| String::from(""));
- let params = match parse_parameter_strings(¶m_list, &info.parameters, true) {
- Ok(v) => v,
- Err(err) => {
- let resp = (formatter.format_error)(Error::from(err));
- return Box::new(future::ok(resp));
- }
- };
+ let mut debug = false;
- match (info.handler)(parts, req_body, params, info, &mut rpcenv) {
- Ok(future) => future,
- Err(err) => {
- let resp = (formatter.format_error)(Error::from(err));
- Box::new(future::ok(resp))
+ if let Some(query_str) = parts.uri.query() {
+ for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
+ if k == "debug" && v != "0" && v != "false" {
+ debug = true;
+ }
}
}
-}
-fn get_index(username: Option<String>, token: Option<String>) -> Response<Body> {
-
- let nodename = tools::nodename();
- let username = username.unwrap_or(String::from(""));
-
- let token = token.unwrap_or(String::from(""));
-
- let setup = json!({
- "Setup": { "auth_cookie_name": "PBSAuthCookie" },
+ let data = json!({
"NodeName": nodename,
"UserName": username,
"CSRFPreventionToken": token,
+ "debug": debug,
});
- let index = format!(r###"
-<!DOCTYPE html>
-<html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
- <title>Proxmox Backup Server</title>
- <link rel="icon" sizes="128x128" href="/images/logo-128.png" />
- <link rel="apple-touch-icon" sizes="128x128" href="/pve2/images/logo-128.png" />
- <link rel="stylesheet" type="text/css" href="/extjs/theme-crisp/resources/theme-crisp-all.css" />
- <link rel="stylesheet" type="text/css" href="/extjs/crisp/resources/charts-all.css" />
- <link rel="stylesheet" type="text/css" href="/fontawesome/css/font-awesome.css" />
- <script type='text/javascript'> function gettext(buf) {{ return buf; }} </script>
- <script type="text/javascript" src="/extjs/ext-all-debug.js"></script>
- <script type="text/javascript" src="/extjs/charts-debug.js"></script>
- <script type="text/javascript">
- Proxmox = {};
- </script>
- <script type="text/javascript" src="/widgettoolkit/proxmoxlib.js"></script>
- <script type="text/javascript" src="/extjs/locale/locale-en.js"></script>
- <script type="text/javascript">
- Ext.History.fieldid = 'x-history-field';
- </script>
- <script type="text/javascript" src="/js/proxmox-backup-gui.js"></script>
- </head>
- <body>
- <!-- Fields required for history management -->
- <form id="history-form" class="x-hidden">
- <input type="hidden" id="x-history-field"/>
- </form>
- </body>
-</html>
-"###, setup.to_string());
+ let mut ct = "text/html";
+
+ let index = match api.render_template("index", &data) {
+ Ok(index) => index,
+ Err(err) => {
+ ct = "text/plain";
+ format!("Error rendering template: {}", err)
+ },
+ };
Response::builder()
.status(StatusCode::OK)
- .header(header::CONTENT_TYPE, "text/html")
+ .header(header::CONTENT_TYPE, ct)
.body(index.into())
.unwrap()
}
("application/octet-stream", false)
}
-fn simple_static_file_download(filename: PathBuf) -> BoxFut {
+async fn simple_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
let (content_type, _nocomp) = extension_to_content_type(&filename);
- Box::new(File::open(filename)
- .map_err(|err| http_err!(BAD_REQUEST, format!("File open failed: {}", err)))
- .and_then(move |file| {
- let buf: Vec<u8> = Vec::new();
- tokio::io::read_to_end(file, buf)
- .map_err(|err| http_err!(BAD_REQUEST, format!("File read failed: {}", err)))
- .and_then(move |data| {
- let mut response = Response::new(data.1.into());
- response.headers_mut().insert(
- header::CONTENT_TYPE,
- header::HeaderValue::from_static(content_type));
- Ok(response)
- })
- }))
-}
+ use tokio::io::AsyncReadExt;
+
+ let mut file = File::open(filename)
+ .await
+ .map_err(|err| http_err!(BAD_REQUEST, format!("File open failed: {}", err)))?;
-fn chuncked_static_file_download(filename: PathBuf) -> BoxFut {
+ let mut data: Vec<u8> = Vec::new();
+ file.read_to_end(&mut data)
+ .await
+ .map_err(|err| http_err!(BAD_REQUEST, format!("File read failed: {}", err)))?;
+ let mut response = Response::new(data.into());
+ response.headers_mut().insert(
+ header::CONTENT_TYPE,
+ header::HeaderValue::from_static(content_type));
+ Ok(response)
+}
+
+async fn chuncked_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
let (content_type, _nocomp) = extension_to_content_type(&filename);
- Box::new(File::open(filename)
- .map_err(|err| http_err!(BAD_REQUEST, format!("File open failed: {}", err)))
- .and_then(move |file| {
- let payload = tokio::codec::FramedRead::new(file, tokio::codec::BytesCodec::new()).
- map(|bytes| {
- //sigh - howto avoid copy here? or the whole map() ??
- hyper::Chunk::from(bytes.to_vec())
- });
- let body = Body::wrap_stream(payload);
-
- // fixme: set other headers ?
- Ok(Response::builder()
- .status(StatusCode::OK)
- .header(header::CONTENT_TYPE, content_type)
- .body(body)
- .unwrap())
- }))
+ let file = File::open(filename)
+ .await
+ .map_err(|err| http_err!(BAD_REQUEST, format!("File open failed: {}", err)))?;
+
+ let payload = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new())
+ .map_ok(|bytes| hyper::body::Bytes::from(bytes.freeze()));
+ let body = Body::wrap_stream(payload);
+
+ // fixme: set other headers ?
+ Ok(Response::builder()
+ .status(StatusCode::OK)
+ .header(header::CONTENT_TYPE, content_type)
+ .body(body)
+ .unwrap()
+ )
}
-fn handle_static_file_download(filename: PathBuf) -> BoxFut {
+async fn handle_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
- let response = tokio::fs::metadata(filename.clone())
+ let metadata = tokio::fs::metadata(filename.clone())
.map_err(|err| http_err!(BAD_REQUEST, format!("File access problems: {}", err)))
- .and_then(|metadata| {
- if metadata.len() < 1024*32 {
- Either::A(simple_static_file_download(filename))
- } else {
- Either::B(chuncked_static_file_download(filename))
- }
- });
+ .await?;
- return Box::new(response);
+ if metadata.len() < 1024*32 {
+ simple_static_file_download(filename).await
+ } else {
+ chuncked_static_file_download(filename).await
+ }
}
fn extract_auth_data(headers: &http::HeaderMap) -> (Option<String>, Option<String>) {
(ticket, token)
}
-fn check_auth(method: &hyper::Method, ticket: &Option<String>, token: &Option<String>) -> Result<String, Error> {
+fn check_auth(
+ method: &hyper::Method,
+ ticket: &Option<String>,
+ token: &Option<String>,
+ user_info: &CachedUserInfo,
+) -> Result<String, Error> {
let ticket_lifetime = tools::ticket::TICKET_LIFETIME;
None => bail!("missing ticket"),
};
+ if !user_info.is_active_user(&username) {
+ bail!("user account disabled or expired.");
+ }
+
if method != hyper::Method::GET {
if let Some(token) = token {
println!("CSRF prevention token: {:?}", token);
Ok(username)
}
-// normalize path
-// do not allow ".", "..", or hidden files ".XXXX"
-// also remove empty path components
-fn normalize_path(path: &str) -> Result<(String, Vec<&str>), Error> {
-
- let items = path.split('/');
-
- let mut path = String::new();
- let mut components = vec![];
-
- for name in items {
- if name.is_empty() { continue; }
- if name.starts_with(".") {
- bail!("Path contains illegal components.");
- }
- path.push('/');
- path.push_str(name);
- components.push(name);
- }
-
- Ok((path, components))
-}
-
-fn delayed_response(resp: Response<Body>, delay_unauth_time: std::time::Instant) -> BoxFut {
-
- Box::new(tokio::timer::Delay::new(delay_unauth_time)
- .map_err(|err| http_err!(INTERNAL_SERVER_ERROR, format!("tokio timer delay error: {}", err)))
- .and_then(|_| Ok(resp)))
-}
-
-pub fn handle_request(api: Arc<ApiConfig>, req: Request<Body>) -> BoxFut {
+pub async fn handle_request(api: Arc<ApiConfig>, req: Request<Body>) -> Result<Response<Body>, Error> {
let (parts, body) = req.into_parts();
let method = parts.method.clone();
-
- let (path, components) = match normalize_path(parts.uri.path()) {
- Ok((p,c)) => (p, c),
- Err(err) => return Box::new(future::err(http_err!(BAD_REQUEST, err.to_string()))),
- };
+ let (path, components) = tools::normalize_uri_path(parts.uri.path())?;
let comp_len = components.len();
- println!("REQUEST {} {}", method, path);
- println!("COMPO {:?}", components);
+ //println!("REQUEST {} {}", method, path);
+ //println!("COMPO {:?}", components);
let env_type = api.env_type();
let mut rpcenv = RestEnvironment::new(env_type);
+ let user_info = CachedUserInfo::new()?;
+
let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
+ let access_forbidden_time = std::time::Instant::now() + std::time::Duration::from_millis(500);
if comp_len >= 1 && components[0] == "api2" {
if comp_len >= 2 {
+
let format = components[1];
+
let formatter = match format {
"json" => &JSON_FORMATTER,
"extjs" => &EXTJS_FORMATTER,
- _ => {
- return Box::new(future::err(http_err!(BAD_REQUEST, format!("Unsupported output format '{}'.", format))));
- }
+ _ => bail!("Unsupported output format '{}'.", format),
};
let mut uri_param = HashMap::new();
- if comp_len == 4 && components[2] == "access" && components[3] == "ticket" {
+ if comp_len == 4 && components[2] == "access" && (
+ (components[3] == "ticket" && method == hyper::Method::POST) ||
+ (components[3] == "domains" && method == hyper::Method::GET)
+ ) {
// explicitly allow those calls without auth
} else {
let (ticket, token) = extract_auth_data(&parts.headers);
- match check_auth(&method, &ticket, &token) {
- Ok(username) => {
-
- // fixme: check permissions
-
- rpcenv.set_user(Some(username));
- }
+ match check_auth(&method, &ticket, &token, &user_info) {
+ Ok(username) => rpcenv.set_user(Some(username)),
Err(err) => {
// always delay unauthorized calls by 3 seconds (from start of request)
- let err = http_err!(UNAUTHORIZED, format!("permission check failed - {}", err));
- return delayed_response((formatter.format_error)(err), delay_unauth_time);
+ let err = http_err!(UNAUTHORIZED, format!("authentication failed - {}", err));
+ tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
+ return Ok((formatter.format_error)(err));
}
}
}
match api.find_method(&components[2..], method, &mut uri_param) {
- MethodDefinition::None => {}
- MethodDefinition::Simple(api_method) => {
- if api_method.protected && env_type == RpcEnvironmentType::PUBLIC {
- return proxy_protected_request(api_method, parts, body);
+ None => {
+ let err = http_err!(NOT_FOUND, format!("Path '{}' not found.", path).to_string());
+ return Ok((formatter.format_error)(err));
+ }
+ Some(api_method) => {
+ let user = rpcenv.get_user();
+ if !check_api_permission(api_method.access.permission, user.as_deref(), &uri_param, user_info.as_ref()) {
+ let err = http_err!(FORBIDDEN, format!("permission check failed"));
+ tokio::time::delay_until(Instant::from_std(access_forbidden_time)).await;
+ return Ok((formatter.format_error)(err));
+ }
+
+ let result = if api_method.protected && env_type == RpcEnvironmentType::PUBLIC {
+ proxy_protected_request(api_method, parts, body).await
} else {
- return handle_sync_api_request(rpcenv, api_method, formatter, parts, body, uri_param);
+ handle_api_request(rpcenv, api_method, formatter, parts, body, uri_param).await
+ };
+
+ if let Err(err) = result {
+ return Ok((formatter.format_error)(err));
}
- }
- MethodDefinition::Async(async_method) => {
- return handle_async_api_request(rpcenv, async_method, formatter, parts, body, uri_param);
+ return result;
}
}
+
}
- } else {
+ } else {
// not Auth required for accessing files!
+ if method != hyper::Method::GET {
+ bail!("Unsupported HTTP method {}", method);
+ }
+
if comp_len == 0 {
let (ticket, token) = extract_auth_data(&parts.headers);
if ticket != None {
- match check_auth(&method, &ticket, &token) {
- Ok(username) => return Box::new(future::ok(get_index(Some(username), token))),
- _ => return delayed_response(get_index(None, None), delay_unauth_time),
+ match check_auth(&method, &ticket, &token, &user_info) {
+ Ok(username) => {
+ let new_token = assemble_csrf_prevention_token(csrf_secret(), &username);
+ return Ok(get_index(Some(username), Some(new_token), &api, parts));
+ }
+ _ => {
+ tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
+ return Ok(get_index(None, None, &api, parts));
+ }
}
} else {
- return Box::new(future::ok(get_index(None, None)));
+ return Ok(get_index(None, None, &api, parts));
}
} else {
let filename = api.find_alias(&components);
- return handle_static_file_download(filename);
+ return handle_static_file_download(filename).await;
}
}
- Box::new(future::err(http_err!(NOT_FOUND, "Path not found.".to_string())))
+ Err(http_err!(NOT_FOUND, format!("Path '{}' not found.", path).to_string()))
}