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';
|
2020-05-04 20:08:20 +00:00
|
|
|
import 'package:photos/core/configuration.dart';
|
|
|
|
import 'package:photos/core/event_bus.dart';
|
2020-04-30 15:09:41 +00:00
|
|
|
|
2020-05-04 20:08:20 +00:00
|
|
|
import 'package:photos/events/user_authenticated_event.dart';
|
2020-08-25 06:00:19 +00:00
|
|
|
import 'package:photos/ui/ott_verification_page.dart';
|
2020-08-26 01:43:10 +00:00
|
|
|
import 'package:photos/ui/passphrase_entry_page.dart';
|
2020-08-26 02:34:13 +00:00
|
|
|
import 'package:photos/ui/passphrase_reentry_page.dart';
|
2020-08-25 06:00:19 +00:00
|
|
|
import 'package:photos/utils/dialog_util.dart';
|
2020-08-26 02:34:13 +00:00
|
|
|
import 'package:photos/utils/toast_util.dart';
|
2020-04-30 15:09:41 +00:00
|
|
|
|
|
|
|
class UserAuthenticator {
|
|
|
|
final _dio = Dio();
|
2020-05-02 16:28:54 +00:00
|
|
|
final _logger = Logger("UserAuthenticator");
|
2020-04-30 15:09:41 +00:00
|
|
|
|
|
|
|
UserAuthenticator._privateConstructor();
|
|
|
|
|
|
|
|
static final UserAuthenticator instance =
|
|
|
|
UserAuthenticator._privateConstructor();
|
|
|
|
|
2020-08-25 06:00:19 +00:00
|
|
|
Future<void> getOtt(BuildContext context, String email) async {
|
|
|
|
final dialog = createProgressDialog(context, "Please wait...");
|
|
|
|
await dialog.show();
|
2020-08-28 23:50:18 +00:00
|
|
|
await _dio.get(
|
2020-08-25 06:00:19 +00:00
|
|
|
Configuration.instance.getHttpEndpoint() + "/users/ott",
|
|
|
|
queryParameters: {
|
|
|
|
"email": email,
|
|
|
|
},
|
|
|
|
).catchError((e) async {
|
|
|
|
_logger.severe(e);
|
|
|
|
}).then((response) async {
|
|
|
|
await dialog.hide();
|
2020-08-26 02:34:13 +00:00
|
|
|
if (response != null && response.statusCode == 200) {
|
2020-08-25 06:00:19 +00:00
|
|
|
Navigator.of(context).push(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (BuildContext context) {
|
2020-08-25 23:02:43 +00:00
|
|
|
return OTTVerificationPage();
|
2020-08-25 06:00:19 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
showGenericErrorDialog(context);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-25 23:02:43 +00:00
|
|
|
Future<void> getCredentials(BuildContext context, String ott) async {
|
2020-08-25 06:00:19 +00:00
|
|
|
final dialog = createProgressDialog(context, "Please wait...");
|
|
|
|
await dialog.show();
|
2020-08-28 23:50:18 +00:00
|
|
|
await _dio.get(
|
2020-08-25 06:00:19 +00:00
|
|
|
Configuration.instance.getHttpEndpoint() + "/users/credentials",
|
|
|
|
queryParameters: {
|
2020-08-25 23:02:43 +00:00
|
|
|
"email": Configuration.instance.getEmail(),
|
2020-08-25 06:00:19 +00:00
|
|
|
"ott": ott,
|
|
|
|
},
|
|
|
|
).catchError((e) async {
|
|
|
|
_logger.severe(e);
|
|
|
|
}).then((response) async {
|
|
|
|
await dialog.hide();
|
2020-08-26 00:26:07 +00:00
|
|
|
if (response != null && response.statusCode == 200) {
|
2020-08-25 23:02:43 +00:00
|
|
|
_saveConfiguration(response);
|
2020-08-26 02:34:13 +00:00
|
|
|
showToast("Email verification successful!");
|
|
|
|
var page;
|
2020-08-26 01:43:10 +00:00
|
|
|
if (Configuration.instance.getEncryptedKey() != null) {
|
2020-08-26 02:34:13 +00:00
|
|
|
page = PassphraseReentryPage();
|
2020-08-26 01:43:10 +00:00
|
|
|
} else {
|
2020-08-26 02:34:13 +00:00
|
|
|
page = PassphraseEntryPage();
|
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;
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(route) => route.isFirst,
|
|
|
|
);
|
2020-08-25 06:00:19 +00:00
|
|
|
} else {
|
|
|
|
showErrorDialog(
|
|
|
|
context, "Oops.", "Verification failed, please try again.");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-26 01:43:10 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-26 00:26:07 +00:00
|
|
|
@deprecated
|
2020-04-30 15:09:41 +00:00
|
|
|
Future<bool> login(String username, String password) {
|
|
|
|
return _dio.post(
|
2020-05-17 12:39:38 +00:00
|
|
|
Configuration.instance.getHttpEndpoint() + "/users/authenticate",
|
|
|
|
data: {
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
|
|
|
}).then((response) {
|
2020-04-30 15:09:41 +00:00
|
|
|
if (response.statusCode == 200 && response.data != null) {
|
2020-08-25 23:02:43 +00:00
|
|
|
_saveConfiguration(response);
|
2020-04-30 15:09:41 +00:00
|
|
|
Bus.instance.fire(UserAuthenticatedEvent());
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}).catchError((e) {
|
2020-05-02 16:28:54 +00:00
|
|
|
_logger.severe(e.toString());
|
2020-04-30 15:09:41 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
2020-04-30 21:51:42 +00:00
|
|
|
|
2020-08-26 00:26:07 +00:00
|
|
|
@deprecated
|
2020-04-30 21:51:42 +00:00
|
|
|
Future<bool> create(String username, String password) {
|
2020-05-17 12:39:38 +00:00
|
|
|
return _dio
|
|
|
|
.post(Configuration.instance.getHttpEndpoint() + "/users", data: {
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
|
|
|
}).then((response) {
|
2020-04-30 21:51:42 +00:00
|
|
|
if (response.statusCode == 200 && response.data != null) {
|
2020-08-25 23:02:43 +00:00
|
|
|
_saveConfiguration(response);
|
2020-04-30 21:51:42 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if (response.data != null && response.data["message"] != null) {
|
|
|
|
throw Exception(response.data["message"]);
|
|
|
|
} else {
|
|
|
|
throw Exception("Something went wrong");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).catchError((e) {
|
2020-05-02 16:28:54 +00:00
|
|
|
_logger.severe(e.toString());
|
2020-04-30 21:51:42 +00:00
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
}
|
2020-07-31 22:58:30 +00:00
|
|
|
|
2020-08-15 01:22:14 +00:00
|
|
|
Future<void> setEncryptedKeyOnServer() {
|
|
|
|
return _dio.put(
|
|
|
|
Configuration.instance.getHttpEndpoint() + "/users/encrypted-key",
|
|
|
|
data: {
|
|
|
|
"encryptedKey": Configuration.instance.getEncryptedKey(),
|
|
|
|
},
|
|
|
|
options: Options(headers: {
|
|
|
|
"X-Auth-Token": Configuration.instance.getToken(),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-25 23:02:43 +00:00
|
|
|
void _saveConfiguration(Response response) {
|
2020-07-31 22:58:30 +00:00
|
|
|
Configuration.instance.setUserID(response.data["id"]);
|
|
|
|
Configuration.instance.setToken(response.data["token"]);
|
2020-08-15 01:22:14 +00:00
|
|
|
final String encryptedKey = response.data["encryptedKey"];
|
|
|
|
if (encryptedKey != null && encryptedKey.isNotEmpty) {
|
|
|
|
Configuration.instance.setEncryptedKey(encryptedKey);
|
|
|
|
}
|
2020-07-31 22:58:30 +00:00
|
|
|
}
|
2020-04-30 15:09:41 +00:00
|
|
|
}
|