From 8dd218235d257df8d2ba1ba90a52dd12f2e5af8a Mon Sep 17 00:00:00 2001 From: fufesou Date: Wed, 21 Jun 2023 23:58:13 +0800 Subject: [PATCH 1/3] store user info after login Signed-off-by: fufesou --- flutter/lib/common/hbbs/hbbs.dart | 12 ++++++++++ flutter/lib/common/widgets/login.dart | 10 ++++---- flutter/lib/models/user_model.dart | 1 + src/hbbs_http/account.rs | 34 +++++++++++++++++++++++++-- src/ui_interface.rs | 6 ++++- 5 files changed, 54 insertions(+), 9 deletions(-) diff --git a/flutter/lib/common/hbbs/hbbs.dart b/flutter/lib/common/hbbs/hbbs.dart index d6baabdc0..2d559ec74 100644 --- a/flutter/lib/common/hbbs/hbbs.dart +++ b/flutter/lib/common/hbbs/hbbs.dart @@ -14,6 +14,8 @@ class HttpType { static const kAuthResTypeEmailCheck = "email_check"; } +// to-do: The UserPayload does not contain all the fields of the user. +// Is all the fields of the user needed? class UserPayload { String id = ''; String name = ''; @@ -29,6 +31,16 @@ class UserPayload { note = json['note'] ?? '', status = json['status'], isAdmin = json['is_admin'] == true; + + Map toJson() { + final Map map = { + 'name': name, + }; + if (status != null) { + map['status'] = status!; + } + return map; + } } class PeerPayload { diff --git a/flutter/lib/common/widgets/login.dart b/flutter/lib/common/widgets/login.dart index 99422cb9d..10960dc33 100644 --- a/flutter/lib/common/widgets/login.dart +++ b/flutter/lib/common/widgets/login.dart @@ -424,6 +424,8 @@ Future loginDialog() async { if (resp.access_token != null) { await bind.mainSetLocalOption( key: 'access_token', value: resp.access_token!); + await bind.mainSetLocalOption( + key: 'user_info', value: jsonEncode(resp.user ?? {})); close(true); return; } @@ -482,12 +484,8 @@ Future loginDialog() async { curOP: curOP, cbLogin: (Map authBody) { try { - final loginResp = - gFFI.userModel.getLoginResponseFromAuthBody(authBody); - if (loginResp.access_token != null) { - bind.mainSetLocalOption( - key: 'access_token', value: loginResp.access_token!); - } + // access_token is already stored in the rust side. + gFFI.userModel.getLoginResponseFromAuthBody(authBody); } catch (e) { debugPrint('Failed too parse oidc login body: "$authBody"'); } diff --git a/flutter/lib/models/user_model.dart b/flutter/lib/models/user_model.dart index 9f3f1571a..a6d2117c0 100644 --- a/flutter/lib/models/user_model.dart +++ b/flutter/lib/models/user_model.dart @@ -75,6 +75,7 @@ class UserModel { Future reset() async { await bind.mainSetLocalOption(key: 'access_token', value: ''); + await bind.mainSetLocalOption(key: 'user_info', value: ''); await gFFI.abModel.reset(); await gFFI.groupModel.reset(); userName.value = ''; diff --git a/src/hbbs_http/account.rs b/src/hbbs_http/account.rs index 95868e194..08b15ba90 100644 --- a/src/hbbs_http/account.rs +++ b/src/hbbs_http/account.rs @@ -4,6 +4,7 @@ use hbb_common::{ log, ResultType, }; use reqwest::blocking::Client; +use serde::ser::SerializeStruct; use serde_derive::{Deserialize, Serialize}; use serde_repr::{Deserialize_repr, Serialize_repr}; use std::{ @@ -90,7 +91,7 @@ pub enum UserRole { Member = 0, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Deserialize)] pub struct UserPayload { pub name: String, pub email: Option, @@ -99,6 +100,9 @@ pub struct UserPayload { pub info: UserInfo, pub role: UserRole, pub is_admin: bool, + // helper field for serialize + #[serde(default)] + pub ser_store_local: bool, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -127,6 +131,30 @@ pub struct AuthResult { pub auth_body: Option, } +impl serde::Serialize for UserPayload { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + if self.ser_store_local { + let mut state = serializer.serialize_struct("UserPayload", 1)?; + state.serialize_field("name", &self.name)?; + state.serialize_field("status", &self.status)?; + state.end() + } else { + let mut state = serializer.serialize_struct("UserPayload", 7)?; + state.serialize_field("name", &self.name)?; + state.serialize_field("email", &self.email)?; + state.serialize_field("note", &self.note)?; + state.serialize_field("status", &self.status)?; + state.serialize_field("info", &self.info)?; + state.serialize_field("role", &self.role)?; + state.serialize_field("is_admin", &self.is_admin)?; + state.end() + } + } +} + impl OidcSession { fn new() -> Self { Self { @@ -226,16 +254,18 @@ impl OidcSession { let query_timeout = OIDC_SESSION.read().unwrap().query_timeout; while OIDC_SESSION.read().unwrap().keep_querying && begin.elapsed() < query_timeout { match Self::query(&code_url.code, &id, &uuid) { - Ok(HbbHttpResponse::<_>::Data(auth_body)) => { + Ok(HbbHttpResponse::<_>::Data(mut auth_body)) => { if remember_me { LocalConfig::set_option( "access_token".to_owned(), auth_body.access_token.clone(), ); + auth_body.user.ser_store_local = true; LocalConfig::set_option( "user_info".to_owned(), serde_json::to_string(&auth_body.user).unwrap_or_default(), ); + auth_body.user.ser_store_local = false; } OIDC_SESSION .write() diff --git a/src/ui_interface.rs b/src/ui_interface.rs index e298e1167..834a2a581 100644 --- a/src/ui_interface.rs +++ b/src/ui_interface.rs @@ -822,7 +822,11 @@ pub fn account_auth_cancel() { #[cfg(feature = "flutter")] pub fn account_auth_result() -> String { - serde_json::to_string(&account::OidcSession::get_result()).unwrap_or_default() + let mut auth_result = account::OidcSession::get_result(); + if let Some(auth) = auth_result.auth_body.as_mut() { + auth.user.ser_store_local = false; + } + serde_json::to_string(&auth_result).unwrap_or_default() } #[cfg(feature = "flutter")] From 54cc45dd66510db086ada9ff40cd58df8e422764 Mon Sep 17 00:00:00 2001 From: fufesou Date: Thu, 22 Jun 2023 00:43:17 +0800 Subject: [PATCH 2/3] change user payload Signed-off-by: fufesou --- flutter/lib/common/hbbs/hbbs.dart | 23 +++++++++++++++-------- src/hbbs_http/account.rs | 14 ++------------ src/ui/index.tis | 11 ++++++++--- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/flutter/lib/common/hbbs/hbbs.dart b/flutter/lib/common/hbbs/hbbs.dart index 2d559ec74..50960e59c 100644 --- a/flutter/lib/common/hbbs/hbbs.dart +++ b/flutter/lib/common/hbbs/hbbs.dart @@ -14,31 +14,37 @@ class HttpType { static const kAuthResTypeEmailCheck = "email_check"; } +enum UserStatus { kDisabled, kNormal, kUnverified } + // to-do: The UserPayload does not contain all the fields of the user. // Is all the fields of the user needed? class UserPayload { - String id = ''; String name = ''; String email = ''; String note = ''; - int? status; + UserStatus status; bool isAdmin = false; UserPayload.fromJson(Map json) - : id = json['id'] ?? '', - name = json['name'] ?? '', + : name = json['name'] ?? '', email = json['email'] ?? '', note = json['note'] ?? '', - status = json['status'], + status = json['status'] == 0 + ? UserStatus.kDisabled + : json['status'] == -1 + ? UserStatus.kUnverified + : UserStatus.kNormal, isAdmin = json['is_admin'] == true; Map toJson() { final Map map = { 'name': name, + 'status': status == UserStatus.kDisabled + ? 0 + : status == UserStatus.kUnverified + ? -1 + : 1, }; - if (status != null) { - map['status'] = status!; - } return map; } } @@ -117,6 +123,7 @@ class LoginResponse { LoginResponse.fromJson(Map json) { access_token = json['access_token']; type = json['type']; + print('REMOVE ME ================== $json'); user = json['user'] != null ? UserPayload.fromJson(json['user']) : null; } } diff --git a/src/hbbs_http/account.rs b/src/hbbs_http/account.rs index 08b15ba90..116c0ad02 100644 --- a/src/hbbs_http/account.rs +++ b/src/hbbs_http/account.rs @@ -60,8 +60,6 @@ pub struct UserInfo { #[serde(default)] pub settings: UserSettings, #[serde(default)] - pub login_ip_whitelist: Vec, - #[serde(default)] pub login_device_whitelist: Vec, #[serde(default)] pub other: HashMap, @@ -83,14 +81,6 @@ pub enum UserStatus { Unverified = -1, } -#[derive(Debug, Clone, Copy, PartialEq, Serialize_repr, Deserialize_repr)] -#[repr(i64)] -pub enum UserRole { - Owner = 10, - Admin = 1, - Member = 0, -} - #[derive(Debug, Clone, Deserialize)] pub struct UserPayload { pub name: String, @@ -98,8 +88,8 @@ pub struct UserPayload { pub note: Option, pub status: UserStatus, pub info: UserInfo, - pub role: UserRole, pub is_admin: bool, + pub third_auth_type: Option, // helper field for serialize #[serde(default)] pub ser_store_local: bool, @@ -148,8 +138,8 @@ impl serde::Serialize for UserPayload { state.serialize_field("note", &self.note)?; state.serialize_field("status", &self.status)?; state.serialize_field("info", &self.info)?; - state.serialize_field("role", &self.role)?; state.serialize_field("is_admin", &self.is_admin)?; + state.serialize_field("third_auth_type", &self.third_auth_type)?; state.end() } } diff --git a/src/ui/index.tis b/src/ui/index.tis index adfa5a035..2085f998d 100644 --- a/src/ui/index.tis +++ b/src/ui/index.tis @@ -1174,6 +1174,11 @@ function check_if_overlay() { checkConnectStatus(); +function set_local_user_info(user) { + var user_info = {name: user.name, status: user.status}; + handler.set_local_option("user_info", JSON.stringify(user_info)); +} + function login() { var name0 = getUserName(); var pass0 = ''; @@ -1209,7 +1214,7 @@ function login() { return; } handler.set_local_option("access_token", data.access_token); - handler.set_local_option("user_info", JSON.stringify(data.user)); + set_local_user_info(data.user); show_progress(-1); myIdMenu.update(); getAb(); @@ -1248,7 +1253,7 @@ function on_email_check(last_msg) { return; } handler.set_local_option("access_token", data.access_token); - handler.set_local_option("user_info", JSON.stringify(data.user)); + set_local_user_info(data.user); show_progress(-1); myIdMenu.update(); getAb(); @@ -1298,7 +1303,7 @@ function refreshCurrentUser() { handleAbError(data.error); return; } - handler.set_local_option("user_info", JSON.stringify(data)); + set_local_user_info(data); myIdMenu.update(); getAb(); }, function(err, status) { From 4f50e3d93444e73d6875cc8be91ea22734ad82a8 Mon Sep 17 00:00:00 2001 From: fufesou Date: Thu, 22 Jun 2023 00:54:00 +0800 Subject: [PATCH 3/3] debug, store user info Signed-off-by: fufesou --- flutter/lib/common/hbbs/hbbs.dart | 1 - src/ui/index.tis | 7 +++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/flutter/lib/common/hbbs/hbbs.dart b/flutter/lib/common/hbbs/hbbs.dart index 50960e59c..d1e44abc8 100644 --- a/flutter/lib/common/hbbs/hbbs.dart +++ b/flutter/lib/common/hbbs/hbbs.dart @@ -123,7 +123,6 @@ class LoginResponse { LoginResponse.fromJson(Map json) { access_token = json['access_token']; type = json['type']; - print('REMOVE ME ================== $json'); user = json['user'] != null ? UserPayload.fromJson(json['user']) : null; } } diff --git a/src/ui/index.tis b/src/ui/index.tis index 2085f998d..1227ccddb 100644 --- a/src/ui/index.tis +++ b/src/ui/index.tis @@ -1175,8 +1175,11 @@ function check_if_overlay() { checkConnectStatus(); function set_local_user_info(user) { - var user_info = {name: user.name, status: user.status}; - handler.set_local_option("user_info", JSON.stringify(user_info)); + var user_info = {name: user.name}; + if (user.status) { + user_info.status = user.status; + } + handler.set_local_option("user_info", JSON.stringify(user_info)); } function login() {