user_service.dart 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import 'package:dio/dio.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/widgets.dart';
  4. import 'package:logging/logging.dart';
  5. import 'package:photos/core/configuration.dart';
  6. import 'package:photos/core/network.dart';
  7. import 'package:photos/db/public_keys_db.dart';
  8. import 'package:photos/models/key_attributes.dart';
  9. import 'package:photos/models/key_gen_result.dart';
  10. import 'package:photos/models/public_key.dart';
  11. import 'package:photos/models/set_keys_request.dart';
  12. import 'package:photos/models/set_recovery_key_request.dart';
  13. import 'package:photos/ui/login_page.dart';
  14. import 'package:photos/ui/ott_verification_page.dart';
  15. import 'package:photos/ui/password_entry_page.dart';
  16. import 'package:photos/ui/password_reentry_page.dart';
  17. import 'package:photos/ui/two_factor_authentication_page.dart';
  18. import 'package:photos/utils/dialog_util.dart';
  19. import 'package:photos/utils/toast_util.dart';
  20. class UserService {
  21. final _dio = Network.instance.getDio();
  22. final _logger = Logger("UserAuthenticator");
  23. final _config = Configuration.instance;
  24. UserService._privateConstructor();
  25. static final UserService instance = UserService._privateConstructor();
  26. Future<void> getOtt(BuildContext context, String email) async {
  27. final dialog = createProgressDialog(context, "please wait...");
  28. await dialog.show();
  29. await _dio.get(
  30. _config.getHttpEndpoint() + "/users/ott",
  31. queryParameters: {
  32. "email": email,
  33. },
  34. ).catchError((e) async {
  35. _logger.severe(e);
  36. }).then((response) async {
  37. await dialog.hide();
  38. if (response != null) {
  39. if (response.statusCode == 200) {
  40. Navigator.of(context).push(
  41. MaterialPageRoute(
  42. builder: (BuildContext context) {
  43. return OTTVerificationPage();
  44. },
  45. ),
  46. );
  47. } else if (response.statusCode == 403) {
  48. showErrorDialog(
  49. context,
  50. "please wait...",
  51. "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.",
  52. );
  53. }
  54. } else {
  55. showGenericErrorDialog(context);
  56. }
  57. });
  58. }
  59. Future<String> getPublicKey(String email) async {
  60. try {
  61. final response = await _dio.get(
  62. _config.getHttpEndpoint() + "/users/public-key",
  63. queryParameters: {"email": email},
  64. options: Options(
  65. headers: {
  66. "X-Auth-Token": _config.getToken(),
  67. },
  68. ),
  69. );
  70. final publicKey = response.data["publicKey"];
  71. await PublicKeysDB.instance.setKey(PublicKey(email, publicKey));
  72. return publicKey;
  73. } on DioError catch (e) {
  74. _logger.info(e);
  75. return null;
  76. }
  77. }
  78. Future<void> verifyEmail(BuildContext context, String ott) async {
  79. final dialog = createProgressDialog(context, "please wait...");
  80. await dialog.show();
  81. try {
  82. final response = await _dio.post(
  83. _config.getHttpEndpoint() + "/users/verify-email",
  84. data: {
  85. "email": _config.getEmail(),
  86. "ott": ott,
  87. },
  88. );
  89. await dialog.hide();
  90. if (response != null && response.statusCode == 200) {
  91. showToast("email verification successful!");
  92. var page;
  93. final String twoFASessionID = response.data["twoFactorSessionID"];
  94. if (twoFASessionID != null && twoFASessionID.isNotEmpty) {
  95. page = TwoFactorAuthenticationPage(twoFASessionID);
  96. } else {
  97. await _saveConfiguration(response);
  98. if (Configuration.instance.getEncryptedToken() != null) {
  99. page = PasswordReentryPage();
  100. } else {
  101. page = PasswordEntryPage();
  102. }
  103. }
  104. Navigator.of(context).pushAndRemoveUntil(
  105. MaterialPageRoute(
  106. builder: (BuildContext context) {
  107. return page;
  108. },
  109. ),
  110. (route) => route.isFirst,
  111. );
  112. } else {
  113. showErrorDialog(
  114. context, "oops", "verification failed, please try again");
  115. }
  116. } catch (e) {
  117. await dialog.hide();
  118. _logger.severe(e);
  119. showErrorDialog(context, "oops", "verification failed, please try again");
  120. }
  121. }
  122. Future<void> setAttributes(KeyGenResult result) async {
  123. try {
  124. final name = _config.getName();
  125. await _dio.put(
  126. _config.getHttpEndpoint() + "/users/attributes",
  127. data: {
  128. "name": name,
  129. "keyAttributes": result.keyAttributes.toMap(),
  130. },
  131. options: Options(
  132. headers: {
  133. "X-Auth-Token": _config.getToken(),
  134. },
  135. ),
  136. );
  137. await _config.setKey(result.privateKeyAttributes.key);
  138. await _config.setSecretKey(result.privateKeyAttributes.secretKey);
  139. await _config.setKeyAttributes(result.keyAttributes);
  140. } catch (e) {
  141. _logger.severe(e);
  142. throw e;
  143. }
  144. }
  145. Future<void> updateKeyAttributes(KeyAttributes keyAttributes) async {
  146. try {
  147. final setKeyRequest = SetKeysRequest(
  148. kekSalt: keyAttributes.kekSalt,
  149. encryptedKey: keyAttributes.encryptedKey,
  150. keyDecryptionNonce: keyAttributes.keyDecryptionNonce,
  151. memLimit: keyAttributes.memLimit,
  152. opsLimit: keyAttributes.opsLimit,
  153. );
  154. await _dio.put(
  155. _config.getHttpEndpoint() + "/users/keys",
  156. data: setKeyRequest.toMap(),
  157. options: Options(
  158. headers: {
  159. "X-Auth-Token": _config.getToken(),
  160. },
  161. ),
  162. );
  163. await _config.setKeyAttributes(keyAttributes);
  164. } catch (e) {
  165. _logger.severe(e);
  166. throw e;
  167. }
  168. }
  169. Future<void> setRecoveryKey(KeyAttributes keyAttributes) async {
  170. try {
  171. final setRecoveryKeyRequest = SetRecoveryKeyRequest(
  172. keyAttributes.masterKeyEncryptedWithRecoveryKey,
  173. keyAttributes.masterKeyDecryptionNonce,
  174. keyAttributes.recoveryKeyEncryptedWithMasterKey,
  175. keyAttributes.recoveryKeyDecryptionNonce,
  176. );
  177. await _dio.put(
  178. _config.getHttpEndpoint() + "/users/recovery-key",
  179. data: setRecoveryKeyRequest.toMap(),
  180. options: Options(
  181. headers: {
  182. "X-Auth-Token": _config.getToken(),
  183. },
  184. ),
  185. );
  186. await _config.setKeyAttributes(keyAttributes);
  187. } catch (e) {
  188. _logger.severe(e);
  189. throw e;
  190. }
  191. }
  192. Future<void> verifyTwoFactor(
  193. BuildContext context, String sessionID, String code) async {
  194. final dialog = createProgressDialog(context, "authenticating...");
  195. await dialog.show();
  196. try {
  197. final response = await _dio.post(
  198. _config.getHttpEndpoint() + "/users/two-factor/verify",
  199. data: {
  200. "sessionID": sessionID,
  201. "code": code,
  202. },
  203. );
  204. await dialog.hide();
  205. if (response != null && response.statusCode == 200) {
  206. showToast("authentication successful!");
  207. await _saveConfiguration(response);
  208. Navigator.of(context).pushAndRemoveUntil(
  209. MaterialPageRoute(
  210. builder: (BuildContext context) {
  211. return PasswordReentryPage();
  212. },
  213. ),
  214. (route) => route.isFirst,
  215. );
  216. } else {
  217. showErrorDialog(
  218. context, "oops", "authentication failed, please try again");
  219. }
  220. } on DioError catch (e) {
  221. await dialog.hide();
  222. _logger.severe(e);
  223. if (e.response != null && e.response.statusCode == 404) {
  224. showToast("session expired");
  225. Navigator.of(context).pushAndRemoveUntil(
  226. MaterialPageRoute(
  227. builder: (BuildContext context) {
  228. return LoginPage();
  229. },
  230. ),
  231. (route) => route.isFirst,
  232. );
  233. } else {
  234. showErrorDialog(context, "incorrect code",
  235. "authentication failed, please try again");
  236. }
  237. } catch (e) {
  238. await dialog.hide();
  239. _logger.severe(e);
  240. showErrorDialog(
  241. context, "oops", "authentication failed, please try again");
  242. }
  243. }
  244. Future<void> _saveConfiguration(Response response) async {
  245. await Configuration.instance.setUserID(response.data["id"]);
  246. if (response.data["encryptedToken"] != null) {
  247. await Configuration.instance
  248. .setEncryptedToken(response.data["encryptedToken"]);
  249. await Configuration.instance.setKeyAttributes(
  250. KeyAttributes.fromMap(response.data["keyAttributes"]));
  251. } else {
  252. await Configuration.instance.setToken(response.data["token"]);
  253. }
  254. }
  255. }