From 744e0adfcf3ddaeab51c6e847e5201410d8aa34d Mon Sep 17 00:00:00 2001 From: timokoesters Date: Sat, 28 Mar 2020 23:08:59 +0100 Subject: [PATCH] Try to impl auth in ruma_wrapper --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 2 +- src/ruma_wrapper.rs | 42 ++++++++++++++++++++++++++---------------- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d024f974..91c24415 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -492,6 +492,7 @@ dependencies = [ "log 0.4.8", "pretty_env_logger", "rocket", + "ruma-api", "ruma-client-api", "ruma-identifiers", "sled", diff --git a/Cargo.toml b/Cargo.toml index 32c71a25..62322d77 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,4 @@ log = "0.4.8" sled = "0.31.0" directories = "2.0.2" ruma-identifiers = "0.14.1" +ruma-api = "0.15.0-dev.1" diff --git a/src/main.rs b/src/main.rs index 419a01c1..ba953195 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ mod data; mod ruma_wrapper; -use data::Data; +pub use data::Data; use log::debug; use rocket::{get, post, put, routes, State}; use ruma_client_api::{ diff --git a/src/ruma_wrapper.rs b/src/ruma_wrapper.rs index 1f294507..ceaec3d7 100644 --- a/src/ruma_wrapper.rs +++ b/src/ruma_wrapper.rs @@ -1,32 +1,35 @@ -use rocket::{ - data::{FromDataSimple, Outcome}, - http::Status, - response::Responder, - Data, - Outcome::*, - Request, -}; -use ruma_client_api::error::Error; -use std::{ - convert::{TryFrom, TryInto}, - fmt, - io::{Cursor, Read}, - ops::Deref, +use { + rocket::data::{FromDataSimple, Outcome}, + rocket::http::Status, + rocket::response::Responder, + rocket::Outcome::*, + rocket::Request, + rocket::State, + ruma_client_api::error::Error, + std::ops::Deref, + std::{ + convert::{TryFrom, TryInto}, + fmt, + io::{Cursor, Read}, + }, }; const MESSAGE_LIMIT: u64 = 65535; +/// This struct converts rocket requests into ruma structs by converting them into http requests +/// first. pub struct Ruma { body: T, headers: http::HeaderMap, } + impl>>> FromDataSimple for Ruma where T::Error: fmt::Debug, { type Error = (); - fn from_data(request: &Request, data: Data) -> Outcome { + fn from_data(request: &Request, data: rocket::Data) -> Outcome { let mut http_request = http::Request::builder() .uri(request.uri().to_string()) .method(&*request.method().to_string()); @@ -43,7 +46,13 @@ where log::info!("{:?}", http_request); match T::try_from(http_request) { - Ok(t) => Success(Ruma { body: t, headers }), + Ok(t) => { + //if T::METADATA.requires_authentication { + //let data = request.guard::>(); + // TODO: auth + //} + Success(Ruma { body: t, headers }) + } Err(e) => { log::error!("{:?}", e); Failure((Status::InternalServerError, ())) @@ -69,6 +78,7 @@ impl fmt::Debug for Ruma { } } +/// This struct converts ruma responses into rocket http responses. pub struct MatrixResult(pub std::result::Result); impl>>> TryInto>> for MatrixResult { type Error = T::Error;