[fractal] API: rework authentication in register model



commit 1ce06f4245d82cf62f335014dac51225e5d03977
Author: Alejandro Domínguez <adomu net-c com>
Date:   Mon Jan 28 18:36:32 2019 +0100

    API: rework authentication in register model

 fractal-matrix-api/src/backend/register.rs |   8 +--
 fractal-matrix-api/src/model/mod.rs        | 110 +++++++++++++++++++++++++++++
 fractal-matrix-api/src/model/register.rs   | 102 +++-----------------------
 3 files changed, 122 insertions(+), 98 deletions(-)
---
diff --git a/fractal-matrix-api/src/backend/register.rs b/fractal-matrix-api/src/backend/register.rs
index 1bb3b79a..26cfdf40 100644
--- a/fractal-matrix-api/src/backend/register.rs
+++ b/fractal-matrix-api/src/backend/register.rs
@@ -8,8 +8,6 @@ use crate::error::Error;
 use crate::globals;
 use crate::util::json_q;
 
-use crate::types::AuthenticationData;
-use crate::types::AuthenticationKind;
 use crate::types::LoginRequest;
 use crate::types::LoginResponse;
 use crate::types::RegisterRequest;
@@ -57,7 +55,7 @@ pub fn login(bk: &Backend, user: String, password: String, server: &str) -> Resu
     bk.data.lock().unwrap().server_url = Url::parse(server)?;
     let url = bk.url("login", vec![])?;
 
-    let attrs = LoginRequest::new(user.clone(), password);
+    let attrs = LoginRequest::new(user.clone(), password, Some(String::from("Fractal")), None);
     let attrs_json = serde_json::to_value(attrs).expect("Failed to serialize login request");
     let data = bk.data.clone();
 
