From e5ed4fadda92dcab410d4d11d0b0f0017da3f012 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 13 Feb 2021 16:03:07 +0100 Subject: [PATCH] Upgrade login system --- lib/helpers/account_helper.dart | 20 ++++++++------------ lib/helpers/api_helper.dart | 10 ++++------ lib/helpers/preferences_helper.dart | 26 ++++++++++++++------------ lib/main_dev.dart | 3 +-- lib/main_online.dart | 3 +-- lib/models/config.dart | 10 ++++------ lib/models/login_tokens.dart | 23 ----------------------- 7 files changed, 32 insertions(+), 63 deletions(-) delete mode 100644 lib/models/login_tokens.dart diff --git a/lib/helpers/account_helper.dart b/lib/helpers/account_helper.dart index e3b0c44..47fcfb8 100644 --- a/lib/helpers/account_helper.dart +++ b/lib/helpers/account_helper.dart @@ -3,7 +3,6 @@ import 'package:comunic/helpers/preferences_helper.dart'; import 'package:comunic/helpers/websocket_helper.dart'; import 'package:comunic/models/api_request.dart'; import 'package:comunic/models/authentication_details.dart'; -import 'package:comunic/models/login_tokens.dart'; import 'package:comunic/models/new_account.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -36,7 +35,7 @@ class AccountHelper { /// Warning : This method MUST BE CALLED AT LEAST ONCE AFTER APP START !!! Future signedIn() async { bool signedIn = - (await PreferencesHelper.getInstance()).getLoginTokens() != null; + (await PreferencesHelper.getInstance()).getLoginToken() != null; // Load current user ID for later use if (signedIn && _currentUserID == -1) await _loadCurrentUserID(); @@ -47,8 +46,8 @@ class AccountHelper { /// Sign in user Future signIn(AuthenticationDetails auth) async { final request = APIRequest(uri: "account/login"); - request.addString("userMail", auth.email); - request.addString("userPassword", auth.password); + request.addString("mail", auth.email); + request.addString("password", auth.password); final response = await APIHelper().exec(request); @@ -59,10 +58,9 @@ class AccountHelper { return AuthResult.TOO_MANY_ATTEMPTS; else if (response.code != 200) return AuthResult.NETWORK_ERROR; - // Save login tokens - final tokensObj = response.getObject()["tokens"]; + // Save login token await (await PreferencesHelper.getInstance()) - .setLoginTokens(LoginTokens(tokensObj["token1"], tokensObj["token2"])); + .setLoginToken(response.getObject()["token"]); // Get current user ID final userID = await _downloadCurrentUserID(); @@ -81,10 +79,11 @@ class AccountHelper { /// Sign out user Future signOut() async { - await (await PreferencesHelper.getInstance()).setLoginTokens(null); + await (await PreferencesHelper.getInstance()).setLoginToken(null); _currentUserID = 0; // Close current web socket + await APIRequest.withLogin("account/logout").exec(); WebSocketHelper.close(); } @@ -178,10 +177,7 @@ class AccountHelper { /// Get current user ID from the server Future _downloadCurrentUserID() async { - final response = await APIRequest( - uri: "user/getCurrentUserID", - needLogin: true, - ).exec(); + final response = await APIRequest.withLogin("account/id").exec(); if (response.code != 200) return null; diff --git a/lib/helpers/api_helper.dart b/lib/helpers/api_helper.dart index aeafeec..0c961f6 100644 --- a/lib/helpers/api_helper.dart +++ b/lib/helpers/api_helper.dart @@ -19,15 +19,13 @@ class APIHelper { Future exec(APIRequest request, {bool multipart = false}) async { try { //Add API tokens - request.addString("serviceName", config().serviceName); - request.addString("serviceToken", config().serviceToken); + request.addString("client", config().clientName); //Add user tokens (if required) if (request.needLogin) { - final tokens = (await PreferencesHelper.getInstance()).getLoginTokens(); - assert(tokens != null); - request.addString("userToken1", tokens.tokenOne); - request.addString("userToken2", tokens.tokenTwo); + final token = (await PreferencesHelper.getInstance()).getLoginToken(); + assert(token != null); + request.addString("token", token); } // Determine server URL diff --git a/lib/helpers/preferences_helper.dart b/lib/helpers/preferences_helper.dart index 69f596f..1ab638c 100644 --- a/lib/helpers/preferences_helper.dart +++ b/lib/helpers/preferences_helper.dart @@ -1,7 +1,4 @@ -import 'dart:convert'; - import 'package:comunic/models/application_preferences.dart'; -import 'package:comunic/models/login_tokens.dart'; import 'package:shared_preferences/shared_preferences.dart'; /// Preferences helper @@ -11,14 +8,14 @@ import 'package:shared_preferences/shared_preferences.dart'; /// @author Pierre HUBERT enum PreferencesKeyList { - LOGIN_TOKENS, + LOGIN_TOKEN, ENABLE_DARK_THEME, FORCE_MOBILE_MODE, SHOW_PERFORMANCE_OVERLAY, } const _PreferenceKeysName = { - PreferencesKeyList.LOGIN_TOKENS: "login_tokens", + PreferencesKeyList.LOGIN_TOKEN: "login_token", PreferencesKeyList.ENABLE_DARK_THEME: "dark_theme", PreferencesKeyList.FORCE_MOBILE_MODE: "force_mobile_mode", PreferencesKeyList.SHOW_PERFORMANCE_OVERLAY: "perfs_overlay", @@ -45,23 +42,28 @@ class PreferencesHelper { } /// Set new login tokens - Future setLoginTokens(LoginTokens tokens) async { - await setString(PreferencesKeyList.LOGIN_TOKENS, - tokens == null ? "null" : tokens.toString()); + Future setLoginToken(String token) async { + if (token != null) + await setString(PreferencesKeyList.LOGIN_TOKEN, token); + else + await _sharedPreferences.remove(token); } /// Get current [LoginTokens]. Returns null if none or in case of failure - LoginTokens getLoginTokens() { + String getLoginToken() { try { - final string = getString(PreferencesKeyList.LOGIN_TOKENS); - if (string == null || string == "null") return null; - return LoginTokens.fromJSON(jsonDecode(string)); + final string = getString(PreferencesKeyList.LOGIN_TOKEN); + return string; } on Exception catch (e) { print(e.toString()); return null; } } + Future removeKey(PreferencesKeyList key) async { + return await _sharedPreferences.remove(_PreferenceKeysName[key]); + } + Future setString(PreferencesKeyList key, String value) async { return await _sharedPreferences.setString(_PreferenceKeysName[key], value); } diff --git a/lib/main_dev.dart b/lib/main_dev.dart index 5d39cfa..20b4b2b 100644 --- a/lib/main_dev.dart +++ b/lib/main_dev.dart @@ -23,8 +23,7 @@ void main() { apiServerName: "192.168.1.9:3000", apiServerUri: "/", apiServerSecure: false, - serviceName: "ComunicFlutter", - serviceToken: "G9sZCBmb3IgVWJ1bnR1CkNvbW1lbnRbbmVdPeCkieCkrOCkq", + clientName: "ComunicFlutter", termsOfServicesURL: "http://devweb.local/comunic/current/about.php?cgu", )); diff --git a/lib/main_online.dart b/lib/main_online.dart index 8161a4e..5c95189 100644 --- a/lib/main_online.dart +++ b/lib/main_online.dart @@ -10,8 +10,7 @@ void main() { apiServerName: "api.communiquons.org", apiServerUri: "/", apiServerSecure: true, - serviceName: "ComunicFlutter", - serviceToken: "9KfSwmB76U9UUwjXngDG7PeYccNfy", + clientName: "ComunicFlutter", termsOfServicesURL: "https://about.communiquons.org/about/terms/", )); diff --git a/lib/models/config.dart b/lib/models/config.dart index bd177bb..22ad856 100644 --- a/lib/models/config.dart +++ b/lib/models/config.dart @@ -9,22 +9,20 @@ class Config { final String apiServerName; final String apiServerUri; final bool apiServerSecure; - final String serviceName; - final String serviceToken; + final String clientName; + final String termsOfServicesURL; const Config({ @required this.apiServerName, @required this.apiServerUri, @required this.apiServerSecure, - @required this.serviceName, - @required this.serviceToken, + @required this.clientName, @required this.termsOfServicesURL, }) : assert(apiServerName != null), assert(apiServerUri != null), assert(apiServerSecure != null), - assert(serviceName != null), - assert(serviceToken != null), + assert(clientName != null), assert(termsOfServicesURL != null); /// Get and set static configuration diff --git a/lib/models/login_tokens.dart b/lib/models/login_tokens.dart deleted file mode 100644 index 67cc5e0..0000000 --- a/lib/models/login_tokens.dart +++ /dev/null @@ -1,23 +0,0 @@ -import 'dart:convert'; - -/// Login tokens model -/// -/// @author Pierre HUBERT - -class LoginTokens { - final String tokenOne; - final String tokenTwo; - - const LoginTokens(this.tokenOne, this.tokenTwo) - : assert(tokenOne != null), - assert(tokenTwo != null); - - LoginTokens.fromJSON(Map json) - : tokenOne = json["token_one"], - tokenTwo = json["token_two"]; - - @override - String toString() { - return jsonEncode({"token_one": tokenOne, "token_two": tokenTwo}); - } -}