[fractal] Change password through matrix-sdk
- From: Alexandre Franke <afranke src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal] Change password through matrix-sdk
- Date: Tue, 1 Dec 2020 20:22:00 +0000 (UTC)
commit f9a5f51dfe5c4fc5fa88ba595adbf45a4c8210dc
Author: Alejandro DomÃnguez <adomu net-c com>
Date: Sun Sep 13 07:50:53 2020 +0200
Change password through matrix-sdk
fractal-gtk/src/appop/account.rs | 19 ++++----
fractal-gtk/src/backend/user.rs | 51 +++++++++++++---------
fractal-matrix-api/src/meson.build | 1 -
fractal-matrix-api/src/r0/account.rs | 1 -
.../src/r0/account/change_password.rs | 27 ------------
5 files changed, 39 insertions(+), 60 deletions(-)
---
diff --git a/fractal-gtk/src/appop/account.rs b/fractal-gtk/src/appop/account.rs
index 1ddf6d19..f401c582 100644
--- a/fractal-gtk/src/appop/account.rs
+++ b/fractal-gtk/src/appop/account.rs
@@ -649,7 +649,10 @@ impl AppOp {
}
pub fn set_new_password(&mut self) {
- let login_data = unwrap_or_unit_return!(self.login_data.clone());
+ let (session_client, user_id) = unwrap_or_unit_return!(self
+ .login_data
+ .as_ref()
+ .map(|ld| (ld.session_client.clone(), ld.uid.clone())));
let old_password = self
.ui
.builder
@@ -671,19 +674,13 @@ impl AppOp {
.get_object::<gtk::Stack>("account_settings_password_stack")
.expect("Can't find account_settings_password_stack in ui file.");
- let old = old_password.get_text();
- let new = new_password.get_text();
+ let old: String = old_password.get_text().into();
+ let new: String = new_password.get_text().into();
if !old.is_empty() && !new.is_empty() {
password_btn.set_sensitive(false);
password_btn_stack.set_visible_child_name("spinner");
- thread::spawn(move || {
- match user::change_password(
- login_data.session_client.homeserver().clone(),
- login_data.access_token,
- login_data.uid.localpart().into(),
- old.to_string(),
- new.to_string(),
- ) {
+ RUNTIME.spawn(async move {
+ match user::change_password(session_client, &user_id, old, &new).await {
Ok(_) => {
APPOP!(password_changed);
}
diff --git a/fractal-gtk/src/backend/user.rs b/fractal-gtk/src/backend/user.rs
index 3c614d83..88bdfe28 100644
--- a/fractal-gtk/src/backend/user.rs
+++ b/fractal-gtk/src/backend/user.rs
@@ -2,6 +2,7 @@ use fractal_api::identifiers::UserId;
use fractal_api::reqwest::Error as ReqwestError;
use fractal_api::url::{ParseError as UrlError, Url};
use fractal_api::{Client as MatrixClient, Error as MatrixError};
+use std::collections::BTreeMap;
use std::io::Error as IoError;
use super::MediaError;
@@ -15,19 +16,19 @@ use std::path::PathBuf;
use super::room::AttachedFileError;
use crate::model::member::Member;
+use fractal_api::api::r0::account::change_password::Request as ChangePasswordRequest;
use fractal_api::api::r0::contact::get_contacts::Request as GetContactsRequest;
use fractal_api::api::r0::contact::get_contacts::ThirdPartyIdentifier;
use fractal_api::api::r0::profile::get_display_name::Request as GetDisplayNameRequest;
use fractal_api::api::r0::profile::get_profile::Request as GetProfileRequest;
use fractal_api::api::r0::profile::set_avatar_url::Request as SetAvatarUrlRequest;
use fractal_api::api::r0::profile::set_display_name::Request as SetDisplayNameRequest;
+use fractal_api::api::r0::uiaa::AuthData;
use fractal_api::api::r0::user_directory::search_users::Request as UserDirectoryRequest;
+use fractal_api::assign;
use fractal_api::identity::r0::association::msisdn::submit_token::request as submit_phone_token_req;
use fractal_api::identity::r0::association::msisdn::submit_token::Body as SubmitPhoneTokenBody;
use fractal_api::identity::r0::association::msisdn::submit_token::Response as SubmitPhoneTokenResponse;
-use fractal_api::r0::account::change_password::request as change_password_req;
-use fractal_api::r0::account::change_password::Body as ChangePasswordBody;
-use fractal_api::r0::account::change_password::Parameters as ChangePasswordParameters;
use fractal_api::r0::account::deactivate::request as deactivate;
use fractal_api::r0::account::deactivate::Body as DeactivateBody;
use fractal_api::r0::account::deactivate::Parameters as DeactivateParameters;
@@ -58,6 +59,7 @@ use super::{remove_matrix_access_token_if_present, HandleError};
use crate::app::App;
use crate::util::i18n::i18n;
use crate::APPOP;
+use serde_json::json;
pub type UserInfo = (String, PathBuf);
@@ -382,10 +384,10 @@ pub fn delete_three_pid(
}
#[derive(Debug)]
-pub struct ChangePasswordError(ReqwestError);
+pub struct ChangePasswordError(MatrixError);
-impl From<ReqwestError> for ChangePasswordError {
- fn from(err: ReqwestError) -> Self {
+impl From<MatrixError> for ChangePasswordError {
+ fn from(err: MatrixError) -> Self {
Self(err)
}
}
@@ -402,25 +404,34 @@ impl HandleError for ChangePasswordError {
}
}
-pub fn change_password(
- base: Url,
- access_token: AccessToken,
- user: String,
+pub async fn change_password(
+ session_client: MatrixClient,
+ user_id: &UserId,
old_password: String,
- new_password: String,
+ new_password: &str,
) -> Result<(), ChangePasswordError> {
- let params = ChangePasswordParameters { access_token };
- let body = ChangePasswordBody {
- new_password,
- auth: Some(AuthenticationData::Password {
- identifier: Identifier::new(UserIdentifier::User { user }),
- password: old_password,
+ let auth_parameters = {
+ let mut param = BTreeMap::new();
+ let identifier = json!({
+ "type": "m.id.user",
+ "user": user_id.localpart(),
+ });
+
+ param.insert(String::from("identifier"), identifier);
+ param.insert(String::from("password"), json!(old_password));
+
+ param
+ };
+
+ let request = assign!(ChangePasswordRequest::new(new_password), {
+ auth: Some(AuthData::DirectRequest {
+ kind: "m.login.password",
session: None,
+ auth_parameters,
}),
- };
+ });
- let request = change_password_req(base, ¶ms, &body)?;
- HTTP_CLIENT.get_client().execute(request)?;
+ session_client.send(request).await?;
Ok(())
}
diff --git a/fractal-matrix-api/src/meson.build b/fractal-matrix-api/src/meson.build
index 1a0f3b20..6ad1fe7b 100644
--- a/fractal-matrix-api/src/meson.build
+++ b/fractal-matrix-api/src/meson.build
@@ -3,7 +3,6 @@ api_sources = files(
'identity/r0/association/msisdn.rs',
'identity/r0/association.rs',
'identity/r0.rs',
- 'r0/account/change_password.rs',
'r0/account/deactivate.rs',
'r0/account/login.rs',
'r0/account/logout.rs',
diff --git a/fractal-matrix-api/src/r0/account.rs b/fractal-matrix-api/src/r0/account.rs
index 9e4b667d..021cc879 100644
--- a/fractal-matrix-api/src/r0/account.rs
+++ b/fractal-matrix-api/src/r0/account.rs
@@ -1,4 +1,3 @@
-pub mod change_password;
pub mod deactivate;
pub mod login;
pub mod logout;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]