1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2024-11-17 13:50:49 +01:00

Try to impl auth in ruma_wrapper

This commit is contained in:
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",
"pretty_env_logger",
"rocket",
"ruma-api",
"ruma-client-api",
"ruma-identifiers",
"sled",

View file

@ -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"

View file

@ -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::{

View file

@ -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<T> {
body: T,
headers: http::HeaderMap<http::header::HeaderValue>,
}
impl<T: TryFrom<http::Request<Vec<u8>>>> FromDataSimple for Ruma<T>
where
T::Error: fmt::Debug,
{
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()
.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::<State<crate::Data>>();
// TODO: auth
//}
Success(Ruma { body: t, headers })
}
Err(e) => {
log::error!("{:?}", e);
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>);
impl<T: TryInto<http::Response<Vec<u8>>>> TryInto<http::Response<Vec<u8>>> for MatrixResult<T> {
type Error = T::Error;