|
@@ -1,9 +1,16 @@
|
|
import 'package:dio/dio.dart';
|
|
import 'package:dio/dio.dart';
|
|
|
|
+import 'package:flutter/material.dart';
|
|
|
|
+import 'package:flutter/widgets.dart';
|
|
import 'package:logging/logging.dart';
|
|
import 'package:logging/logging.dart';
|
|
import 'package:photos/core/configuration.dart';
|
|
import 'package:photos/core/configuration.dart';
|
|
import 'package:photos/core/event_bus.dart';
|
|
import 'package:photos/core/event_bus.dart';
|
|
|
|
|
|
import 'package:photos/events/user_authenticated_event.dart';
|
|
import 'package:photos/events/user_authenticated_event.dart';
|
|
|
|
+import 'package:photos/ui/ott_verification_page.dart';
|
|
|
|
+import 'package:photos/ui/passphrase_entry_page.dart';
|
|
|
|
+import 'package:photos/ui/passphrase_reentry_page.dart';
|
|
|
|
+import 'package:photos/utils/dialog_util.dart';
|
|
|
|
+import 'package:photos/utils/toast_util.dart';
|
|
|
|
|
|
class UserAuthenticator {
|
|
class UserAuthenticator {
|
|
final _dio = Dio();
|
|
final _dio = Dio();
|
|
@@ -14,6 +21,105 @@ class UserAuthenticator {
|
|
static final UserAuthenticator instance =
|
|
static final UserAuthenticator instance =
|
|
UserAuthenticator._privateConstructor();
|
|
UserAuthenticator._privateConstructor();
|
|
|
|
|
|
|
|
+ Future<void> getOtt(BuildContext context, String email) async {
|
|
|
|
+ final dialog = createProgressDialog(context, "Please wait...");
|
|
|
|
+ await dialog.show();
|
|
|
|
+ await Dio().get(
|
|
|
|
+ Configuration.instance.getHttpEndpoint() + "/users/ott",
|
|
|
|
+ queryParameters: {
|
|
|
|
+ "email": email,
|
|
|
|
+ },
|
|
|
|
+ ).catchError((e) async {
|
|
|
|
+ _logger.severe(e);
|
|
|
|
+ }).then((response) async {
|
|
|
|
+ await dialog.hide();
|
|
|
|
+ if (response != null && response.statusCode == 200) {
|
|
|
|
+ Navigator.of(context).push(
|
|
|
|
+ MaterialPageRoute(
|
|
|
|
+ builder: (BuildContext context) {
|
|
|
|
+ return OTTVerificationPage();
|
|
|
|
+ },
|
|
|
|
+ ),
|
|
|
|
+ );
|
|
|
|
+ } else {
|
|
|
|
+ showGenericErrorDialog(context);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Future<void> getCredentials(BuildContext context, String ott) async {
|
|
|
|
+ final dialog = createProgressDialog(context, "Please wait...");
|
|
|
|
+ await dialog.show();
|
|
|
|
+ await Dio().get(
|
|
|
|
+ Configuration.instance.getHttpEndpoint() + "/users/credentials",
|
|
|
|
+ queryParameters: {
|
|
|
|
+ "email": Configuration.instance.getEmail(),
|
|
|
|
+ "ott": ott,
|
|
|
|
+ },
|
|
|
|
+ ).catchError((e) async {
|
|
|
|
+ _logger.severe(e);
|
|
|
|
+ }).then((response) async {
|
|
|
|
+ await dialog.hide();
|
|
|
|
+ if (response != null && response.statusCode == 200) {
|
|
|
|
+ _saveConfiguration(response);
|
|
|
|
+ showToast("Email verification successful!");
|
|
|
|
+ var page;
|
|
|
|
+ if (Configuration.instance.getEncryptedKey() != null) {
|
|
|
|
+ page = PassphraseReentryPage();
|
|
|
|
+ } else {
|
|
|
|
+ page = PassphraseEntryPage();
|
|
|
|
+ }
|
|
|
|
+ Navigator.of(context).pushAndRemoveUntil(
|
|
|
|
+ MaterialPageRoute(
|
|
|
|
+ builder: (BuildContext context) {
|
|
|
|
+ return page;
|
|
|
|
+ },
|
|
|
|
+ ),
|
|
|
|
+ (route) => route.isFirst,
|
|
|
|
+ );
|
|
|
|
+ } else {
|
|
|
|
+ showErrorDialog(
|
|
|
|
+ context, "Oops.", "Verification failed, please try again.");
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Future<void> setPassphrase(BuildContext context, String passphrase) async {
|
|
|
|
+ final dialog = createProgressDialog(context, "Please wait...");
|
|
|
|
+ await dialog.show();
|
|
|
|
+ await Configuration.instance.generateAndSaveKey(passphrase);
|
|
|
|
+ await _dio
|
|
|
|
+ .put(
|
|
|
|
+ Configuration.instance.getHttpEndpoint() + "/users/encrypted-key",
|
|
|
|
+ data: {
|
|
|
|
+ "encryptedKey": Configuration.instance.getEncryptedKey(),
|
|
|
|
+ },
|
|
|
|
+ options: Options(
|
|
|
|
+ headers: {
|
|
|
|
+ "X-Auth-Token": Configuration.instance.getToken(),
|
|
|
|
+ },
|
|
|
|
+ ),
|
|
|
|
+ )
|
|
|
|
+ .catchError((e) async {
|
|
|
|
+ await dialog.hide();
|
|
|
|
+ Configuration.instance.setKey(null);
|
|
|
|
+ Configuration.instance.setEncryptedKey(null);
|
|
|
|
+ _logger.severe(e);
|
|
|
|
+ showGenericErrorDialog(context);
|
|
|
|
+ }).then((response) async {
|
|
|
|
+ await dialog.hide();
|
|
|
|
+ if (response != null && response.statusCode == 200) {
|
|
|
|
+ Bus.instance.fire(UserAuthenticatedEvent());
|
|
|
|
+ Navigator.of(context).popUntil((route) => route.isFirst);
|
|
|
|
+ } else {
|
|
|
|
+ Configuration.instance.setKey(null);
|
|
|
|
+ Configuration.instance.setEncryptedKey(null);
|
|
|
|
+ showGenericErrorDialog(context);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @deprecated
|
|
Future<bool> login(String username, String password) {
|
|
Future<bool> login(String username, String password) {
|
|
return _dio.post(
|
|
return _dio.post(
|
|
Configuration.instance.getHttpEndpoint() + "/users/authenticate",
|
|
Configuration.instance.getHttpEndpoint() + "/users/authenticate",
|
|
@@ -22,7 +128,7 @@ class UserAuthenticator {
|
|
"password": password,
|
|
"password": password,
|
|
}).then((response) {
|
|
}).then((response) {
|
|
if (response.statusCode == 200 && response.data != null) {
|
|
if (response.statusCode == 200 && response.data != null) {
|
|
- _saveConfiguration(username, password, response);
|
|
|
|
|
|
+ _saveConfiguration(response);
|
|
Bus.instance.fire(UserAuthenticatedEvent());
|
|
Bus.instance.fire(UserAuthenticatedEvent());
|
|
return true;
|
|
return true;
|
|
} else {
|
|
} else {
|
|
@@ -34,6 +140,7 @@ class UserAuthenticator {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @deprecated
|
|
Future<bool> create(String username, String password) {
|
|
Future<bool> create(String username, String password) {
|
|
return _dio
|
|
return _dio
|
|
.post(Configuration.instance.getHttpEndpoint() + "/users", data: {
|
|
.post(Configuration.instance.getHttpEndpoint() + "/users", data: {
|
|
@@ -41,7 +148,7 @@ class UserAuthenticator {
|
|
"password": password,
|
|
"password": password,
|
|
}).then((response) {
|
|
}).then((response) {
|
|
if (response.statusCode == 200 && response.data != null) {
|
|
if (response.statusCode == 200 && response.data != null) {
|
|
- _saveConfiguration(username, password, response);
|
|
|
|
|
|
+ _saveConfiguration(response);
|
|
return true;
|
|
return true;
|
|
} else {
|
|
} else {
|
|
if (response.data != null && response.data["message"] != null) {
|
|
if (response.data != null && response.data["message"] != null) {
|
|
@@ -68,9 +175,7 @@ class UserAuthenticator {
|
|
);
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
- void _saveConfiguration(String username, String password, Response response) {
|
|
|
|
- Configuration.instance.setUsername(username);
|
|
|
|
- Configuration.instance.setPassword(password);
|
|
|
|
|
|
+ void _saveConfiguration(Response response) {
|
|
Configuration.instance.setUserID(response.data["id"]);
|
|
Configuration.instance.setUserID(response.data["id"]);
|
|
Configuration.instance.setToken(response.data["token"]);
|
|
Configuration.instance.setToken(response.data["token"]);
|
|
final String encryptedKey = response.data["encryptedKey"];
|
|
final String encryptedKey = response.data["encryptedKey"];
|