[fractal] API: Use UserId from ruma-identifiers for stronger validation



commit 7b4937e4bd2b2652aec7cdc9faae6bd179413a27
Author: Alejandro Domínguez <adomu net-c com>
Date:   Mon Oct 14 00:03:42 2019 +0200

    API: Use UserId from ruma-identifiers for stronger validation

 Cargo.lock                                    | 42 +++++++++++++++++++++++++++
 fractal-matrix-api/Cargo.toml                 |  1 +
 fractal-matrix-api/src/backend/register.rs    | 16 ++++++----
 fractal-matrix-api/src/lib.rs                 |  4 +++
 fractal-matrix-api/src/model/member.rs        |  2 +-
 fractal-matrix-api/src/r0/account/login.rs    |  3 +-
 fractal-matrix-api/src/r0/account/register.rs |  3 +-
 fractal-matrix-api/src/r0/search/user.rs      |  3 +-
 fractal-matrix-api/src/r0/sync/sync_events.rs |  5 ++--
 9 files changed, 67 insertions(+), 12 deletions(-)
---
diff --git a/Cargo.lock b/Cargo.lock
index 453f3281..9c7c1c32 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -594,6 +594,7 @@ dependencies = [
  "md5 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "regex 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "reqwest 0.9.19 (registry+https://github.com/rust-lang/crates.io-index)",
+ "ruma-identifiers 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "serde 1.0.98 (registry+https://github.com/rust-lang/crates.io-index)",
  "serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)",
  "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1217,6 +1218,16 @@ dependencies = [
  "unicode-normalization 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "idna"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "unicode-normalization 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "indexmap"
 version = "1.0.2"
@@ -1761,6 +1772,11 @@ name = "percent-encoding"
 version = "1.0.1"
 source = "registry+https://github.com/rust-lang/crates.io-index";
 
+[[package]]
+name = "percent-encoding"
+version = "2.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+
 [[package]]
 name = "pest"
 version = "2.1.1"
@@ -2096,6 +2112,18 @@ dependencies = [
  "winreg 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "ruma-identifiers"
+version = "0.14.0"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "regex 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.98 (registry+https://github.com/rust-lang/crates.io-index)",
+ "url 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "rustc-demangle"
 version = "0.1.15"
@@ -2649,6 +2677,16 @@ dependencies = [
  "percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "url"
+version = "2.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "idna 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "urlencoding"
 version = "1.0.0"
@@ -2855,6 +2893,7 @@ dependencies = [
 "checksum hyper 0.12.33 (registry+https://github.com/rust-lang/crates.io-index)" = 
"7cb44cbce9d8ee4fb36e4c0ad7b794ac44ebaad924b9c8291a63215bb44c2c8f"
 "checksum hyper-tls 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"3a800d6aa50af4b5850b2b0f659625ce9504df908e9733b635720483be26174f"
 "checksum idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = 
"38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e"
+"checksum idna 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9"
 "checksum indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"7e81a7c05f79578dbc15793d8b619db9ba32b4577003ef3af1a91c416798c58d"
 "checksum iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"dbe6e417e7d0975db6512b90796e8ce223145ac4e33c377e4a42882a0e88bb08"
 "checksum itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"5b8467d9c1cebe26feb08c640139247fac215782d35371ade9a2136ed6085358"
@@ -2912,6 +2951,7 @@ dependencies = [
 "checksum parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"ab41b4aed082705d1056416ae4468b6ea99d52599ecf3169b00088d43113e337"
 "checksum parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"94c8c7923936b28d546dfd14d4472eaf34c99b14e1c973a32b3e6d4eb04298c9"
 "checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831"
+"checksum percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
 "checksum pest 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"933085deae3f32071f135d799d75667b63c8dc1f4537159756e3d4ceab41868c"
 "checksum pest_derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"833d1ae558dc601e9a60366421196a8d94bc0ac980476d0b67e1d0988d72b2d0"
 "checksum pest_generator 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"63120576c4efd69615b5537d3d052257328a4ca82876771d6944424ccfd9f646"
@@ -2947,6 +2987,7 @@ dependencies = [
 "checksum regex-syntax 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = 
"cd5485bf1523a9ed51c4964273f22f63f24e31632adb5dad134f488f86a3875c"
 "checksum remove_dir_all 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"4a83fa3702a688b9359eccba92d153ac33fd2e8462f9e0e3fdf155239ea7792e"
 "checksum reqwest 0.9.19 (registry+https://github.com/rust-lang/crates.io-index)" = 
"1d0777154c2c3eb54f5c480db01de845652d941e47191277cc673634c3853939"
+"checksum ruma-identifiers 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"a5f59ef217b20d0f3549dba4e0c140ac9c5499a5d68c6e8c3e09470fdbc55ae1"
 "checksum rustc-demangle 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = 
"a7f4dccf6f4891ebcc0c39f9b6eb1a83b9bf5d747cb439ec6fba4f3b977038af"
 "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = 
"138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
 "checksum ryu 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"c92464b447c0ee8c4fb3824ecc8383b81717b9f1e74ba2e72540aef7b9f82997"
@@ -3010,6 +3051,7 @@ dependencies = [
 "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
 "checksum unicode_categories 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e"
 "checksum url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a"
+"checksum url 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"75b414f6c464c879d7f9babf951f23bc3743fb7313c081b2e6ca719067ea9d61"
 "checksum urlencoding 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"3df3561629a8bb4c57e5a2e4c43348d9e29c7c29d9b1c4c1f47166deca8f37ed"
 "checksum utf-8 0.7.5 (registry+https://github.com/rust-lang/crates.io-index)" = 
"05e42f7c18b8f902290b009cde6d651262f956c98bc51bca4cd1d511c9cd85c7"
 "checksum utf8-ranges 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = 
"9d50aa7650df78abf942826607c62468ce18d9019673d4a2ebe1865dbb96ffde"
diff --git a/fractal-matrix-api/Cargo.toml b/fractal-matrix-api/Cargo.toml
index af8702c4..a7f4156f 100644
--- a/fractal-matrix-api/Cargo.toml
+++ b/fractal-matrix-api/Cargo.toml
@@ -22,6 +22,7 @@ log = "0.4.6"
 md5 = "0.6.1"
 regex = "1.1.0"
 reqwest = "0.9.5"
+ruma-identifiers = "0.14.0"
 serde_json = "1.0.33"
 time = "0.1.41"
 url = "1.7.2"
diff --git a/fractal-matrix-api/src/backend/register.rs b/fractal-matrix-api/src/backend/register.rs
index 6e4cc0ae..b2654cc1 100644
--- a/fractal-matrix-api/src/backend/register.rs
+++ b/fractal-matrix-api/src/backend/register.rs
@@ -50,7 +50,7 @@ pub fn guest(bk: &Backend, server: &str) -> Result<(), Error> {
 
         match query {
             Ok(response) => {
-                let uid = response.user_id;
+                let uid = response.user_id.to_string();
                 let tk = response
                     .access_token
                     .as_ref()
@@ -61,7 +61,7 @@ pub fn guest(bk: &Backend, server: &str) -> Result<(), Error> {
                 data.lock().unwrap().user_id = uid.clone();
                 data.lock().unwrap().access_token = tk.clone();
                 data.lock().unwrap().since = None;
-                tx.send(BKResponse::Token(uid, tk, dev))
+                tx.send(BKResponse::Token(uid, tk, dev))  // TODO: Use UserId
                     .expect_log("Connection closed");
                 tx.send(BKResponse::Rooms(vec![], None))
                     .expect_log("Connection closed");
@@ -114,7 +114,11 @@ pub fn login(bk: &Backend, user: String, password: String, server: &str) -> Resu
 
         match query {
             Ok(response) => {
-                let uid = response.user_id.unwrap_or(user);
+                let uid = response
+                    .user_id
+                    .as_ref()
+                    .map(ToString::to_string)
+                    .unwrap_or(user);
                 let tk = response
                     .access_token
                     .as_ref()
@@ -129,7 +133,7 @@ pub fn login(bk: &Backend, user: String, password: String, server: &str) -> Resu
                     data.lock().unwrap().user_id = uid.clone();
                     data.lock().unwrap().access_token = tk.clone();
                     data.lock().unwrap().since = None;
-                    tx.send(BKResponse::Token(uid, tk, dev))
+                    tx.send(BKResponse::Token(uid, tk, dev))  // TODO: Use UserId
                         .expect_log("Connection closed");
                 }
             }
@@ -206,7 +210,7 @@ pub fn register(bk: &Backend, user: String, password: String, server: &str) -> R
 
         match query {
             Ok(response) => {
-                let uid = response.user_id;
+                let uid = response.user_id.to_string();
                 let tk = response
                     .access_token
                     .as_ref()
@@ -217,7 +221,7 @@ pub fn register(bk: &Backend, user: String, password: String, server: &str) -> R
                 data.lock().unwrap().user_id = uid.clone();
                 data.lock().unwrap().access_token = tk.clone();
                 data.lock().unwrap().since = None;
-                tx.send(BKResponse::Token(uid, tk, dev))
+                tx.send(BKResponse::Token(uid, tk, dev))  // TODO: Use UserId
                     .expect_log("Connection closed");
             }
             Err(err) => {
diff --git a/fractal-matrix-api/src/lib.rs b/fractal-matrix-api/src/lib.rs
index 8ea0976d..197f6620 100644
--- a/fractal-matrix-api/src/lib.rs
+++ b/fractal-matrix-api/src/lib.rs
@@ -12,6 +12,10 @@ pub mod r0;
 mod serde;
 pub mod types;
 
+pub mod prelude {
+    pub use ruma_identifiers::UserId;
+}
+
 #[cfg(test)]
 mod tests {
     #[test]
diff --git a/fractal-matrix-api/src/model/member.rs b/fractal-matrix-api/src/model/member.rs
index 3f9ae5cd..bc9915e7 100644
--- a/fractal-matrix-api/src/model/member.rs
+++ b/fractal-matrix-api/src/model/member.rs
@@ -36,7 +36,7 @@ impl PartialEq for Member {
 impl From<User> for Member {
     fn from(user: User) -> Self {
         Self {
-            uid: user.user_id,
+            uid: user.user_id.to_string(),
             alias: user.display_name,
             avatar: user.avatar_url.map(Url::into_string),
         }
diff --git a/fractal-matrix-api/src/r0/account/login.rs b/fractal-matrix-api/src/r0/account/login.rs
index 6d91098e..5023e891 100644
--- a/fractal-matrix-api/src/r0/account/login.rs
+++ b/fractal-matrix-api/src/r0/account/login.rs
@@ -3,6 +3,7 @@ use crate::r0::AccessToken;
 use reqwest::Client;
 use reqwest::Error;
 use reqwest::Request;
+use ruma_identifiers::UserId;
 use serde::{Deserialize, Serialize};
 use url::Url;
 
@@ -30,7 +31,7 @@ pub enum Auth {
 #[derive(Clone, Debug, Deserialize)]
 pub struct Response {
     pub access_token: Option<AccessToken>,
-    pub user_id: Option<String>,
+    pub user_id: Option<UserId>,
     pub device_id: Option<String>,
 }
 
diff --git a/fractal-matrix-api/src/r0/account/register.rs b/fractal-matrix-api/src/r0/account/register.rs
index 7cc320e9..6d52d052 100644
--- a/fractal-matrix-api/src/r0/account/register.rs
+++ b/fractal-matrix-api/src/r0/account/register.rs
@@ -3,6 +3,7 @@ use crate::r0::AccessToken;
 use reqwest::Client;
 use reqwest::Error;
 use reqwest::Request;
+use ruma_identifiers::UserId;
 use serde::{Deserialize, Serialize};
 use std::ops::Not;
 use url::Url;
@@ -53,7 +54,7 @@ pub struct Body {
 
 #[derive(Clone, Debug, Deserialize)]
 pub struct Response {
-    pub user_id: String,
+    pub user_id: UserId,
     pub access_token: Option<AccessToken>,
     pub device_id: Option<String>,
 }
diff --git a/fractal-matrix-api/src/r0/search/user.rs b/fractal-matrix-api/src/r0/search/user.rs
index 2fc55367..af5191c8 100644
--- a/fractal-matrix-api/src/r0/search/user.rs
+++ b/fractal-matrix-api/src/r0/search/user.rs
@@ -3,6 +3,7 @@ use crate::serde::option_url;
 use reqwest::Client;
 use reqwest::Error;
 use reqwest::Request;
+use ruma_identifiers::UserId;
 use serde::{Deserialize, Serialize};
 use url::Url;
 
@@ -35,7 +36,7 @@ pub struct Response {
 
 #[derive(Clone, Debug, Deserialize)]
 pub struct User {
-    pub user_id: String,
+    pub user_id: UserId,
     #[serde(default)]
     pub display_name: Option<String>,
     #[serde(with = "option_url")]
diff --git a/fractal-matrix-api/src/r0/sync/sync_events.rs b/fractal-matrix-api/src/r0/sync/sync_events.rs
index e23c3298..ebd658d2 100644
--- a/fractal-matrix-api/src/r0/sync/sync_events.rs
+++ b/fractal-matrix-api/src/r0/sync/sync_events.rs
@@ -4,6 +4,7 @@ use crate::serde::duration_as_millis;
 use reqwest::Client;
 use reqwest::Error;
 use reqwest::Request;
+use ruma_identifiers::UserId;
 use serde::{Deserialize, Serialize};
 use serde_json::Value as JsonValue;
 use std::collections::HashMap;
@@ -183,9 +184,9 @@ pub struct ToDevice {
 #[derive(Clone, Debug, Deserialize)]
 pub struct DeviceLists {
     #[serde(default)]
-    pub changed: Vec<String>,
+    pub changed: Vec<UserId>,
     #[serde(default)]
-    pub left: Vec<String>,
+    pub left: Vec<UserId>,
 }
 
 pub fn request(base: Url, params: &Parameters) -> Result<Request, Error> {


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