From 51d915a181c0e4855e7759e2b993b07a54bebc08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Tue, 23 Mar 2021 14:27:55 +0100 Subject: [PATCH] client: Add get_login_types --- matrix_sdk/src/client.rs | 33 +++++++++++++++++++++---- matrix_sdk_test/src/test_json/events.rs | 16 ++++++++++++ matrix_sdk_test/src/test_json/mod.rs | 8 +++--- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index 8503ae76..58e54492 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -74,7 +74,7 @@ use matrix_sdk_common::{ message::send_message_event, profile::{get_avatar_url, get_display_name, set_avatar_url, set_display_name}, room::create_room, - session::login, + session::{get_login_types, login}, sync::sync_events, uiaa::AuthData, }, @@ -636,6 +636,15 @@ impl Client { .and_then(|room| room::Left::new(self.clone(), room)) } + /// Gets the homeserver’s supported login types. + /// + /// This should be the first step when trying to login so you can call the + /// appropriate method for the next step. + pub async fn get_login_types(&self) -> Result { + let request = get_login_types::Request::new(); + self.send(request, None).await + } + /// Login to the server. /// /// This can be used for the first login as well as for subsequent logins, @@ -1816,7 +1825,7 @@ mod test { api::r0::{ account::register::Request as RegistrationRequest, directory::get_public_rooms_filtered::Request as PublicRoomsFilterRequest, - membership::Invite3pid, uiaa::AuthData, + membership::Invite3pid, session::get_login_types::LoginType, uiaa::AuthData, }, assign, directory::Filter, @@ -1847,13 +1856,27 @@ mod test { async fn login() { let homeserver = Url::from_str(&mockito::server_url()).unwrap(); - let _m = mock("POST", "/_matrix/client/r0/login") + let client = Client::new(homeserver).unwrap(); + + let _m_types = mock("GET", "/_matrix/client/r0/login") + .with_status(200) + .with_body(test_json::LOGIN_TYPES.to_string()) + .create(); + + let can_password = client + .get_login_types() + .await + .unwrap() + .flows + .iter() + .any(|flow| flow == &LoginType::Password); + assert!(can_password); + + let _m_login = mock("POST", "/_matrix/client/r0/login") .with_status(200) .with_body(test_json::LOGIN.to_string()) .create(); - let client = Client::new(homeserver).unwrap(); - client .login("example", "wordpass", None, None) .await diff --git a/matrix_sdk_test/src/test_json/events.rs b/matrix_sdk_test/src/test_json/events.rs index 4ad72095..022d30d8 100644 --- a/matrix_sdk_test/src/test_json/events.rs +++ b/matrix_sdk_test/src/test_json/events.rs @@ -198,6 +198,22 @@ lazy_static! { }); } +lazy_static! { + pub static ref LOGIN_TYPES: JsonValue = json!({ + "flows": [ + { + "type": "m.login.password" + }, + { + "type": "m.login.sso" + }, + { + "type": "m.login.token" + } + ] + }); +} + lazy_static! { pub static ref LOGOUT: JsonValue = json!({}); } diff --git a/matrix_sdk_test/src/test_json/mod.rs b/matrix_sdk_test/src/test_json/mod.rs index 9aa96482..f229949f 100644 --- a/matrix_sdk_test/src/test_json/mod.rs +++ b/matrix_sdk_test/src/test_json/mod.rs @@ -12,10 +12,10 @@ pub mod members; pub mod sync; pub use events::{ - ALIAS, ALIASES, EVENT_ID, KEYS_QUERY, KEYS_UPLOAD, LOGIN, LOGIN_RESPONSE_ERR, LOGOUT, MEMBER, - MEMBER_NAME_CHANGE, MESSAGE_EDIT, MESSAGE_TEXT, NAME, POWER_LEVELS, PRESENCE, PUBLIC_ROOMS, - REACTION, REDACTED, REDACTED_INVALID, REDACTED_STATE, REDACTION, REGISTRATION_RESPONSE_ERR, - ROOM_ID, ROOM_MESSAGES, TYPING, + ALIAS, ALIASES, EVENT_ID, KEYS_QUERY, KEYS_UPLOAD, LOGIN, LOGIN_RESPONSE_ERR, LOGIN_TYPES, + LOGOUT, MEMBER, MEMBER_NAME_CHANGE, MESSAGE_EDIT, MESSAGE_TEXT, NAME, POWER_LEVELS, PRESENCE, + PUBLIC_ROOMS, REACTION, REDACTED, REDACTED_INVALID, REDACTED_STATE, REDACTION, + REGISTRATION_RESPONSE_ERR, ROOM_ID, ROOM_MESSAGES, TYPING, }; pub use sync::{ DEFAULT_SYNC_SUMMARY, INVITE_SYNC, LEAVE_SYNC, LEAVE_SYNC_EVENT, MORE_SYNC, SYNC, VOIP_SYNC,