1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2024-11-04 16:38:52 +01:00

Improve error messages in Ruma wrapper FromRequest impl

This commit is contained in:
Jonas Platte 2022-02-09 14:03:38 +01:00
parent 5d8c80b170
commit 9db0473ed5
No known key found for this signature in database
GPG key ID: 7D261D771D915378

View file

@ -3,7 +3,7 @@ use std::{collections::BTreeMap, iter::FromIterator, str};
use axum::{ use axum::{
async_trait, async_trait,
body::{Full, HttpBody}, body::{Full, HttpBody},
extract::{FromRequest, RequestParts, TypedHeader}, extract::{rejection::TypedHeaderRejectionReason, FromRequest, RequestParts, TypedHeader},
headers::{ headers::{
authorization::{Bearer, Credentials}, authorization::{Bearer, Credentials},
Authorization, Authorization,
@ -97,7 +97,10 @@ where
); );
if !db.users.exists(&user_id).unwrap() { if !db.users.exists(&user_id).unwrap() {
return Err(forbidden()); return Err(Error::BadRequest(
ErrorKind::Forbidden,
"User does not exist.",
));
} }
// TODO: Check if appservice is allowed to be that user // TODO: Check if appservice is allowed to be that user
@ -111,11 +114,21 @@ where
AuthScheme::AccessToken | AuthScheme::QueryOnlyAccessToken => { AuthScheme::AccessToken | AuthScheme::QueryOnlyAccessToken => {
let token = match token { let token = match token {
Some(token) => token, Some(token) => token,
_ => return Err(missing_token()), _ => {
return Err(Error::BadRequest(
ErrorKind::MissingToken,
"Missing access token.",
))
}
}; };
match db.users.find_from_token(token).unwrap() { match db.users.find_from_token(token).unwrap() {
None => return Err(unknown_token()), None => {
return Err(Error::BadRequest(
ErrorKind::UnknownToken { soft_logout: false },
"Unknown access token.",
))
}
Some((user_id, device_id)) => ( Some((user_id, device_id)) => (
Some(user_id), Some(user_id),
Some(Box::<DeviceId>::from(device_id)), Some(Box::<DeviceId>::from(device_id)),
@ -130,7 +143,17 @@ where
.await .await
.map_err(|e| { .map_err(|e| {
warn!("Missing or invalid Authorization header: {}", e); warn!("Missing or invalid Authorization header: {}", e);
forbidden()
let msg = match e.reason() {
TypedHeaderRejectionReason::Missing => {
"Missing Authorization header."
}
TypedHeaderRejectionReason::Error(_) => {
"Invalid X-Matrix signatures."
}
};
Error::BadRequest(ErrorKind::Forbidden, msg)
})?; })?;
let origin_signatures = BTreeMap::from_iter([( let origin_signatures = BTreeMap::from_iter([(
@ -183,7 +206,10 @@ where
Ok(b) => b, Ok(b) => b,
Err(e) => { Err(e) => {
warn!("Failed to fetch signing keys: {}", e); warn!("Failed to fetch signing keys: {}", e);
return Err(forbidden()); return Err(Error::BadRequest(
ErrorKind::Forbidden,
"Failed to fetch signing keys.",
));
} }
}; };
@ -206,7 +232,10 @@ where
); );
} }
return Err(forbidden()); return Err(Error::BadRequest(
ErrorKind::Forbidden,
"Failed to verify X-Matrix signatures.",
));
} }
} }
} }
@ -255,7 +284,7 @@ where
let body = let body =
<T::Incoming as IncomingRequest>::try_from_http_request(http_request).map_err(|e| { <T::Incoming as IncomingRequest>::try_from_http_request(http_request).map_err(|e| {
warn!("{:?}", e); warn!("{:?}", e);
bad_json() Error::BadRequest(ErrorKind::BadJson, "Failed to deserialize request.")
})?; })?;
Ok(Ruma { Ok(Ruma {
@ -269,25 +298,6 @@ where
} }
} }
fn forbidden() -> Error {
Error::BadRequest(ErrorKind::Forbidden, "Forbidden.")
}
fn unknown_token() -> Error {
Error::BadRequest(
ErrorKind::UnknownToken { soft_logout: false },
"Unknown token.",
)
}
fn missing_token() -> Error {
Error::BadRequest(ErrorKind::MissingToken, "Missing token.")
}
fn bad_json() -> Error {
Error::BadRequest(ErrorKind::BadJson, "Bad json.")
}
struct XMatrix { struct XMatrix {
origin: Box<ServerName>, origin: Box<ServerName>,
key: String, // KeyName? key: String, // KeyName?