Try to impl auth in ruma_wrapper

next
timokoesters 2020-03-28 23:08:59 +01:00
parent 34a53ce20a
commit 744e0adfcf
No known key found for this signature in database
GPG Key ID: 24DA7517711A2BA4
4 changed files with 29 additions and 17 deletions

1
Cargo.lock generated
View File

@ -492,6 +492,7 @@ dependencies = [
"log 0.4.8", "log 0.4.8",
"pretty_env_logger", "pretty_env_logger",
"rocket", "rocket",
"ruma-api",
"ruma-client-api", "ruma-client-api",
"ruma-identifiers", "ruma-identifiers",
"sled", "sled",

View File

@ -15,3 +15,4 @@ log = "0.4.8"
sled = "0.31.0" sled = "0.31.0"
directories = "2.0.2" directories = "2.0.2"
ruma-identifiers = "0.14.1" ruma-identifiers = "0.14.1"
ruma-api = "0.15.0-dev.1"

View File

@ -2,7 +2,7 @@
mod data; mod data;
mod ruma_wrapper; mod ruma_wrapper;
use data::Data; pub use data::Data;
use log::debug; use log::debug;
use rocket::{get, post, put, routes, State}; use rocket::{get, post, put, routes, State};
use ruma_client_api::{ use ruma_client_api::{

View File

@ -1,32 +1,35 @@
use rocket::{ use {
data::{FromDataSimple, Outcome}, rocket::data::{FromDataSimple, Outcome},
http::Status, rocket::http::Status,
response::Responder, rocket::response::Responder,
Data, rocket::Outcome::*,
Outcome::*, rocket::Request,
Request, rocket::State,
}; ruma_client_api::error::Error,
use ruma_client_api::error::Error; std::ops::Deref,
use std::{ std::{
convert::{TryFrom, TryInto}, convert::{TryFrom, TryInto},
fmt, fmt,
io::{Cursor, Read}, io::{Cursor, Read},
ops::Deref, },
}; };
const MESSAGE_LIMIT: u64 = 65535; const MESSAGE_LIMIT: u64 = 65535;
/// This struct converts rocket requests into ruma structs by converting them into http requests
/// first.
pub struct Ruma<T> { pub struct Ruma<T> {
body: T, body: T,
headers: http::HeaderMap<http::header::HeaderValue>, headers: http::HeaderMap<http::header::HeaderValue>,
} }
impl<T: TryFrom<http::Request<Vec<u8>>>> FromDataSimple for Ruma<T> impl<T: TryFrom<http::Request<Vec<u8>>>> FromDataSimple for Ruma<T>
where where
T::Error: fmt::Debug, T::Error: fmt::Debug,
{ {
type Error = (); type Error = ();
fn from_data(request: &Request, data: Data) -> Outcome<Self, Self::Error> { fn from_data(request: &Request, data: rocket::Data) -> Outcome<Self, Self::Error> {
let mut http_request = http::Request::builder() let mut http_request = http::Request::builder()
.uri(request.uri().to_string()) .uri(request.uri().to_string())
.method(&*request.method().to_string()); .method(&*request.method().to_string());
@ -43,7 +46,13 @@ where
log::info!("{:?}", http_request); log::info!("{:?}", http_request);
match T::try_from(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::<State<crate::Data>>();
// TODO: auth
//}
Success(Ruma { body: t, headers })
}
Err(e) => { Err(e) => {
log::error!("{:?}", e); log::error!("{:?}", e);
Failure((Status::InternalServerError, ())) Failure((Status::InternalServerError, ()))
@ -69,6 +78,7 @@ impl<T: fmt::Debug> fmt::Debug for Ruma<T> {
} }
} }
/// This struct converts ruma responses into rocket http responses.
pub struct MatrixResult<T>(pub std::result::Result<T, Error>); pub struct MatrixResult<T>(pub std::result::Result<T, Error>);
impl<T: TryInto<http::Response<Vec<u8>>>> TryInto<http::Response<Vec<u8>>> for MatrixResult<T> { impl<T: TryInto<http::Response<Vec<u8>>>> TryInto<http::Response<Vec<u8>>> for MatrixResult<T> {
type Error = T::Error; type Error = T::Error;