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';
|
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';
|
2020-09-05 08:53:23 +00:00
|
|
|
import 'package:photos/models/key_attributes.dart';
|
2020-10-18 21:39:55 +00:00
|
|
|
import 'package:photos/models/public_key.dart';
|
2021-01-06 11:56:03 +00:00
|
|
|
import 'package:photos/models/subscription.dart';
|
|
|
|
import 'package:photos/services/billing_service.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';
|
2021-01-06 16:09:42 +00:00
|
|
|
import 'package:photos/ui/subscription_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
|
|
|
|
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");
|
2020-10-21 21:29:16 +00:00
|
|
|
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(
|
2020-10-21 21:29:16 +00:00
|
|
|
_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();
|
2020-11-17 06:18:46 +00:00
|
|
|
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-11-17 06:18:46 +00:00
|
|
|
);
|
|
|
|
}
|
2020-08-25 06:00:19 +00:00
|
|
|
} else {
|
|
|
|
showGenericErrorDialog(context);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-17 18:04:41 +00:00
|
|
|
Future<String> getPublicKey(String email) async {
|
2020-10-09 21:45:19 +00:00
|
|
|
try {
|
|
|
|
final response = await _dio.get(
|
2020-10-21 21:29:16 +00:00
|
|
|
_config.getHttpEndpoint() + "/users/public-key",
|
2020-10-17 18:04:41 +00:00
|
|
|
queryParameters: {"email": email},
|
2020-10-09 21:45:19 +00:00
|
|
|
options: Options(
|
|
|
|
headers: {
|
2020-10-21 21:29:16 +00:00
|
|
|
"X-Auth-Token": _config.getToken(),
|
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) {
|
|
|
|
_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();
|
2021-03-21 12:04:57 +00:00
|
|
|
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();
|
2020-08-26 00:26:07 +00:00
|
|
|
if (response != null && response.statusCode == 200) {
|
2020-09-06 06:30:44 +00:00
|
|
|
await _saveConfiguration(response);
|
2021-01-08 17:11:32 +00:00
|
|
|
showToast("email verification successful!");
|
2020-08-26 02:34:13 +00:00
|
|
|
var page;
|
2020-10-02 21:45:26 +00:00
|
|
|
if (Configuration.instance.getKeyAttributes() != null) {
|
2021-01-05 14:27:02 +00:00
|
|
|
page = PasswordReentryPage();
|
2020-08-26 01:43:10 +00:00
|
|
|
} else {
|
2021-01-05 14:27:02 +00:00
|
|
|
page = PasswordEntryPage();
|
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(
|
2021-01-08 17:06:52 +00:00
|
|
|
context, "oops", "verification failed, please try again");
|
2020-08-25 06:00:19 +00:00
|
|
|
}
|
2021-03-21 12:04:57 +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-01-05 14:27:02 +00:00
|
|
|
Future<void> setupAttributes(BuildContext context, String password) async {
|
2021-01-08 17:02:41 +00:00
|
|
|
final dialog = createProgressDialog(context, "please wait...");
|
2020-08-26 01:43:10 +00:00
|
|
|
await dialog.show();
|
2021-03-26 16:13:32 +00:00
|
|
|
try {
|
|
|
|
final result = await _config.generateKey(password);
|
|
|
|
final name = _config.getName();
|
|
|
|
final response = await _dio.put(
|
|
|
|
_config.getHttpEndpoint() + "/users/attributes",
|
|
|
|
data: {
|
|
|
|
"name": name,
|
|
|
|
"keyAttributes": result.keyAttributes.toMap(),
|
2020-08-26 01:43:10 +00:00
|
|
|
},
|
2021-03-26 16:13:32 +00:00
|
|
|
options: Options(
|
|
|
|
headers: {
|
|
|
|
"X-Auth-Token": _config.getToken(),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2020-08-26 01:43:10 +00:00
|
|
|
await dialog.hide();
|
|
|
|
if (response != null && response.statusCode == 200) {
|
2020-10-21 21:29:16 +00:00
|
|
|
await _config.setKey(result.privateKeyAttributes.key);
|
|
|
|
await _config.setSecretKey(result.privateKeyAttributes.secretKey);
|
|
|
|
await _config.setKeyAttributes(result.keyAttributes);
|
2021-01-06 16:09:42 +00:00
|
|
|
Navigator.of(context).pushAndRemoveUntil(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (BuildContext context) {
|
2021-02-25 15:14:31 +00:00
|
|
|
return SubscriptionPage(isOnboarding: true);
|
2021-01-06 16:09:42 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
(route) => route.isFirst,
|
|
|
|
);
|
2020-08-26 01:43:10 +00:00
|
|
|
} else {
|
|
|
|
showGenericErrorDialog(context);
|
|
|
|
}
|
2021-03-26 16:13:32 +00:00
|
|
|
} catch (e) {
|
|
|
|
await dialog.hide();
|
|
|
|
_logger.severe(e);
|
|
|
|
showGenericErrorDialog(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> updateKeyAttributes(
|
|
|
|
BuildContext context, String password) async {
|
|
|
|
final dialog = createProgressDialog(context, "please wait...");
|
|
|
|
await dialog.show();
|
|
|
|
try {
|
|
|
|
final keyAttributes = await _config.updatePassword(password);
|
|
|
|
final response = await _dio.put(
|
|
|
|
_config.getHttpEndpoint() + "/users/key-attributes",
|
|
|
|
data: keyAttributes.toMap(),
|
|
|
|
options: Options(
|
|
|
|
headers: {
|
|
|
|
"X-Auth-Token": _config.getToken(),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
await dialog.hide();
|
|
|
|
if (response != null && response.statusCode == 200) {
|
|
|
|
await _config.setKeyAttributes(keyAttributes);
|
|
|
|
Navigator.of(context).pushAndRemoveUntil(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return SubscriptionPage(isOnboarding: true);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(route) => route.isFirst,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
showGenericErrorDialog(context);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
await dialog.hide();
|
|
|
|
_logger.severe(e);
|
|
|
|
showGenericErrorDialog(context);
|
|
|
|
}
|
2020-08-26 01:43:10 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 06:30:44 +00:00
|
|
|
Future<void> _saveConfiguration(Response response) async {
|
|
|
|
await Configuration.instance.setUserID(response.data["id"]);
|
|
|
|
await Configuration.instance.setToken(response.data["token"]);
|
2020-09-05 09:38:03 +00:00
|
|
|
final keyAttributes = response.data["keyAttributes"];
|
2020-09-05 08:53:23 +00:00
|
|
|
if (keyAttributes != null) {
|
2020-09-06 06:30:44 +00:00
|
|
|
await Configuration.instance
|
2020-09-05 08:53:23 +00:00
|
|
|
.setKeyAttributes(KeyAttributes.fromMap(keyAttributes));
|
2020-08-15 01:22:14 +00:00
|
|
|
}
|
2021-01-06 11:56:03 +00:00
|
|
|
final subscription = response.data["subscription"];
|
|
|
|
if (subscription != null) {
|
|
|
|
await BillingService.instance
|
|
|
|
.setSubscription(Subscription.fromMap(subscription));
|
|
|
|
}
|
2020-07-31 22:58:30 +00:00
|
|
|
}
|
2020-04-30 15:09:41 +00:00
|
|
|
}
|