2022-11-06 10:36:33 +00:00
|
|
|
import 'dart:async';
|
2021-06-29 15:03:54 +00:00
|
|
|
import 'dart:typed_data';
|
|
|
|
|
2023-03-14 01:40:54 +00:00
|
|
|
import 'package:bip39/bip39.dart' as bip39;
|
2020-04-30 15:09:41 +00:00
|
|
|
import 'package:dio/dio.dart';
|
2020-08-25 06:00:19 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2020-05-02 16:28:54 +00:00
|
|
|
import 'package:logging/logging.dart';
|
2020-05-04 20:08:20 +00:00
|
|
|
import 'package:photos/core/configuration.dart';
|
2023-03-14 01:40:54 +00:00
|
|
|
import 'package:photos/core/constants.dart';
|
2021-06-29 09:48:01 +00:00
|
|
|
import 'package:photos/core/event_bus.dart';
|
2023-02-03 07:39:04 +00:00
|
|
|
import 'package:photos/core/network/network.dart';
|
2020-10-17 18:16:30 +00:00
|
|
|
import 'package:photos/db/public_keys_db.dart';
|
2021-06-29 09:48:01 +00:00
|
|
|
import 'package:photos/events/two_factor_status_change_event.dart';
|
2021-10-30 05:43:56 +00:00
|
|
|
import 'package:photos/events/user_details_changed_event.dart';
|
2023-04-07 06:33:40 +00:00
|
|
|
import "package:photos/generated/l10n.dart";
|
2022-07-08 08:50:19 +00:00
|
|
|
import 'package:photos/models/delete_account.dart';
|
2020-09-05 08:53:23 +00:00
|
|
|
import 'package:photos/models/key_attributes.dart';
|
2021-03-29 15:09:12 +00:00
|
|
|
import 'package:photos/models/key_gen_result.dart';
|
2020-10-18 21:39:55 +00:00
|
|
|
import 'package:photos/models/public_key.dart';
|
2021-11-23 19:40:33 +00:00
|
|
|
import 'package:photos/models/sessions.dart';
|
2021-03-28 12:43:44 +00:00
|
|
|
import 'package:photos/models/set_keys_request.dart';
|
2021-04-01 17:33:15 +00:00
|
|
|
import 'package:photos/models/set_recovery_key_request.dart';
|
2021-07-28 14:08:27 +00:00
|
|
|
import 'package:photos/models/user_details.dart';
|
2022-07-01 14:18:05 +00:00
|
|
|
import 'package:photos/ui/account/login_page.dart';
|
|
|
|
import 'package:photos/ui/account/ott_verification_page.dart';
|
|
|
|
import 'package:photos/ui/account/password_entry_page.dart';
|
|
|
|
import 'package:photos/ui/account/password_reentry_page.dart';
|
|
|
|
import 'package:photos/ui/account/two_factor_authentication_page.dart';
|
2023-03-14 01:40:54 +00:00
|
|
|
import 'package:photos/ui/account/two_factor_recovery_page.dart';
|
2022-07-01 14:18:05 +00:00
|
|
|
import 'package:photos/ui/account/two_factor_setup_page.dart';
|
2023-03-14 01:40:54 +00:00
|
|
|
import 'package:photos/utils/crypto_util.dart';
|
2020-08-25 06:00:19 +00:00
|
|
|
import 'package:photos/utils/dialog_util.dart';
|
2021-06-29 09:48:01 +00:00
|
|
|
import 'package:photos/utils/navigation_util.dart';
|
2020-08-26 02:34:13 +00:00
|
|
|
import 'package:photos/utils/toast_util.dart';
|
2022-11-16 12:20:02 +00:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2021-10-30 05:43:56 +00:00
|
|
|
|
2020-10-03 17:56:18 +00:00
|
|
|
class UserService {
|
2022-11-16 12:20:02 +00:00
|
|
|
static const keyHasEnabledTwoFactor = "has_enabled_two_factor";
|
2022-12-27 11:51:26 +00:00
|
|
|
static const keyUserDetails = "user_details";
|
2023-02-03 07:39:04 +00:00
|
|
|
final _dio = NetworkClient.instance.getDio();
|
|
|
|
final _enteDio = NetworkClient.instance.enteDio;
|
2022-06-03 01:43:45 +00:00
|
|
|
final _logger = Logger((UserService).toString());
|
2020-10-21 21:29:16 +00:00
|
|
|
final _config = Configuration.instance;
|
2022-12-30 10:03:50 +00:00
|
|
|
late SharedPreferences _preferences;
|
2022-11-16 12:20:02 +00:00
|
|
|
|
2022-12-30 10:03:50 +00:00
|
|
|
late ValueNotifier<String?> emailValueNotifier;
|
2020-04-30 15:09:41 +00:00
|
|
|
|
2020-10-03 17:56:18 +00:00
|
|
|
UserService._privateConstructor();
|
2022-11-06 10:36:33 +00:00
|
|
|
|
2020-10-03 17:56:18 +00:00
|
|
|
static final UserService instance = UserService._privateConstructor();
|
2020-04-30 15:09:41 +00:00
|
|
|
|
2022-06-23 13:10:35 +00:00
|
|
|
Future<void> init() async {
|
|
|
|
emailValueNotifier =
|
2022-12-30 10:03:50 +00:00
|
|
|
ValueNotifier<String?>(Configuration.instance.getEmail());
|
2022-11-16 12:20:02 +00:00
|
|
|
_preferences = await SharedPreferences.getInstance();
|
2022-11-22 14:10:56 +00:00
|
|
|
if (Configuration.instance.isLoggedIn()) {
|
2022-11-22 14:15:38 +00:00
|
|
|
// add artificial delay in refreshing 2FA status
|
|
|
|
Future.delayed(
|
|
|
|
const Duration(seconds: 5),
|
|
|
|
() => {setTwoFactor(fetchTwoFactorStatus: true).ignore()},
|
|
|
|
);
|
2022-11-22 14:10:56 +00:00
|
|
|
}
|
2022-11-16 12:59:25 +00:00
|
|
|
Bus.instance.on<TwoFactorStatusChangeEvent>().listen((event) {
|
|
|
|
setTwoFactor(value: event.status);
|
|
|
|
});
|
2022-06-23 13:10:35 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 16:57:31 +00:00
|
|
|
Future<void> sendOtt(
|
2021-07-28 18:06:30 +00:00
|
|
|
BuildContext context,
|
|
|
|
String email, {
|
|
|
|
bool isChangeEmail = false,
|
2022-06-23 12:12:19 +00:00
|
|
|
bool isCreateAccountScreen = false,
|
2021-07-28 18:06:30 +00:00
|
|
|
}) async {
|
2023-04-07 06:33:40 +00:00
|
|
|
final dialog = createProgressDialog(context, S.of(context).pleaseWait);
|
2020-08-25 06:00:19 +00:00
|
|
|
await dialog.show();
|
2021-07-28 18:41:46 +00:00
|
|
|
try {
|
2022-07-06 16:57:31 +00:00
|
|
|
final response = await _dio.post(
|
2021-07-28 18:41:46 +00:00
|
|
|
_config.getHttpEndpoint() + "/users/ott",
|
2022-07-06 16:57:31 +00:00
|
|
|
data: {"email": email, "purpose": isChangeEmail ? "change" : ""},
|
2021-07-28 18:41:46 +00:00
|
|
|
);
|
2020-08-25 06:00:19 +00:00
|
|
|
await dialog.hide();
|
2022-12-30 10:03:50 +00:00
|
|
|
if (response.statusCode == 200) {
|
2022-11-06 10:36:33 +00:00
|
|
|
unawaited(
|
|
|
|
Navigator.of(context).push(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return OTTVerificationPage(
|
|
|
|
email,
|
|
|
|
isChangeEmail: isChangeEmail,
|
|
|
|
isCreateAccountScreen: isCreateAccountScreen,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2021-07-28 18:06:30 +00:00
|
|
|
),
|
|
|
|
);
|
2021-07-28 18:41:46 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-12-26 11:09:29 +00:00
|
|
|
unawaited(showGenericErrorDialog(context: context));
|
2021-07-28 18:41:46 +00:00
|
|
|
} on DioError catch (e) {
|
|
|
|
await dialog.hide();
|
2022-02-05 16:43:40 +00:00
|
|
|
_logger.info(e);
|
2022-12-30 10:03:50 +00:00
|
|
|
if (e.response != null && e.response!.statusCode == 403) {
|
2022-11-06 10:36:33 +00:00
|
|
|
unawaited(
|
|
|
|
showErrorDialog(
|
|
|
|
context,
|
2023-04-07 06:33:40 +00:00
|
|
|
S.of(context).oops,
|
|
|
|
S.of(context).thisEmailIsAlreadyInUse,
|
2022-11-06 10:36:33 +00:00
|
|
|
),
|
|
|
|
);
|
2020-08-25 06:00:19 +00:00
|
|
|
} else {
|
2022-12-26 11:09:29 +00:00
|
|
|
unawaited(showGenericErrorDialog(context: context));
|
2020-08-25 06:00:19 +00:00
|
|
|
}
|
2021-07-28 18:41:46 +00:00
|
|
|
} catch (e) {
|
|
|
|
await dialog.hide();
|
|
|
|
_logger.severe(e);
|
2022-12-26 11:09:29 +00:00
|
|
|
unawaited(showGenericErrorDialog(context: context));
|
2021-07-28 18:41:46 +00:00
|
|
|
}
|
2020-08-25 06:00:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-15 13:05:11 +00:00
|
|
|
Future<void> sendFeedback(
|
|
|
|
BuildContext context,
|
|
|
|
String feedback, {
|
|
|
|
String type = "SubCancellation",
|
|
|
|
}) async {
|
2023-03-11 10:53:20 +00:00
|
|
|
await _dio.post(
|
|
|
|
_config.getHttpEndpoint() + "/anonymous/feedback",
|
2023-03-15 13:05:11 +00:00
|
|
|
data: {"feedback": feedback, "type": "type"},
|
2023-03-11 10:53:20 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-31 07:45:34 +00:00
|
|
|
// getPublicKey returns null value if email id is not
|
|
|
|
// associated with another ente account
|
2022-12-30 10:03:50 +00:00
|
|
|
Future<String?> getPublicKey(String email) async {
|
2020-10-09 21:45:19 +00:00
|
|
|
try {
|
2022-10-14 09:32:51 +00:00
|
|
|
final response = await _enteDio.get(
|
|
|
|
"/users/public-key",
|
2020-10-17 18:04:41 +00:00
|
|
|
queryParameters: {"email": email},
|
2020-10-09 21:45:19 +00:00
|
|
|
);
|
2020-10-18 21:39:55 +00:00
|
|
|
final publicKey = response.data["publicKey"];
|
|
|
|
await PublicKeysDB.instance.setKey(PublicKey(email, publicKey));
|
2020-10-17 18:16:30 +00:00
|
|
|
return publicKey;
|
2020-10-09 21:45:19 +00:00
|
|
|
} on DioError catch (e) {
|
2023-01-31 07:45:34 +00:00
|
|
|
if (e.response != null && e.response?.statusCode == 404) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
rethrow;
|
2020-10-09 21:45:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-30 10:03:50 +00:00
|
|
|
UserDetails? getCachedUserDetails() {
|
2022-12-27 11:51:26 +00:00
|
|
|
if (_preferences.containsKey(keyUserDetails)) {
|
2022-12-30 10:03:50 +00:00
|
|
|
return UserDetails.fromJson(_preferences.getString(keyUserDetails)!);
|
2022-12-27 11:51:26 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<UserDetails> getUserDetailsV2({
|
|
|
|
bool memoryCount = true,
|
|
|
|
bool shouldCache = false,
|
|
|
|
}) async {
|
2022-12-27 11:54:25 +00:00
|
|
|
_logger.info("Fetching user details");
|
2022-04-14 13:01:24 +00:00
|
|
|
try {
|
2022-10-14 09:32:51 +00:00
|
|
|
final response = await _enteDio.get(
|
2022-12-13 12:29:55 +00:00
|
|
|
"/users/details/v2",
|
2022-05-30 09:23:55 +00:00
|
|
|
queryParameters: {
|
|
|
|
"memoryCount": memoryCount,
|
|
|
|
},
|
2022-04-14 13:01:24 +00:00
|
|
|
);
|
2022-12-27 11:51:26 +00:00
|
|
|
final userDetails = UserDetails.fromMap(response.data);
|
|
|
|
if (shouldCache) {
|
|
|
|
await _preferences.setString(keyUserDetails, userDetails.toJson());
|
2023-03-17 11:00:40 +00:00
|
|
|
// handle email change from different client
|
|
|
|
if (userDetails.email != _config.getEmail()) {
|
|
|
|
setEmail(userDetails.email);
|
|
|
|
}
|
2022-12-27 11:51:26 +00:00
|
|
|
}
|
|
|
|
return userDetails;
|
2022-04-14 13:01:24 +00:00
|
|
|
} on DioError catch (e) {
|
|
|
|
_logger.info(e);
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 19:40:33 +00:00
|
|
|
Future<Sessions> getActiveSessions() async {
|
|
|
|
try {
|
2022-10-14 09:32:51 +00:00
|
|
|
final response = await _enteDio.get("/users/sessions");
|
2021-11-23 19:40:33 +00:00
|
|
|
return Sessions.fromMap(response.data);
|
|
|
|
} on DioError catch (e) {
|
|
|
|
_logger.info(e);
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 19:48:24 +00:00
|
|
|
Future<void> terminateSession(String token) async {
|
|
|
|
try {
|
2022-10-14 09:32:51 +00:00
|
|
|
await _enteDio.delete(
|
|
|
|
"/users/session",
|
2022-06-11 08:23:52 +00:00
|
|
|
queryParameters: {
|
|
|
|
"token": token,
|
|
|
|
},
|
|
|
|
);
|
2021-11-23 19:48:24 +00:00
|
|
|
} on DioError catch (e) {
|
|
|
|
_logger.info(e);
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 19:36:33 +00:00
|
|
|
Future<void> leaveFamilyPlan() async {
|
|
|
|
try {
|
2022-10-14 09:32:51 +00:00
|
|
|
await _enteDio.delete("/family/leave");
|
2022-04-20 19:36:33 +00:00
|
|
|
} on DioError catch (e) {
|
|
|
|
_logger.warning('failed to leave family plan', e);
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-05 13:31:37 +00:00
|
|
|
Future<void> logout(BuildContext context) async {
|
2021-07-05 09:31:03 +00:00
|
|
|
try {
|
2022-10-14 09:32:51 +00:00
|
|
|
final response = await _enteDio.post("/users/logout");
|
2022-12-30 10:03:50 +00:00
|
|
|
if (response.statusCode == 200) {
|
2021-07-05 13:31:37 +00:00
|
|
|
await Configuration.instance.logout();
|
|
|
|
Navigator.of(context).popUntil((route) => route.isFirst);
|
|
|
|
} else {
|
2022-06-03 01:49:05 +00:00
|
|
|
throw Exception("Log out action failed");
|
2021-07-05 13:31:37 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
_logger.severe(e);
|
2023-01-27 13:00:32 +00:00
|
|
|
//This future is for waiting for the dialog from which logout() is called
|
|
|
|
//to close and only then to show the error dialog.
|
2023-01-27 10:23:31 +00:00
|
|
|
Future.delayed(
|
|
|
|
const Duration(milliseconds: 150),
|
|
|
|
() => showGenericErrorDialog(context: context),
|
|
|
|
);
|
|
|
|
rethrow;
|
2021-07-05 09:31:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-30 10:03:50 +00:00
|
|
|
Future<DeleteChallengeResponse?> getDeleteChallenge(
|
2022-07-08 08:50:19 +00:00
|
|
|
BuildContext context,
|
|
|
|
) async {
|
|
|
|
try {
|
2022-10-14 09:32:51 +00:00
|
|
|
final response = await _enteDio.get("/users/delete-challenge");
|
2022-12-30 10:03:50 +00:00
|
|
|
if (response.statusCode == 200) {
|
2022-07-08 08:50:19 +00:00
|
|
|
return DeleteChallengeResponse(
|
|
|
|
allowDelete: response.data["allowDelete"] as bool,
|
|
|
|
encryptedChallenge: response.data["encryptedChallenge"],
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
throw Exception("delete action failed");
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
_logger.severe(e);
|
2022-12-26 11:09:29 +00:00
|
|
|
await showGenericErrorDialog(context: context);
|
2022-07-08 08:50:19 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> deleteAccount(
|
|
|
|
BuildContext context,
|
2023-03-15 13:09:07 +00:00
|
|
|
String challengeResponse, {
|
|
|
|
required String reasonCategory,
|
|
|
|
required String feedback,
|
|
|
|
}) async {
|
2022-07-07 08:09:38 +00:00
|
|
|
try {
|
2022-10-14 09:32:51 +00:00
|
|
|
final response = await _enteDio.delete(
|
|
|
|
"/users/delete",
|
2022-07-08 08:50:19 +00:00
|
|
|
data: {
|
|
|
|
"challenge": challengeResponse,
|
2023-03-15 13:09:07 +00:00
|
|
|
"reasonCategory": reasonCategory,
|
|
|
|
"feedback": feedback,
|
2022-07-08 08:50:19 +00:00
|
|
|
},
|
2022-07-07 08:09:38 +00:00
|
|
|
);
|
2022-12-30 10:03:50 +00:00
|
|
|
if (response.statusCode == 200) {
|
2022-07-07 08:09:38 +00:00
|
|
|
// clear data
|
|
|
|
await Configuration.instance.logout();
|
|
|
|
} else {
|
|
|
|
throw Exception("delete action failed");
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
_logger.severe(e);
|
2022-12-22 13:38:39 +00:00
|
|
|
rethrow;
|
2022-07-07 08:09:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-12 09:44:00 +00:00
|
|
|
Future<void> verifyEmail(BuildContext context, String ott) async {
|
2023-04-07 06:33:40 +00:00
|
|
|
final dialog = createProgressDialog(context, S.of(context).pleaseWait);
|
2020-08-25 06:00:19 +00:00
|
|
|
await dialog.show();
|
2021-03-21 12:04:57 +00:00
|
|
|
try {
|
2021-06-12 09:44:00 +00:00
|
|
|
final response = await _dio.post(
|
|
|
|
_config.getHttpEndpoint() + "/users/verify-email",
|
|
|
|
data: {
|
2021-03-21 12:04:57 +00:00
|
|
|
"email": _config.getEmail(),
|
|
|
|
"ott": ott,
|
|
|
|
},
|
|
|
|
);
|
2020-08-25 06:00:19 +00:00
|
|
|
await dialog.hide();
|
2022-12-30 10:03:50 +00:00
|
|
|
if (response.statusCode == 200) {
|
2021-07-22 18:41:58 +00:00
|
|
|
Widget page;
|
2021-06-26 10:37:17 +00:00
|
|
|
final String twoFASessionID = response.data["twoFactorSessionID"];
|
2022-12-30 10:03:50 +00:00
|
|
|
if (twoFASessionID.isNotEmpty) {
|
2023-01-04 14:47:27 +00:00
|
|
|
setTwoFactor(value: true);
|
2021-06-26 10:37:17 +00:00
|
|
|
page = TwoFactorAuthenticationPage(twoFASessionID);
|
2020-08-26 01:43:10 +00:00
|
|
|
} else {
|
2021-06-26 10:37:17 +00:00
|
|
|
await _saveConfiguration(response);
|
|
|
|
if (Configuration.instance.getEncryptedToken() != null) {
|
2022-07-04 08:43:01 +00:00
|
|
|
page = const PasswordReentryPage();
|
2021-06-26 10:37:17 +00:00
|
|
|
} else {
|
2022-07-04 08:43:01 +00:00
|
|
|
page = const PasswordEntryPage();
|
2021-06-26 10:37:17 +00:00
|
|
|
}
|
2020-08-26 01:43:10 +00:00
|
|
|
}
|
2020-08-26 02:34:13 +00:00
|
|
|
Navigator.of(context).pushAndRemoveUntil(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return page;
|
|
|
|
},
|
|
|
|
),
|
2021-10-30 05:43:56 +00:00
|
|
|
(route) => route.isFirst,
|
2020-08-26 02:34:13 +00:00
|
|
|
);
|
2020-08-25 06:00:19 +00:00
|
|
|
} else {
|
2021-09-04 10:21:07 +00:00
|
|
|
// should never reach here
|
|
|
|
throw Exception("unexpected response during email verification");
|
2020-08-25 06:00:19 +00:00
|
|
|
}
|
2021-09-04 10:21:07 +00:00
|
|
|
} on DioError catch (e) {
|
2022-02-05 16:43:40 +00:00
|
|
|
_logger.info(e);
|
2021-09-04 10:21:07 +00:00
|
|
|
await dialog.hide();
|
2022-12-30 10:03:50 +00:00
|
|
|
if (e.response != null && e.response!.statusCode == 410) {
|
2022-02-04 05:31:07 +00:00
|
|
|
await showErrorDialog(
|
2022-06-11 08:23:52 +00:00
|
|
|
context,
|
2023-04-07 06:33:40 +00:00
|
|
|
S.of(context).oops,
|
|
|
|
S.of(context).yourVerificationCodeHasExpired,
|
2022-06-11 08:23:52 +00:00
|
|
|
);
|
2021-09-04 10:21:07 +00:00
|
|
|
Navigator.of(context).pop();
|
|
|
|
} else {
|
2022-06-11 08:23:52 +00:00
|
|
|
showErrorDialog(
|
|
|
|
context,
|
2023-04-07 06:33:40 +00:00
|
|
|
S.of(context).incorrectCode,
|
|
|
|
S.of(context).sorryTheCodeYouveEnteredIsIncorrect,
|
2022-06-11 08:23:52 +00:00
|
|
|
);
|
2021-09-04 10:21:07 +00:00
|
|
|
}
|
2021-09-04 11:32:39 +00:00
|
|
|
} catch (e) {
|
2021-03-21 12:04:57 +00:00
|
|
|
await dialog.hide();
|
|
|
|
_logger.severe(e);
|
2023-04-08 04:17:44 +00:00
|
|
|
showErrorDialog(
|
|
|
|
context,
|
|
|
|
S.of(context).oops,
|
|
|
|
S.of(context).verificationFailedPleaseTryAgain,
|
|
|
|
);
|
2021-03-21 12:04:57 +00:00
|
|
|
}
|
2020-08-25 06:00:19 +00:00
|
|
|
}
|
|
|
|
|
2022-06-23 13:06:35 +00:00
|
|
|
Future<void> setEmail(String email) async {
|
|
|
|
await _config.setEmail(email);
|
2022-12-30 10:03:50 +00:00
|
|
|
emailValueNotifier.value = email;
|
2022-06-23 13:06:35 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 18:06:30 +00:00
|
|
|
Future<void> changeEmail(
|
|
|
|
BuildContext context,
|
|
|
|
String email,
|
|
|
|
String ott,
|
|
|
|
) async {
|
2023-04-07 06:33:40 +00:00
|
|
|
final dialog = createProgressDialog(context, S.of(context).pleaseWait);
|
2021-07-28 18:06:30 +00:00
|
|
|
await dialog.show();
|
|
|
|
try {
|
2022-10-14 09:32:51 +00:00
|
|
|
final response = await _enteDio.post(
|
|
|
|
"/users/change-email",
|
2021-07-28 18:06:30 +00:00
|
|
|
data: {
|
|
|
|
"email": email,
|
|
|
|
"ott": ott,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
await dialog.hide();
|
2022-12-30 10:03:50 +00:00
|
|
|
if (response.statusCode == 200) {
|
2023-04-07 06:33:40 +00:00
|
|
|
showShortToast(context, S.of(context).emailChangedTo(email));
|
2022-06-23 13:06:35 +00:00
|
|
|
await setEmail(email);
|
2021-07-28 18:06:30 +00:00
|
|
|
Navigator.of(context).popUntil((route) => route.isFirst);
|
2021-09-15 20:56:05 +00:00
|
|
|
Bus.instance.fire(UserDetailsChangedEvent());
|
2021-07-28 18:06:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-04-08 04:17:44 +00:00
|
|
|
showErrorDialog(
|
|
|
|
context,
|
|
|
|
S.of(context).oops,
|
|
|
|
S.of(context).verificationFailedPleaseTryAgain,
|
|
|
|
);
|
2021-07-28 18:06:30 +00:00
|
|
|
} on DioError catch (e) {
|
|
|
|
await dialog.hide();
|
2022-12-30 10:03:50 +00:00
|
|
|
if (e.response != null && e.response!.statusCode == 403) {
|
2023-04-07 06:33:40 +00:00
|
|
|
showErrorDialog(
|
2023-04-08 04:17:44 +00:00
|
|
|
context,
|
|
|
|
S.of(context).oops,
|
|
|
|
S.of(context).thisEmailIsAlreadyInUse,
|
|
|
|
);
|
2021-07-28 18:06:30 +00:00
|
|
|
} else {
|
2022-06-11 08:23:52 +00:00
|
|
|
showErrorDialog(
|
|
|
|
context,
|
2023-04-07 06:33:40 +00:00
|
|
|
S.of(context).incorrectCode,
|
|
|
|
S.of(context).authenticationFailedPleaseTryAgain,
|
2022-06-11 08:23:52 +00:00
|
|
|
);
|
2021-07-28 18:06:30 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
await dialog.hide();
|
|
|
|
_logger.severe(e);
|
2023-04-08 04:17:44 +00:00
|
|
|
showErrorDialog(
|
|
|
|
context,
|
|
|
|
S.of(context).oops,
|
|
|
|
S.of(context).verificationFailedPleaseTryAgain,
|
|
|
|
);
|
2021-07-28 18:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-01 14:26:08 +00:00
|
|
|
Future<void> setAttributes(KeyGenResult result) async {
|
2021-03-26 16:13:32 +00:00
|
|
|
try {
|
|
|
|
final name = _config.getName();
|
2022-10-14 09:32:51 +00:00
|
|
|
await _enteDio.put(
|
|
|
|
"/users/attributes",
|
2021-03-26 16:13:32 +00:00
|
|
|
data: {
|
|
|
|
"name": name,
|
|
|
|
"keyAttributes": result.keyAttributes.toMap(),
|
2020-08-26 01:43:10 +00:00
|
|
|
},
|
2021-03-26 16:13:32 +00:00
|
|
|
);
|
2021-03-29 18:35:46 +00:00
|
|
|
await _config.setKey(result.privateKeyAttributes.key);
|
|
|
|
await _config.setSecretKey(result.privateKeyAttributes.secretKey);
|
|
|
|
await _config.setKeyAttributes(result.keyAttributes);
|
2021-03-26 16:13:32 +00:00
|
|
|
} catch (e) {
|
|
|
|
_logger.severe(e);
|
2021-07-22 18:41:58 +00:00
|
|
|
rethrow;
|
2021-03-26 16:13:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-01 14:26:08 +00:00
|
|
|
Future<void> updateKeyAttributes(KeyAttributes keyAttributes) async {
|
2021-03-26 16:13:32 +00:00
|
|
|
try {
|
2021-03-28 12:43:44 +00:00
|
|
|
final setKeyRequest = SetKeysRequest(
|
|
|
|
kekSalt: keyAttributes.kekSalt,
|
|
|
|
encryptedKey: keyAttributes.encryptedKey,
|
|
|
|
keyDecryptionNonce: keyAttributes.keyDecryptionNonce,
|
2023-01-11 05:02:47 +00:00
|
|
|
memLimit: keyAttributes.memLimit!,
|
|
|
|
opsLimit: keyAttributes.opsLimit!,
|
2021-03-28 12:43:44 +00:00
|
|
|
);
|
2022-10-14 09:32:51 +00:00
|
|
|
await _enteDio.put(
|
|
|
|
"/users/keys",
|
2021-03-28 12:43:44 +00:00
|
|
|
data: setKeyRequest.toMap(),
|
2021-03-26 16:13:32 +00:00
|
|
|
);
|
2021-04-01 14:26:08 +00:00
|
|
|
await _config.setKeyAttributes(keyAttributes);
|
2021-03-26 16:13:32 +00:00
|
|
|
} catch (e) {
|
|
|
|
_logger.severe(e);
|
2021-07-22 18:41:58 +00:00
|
|
|
rethrow;
|
2021-03-26 16:13:32 +00:00
|
|
|
}
|
2020-08-26 01:43:10 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 17:33:15 +00:00
|
|
|
Future<void> setRecoveryKey(KeyAttributes keyAttributes) async {
|
|
|
|
try {
|
|
|
|
final setRecoveryKeyRequest = SetRecoveryKeyRequest(
|
2023-01-11 05:11:02 +00:00
|
|
|
keyAttributes.masterKeyEncryptedWithRecoveryKey!,
|
|
|
|
keyAttributes.masterKeyDecryptionNonce!,
|
2022-12-30 10:03:50 +00:00
|
|
|
keyAttributes.recoveryKeyEncryptedWithMasterKey!,
|
|
|
|
keyAttributes.recoveryKeyDecryptionNonce!,
|
2021-04-01 17:33:15 +00:00
|
|
|
);
|
2022-10-14 09:54:57 +00:00
|
|
|
await _enteDio.put(
|
|
|
|
"/users/recovery-key",
|
2021-04-01 17:33:15 +00:00
|
|
|
data: setRecoveryKeyRequest.toMap(),
|
|
|
|
);
|
|
|
|
await _config.setKeyAttributes(keyAttributes);
|
|
|
|
} catch (e) {
|
|
|
|
_logger.severe(e);
|
2021-07-22 18:41:58 +00:00
|
|
|
rethrow;
|
2021-04-01 17:33:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-26 10:37:17 +00:00
|
|
|
Future<void> verifyTwoFactor(
|
2022-06-11 08:23:52 +00:00
|
|
|
BuildContext context,
|
|
|
|
String sessionID,
|
|
|
|
String code,
|
|
|
|
) async {
|
2023-04-07 06:33:40 +00:00
|
|
|
final dialog = createProgressDialog(context, S.of(context).authenticating);
|
2021-06-26 10:37:17 +00:00
|
|
|
await dialog.show();
|
|
|
|
try {
|
|
|
|
final response = await _dio.post(
|
|
|
|
_config.getHttpEndpoint() + "/users/two-factor/verify",
|
|
|
|
data: {
|
|
|
|
"sessionID": sessionID,
|
|
|
|
"code": code,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
await dialog.hide();
|
2022-12-30 10:03:50 +00:00
|
|
|
if (response.statusCode == 200) {
|
2023-04-07 06:33:40 +00:00
|
|
|
showShortToast(context, S.of(context).authenticationSuccessful);
|
2021-06-26 10:37:17 +00:00
|
|
|
await _saveConfiguration(response);
|
|
|
|
Navigator.of(context).pushAndRemoveUntil(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (BuildContext context) {
|
2022-07-04 08:43:01 +00:00
|
|
|
return const PasswordReentryPage();
|
2021-06-26 10:37:17 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
(route) => route.isFirst,
|
|
|
|
);
|
|
|
|
}
|
2021-06-26 10:56:52 +00:00
|
|
|
} on DioError catch (e) {
|
|
|
|
await dialog.hide();
|
|
|
|
_logger.severe(e);
|
2022-12-30 10:03:50 +00:00
|
|
|
if (e.response != null && e.response!.statusCode == 404) {
|
2022-06-10 14:29:56 +00:00
|
|
|
showToast(context, "Session expired");
|
2021-06-26 10:56:52 +00:00
|
|
|
Navigator.of(context).pushAndRemoveUntil(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (BuildContext context) {
|
2022-07-04 08:43:01 +00:00
|
|
|
return const LoginPage();
|
2021-06-26 10:56:52 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
(route) => route.isFirst,
|
|
|
|
);
|
|
|
|
} else {
|
2022-06-11 08:23:52 +00:00
|
|
|
showErrorDialog(
|
|
|
|
context,
|
2023-04-07 06:33:40 +00:00
|
|
|
S.of(context).incorrectCode,
|
|
|
|
S.of(context).authenticationFailedPleaseTryAgain,
|
2022-06-11 08:23:52 +00:00
|
|
|
);
|
2021-06-26 10:56:52 +00:00
|
|
|
}
|
2021-06-26 10:37:17 +00:00
|
|
|
} catch (e) {
|
|
|
|
await dialog.hide();
|
|
|
|
_logger.severe(e);
|
2022-02-04 05:31:07 +00:00
|
|
|
showErrorDialog(
|
2022-06-11 08:23:52 +00:00
|
|
|
context,
|
2023-04-07 06:33:40 +00:00
|
|
|
S.of(context).oops,
|
|
|
|
S.of(context).authenticationFailedPleaseTryAgain,
|
2022-06-11 08:23:52 +00:00
|
|
|
);
|
2021-06-26 10:37:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-14 01:40:54 +00:00
|
|
|
Future<void> recoverTwoFactor(BuildContext context, String sessionID) async {
|
2023-04-07 06:33:40 +00:00
|
|
|
final dialog = createProgressDialog(context, S.of(context).pleaseWait);
|
2023-03-14 01:40:54 +00:00
|
|
|
await dialog.show();
|
|
|
|
try {
|
|
|
|
final response = await _dio.get(
|
|
|
|
_config.getHttpEndpoint() + "/users/two-factor/recover",
|
|
|
|
queryParameters: {
|
|
|
|
"sessionID": sessionID,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
Navigator.of(context).pushAndRemoveUntil(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return TwoFactorRecoveryPage(
|
|
|
|
sessionID,
|
|
|
|
response.data["encryptedSecret"],
|
|
|
|
response.data["secretDecryptionNonce"],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(route) => route.isFirst,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} on DioError catch (e) {
|
|
|
|
_logger.severe(e);
|
|
|
|
if (e.response != null && e.response!.statusCode == 404) {
|
2023-04-07 06:33:40 +00:00
|
|
|
showToast(context, S.of(context).sessionExpired);
|
2023-03-14 01:40:54 +00:00
|
|
|
Navigator.of(context).pushAndRemoveUntil(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return const LoginPage();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(route) => route.isFirst,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
showErrorDialog(
|
|
|
|
context,
|
2023-04-07 06:33:40 +00:00
|
|
|
S.of(context).oops,
|
|
|
|
S.of(context).somethingWentWrongPleaseTryAgain,
|
2023-03-14 01:40:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
_logger.severe(e);
|
|
|
|
showErrorDialog(
|
|
|
|
context,
|
2023-04-07 06:33:40 +00:00
|
|
|
S.of(context).oops,
|
|
|
|
S.of(context).somethingWentWrongPleaseTryAgain,
|
2023-03-14 01:40:54 +00:00
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
await dialog.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> removeTwoFactor(
|
|
|
|
BuildContext context,
|
|
|
|
String sessionID,
|
|
|
|
String recoveryKey,
|
|
|
|
String encryptedSecret,
|
|
|
|
String secretDecryptionNonce,
|
|
|
|
) async {
|
2023-04-07 06:33:40 +00:00
|
|
|
final dialog = createProgressDialog(context, S.of(context).pleaseWait);
|
2023-03-14 01:40:54 +00:00
|
|
|
await dialog.show();
|
|
|
|
String secret;
|
|
|
|
try {
|
|
|
|
if (recoveryKey.contains(' ')) {
|
|
|
|
if (recoveryKey.split(' ').length != mnemonicKeyWordCount) {
|
|
|
|
throw AssertionError(
|
|
|
|
'recovery code should have $mnemonicKeyWordCount words',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
recoveryKey = bip39.mnemonicToEntropy(recoveryKey);
|
|
|
|
}
|
|
|
|
secret = CryptoUtil.bin2base64(
|
|
|
|
await CryptoUtil.decrypt(
|
|
|
|
CryptoUtil.base642bin(encryptedSecret),
|
|
|
|
CryptoUtil.hex2bin(recoveryKey.trim()),
|
|
|
|
CryptoUtil.base642bin(secretDecryptionNonce),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
await dialog.hide();
|
|
|
|
await showErrorDialog(
|
|
|
|
context,
|
2023-04-07 06:33:40 +00:00
|
|
|
S.of(context).incorrectRecoveryKey,
|
|
|
|
S.of(context).theRecoveryKeyYouEnteredIsIncorrect,
|
2023-03-14 01:40:54 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
final response = await _dio.post(
|
|
|
|
_config.getHttpEndpoint() + "/users/two-factor/remove",
|
|
|
|
data: {
|
|
|
|
"sessionID": sessionID,
|
|
|
|
"secret": secret,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
2023-04-07 06:33:40 +00:00
|
|
|
showShortToast(
|
2023-04-08 04:17:44 +00:00
|
|
|
context,
|
|
|
|
S.of(context).twofactorAuthenticationSuccessfullyReset,
|
|
|
|
);
|
2023-03-14 01:40:54 +00:00
|
|
|
await _saveConfiguration(response);
|
|
|
|
Navigator.of(context).pushAndRemoveUntil(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return const PasswordReentryPage();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(route) => route.isFirst,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} on DioError catch (e) {
|
|
|
|
_logger.severe(e);
|
|
|
|
if (e.response != null && e.response!.statusCode == 404) {
|
|
|
|
showToast(context, "Session expired");
|
|
|
|
Navigator.of(context).pushAndRemoveUntil(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return const LoginPage();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(route) => route.isFirst,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
showErrorDialog(
|
|
|
|
context,
|
2023-04-07 06:33:40 +00:00
|
|
|
S.of(context).oops,
|
|
|
|
S.of(context).somethingWentWrongPleaseTryAgain,
|
2023-03-14 01:40:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
_logger.severe(e);
|
|
|
|
showErrorDialog(
|
|
|
|
context,
|
2023-04-07 06:33:40 +00:00
|
|
|
S.of(context).oops,
|
|
|
|
S.of(context).somethingWentWrongPleaseTryAgain,
|
2023-03-14 01:40:54 +00:00
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
await dialog.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-15 13:42:26 +00:00
|
|
|
Future<void> setupTwoFactor(BuildContext context, Completer completer) async {
|
2023-04-07 06:33:40 +00:00
|
|
|
final dialog = createProgressDialog(context, S.of(context).pleaseWait);
|
2021-06-29 09:48:01 +00:00
|
|
|
await dialog.show();
|
|
|
|
try {
|
2022-10-14 09:54:57 +00:00
|
|
|
final response = await _enteDio.post("/users/two-factor/setup");
|
2021-06-29 09:48:01 +00:00
|
|
|
await dialog.hide();
|
2022-11-06 10:36:33 +00:00
|
|
|
unawaited(
|
|
|
|
routeToPage(
|
|
|
|
context,
|
|
|
|
TwoFactorSetupPage(
|
|
|
|
response.data["secretCode"],
|
|
|
|
response.data["qrCode"],
|
2022-11-15 13:42:26 +00:00
|
|
|
completer,
|
2022-11-06 10:36:33 +00:00
|
|
|
),
|
2022-06-11 08:23:52 +00:00
|
|
|
),
|
|
|
|
);
|
2022-07-13 03:42:37 +00:00
|
|
|
} catch (e) {
|
2021-06-29 09:48:01 +00:00
|
|
|
await dialog.hide();
|
2022-07-13 03:42:37 +00:00
|
|
|
_logger.severe("Failed to setup tfa", e);
|
2022-11-15 13:42:26 +00:00
|
|
|
completer.complete();
|
2021-07-22 18:41:58 +00:00
|
|
|
rethrow;
|
2021-06-29 09:48:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 11:13:55 +00:00
|
|
|
Future<bool> enableTwoFactor(
|
2022-06-11 08:23:52 +00:00
|
|
|
BuildContext context,
|
|
|
|
String secret,
|
|
|
|
String code,
|
|
|
|
) async {
|
2023-03-14 01:40:54 +00:00
|
|
|
Uint8List recoveryKey;
|
|
|
|
try {
|
|
|
|
recoveryKey = await getOrCreateRecoveryKey(context);
|
|
|
|
} catch (e) {
|
|
|
|
showGenericErrorDialog(context: context);
|
|
|
|
return false;
|
|
|
|
}
|
2023-04-07 06:33:40 +00:00
|
|
|
final dialog = createProgressDialog(context, S.of(context).verifying);
|
2021-06-29 15:03:54 +00:00
|
|
|
await dialog.show();
|
2023-03-14 01:40:54 +00:00
|
|
|
final encryptionResult =
|
|
|
|
CryptoUtil.encryptSync(CryptoUtil.base642bin(secret), recoveryKey);
|
2021-06-29 09:48:01 +00:00
|
|
|
try {
|
2022-10-14 09:54:57 +00:00
|
|
|
await _enteDio.post(
|
|
|
|
"/users/two-factor/enable",
|
2023-03-14 01:40:54 +00:00
|
|
|
data: {
|
|
|
|
"code": code,
|
|
|
|
"encryptedTwoFactorSecret":
|
|
|
|
CryptoUtil.bin2base64(encryptionResult.encryptedData!),
|
|
|
|
"twoFactorSecretDecryptionNonce":
|
|
|
|
CryptoUtil.bin2base64(encryptionResult.nonce!),
|
|
|
|
},
|
2021-06-29 09:48:01 +00:00
|
|
|
);
|
|
|
|
await dialog.hide();
|
|
|
|
Navigator.pop(context);
|
|
|
|
Bus.instance.fire(TwoFactorStatusChangeEvent(true));
|
2021-06-29 11:13:55 +00:00
|
|
|
return true;
|
2021-06-29 09:48:01 +00:00
|
|
|
} catch (e, s) {
|
|
|
|
await dialog.hide();
|
|
|
|
_logger.severe(e, s);
|
|
|
|
if (e is DioError) {
|
2022-12-30 10:03:50 +00:00
|
|
|
if (e.response != null && e.response!.statusCode == 401) {
|
2022-06-11 08:23:52 +00:00
|
|
|
showErrorDialog(
|
|
|
|
context,
|
2023-04-07 06:33:40 +00:00
|
|
|
S.of(context).incorrectCode,
|
|
|
|
S.of(context).pleaseVerifyTheCodeYouHaveEntered,
|
2022-06-11 08:23:52 +00:00
|
|
|
);
|
2021-06-29 11:13:55 +00:00
|
|
|
return false;
|
2021-06-29 09:48:01 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-11 08:23:52 +00:00
|
|
|
showErrorDialog(
|
|
|
|
context,
|
2023-04-07 06:33:40 +00:00
|
|
|
S.of(context).somethingWentWrong,
|
|
|
|
S.of(context).pleaseContactSupportIfTheProblemPersists,
|
2022-06-11 08:23:52 +00:00
|
|
|
);
|
2021-06-29 09:48:01 +00:00
|
|
|
}
|
2021-06-29 11:13:55 +00:00
|
|
|
return false;
|
2021-06-29 09:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> disableTwoFactor(BuildContext context) async {
|
2023-04-07 06:33:40 +00:00
|
|
|
final dialog = createProgressDialog(
|
2023-04-08 04:17:44 +00:00
|
|
|
context,
|
|
|
|
S.of(context).disablingTwofactorAuthentication,
|
|
|
|
);
|
2021-06-29 09:48:01 +00:00
|
|
|
await dialog.show();
|
|
|
|
try {
|
2022-10-14 09:54:57 +00:00
|
|
|
await _enteDio.post(
|
|
|
|
"/users/two-factor/disable",
|
2021-06-29 09:48:01 +00:00
|
|
|
);
|
|
|
|
await dialog.hide();
|
2022-11-16 12:59:25 +00:00
|
|
|
Bus.instance.fire(TwoFactorStatusChangeEvent(false));
|
2022-11-06 10:36:33 +00:00
|
|
|
unawaited(
|
2022-12-26 05:14:36 +00:00
|
|
|
showShortToast(
|
2022-11-06 10:36:33 +00:00
|
|
|
context,
|
2023-04-07 06:33:40 +00:00
|
|
|
S.of(context).twofactorAuthenticationHasBeenDisabled,
|
2022-11-06 10:36:33 +00:00
|
|
|
),
|
|
|
|
);
|
2022-07-13 03:42:37 +00:00
|
|
|
} catch (e) {
|
2021-06-29 09:48:01 +00:00
|
|
|
await dialog.hide();
|
2022-07-13 03:42:37 +00:00
|
|
|
_logger.severe("Failed to disabled 2FA", e);
|
2022-11-06 10:36:33 +00:00
|
|
|
await showErrorDialog(
|
2022-06-11 08:23:52 +00:00
|
|
|
context,
|
2023-04-07 06:33:40 +00:00
|
|
|
S.of(context).somethingWentWrong,
|
|
|
|
S.of(context).pleaseContactSupportIfTheProblemPersists,
|
2022-06-11 08:23:52 +00:00
|
|
|
);
|
2021-06-29 09:48:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> fetchTwoFactorStatus() async {
|
|
|
|
try {
|
2022-10-14 09:54:57 +00:00
|
|
|
final response = await _enteDio.get("/users/two-factor/status");
|
2022-11-16 12:20:02 +00:00
|
|
|
setTwoFactor(value: response.data["status"]);
|
2021-06-29 09:48:01 +00:00
|
|
|
return response.data["status"];
|
2022-07-13 03:42:37 +00:00
|
|
|
} catch (e) {
|
|
|
|
_logger.severe("Failed to fetch 2FA status", e);
|
2021-07-22 18:41:58 +00:00
|
|
|
rethrow;
|
2021-06-29 09:48:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 15:03:54 +00:00
|
|
|
Future<Uint8List> getOrCreateRecoveryKey(BuildContext context) async {
|
2022-12-30 10:03:50 +00:00
|
|
|
final String? encryptedRecoveryKey =
|
|
|
|
_config.getKeyAttributes()!.recoveryKeyEncryptedWithMasterKey;
|
2021-06-29 15:03:54 +00:00
|
|
|
if (encryptedRecoveryKey == null || encryptedRecoveryKey.isEmpty) {
|
2023-04-07 06:33:40 +00:00
|
|
|
final dialog = createProgressDialog(context, S.of(context).pleaseWait);
|
2021-06-29 15:03:54 +00:00
|
|
|
await dialog.show();
|
|
|
|
try {
|
|
|
|
final keyAttributes = await _config.createNewRecoveryKey();
|
|
|
|
await setRecoveryKey(keyAttributes);
|
|
|
|
await dialog.hide();
|
|
|
|
} catch (e, s) {
|
|
|
|
await dialog.hide();
|
|
|
|
_logger.severe(e, s);
|
2021-07-22 18:41:58 +00:00
|
|
|
rethrow;
|
2021-06-29 15:03:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
final recoveryKey = _config.getRecoveryKey();
|
|
|
|
return recoveryKey;
|
|
|
|
}
|
|
|
|
|
2022-12-30 10:03:50 +00:00
|
|
|
Future<String?> getPaymentToken() async {
|
2021-08-17 10:51:12 +00:00
|
|
|
try {
|
2022-10-14 09:54:57 +00:00
|
|
|
final response = await _enteDio.get("/users/payment-token");
|
2022-12-30 10:03:50 +00:00
|
|
|
if (response.statusCode == 200) {
|
2021-08-17 10:51:12 +00:00
|
|
|
return response.data["paymentToken"];
|
|
|
|
} else {
|
|
|
|
throw Exception("non 200 ok response");
|
|
|
|
}
|
2022-07-13 03:42:37 +00:00
|
|
|
} catch (e) {
|
|
|
|
_logger.severe("Failed to get payment token", e);
|
2021-08-17 10:51:12 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 10:15:08 +00:00
|
|
|
Future<String> getFamiliesToken() async {
|
|
|
|
try {
|
2022-10-14 09:32:51 +00:00
|
|
|
final response = await _enteDio.get("/users/families-token");
|
2022-12-30 10:03:50 +00:00
|
|
|
if (response.statusCode == 200) {
|
2022-04-20 10:15:08 +00:00
|
|
|
return response.data["familiesToken"];
|
|
|
|
} else {
|
|
|
|
throw Exception("non 200 ok response");
|
|
|
|
}
|
|
|
|
} catch (e, s) {
|
|
|
|
_logger.severe("failed to fetch families token", e, s);
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 06:30:44 +00:00
|
|
|
Future<void> _saveConfiguration(Response response) async {
|
|
|
|
await Configuration.instance.setUserID(response.data["id"]);
|
2021-06-12 09:44:00 +00:00
|
|
|
if (response.data["encryptedToken"] != null) {
|
2020-09-06 06:30:44 +00:00
|
|
|
await Configuration.instance
|
2021-06-12 09:44:00 +00:00
|
|
|
.setEncryptedToken(response.data["encryptedToken"]);
|
|
|
|
await Configuration.instance.setKeyAttributes(
|
2022-06-11 08:23:52 +00:00
|
|
|
KeyAttributes.fromMap(response.data["keyAttributes"]),
|
|
|
|
);
|
2021-06-12 09:44:00 +00:00
|
|
|
} else {
|
|
|
|
await Configuration.instance.setToken(response.data["token"]);
|
2020-08-15 01:22:14 +00:00
|
|
|
}
|
2020-07-31 22:58:30 +00:00
|
|
|
}
|
2022-11-16 12:20:02 +00:00
|
|
|
|
|
|
|
Future<void> setTwoFactor({
|
|
|
|
bool value = false,
|
|
|
|
bool fetchTwoFactorStatus = false,
|
|
|
|
}) async {
|
|
|
|
if (fetchTwoFactorStatus) {
|
|
|
|
value = await UserService.instance.fetchTwoFactorStatus();
|
|
|
|
}
|
|
|
|
_preferences.setBool(keyHasEnabledTwoFactor, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasEnabledTwoFactor() {
|
2022-12-30 10:03:50 +00:00
|
|
|
return _preferences.getBool(keyHasEnabledTwoFactor) ?? false;
|
2022-11-16 12:20:02 +00:00
|
|
|
}
|
2020-04-30 15:09:41 +00:00
|
|
|
}
|