ente/lib/services/user_service.dart

205 lines
6.3 KiB
Dart
Raw Normal View History

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';
import 'package:flutter/widgets.dart';
2020-05-02 16:28:54 +00:00
import 'package:logging/logging.dart';
import 'package:photos/core/configuration.dart';
2020-11-19 18:22:30 +00:00
import 'package:photos/core/network.dart';
2020-10-17 18:16:30 +00:00
import 'package:photos/db/public_keys_db.dart';
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-03-28 12:43:44 +00:00
import 'package:photos/models/set_keys_request.dart';
import 'package:photos/models/set_recovery_key_request.dart';
2020-08-25 06:00:19 +00:00
import 'package:photos/ui/ott_verification_page.dart';
2021-01-05 14:27:02 +00:00
import 'package:photos/ui/password_entry_page.dart';
import 'package:photos/ui/password_reentry_page.dart';
2020-08-25 06:00:19 +00:00
import 'package:photos/utils/dialog_util.dart';
import 'package:photos/utils/toast_util.dart';
2020-04-30 15:09:41 +00:00
2020-10-03 17:56:18 +00:00
class UserService {
2020-11-19 18:22:30 +00:00
final _dio = Network.instance.getDio();
2020-05-02 16:28:54 +00:00
final _logger = Logger("UserAuthenticator");
final _config = Configuration.instance;
2020-04-30 15:09:41 +00:00
2020-10-03 17:56:18 +00:00
UserService._privateConstructor();
2020-04-30 15:09:41 +00:00
2020-10-03 17:56:18 +00:00
static final UserService instance = UserService._privateConstructor();
2020-04-30 15:09:41 +00:00
2020-08-25 06:00:19 +00:00
Future<void> getOtt(BuildContext context, String email) async {
2021-01-08 17:02:41 +00:00
final dialog = createProgressDialog(context, "please wait...");
2020-08-25 06:00:19 +00:00
await dialog.show();
2020-08-28 23:50:18 +00:00
await _dio.get(
_config.getHttpEndpoint() + "/users/ott",
2020-08-25 06:00:19 +00:00
queryParameters: {
"email": email,
},
).catchError((e) async {
_logger.severe(e);
}).then((response) async {
await dialog.hide();
if (response != null) {
if (response.statusCode == 200) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return OTTVerificationPage();
},
),
);
} else if (response.statusCode == 403) {
showErrorDialog(
context,
2021-01-08 17:06:52 +00:00
"please wait...",
"we are currently not accepting new registrations. you have been added to the waitlist and we will let you know once we are ready for you.",
);
}
2020-08-25 06:00:19 +00:00
} else {
showGenericErrorDialog(context);
}
});
}
Future<String> getPublicKey(String email) async {
try {
final response = await _dio.get(
_config.getHttpEndpoint() + "/users/public-key",
queryParameters: {"email": email},
options: Options(
headers: {
"X-Auth-Token": _config.getToken(),
},
),
);
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;
} on DioError catch (e) {
_logger.info(e);
return null;
}
}
2020-08-25 23:02:43 +00:00
Future<void> getCredentials(BuildContext context, String ott) async {
2021-01-08 17:02:41 +00:00
final dialog = createProgressDialog(context, "please wait...");
2020-08-25 06:00:19 +00:00
await dialog.show();
try {
final response = await _dio.get(
_config.getHttpEndpoint() + "/users/credentials",
queryParameters: {
"email": _config.getEmail(),
"ott": ott,
},
);
2020-08-25 06:00:19 +00:00
await dialog.hide();
if (response != null && response.statusCode == 200) {
await _saveConfiguration(response);
2021-01-08 17:11:32 +00:00
showToast("email verification successful!");
var page;
if (Configuration.instance.getKeyAttributes() != null) {
2021-01-05 14:27:02 +00:00
page = PasswordReentryPage();
} else {
2021-01-05 14:27:02 +00:00
page = PasswordEntryPage();
}
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (BuildContext context) {
return page;
},
),
(route) => route.isFirst,
);
2020-08-25 06:00:19 +00:00
} else {
showErrorDialog(
2021-01-08 17:06:52 +00:00
context, "oops", "verification failed, please try again");
2020-08-25 06:00:19 +00:00
}
} catch (e) {
await dialog.hide();
_logger.severe(e);
showErrorDialog(context, "oops", "verification failed, please try again");
}
2020-08-25 06:00:19 +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();
2021-03-29 18:35:46 +00:00
await _dio.put(
2021-03-26 16:13:32 +00:00
_config.getHttpEndpoint() + "/users/attributes",
data: {
"name": name,
"keyAttributes": result.keyAttributes.toMap(),
},
2021-03-26 16:13:32 +00:00
options: Options(
headers: {
"X-Auth-Token": _config.getToken(),
},
),
);
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-03-29 18:35:46 +00:00
throw e;
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,
memLimit: keyAttributes.memLimit,
opsLimit: keyAttributes.opsLimit,
);
2021-04-01 14:26:08 +00:00
await _dio.put(
2021-03-28 12:43:44 +00:00
_config.getHttpEndpoint() + "/users/keys",
data: setKeyRequest.toMap(),
2021-03-26 16:13:32 +00:00
options: Options(
headers: {
"X-Auth-Token": _config.getToken(),
},
),
);
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-04-01 14:26:08 +00:00
throw e;
2021-03-26 16:13:32 +00:00
}
}
Future<void> setRecoveryKey(KeyAttributes keyAttributes) async {
try {
final setRecoveryKeyRequest = SetRecoveryKeyRequest(
keyAttributes.masterKeyEncryptedWithRecoveryKey,
keyAttributes.masterKeyDecryptionNonce,
keyAttributes.recoveryKeyEncryptedWithMasterKey,
keyAttributes.recoveryKeyDecryptionNonce,
);
await _dio.put(
_config.getHttpEndpoint() + "/users/recovery-key",
data: setRecoveryKeyRequest.toMap(),
options: Options(
headers: {
"X-Auth-Token": _config.getToken(),
},
),
);
await _config.setKeyAttributes(keyAttributes);
} catch (e) {
_logger.severe(e);
throw e;
}
}
Future<void> _saveConfiguration(Response response) async {
await Configuration.instance.setUserID(response.data["id"]);
await Configuration.instance.setToken(response.data["token"]);
final keyAttributes = response.data["keyAttributes"];
if (keyAttributes != null) {
await Configuration.instance
.setKeyAttributes(KeyAttributes.fromMap(keyAttributes));
2020-08-15 01:22:14 +00:00
}
}
2020-04-30 15:09:41 +00:00
}