@@ -124,10 +122,6 @@ pub fn register(bk: &Backend, user: String, password: String, server: &str) -> R
     let url = bk.url("register", vec![("kind", String::from("user"))])?;
 
     let attrs = RegisterRequest {
-        auth: Some(AuthenticationData {
-            kind: AuthenticationKind::Password,
-            session: None,
-        }),
         username: Some(user),
         password: Some(password),
         ..Default::default()
diff --git a/fractal-matrix-api/src/model/mod.rs b/fractal-matrix-api/src/model/mod.rs
index a1ce43b9..1e05f375 100644
--- a/fractal-matrix-api/src/model/mod.rs
+++ b/fractal-matrix-api/src/model/mod.rs
@@ -8,3 +8,113 @@ pub mod room;
 pub mod stickers;
 pub mod sync;
 pub mod userinfo;
+
+use serde::{Deserialize, Serialize};
+
+#[derive(Clone, Debug, Deserialize, Serialize)]
+pub enum Medium {
+    #[serde(rename = "email")]
+    Email,
+    #[serde(rename = "msisdn")]
+    MsIsdn,
+}
+
+#[derive(Clone, Debug, Serialize)]
+#[serde(tag = "type")]
+pub enum UserIdentifier {
+    #[serde(rename = "m.id.user")]
+    User { user: String },
+    #[serde(rename = "m.id.thirdparty")]
+    ThirdParty { medium: Medium, address: String },
+    #[serde(rename = "m.id.phone")]
+    Phone { country: String, phone: String },
+}
+
+#[derive(Clone, Debug, Serialize)]
+enum LegacyMedium {
+    #[serde(rename = "email")]
+    Email,
+}
+
+#[derive(Clone, Debug, Serialize)]
+#[serde(untagged)]
+enum LegacyIdentifier {
+    User {
+        user: String,
+    },
+    Email {
+        medium: LegacyMedium,
+        address: String,
+    },
+}
+
+#[derive(Clone, Debug, Serialize)]
+pub struct Identifier {
+    identifier: UserIdentifier,
+    #[serde(flatten)]
+    legacy_identifier: Option<LegacyIdentifier>,
+}
+
+impl Identifier {
+    pub fn new(identifier: UserIdentifier) -> Self {
+        Self {
+            identifier: identifier.clone(),
+            legacy_identifier: match identifier {
+                UserIdentifier::User { user } => Some(LegacyIdentifier::User { user }),
+                UserIdentifier::ThirdParty { medium: _, address } => {
+                    Some(LegacyIdentifier::Email {
+                        medium: LegacyMedium::Email,
+                        address,
+                    })
+                }
+                UserIdentifier::Phone { .. } => None,
+            },
+        }
+    }
+}
+
+#[derive(Clone, Debug, Serialize)]
+pub struct ThreePIDCredentials {
+    pub client_secret: String,
+    pub id_server: String,
+    pub sid: String,
+}
+
+#[derive(Clone, Debug, Serialize)]
+#[serde(tag = "type")]
+pub enum AuthenticationData {
+    #[serde(rename = "m.login.password")]
+    Password {
+        #[serde(flatten)]
+        identifier: Identifier,
+        password: String,
+        #[serde(skip_serializing_if = "Option::is_none")]
+        session: Option<String>,
+    },
+    #[serde(rename = "m.login.recaptcha")]
+    Recaptcha {
+        response: String,
+        #[serde(skip_serializing_if = "Option::is_none")]
+        session: Option<String>,
+    },
+    #[serde(rename = "m.login.token")]
+    Token {
+        token: String,
+        txn_id: String,
+        #[serde(skip_serializing_if = "Option::is_none")]
+        session: Option<String>,
+    },
+    #[serde(rename = "m.login.oauth2")]
+    OAuth2 { uri: String },
+    #[serde(rename = "m.login.email.identity")]
+    Email {
+        threepid_creds: ThreePIDCredentials,
+        #[serde(skip_serializing_if = "Option::is_none")]
+        session: Option<String>,
+    },
+    #[serde(rename = "m.login.dummy")]
+    Dummy {
+        #[serde(skip_serializing_if = "Option::is_none")]
+        session: Option<String>,
+    },
+}
diff --git a/fractal-matrix-api/src/model/register.rs b/fractal-matrix-api/src/model/register.rs
index 4e6230be..82a33cde 100644
--- a/fractal-matrix-api/src/model/register.rs
+++ b/fractal-matrix-api/src/model/register.rs
@@ -1,3 +1,4 @@
+use super::{AuthenticationData, Identifier, Medium, UserIdentifier};
 use crate::globals;
 use serde::{Deserialize, Serialize};
 use std::ops::Not;
@@ -21,14 +22,6 @@ pub struct LoginResponse {
     pub device_id: Option<String>,
 }
 
-#[derive(Clone, Debug, Serialize)]
-pub enum Medium {
-    #[serde(rename = "email")]
-    Email,
-    #[serde(rename = "msisdn")]
-    MsIsdn,
-}
-
 #[derive(Clone, Debug, Serialize)]
 #[serde(tag = "type")]
 pub enum Auth {
@@ -38,78 +31,29 @@ pub enum Auth {
     Token { token: String },
 }
 
-#[derive(Clone, Debug, Serialize)]
-#[serde(tag = "type")]
-pub enum UserIdentifier {
-    #[serde(rename = "m.id.user")]
-    User { user: String },
-    #[serde(rename = "m.id.thirdparty")]
-    ThirdParty { medium: Medium, address: String },
-    #[serde(rename = "m.id.phone")]
-    Phone { country: String, phone: String },
-}
-
-#[derive(Clone, Debug, Serialize)]
-enum LegacyMedium {
-    #[serde(rename = "email")]
-    Email,
-}
-
-#[derive(Clone, Debug, Serialize)]
-#[serde(untagged)]
-enum LegacyIdentifier {
-    User {
-        user: String,
-    },
-    Email {
-        medium: LegacyMedium,
-        address: String,
-    },
-}
-
-#[derive(Clone, Debug, Serialize)]
-pub struct Identifier {
-    identifier: UserIdentifier,
-    #[serde(flatten)]
-    legacy_identifier: Option<LegacyIdentifier>,
-}
-
-impl Identifier {
-    pub fn new(identifier: UserIdentifier) -> Self {
-        Self {
-            identifier: identifier.clone(),
-            legacy_identifier: match identifier {
-                UserIdentifier::User { user } => Some(LegacyIdentifier::User { user }),
-                UserIdentifier::ThirdParty { medium: _, address } => {
-                    Some(LegacyIdentifier::Email {
-                        medium: LegacyMedium::Email,
-                        address,
-                    })
-                }
-                UserIdentifier::Phone { .. } => None,
-            },
-        }
-    }
-}
-
 impl LoginRequest {
-    pub fn new(user: String, password: String) -> Self {
+    pub fn new(
+        user: String,
+        password: String,
+        initial_device_display_name: Option<String>,
+        device_id: Option<String>,
+    ) -> Self {
         if globals::EMAIL_RE.is_match(&user) {
             Self {
                 auth: Auth::Password { password },
-                initial_device_display_name: Some(String::from("Fractal")),
+                initial_device_display_name,
                 identifier: Identifier::new(UserIdentifier::ThirdParty {
                     medium: Medium::Email,
                     address: user,
                 }),
-                device_id: None,
+                device_id,
             }
         } else {
             Self {
                 auth: Auth::Password { password },
-                initial_device_display_name: Some(String::from("Fractal")),
+                initial_device_display_name,
                 identifier: Identifier::new(UserIdentifier::User { user }),
-                device_id: None,
+                device_id,
             }
         }
     }
@@ -139,27 +83,3 @@ pub struct RegisterResponse {
     pub access_token: Option<String>,
     pub device_id: Option<String>,
 }
-
-#[derive(Clone, Debug, Serialize)]
-pub struct AuthenticationData {
-    #[serde(rename = "type")]
-    pub kind: AuthenticationKind,
-    #[serde(skip_serializing_if = "Option::is_none")]
-    pub session: Option<String>,
-}
-
-#[derive(Clone, Debug, Serialize)]
-pub enum AuthenticationKind {
-    #[serde(rename = "m.login.password")]
-    Password,
-    #[serde(rename = "m.login.recaptcha")]
-    Recaptcha,
-    #[serde(rename = "m.login.oauth2")]
-    OAuth2,
-    #[serde(rename = "m.login.email.identity")]
-    Email,
-    #[serde(rename = "m.login.token")]
-    Token,
-    #[serde(rename = "m.login.dummy")]
-    Dummy,
-}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]