register login flow

This commit is contained in:
timokoesters 2020-04-06 17:37:13 +02:00
parent eb7c5b79be
commit 64223b8812
No known key found for this signature in database
GPG key ID: 24DA7517711A2BA4
2 changed files with 29 additions and 7 deletions

View file

@ -44,6 +44,21 @@ fn register_route(
data: State<Data>,
body: Ruma<register::Request>,
) -> MatrixResult<register::Response> {
if body.auth.is_none() {
return MatrixResult(Err(Error {
kind: ErrorKind::InvalidUsername,
message: serde_json::to_string(&json!({
"flows": [
{ "stages": [ "m.login.dummy" ] },
],
"params": {},
"session": "TODO:randomsessionid",
}))
.unwrap(),
status_code: http::StatusCode::UNAUTHORIZED,
}));
}
// Validate user id
let user_id: UserId = match (*format!(
"@{}:{}",
@ -353,7 +368,7 @@ fn options_route(_segments: PathBuf) -> MatrixResult<create_message_event::Respo
MatrixResult(Err(Error {
kind: ErrorKind::NotFound,
message: "This is the options route.".to_owned(),
status_code: http::StatusCode::NOT_FOUND,
status_code: http::StatusCode::OK,
}))
}

View file

@ -1,5 +1,5 @@
use rocket::{
data::{Data, FromData, FromDataFuture, Transform, Transformed, TransformFuture},
data::{Data, FromData, FromDataFuture, Transform, TransformFuture, Transformed},
http::Status,
response::{self, Responder},
Outcome::*,
@ -42,7 +42,10 @@ where
type Owned = Data;
type Borrowed = Self::Owned;
fn transform<'r>(_req: &'r Request, data: Data) -> TransformFuture<'r, Self::Owned, Self::Error> {
fn transform<'r>(
_req: &'r Request,
data: Data,
) -> TransformFuture<'r, Self::Owned, Self::Error> {
Box::pin(async move { Transform::Owned(Success(data)) })
}
@ -123,8 +126,7 @@ impl<T: Outgoing> Deref 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>
{
impl<T: TryInto<http::Response<Vec<u8>>>> TryInto<http::Response<Vec<u8>>> for MatrixResult<T> {
type Error = T::Error;
fn try_into(self) -> Result<http::Response<Vec<u8>>, T::Error> {
@ -136,13 +138,18 @@ impl<T: TryInto<http::Response<Vec<u8>>>> TryInto<http::Response<Vec<u8>>> for M
}
#[rocket::async_trait]
impl<'r, T: Send + TryInto<http::Response<Vec<u8>>>> Responder<'r> for MatrixResult<T> where T::Error: Send{
impl<'r, T: Send + TryInto<http::Response<Vec<u8>>>> Responder<'r> for MatrixResult<T>
where
T::Error: Send,
{
async fn respond_to(self, _: &'r Request<'_>) -> response::Result<'r> {
let http_response: Result<http::Response<_>, _> = self.try_into();
match http_response {
Ok(http_response) => {
let mut response = rocket::response::Response::build();
response.sized_body(Cursor::new(http_response.body().clone())).await;
response
.sized_body(Cursor::new(http_response.body().clone()))
.await;
for header in http_response.headers() {
response