feat: whoami route

next
timokoesters 2020-07-27 17:38:00 +02:00
parent 489cbc0a93
commit dd3dab39ae
No known key found for this signature in database
GPG Key ID: 24DA7517711A2BA4
3 changed files with 83 additions and 104 deletions

View File

@ -11,14 +11,14 @@ use log::warn;
#[cfg(not(feature = "conduit_bin"))] #[cfg(not(feature = "conduit_bin"))]
use super::State; use super::State;
#[cfg(feature = "conduit_bin")] #[cfg(feature = "conduit_bin")]
use rocket::{delete, get, options, post, put, State, tokio}; use rocket::{delete, get, options, post, put, tokio, State};
use ruma::{ use ruma::{
api::client::{ api::client::{
error::ErrorKind, error::ErrorKind,
r0::{ r0::{
account::{ account::{
change_password, deactivate, get_username_availability, register, change_password, deactivate, get_username_availability, register, whoami,
ThirdPartyIdRemovalStatus, ThirdPartyIdRemovalStatus,
}, },
alias::{create_alias, delete_alias, get_alias}, alias::{create_alias, delete_alias, get_alias},
@ -304,6 +304,18 @@ pub fn login_route(
.into()) .into())
} }
#[cfg_attr(
feature = "conduit_bin",
get("/_matrix/client/r0/account/whoami", data = "<body>")
)]
pub fn whoami_route(body: Ruma<whoami::Request>) -> ConduitResult<whoami::Response> {
let sender_id = body.sender_id.as_ref().expect("user is authenticated");
Ok(whoami::Response {
user_id: sender_id.clone(),
}
.into())
}
#[cfg_attr( #[cfg_attr(
feature = "conduit_bin", feature = "conduit_bin",
post("/_matrix/client/r0/logout", data = "<body>") post("/_matrix/client/r0/logout", data = "<body>")
@ -361,9 +373,14 @@ pub fn change_password_route(
}; };
if let Some(auth) = &body.auth { if let Some(auth) = &body.auth {
let (worked, uiaainfo) = let (worked, uiaainfo) = db.uiaa.try_auth(
db.uiaa &sender_id,
.try_auth(&sender_id, device_id, auth, &uiaainfo, &db.users, &db.globals)?; device_id,
auth,
&uiaainfo,
&db.users,
&db.globals,
)?;
if !worked { if !worked {
return Err(Error::Uiaa(uiaainfo)); return Err(Error::Uiaa(uiaainfo));
} }
@ -520,8 +537,7 @@ pub fn get_pushrules_all_route(
"/_matrix/client/r0/pushrules/<_>/<_>/<_>", "/_matrix/client/r0/pushrules/<_>/<_>/<_>",
//data = "<body>" //data = "<body>"
))] ))]
pub fn set_pushrule_route( pub fn set_pushrule_route(//db: State<'_, Database>,
//db: State<'_, Database>,
//body: Ruma<set_pushrule::Request>, //body: Ruma<set_pushrule::Request>,
) -> ConduitResult<set_pushrule::Response> { ) -> ConduitResult<set_pushrule::Response> {
// TODO // TODO
@ -533,19 +549,14 @@ pub fn set_pushrule_route(
feature = "conduit_bin", feature = "conduit_bin",
put("/_matrix/client/r0/pushrules/<_>/<_>/<_>/enabled") put("/_matrix/client/r0/pushrules/<_>/<_>/<_>/enabled")
)] )]
pub fn set_pushrule_enabled_route( pub fn set_pushrule_enabled_route() -> ConduitResult<set_pushrule_enabled::Response> {
) -> ConduitResult<set_pushrule_enabled::Response> {
// TODO // TODO
warn!("TODO: set_pushrule_enabled_route"); warn!("TODO: set_pushrule_enabled_route");
Ok(set_pushrule_enabled::Response.into()) Ok(set_pushrule_enabled::Response.into())
} }
#[cfg_attr( #[cfg_attr(feature = "conduit_bin", get("/_matrix/client/r0/user/<_>/filter/<_>"))]
feature = "conduit_bin", pub fn get_filter_route() -> ConduitResult<get_filter::Response> {
get("/_matrix/client/r0/user/<_>/filter/<_>")
)]
pub fn get_filter_route(
) -> ConduitResult<get_filter::Response> {
// TODO // TODO
Ok(get_filter::Response { Ok(get_filter::Response {
filter: filter::FilterDefinition { filter: filter::FilterDefinition {
@ -559,10 +570,7 @@ pub fn get_filter_route(
.into()) .into())
} }
#[cfg_attr( #[cfg_attr(feature = "conduit_bin", post("/_matrix/client/r0/user/<_>/filter"))]
feature = "conduit_bin",
post("/_matrix/client/r0/user/<_>/filter")
)]
pub fn create_filter_route() -> ConduitResult<create_filter::Response> { pub fn create_filter_route() -> ConduitResult<create_filter::Response> {
// TODO // TODO
Ok(create_filter::Response { Ok(create_filter::Response {
@ -573,10 +581,7 @@ pub fn create_filter_route() -> ConduitResult<create_filter::Response> {
#[cfg_attr( #[cfg_attr(
feature = "conduit_bin", feature = "conduit_bin",
put( put("/_matrix/client/r0/user/<_>/account_data/<_>", data = "<body>")
"/_matrix/client/r0/user/<_>/account_data/<_>",
data = "<body>"
)
)] )]
pub fn set_global_account_data_route( pub fn set_global_account_data_route(
db: State<'_, Database>, db: State<'_, Database>,
@ -607,10 +612,7 @@ pub fn set_global_account_data_route(
#[cfg_attr( #[cfg_attr(
feature = "conduit_bin", feature = "conduit_bin",
get( get("/_matrix/client/r0/user/<_>/account_data/<_>", data = "<body>")
"/_matrix/client/r0/user/<_>/account_data/<_>",
data = "<body>"
)
)] )]
pub fn get_global_account_data_route( pub fn get_global_account_data_route(
db: State<'_, Database>, db: State<'_, Database>,
@ -737,7 +739,8 @@ pub fn set_avatar_url_route(
// TODO also make sure this is valid mxc:// format (not only starting with it) // TODO also make sure this is valid mxc:// format (not only starting with it)
} }
db.users.set_avatar_url(&sender_id, body.avatar_url.clone())?; db.users
.set_avatar_url(&sender_id, body.avatar_url.clone())?;
// Send a new membership event into all joined rooms // Send a new membership event into all joined rooms
for room_id in db.rooms.rooms_joined(&sender_id) { for room_id in db.rooms.rooms_joined(&sender_id) {
@ -1027,10 +1030,7 @@ pub fn create_backup_route(
#[cfg_attr( #[cfg_attr(
feature = "conduit_bin", feature = "conduit_bin",
put( put("/_matrix/client/unstable/room_keys/version/<_>", data = "<body>")
"/_matrix/client/unstable/room_keys/version/<_>",
data = "<body>"
)
)] )]
pub fn update_backup_route( pub fn update_backup_route(
db: State<'_, Database>, db: State<'_, Database>,
@ -1072,18 +1072,15 @@ pub fn get_latest_backup_route(
#[cfg_attr( #[cfg_attr(
feature = "conduit_bin", feature = "conduit_bin",
get( get("/_matrix/client/unstable/room_keys/version/<_>", data = "<body>")
"/_matrix/client/unstable/room_keys/version/<_>",
data = "<body>"
)
)] )]
pub fn get_backup_route( pub fn get_backup_route(
db: State<'_, Database>, db: State<'_, Database>,
body: Ruma<get_backup::Request>, body: Ruma<get_backup::Request>,
) -> ConduitResult<get_backup::Response> { ) -> ConduitResult<get_backup::Response> {
let sender_id = body.sender_id.as_ref().expect("user is authenticated"); let sender_id = body.sender_id.as_ref().expect("user is authenticated");
let algorithm = let algorithm = db
db.key_backups .key_backups
.get_backup(&sender_id, &body.version)? .get_backup(&sender_id, &body.version)?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::NotFound, ErrorKind::NotFound,
@ -1211,10 +1208,7 @@ pub fn set_read_marker_route(
#[cfg_attr( #[cfg_attr(
feature = "conduit_bin", feature = "conduit_bin",
put( put("/_matrix/client/r0/rooms/<_>/typing/<_>", data = "<body>")
"/_matrix/client/r0/rooms/<_>/typing/<_>",
data = "<body>"
)
)] )]
pub fn create_typing_event_route( pub fn create_typing_event_route(
db: State<'_, Database>, db: State<'_, Database>,
@ -1529,10 +1523,7 @@ pub fn joined_rooms_route(
#[cfg_attr( #[cfg_attr(
feature = "conduit_bin", feature = "conduit_bin",
put( put("/_matrix/client/r0/rooms/<_>/redact/<_>/<_>", data = "<body>")
"/_matrix/client/r0/rooms/<_>/redact/<_>/<_>",
data = "<body>"
)
)] )]
pub fn redact_event_route( pub fn redact_event_route(
db: State<'_, Database>, db: State<'_, Database>,
@ -1695,7 +1686,11 @@ pub fn leave_room_route(
let mut event = serde_json::from_value::<Raw<member::MemberEventContent>>( let mut event = serde_json::from_value::<Raw<member::MemberEventContent>>(
db.rooms db.rooms
.room_state_get(&body.room_id, &EventType::RoomMember, &sender_id.to_string())? .room_state_get(
&body.room_id,
&EventType::RoomMember,
&sender_id.to_string(),
)?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::BadState, ErrorKind::BadState,
"Cannot leave a room you are not a member of.", "Cannot leave a room you are not a member of.",
@ -1735,7 +1730,11 @@ pub fn kick_user_route(
let mut event = serde_json::from_value::<Raw<ruma::events::room::member::MemberEventContent>>( let mut event = serde_json::from_value::<Raw<ruma::events::room::member::MemberEventContent>>(
db.rooms db.rooms
.room_state_get(&body.room_id, &EventType::RoomMember, &body.user_id.to_string())? .room_state_get(
&body.room_id,
&EventType::RoomMember,
&body.user_id.to_string(),
)?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::BadState, ErrorKind::BadState,
"Cannot kick member that's not in the room.", "Cannot kick member that's not in the room.",
@ -1774,7 +1773,11 @@ pub fn joined_members_route(
) -> ConduitResult<joined_members::Response> { ) -> ConduitResult<joined_members::Response> {
let sender_id = body.sender_id.as_ref().expect("user is authenticated"); let sender_id = body.sender_id.as_ref().expect("user is authenticated");
if !db.rooms.is_joined(&sender_id, &body.room_id).unwrap_or(false) { if !db
.rooms
.is_joined(&sender_id, &body.room_id)
.unwrap_or(false)
{
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::Forbidden, ErrorKind::Forbidden,
"You aren't a member of the room.", "You aren't a member of the room.",
@ -1812,7 +1815,11 @@ pub fn ban_user_route(
let event = db let event = db
.rooms .rooms
.room_state_get(&body.room_id, &EventType::RoomMember, &body.user_id.to_string())? .room_state_get(
&body.room_id,
&EventType::RoomMember,
&body.user_id.to_string(),
)?
.map_or( .map_or(
Ok::<_, Error>(member::MemberEventContent { Ok::<_, Error>(member::MemberEventContent {
membership: member::MembershipState::Ban, membership: member::MembershipState::Ban,
@ -1859,7 +1866,11 @@ pub fn unban_user_route(
let mut event = serde_json::from_value::<Raw<ruma::events::room::member::MemberEventContent>>( let mut event = serde_json::from_value::<Raw<ruma::events::room::member::MemberEventContent>>(
db.rooms db.rooms
.room_state_get(&body.room_id, &EventType::RoomMember, &body.user_id.to_string())? .room_state_get(
&body.room_id,
&EventType::RoomMember,
&body.user_id.to_string(),
)?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::BadState, ErrorKind::BadState,
"Cannot unban a user who is not banned.", "Cannot unban a user who is not banned.",
@ -2223,10 +2234,7 @@ pub fn get_protocols_route() -> ConduitResult<get_protocols::Response> {
#[cfg_attr( #[cfg_attr(
feature = "conduit_bin", feature = "conduit_bin",
get( get("/_matrix/client/r0/rooms/<_>/event/<_>", data = "<body>")
"/_matrix/client/r0/rooms/<_>/event/<_>",
data = "<body>"
)
)] )]
pub fn get_room_event_route( pub fn get_room_event_route(
db: State<'_, Database>, db: State<'_, Database>,
@ -2253,10 +2261,7 @@ pub fn get_room_event_route(
#[cfg_attr( #[cfg_attr(
feature = "conduit_bin", feature = "conduit_bin",
put( put("/_matrix/client/r0/rooms/<_>/send/<_>/<_>", data = "<body>")
"/_matrix/client/r0/rooms/<_>/send/<_>/<_>",
data = "<body>"
)
)] )]
pub fn create_message_event_route( pub fn create_message_event_route(
db: State<'_, Database>, db: State<'_, Database>,
@ -2288,10 +2293,7 @@ pub fn create_message_event_route(
#[cfg_attr( #[cfg_attr(
feature = "conduit_bin", feature = "conduit_bin",
put( put("/_matrix/client/r0/rooms/<_>/state/<_>/<_>", data = "<body>")
"/_matrix/client/r0/rooms/<_>/state/<_>/<_>",
data = "<body>"
)
)] )]
pub fn create_state_event_for_key_route( pub fn create_state_event_for_key_route(
db: State<'_, Database>, db: State<'_, Database>,
@ -2350,10 +2352,7 @@ pub fn create_state_event_for_key_route(
#[cfg_attr( #[cfg_attr(
feature = "conduit_bin", feature = "conduit_bin",
put( put("/_matrix/client/r0/rooms/<_>/state/<_>", data = "<body>")
"/_matrix/client/r0/rooms/<_>/state/<_>",
data = "<body>"
)
)] )]
pub fn create_state_event_for_empty_key_route( pub fn create_state_event_for_empty_key_route(
db: State<'_, Database>, db: State<'_, Database>,
@ -2423,10 +2422,7 @@ pub fn get_state_events_route(
#[cfg_attr( #[cfg_attr(
feature = "conduit_bin", feature = "conduit_bin",
get( get("/_matrix/client/r0/rooms/<_>/state/<_>/<_>", data = "<body>")
"/_matrix/client/r0/rooms/<_>/state/<_>/<_>",
data = "<body>"
)
)] )]
pub fn get_state_events_for_key_route( pub fn get_state_events_for_key_route(
db: State<'_, Database>, db: State<'_, Database>,
@ -2458,10 +2454,7 @@ pub fn get_state_events_for_key_route(
#[cfg_attr( #[cfg_attr(
feature = "conduit_bin", feature = "conduit_bin",
get( get("/_matrix/client/r0/rooms/<_>/state/<_>", data = "<body>")
"/_matrix/client/r0/rooms/<_>/state/<_>",
data = "<body>"
)
)] )]
pub fn get_state_events_for_empty_key_route( pub fn get_state_events_for_empty_key_route(
db: State<'_, Database>, db: State<'_, Database>,
@ -2877,7 +2870,8 @@ pub async fn sync_events_route(
}; };
// TODO: Retry the endpoint instead of returning (waiting for #118) // TODO: Retry the endpoint instead of returning (waiting for #118)
if !body.full_state && response.rooms.is_empty() if !body.full_state
&& response.rooms.is_empty()
&& response.presence.is_empty() && response.presence.is_empty()
&& response.account_data.is_empty() && response.account_data.is_empty()
&& response.device_lists.is_empty() && response.device_lists.is_empty()
@ -2902,10 +2896,7 @@ pub async fn sync_events_route(
#[cfg_attr( #[cfg_attr(
feature = "conduit_bin", feature = "conduit_bin",
get( get("/_matrix/client/r0/rooms/<_>/context/<_>", data = "<body>")
"/_matrix/client/r0/rooms/<_>/context/<_>",
data = "<body>"
)
)] )]
pub fn get_context_route( pub fn get_context_route(
db: State<'_, Database>, db: State<'_, Database>,
@ -3089,10 +3080,7 @@ pub fn publicised_groups_route() -> ConduitResult<create_message_event::Response
#[cfg_attr( #[cfg_attr(
feature = "conduit_bin", feature = "conduit_bin",
put( put("/_matrix/client/r0/sendToDevice/<_>/<_>", data = "<body>")
"/_matrix/client/r0/sendToDevice/<_>/<_>",
data = "<body>"
)
)] )]
pub fn send_event_to_device_route( pub fn send_event_to_device_route(
db: State<'_, Database>, db: State<'_, Database>,
@ -3507,10 +3495,7 @@ pub fn set_pushers_route() -> ConduitResult<get_pushers::Response> {
#[cfg_attr( #[cfg_attr(
feature = "conduit_bin", feature = "conduit_bin",
put( put("/_matrix/client/r0/user/<_>/rooms/<_>/tags/<_>", data = "<body>")
"/_matrix/client/r0/user/<_>/rooms/<_>/tags/<_>",
data = "<body>"
)
)] )]
pub fn update_tag_route( pub fn update_tag_route(
db: State<'_, Database>, db: State<'_, Database>,
@ -3544,10 +3529,7 @@ pub fn update_tag_route(
#[cfg_attr( #[cfg_attr(
feature = "conduit_bin", feature = "conduit_bin",
delete( delete("/_matrix/client/r0/user/<_>/rooms/<_>/tags/<_>", data = "<body>")
"/_matrix/client/r0/user/<_>/rooms/<_>/tags/<_>",
data = "<body>"
)
)] )]
pub fn delete_tag_route( pub fn delete_tag_route(
db: State<'_, Database>, db: State<'_, Database>,
@ -3578,10 +3560,7 @@ pub fn delete_tag_route(
#[cfg_attr( #[cfg_attr(
feature = "conduit_bin", feature = "conduit_bin",
get( get("/_matrix/client/r0/user/<_>/rooms/<_>/tags", data = "<body>")
"/_matrix/client/r0/user/<_>/rooms/<_>/tags",
data = "<body>"
)
)] )]
pub fn get_tags_route( pub fn get_tags_route(
db: State<'_, Database>, db: State<'_, Database>,
@ -3606,7 +3585,6 @@ pub fn get_tags_route(
#[cfg(feature = "conduit_bin")] #[cfg(feature = "conduit_bin")]
#[options("/<_..>")] #[options("/<_..>")]
pub fn options_route( pub fn options_route() -> ConduitResult<send_event_to_device::Response> {
) -> ConduitResult<send_event_to_device::Response> {
Ok(send_event_to_device::Response.into()) Ok(send_event_to_device::Response.into())
} }

View File

@ -28,6 +28,7 @@ fn setup_rocket() -> rocket::Rocket {
client_server::register_route, client_server::register_route,
client_server::get_login_route, client_server::get_login_route,
client_server::login_route, client_server::login_route,
client_server::whoami_route,
client_server::logout_route, client_server::logout_route,
client_server::logout_all_route, client_server::logout_all_route,
client_server::change_password_route, client_server::change_password_route,

View File

@ -11,9 +11,9 @@ use {
Data, FromDataFuture, FromTransformedData, Transform, TransformFuture, Transformed, Data, FromDataFuture, FromTransformedData, Transform, TransformFuture, Transformed,
}, },
http::Status, http::Status,
outcome::Outcome::*,
response::{self, Responder}, response::{self, Responder},
tokio::io::AsyncReadExt, tokio::io::AsyncReadExt,
outcome::Outcome::*,
Request, State, Request, State,
}, },
ruma::api::Endpoint, ruma::api::Endpoint,