1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-26 06:49:22 +00:00

Upgrade login system

This commit is contained in:
Pierre HUBERT 2021-02-13 16:03:07 +01:00
parent 581059cb1d
commit e5ed4fadda
7 changed files with 32 additions and 63 deletions

View File

@ -3,7 +3,6 @@ import 'package:comunic/helpers/preferences_helper.dart';
import 'package:comunic/helpers/websocket_helper.dart'; import 'package:comunic/helpers/websocket_helper.dart';
import 'package:comunic/models/api_request.dart'; import 'package:comunic/models/api_request.dart';
import 'package:comunic/models/authentication_details.dart'; import 'package:comunic/models/authentication_details.dart';
import 'package:comunic/models/login_tokens.dart';
import 'package:comunic/models/new_account.dart'; import 'package:comunic/models/new_account.dart';
import 'package:shared_preferences/shared_preferences.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 !!! /// Warning : This method MUST BE CALLED AT LEAST ONCE AFTER APP START !!!
Future<bool> signedIn() async { Future<bool> signedIn() async {
bool signedIn = bool signedIn =
(await PreferencesHelper.getInstance()).getLoginTokens() != null; (await PreferencesHelper.getInstance()).getLoginToken() != null;
// Load current user ID for later use // Load current user ID for later use
if (signedIn && _currentUserID == -1) await _loadCurrentUserID(); if (signedIn && _currentUserID == -1) await _loadCurrentUserID();
@ -47,8 +46,8 @@ class AccountHelper {
/// Sign in user /// Sign in user
Future<AuthResult> signIn(AuthenticationDetails auth) async { Future<AuthResult> signIn(AuthenticationDetails auth) async {
final request = APIRequest(uri: "account/login"); final request = APIRequest(uri: "account/login");
request.addString("userMail", auth.email); request.addString("mail", auth.email);
request.addString("userPassword", auth.password); request.addString("password", auth.password);
final response = await APIHelper().exec(request); final response = await APIHelper().exec(request);
@ -59,10 +58,9 @@ class AccountHelper {
return AuthResult.TOO_MANY_ATTEMPTS; return AuthResult.TOO_MANY_ATTEMPTS;
else if (response.code != 200) return AuthResult.NETWORK_ERROR; else if (response.code != 200) return AuthResult.NETWORK_ERROR;
// Save login tokens // Save login token
final tokensObj = response.getObject()["tokens"];
await (await PreferencesHelper.getInstance()) await (await PreferencesHelper.getInstance())
.setLoginTokens(LoginTokens(tokensObj["token1"], tokensObj["token2"])); .setLoginToken(response.getObject()["token"]);
// Get current user ID // Get current user ID
final userID = await _downloadCurrentUserID(); final userID = await _downloadCurrentUserID();
@ -81,10 +79,11 @@ class AccountHelper {
/// Sign out user /// Sign out user
Future<void> signOut() async { Future<void> signOut() async {
await (await PreferencesHelper.getInstance()).setLoginTokens(null); await (await PreferencesHelper.getInstance()).setLoginToken(null);
_currentUserID = 0; _currentUserID = 0;
// Close current web socket // Close current web socket
await APIRequest.withLogin("account/logout").exec();
WebSocketHelper.close(); WebSocketHelper.close();
} }
@ -178,10 +177,7 @@ class AccountHelper {
/// Get current user ID from the server /// Get current user ID from the server
Future<int> _downloadCurrentUserID() async { Future<int> _downloadCurrentUserID() async {
final response = await APIRequest( final response = await APIRequest.withLogin("account/id").exec();
uri: "user/getCurrentUserID",
needLogin: true,
).exec();
if (response.code != 200) return null; if (response.code != 200) return null;

View File

@ -19,15 +19,13 @@ class APIHelper {
Future<APIResponse> exec(APIRequest request, {bool multipart = false}) async { Future<APIResponse> exec(APIRequest request, {bool multipart = false}) async {
try { try {
//Add API tokens //Add API tokens
request.addString("serviceName", config().serviceName); request.addString("client", config().clientName);
request.addString("serviceToken", config().serviceToken);
//Add user tokens (if required) //Add user tokens (if required)
if (request.needLogin) { if (request.needLogin) {
final tokens = (await PreferencesHelper.getInstance()).getLoginTokens(); final token = (await PreferencesHelper.getInstance()).getLoginToken();
assert(tokens != null); assert(token != null);
request.addString("userToken1", tokens.tokenOne); request.addString("token", token);
request.addString("userToken2", tokens.tokenTwo);
} }
// Determine server URL // Determine server URL

View File

@ -1,7 +1,4 @@
import 'dart:convert';
import 'package:comunic/models/application_preferences.dart'; import 'package:comunic/models/application_preferences.dart';
import 'package:comunic/models/login_tokens.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
/// Preferences helper /// Preferences helper
@ -11,14 +8,14 @@ import 'package:shared_preferences/shared_preferences.dart';
/// @author Pierre HUBERT /// @author Pierre HUBERT
enum PreferencesKeyList { enum PreferencesKeyList {
LOGIN_TOKENS, LOGIN_TOKEN,
ENABLE_DARK_THEME, ENABLE_DARK_THEME,
FORCE_MOBILE_MODE, FORCE_MOBILE_MODE,
SHOW_PERFORMANCE_OVERLAY, SHOW_PERFORMANCE_OVERLAY,
} }
const _PreferenceKeysName = { const _PreferenceKeysName = {
PreferencesKeyList.LOGIN_TOKENS: "login_tokens", PreferencesKeyList.LOGIN_TOKEN: "login_token",
PreferencesKeyList.ENABLE_DARK_THEME: "dark_theme", PreferencesKeyList.ENABLE_DARK_THEME: "dark_theme",
PreferencesKeyList.FORCE_MOBILE_MODE: "force_mobile_mode", PreferencesKeyList.FORCE_MOBILE_MODE: "force_mobile_mode",
PreferencesKeyList.SHOW_PERFORMANCE_OVERLAY: "perfs_overlay", PreferencesKeyList.SHOW_PERFORMANCE_OVERLAY: "perfs_overlay",
@ -45,23 +42,28 @@ class PreferencesHelper {
} }
/// Set new login tokens /// Set new login tokens
Future<void> setLoginTokens(LoginTokens tokens) async { Future<void> setLoginToken(String token) async {
await setString(PreferencesKeyList.LOGIN_TOKENS, if (token != null)
tokens == null ? "null" : tokens.toString()); await setString(PreferencesKeyList.LOGIN_TOKEN, token);
else
await _sharedPreferences.remove(token);
} }
/// Get current [LoginTokens]. Returns null if none or in case of failure /// Get current [LoginTokens]. Returns null if none or in case of failure
LoginTokens getLoginTokens() { String getLoginToken() {
try { try {
final string = getString(PreferencesKeyList.LOGIN_TOKENS); final string = getString(PreferencesKeyList.LOGIN_TOKEN);
if (string == null || string == "null") return null; return string;
return LoginTokens.fromJSON(jsonDecode(string));
} on Exception catch (e) { } on Exception catch (e) {
print(e.toString()); print(e.toString());
return null; return null;
} }
} }
Future<bool> removeKey(PreferencesKeyList key) async {
return await _sharedPreferences.remove(_PreferenceKeysName[key]);
}
Future<bool> setString(PreferencesKeyList key, String value) async { Future<bool> setString(PreferencesKeyList key, String value) async {
return await _sharedPreferences.setString(_PreferenceKeysName[key], value); return await _sharedPreferences.setString(_PreferenceKeysName[key], value);
} }

View File

@ -23,8 +23,7 @@ void main() {
apiServerName: "192.168.1.9:3000", apiServerName: "192.168.1.9:3000",
apiServerUri: "/", apiServerUri: "/",
apiServerSecure: false, apiServerSecure: false,
serviceName: "ComunicFlutter", clientName: "ComunicFlutter",
serviceToken: "G9sZCBmb3IgVWJ1bnR1CkNvbW1lbnRbbmVdPeCkieCkrOCkq",
termsOfServicesURL: "http://devweb.local/comunic/current/about.php?cgu", termsOfServicesURL: "http://devweb.local/comunic/current/about.php?cgu",
)); ));

View File

@ -10,8 +10,7 @@ void main() {
apiServerName: "api.communiquons.org", apiServerName: "api.communiquons.org",
apiServerUri: "/", apiServerUri: "/",
apiServerSecure: true, apiServerSecure: true,
serviceName: "ComunicFlutter", clientName: "ComunicFlutter",
serviceToken: "9KfSwmB76U9UUwjXngDG7PeYccNfy",
termsOfServicesURL: "https://about.communiquons.org/about/terms/", termsOfServicesURL: "https://about.communiquons.org/about/terms/",
)); ));

View File

@ -9,22 +9,20 @@ class Config {
final String apiServerName; final String apiServerName;
final String apiServerUri; final String apiServerUri;
final bool apiServerSecure; final bool apiServerSecure;
final String serviceName; final String clientName;
final String serviceToken;
final String termsOfServicesURL; final String termsOfServicesURL;
const Config({ const Config({
@required this.apiServerName, @required this.apiServerName,
@required this.apiServerUri, @required this.apiServerUri,
@required this.apiServerSecure, @required this.apiServerSecure,
@required this.serviceName, @required this.clientName,
@required this.serviceToken,
@required this.termsOfServicesURL, @required this.termsOfServicesURL,
}) : assert(apiServerName != null), }) : assert(apiServerName != null),
assert(apiServerUri != null), assert(apiServerUri != null),
assert(apiServerSecure != null), assert(apiServerSecure != null),
assert(serviceName != null), assert(clientName != null),
assert(serviceToken != null),
assert(termsOfServicesURL != null); assert(termsOfServicesURL != null);
/// Get and set static configuration /// Get and set static configuration

View File

@ -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<String, dynamic> json)
: tokenOne = json["token_one"],
tokenTwo = json["token_two"];
@override
String toString() {
return jsonEncode({"token_one": tokenOne, "token_two": tokenTwo});
}
}