sign_in_widget.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/widgets.dart';
  4. import 'package:photos/core/configuration.dart';
  5. import 'package:photos/core/event_bus.dart';
  6. import 'package:photos/events/user_authenticated_event.dart';
  7. import 'package:photos/user_authenticator.dart';
  8. class SignInWidget extends StatefulWidget {
  9. final Function() onReconfigurationRequested;
  10. const SignInWidget(
  11. this.onReconfigurationRequested, {
  12. Key key,
  13. }) : super(key: key);
  14. @override
  15. _SignInWidgetState createState() => _SignInWidgetState();
  16. }
  17. enum Mode { sign_up, sign_in, unknown }
  18. class _SignInWidgetState extends State<SignInWidget> {
  19. Mode mode = Mode.unknown;
  20. final _usernameController = TextEditingController();
  21. final _passwordController = TextEditingController();
  22. final _repeatPasswordController = TextEditingController();
  23. @override
  24. Widget build(BuildContext context) {
  25. if (mode == Mode.sign_up) {
  26. return _getSignUpWidget(context);
  27. } else {
  28. return _getSignInWidget(context);
  29. }
  30. }
  31. Widget _getSignUpWidget(BuildContext context) {
  32. return Container(
  33. child: SingleChildScrollView(
  34. child: Column(
  35. children: <Widget>[
  36. Padding(
  37. padding: const EdgeInsets.fromLTRB(0, 16, 0, 0),
  38. child: Text("Create an account to get started"),
  39. ),
  40. TextFormField(
  41. decoration: InputDecoration(
  42. hintText: 'username',
  43. contentPadding: EdgeInsets.all(20),
  44. ),
  45. controller: _usernameController,
  46. autofocus: true,
  47. autocorrect: false,
  48. ),
  49. TextFormField(
  50. decoration: InputDecoration(
  51. hintText: 'password',
  52. contentPadding: EdgeInsets.all(20),
  53. ),
  54. autocorrect: false,
  55. obscureText: true,
  56. controller: _passwordController,
  57. ),
  58. TextFormField(
  59. decoration: InputDecoration(
  60. hintText: 'repeat password',
  61. contentPadding: EdgeInsets.all(20),
  62. ),
  63. autocorrect: false,
  64. obscureText: true,
  65. controller: _repeatPasswordController,
  66. ),
  67. CupertinoButton(
  68. child: Text("Sign Up"),
  69. onPressed: () async {
  70. if (_passwordController.text != _repeatPasswordController.text) {
  71. _showPasswordMismatchDialog();
  72. } else {
  73. try {
  74. final userCreated = await UserAuthenticator.instance.create(
  75. _usernameController.text, _passwordController.text);
  76. if (userCreated) {
  77. Navigator.of(context).pop();
  78. _showSelectEncryptionLevelDialog();
  79. } else {
  80. _showGenericErrorDialog();
  81. }
  82. } catch (e) {
  83. _showGenericErrorDialog(error: e);
  84. }
  85. }
  86. },
  87. ),
  88. CupertinoButton(
  89. child: Text("Have an account?"),
  90. onPressed: () {
  91. setState(() {
  92. mode = Mode.sign_in;
  93. });
  94. },
  95. ),
  96. ],
  97. ),
  98. ));
  99. }
  100. Widget _getSignInWidget(BuildContext context) {
  101. return Container(
  102. child: SingleChildScrollView(
  103. child: Column(
  104. mainAxisAlignment: MainAxisAlignment.center,
  105. children: <Widget>[
  106. TextFormField(
  107. // initialValue: Configuration.instance.getUsername(),
  108. decoration: InputDecoration(
  109. hintText: 'username',
  110. contentPadding: EdgeInsets.all(20),
  111. ),
  112. autofocus: true,
  113. autocorrect: false,
  114. controller: _usernameController,
  115. ),
  116. TextFormField(
  117. // initialValue: Configuration.instance.getPassword(),
  118. decoration: InputDecoration(
  119. hintText: 'password',
  120. contentPadding: EdgeInsets.all(20),
  121. ),
  122. autocorrect: false,
  123. obscureText: true,
  124. controller: _passwordController,
  125. ),
  126. CupertinoButton(
  127. child: Text("Sign In"),
  128. onPressed: () async {
  129. final loggedIn = await UserAuthenticator.instance
  130. .login(_usernameController.text, _passwordController.text);
  131. if (loggedIn) {
  132. Navigator.of(context).pop();
  133. if (Configuration.instance.getEncryptedKey() != null) {
  134. showDialog(
  135. context: context,
  136. barrierDismissible: false,
  137. builder: (BuildContext context) {
  138. return PassphraseDialog(false);
  139. },
  140. );
  141. }
  142. } else {
  143. _showAuthenticationFailedErrorDialog();
  144. }
  145. },
  146. ),
  147. CupertinoButton(
  148. child: Text("Don't have an account?"),
  149. onPressed: () {
  150. setState(() {
  151. mode = Mode.sign_up;
  152. });
  153. },
  154. ),
  155. ],
  156. ),
  157. ));
  158. }
  159. void _showPasswordMismatchDialog() {
  160. showDialog<void>(
  161. context: context,
  162. barrierDismissible: false, // user must tap button!
  163. builder: (BuildContext context) {
  164. return CupertinoAlertDialog(
  165. title: Text("Passwords don't match"),
  166. content: Padding(
  167. padding: const EdgeInsets.fromLTRB(0, 16, 0, 0),
  168. child: Text("Please make sure that the passwords you enter match."),
  169. ),
  170. actions: <Widget>[
  171. CupertinoDialogAction(
  172. child: Text('OK'),
  173. onPressed: () {
  174. Navigator.of(context).pop();
  175. },
  176. ),
  177. ],
  178. );
  179. },
  180. );
  181. }
  182. void _showGenericErrorDialog({Exception error}) {
  183. showDialog<void>(
  184. context: context,
  185. barrierDismissible: false, // user must tap button!
  186. builder: (BuildContext context) {
  187. return CupertinoAlertDialog(
  188. title: Text("Ooops."),
  189. content: Padding(
  190. padding: const EdgeInsets.fromLTRB(0, 16, 0, 0),
  191. child: error == null
  192. ? Text("Something went wrong.")
  193. : Text(error.toString()),
  194. ),
  195. actions: <Widget>[
  196. CupertinoDialogAction(
  197. child: Text('OK'),
  198. onPressed: () {
  199. Navigator.of(context).pop();
  200. },
  201. ),
  202. ],
  203. );
  204. },
  205. );
  206. }
  207. void _showAuthenticationFailedErrorDialog() {
  208. showDialog<void>(
  209. context: context,
  210. barrierDismissible: false, // user must tap button!
  211. builder: (BuildContext context) {
  212. return CupertinoAlertDialog(
  213. title: Text('Login failed'),
  214. content: Padding(
  215. padding: const EdgeInsets.fromLTRB(0, 16, 0, 0),
  216. child: Text(
  217. 'Please make sure that the credentials entered are correct.'),
  218. ),
  219. actions: <Widget>[
  220. CupertinoDialogAction(
  221. child: Text('Reconfigure'),
  222. onPressed: () {
  223. Navigator.of(context).pop();
  224. Configuration.instance.setEndpoint(null);
  225. widget.onReconfigurationRequested();
  226. },
  227. ),
  228. CupertinoDialogAction(
  229. child: Text('OK'),
  230. onPressed: () {
  231. Navigator.of(context).pop();
  232. },
  233. ),
  234. ],
  235. );
  236. },
  237. );
  238. }
  239. void _showSelectEncryptionLevelDialog() {
  240. showDialog(
  241. context: context,
  242. barrierDismissible: false,
  243. builder: (BuildContext context) {
  244. return SelectEncryptionLevelWidget();
  245. },
  246. );
  247. }
  248. }
  249. class SelectEncryptionLevelWidget extends StatelessWidget {
  250. const SelectEncryptionLevelWidget({
  251. Key key,
  252. }) : super(key: key);
  253. @override
  254. Widget build(BuildContext context) {
  255. return CupertinoAlertDialog(
  256. title: Text('Choose encryption level'),
  257. content: Padding(
  258. padding: const EdgeInsets.fromLTRB(0, 16, 0, 0),
  259. child: Column(
  260. children: [
  261. Text('Would you like to enable end-to-end encryption?'),
  262. Padding(padding: EdgeInsets.all(8)),
  263. Text(
  264. 'This will mean you will not be able to use features like search and sharing.'),
  265. ],
  266. ),
  267. ),
  268. actions: <Widget>[
  269. CupertinoDialogAction(
  270. child: Text('Use E2E encryption'),
  271. onPressed: () {
  272. Navigator.of(context).pop();
  273. Configuration.instance.setOptInForE2E(true);
  274. showDialog(
  275. context: context,
  276. barrierDismissible: false,
  277. builder: (BuildContext context) {
  278. return PassphraseDialog(true);
  279. },
  280. );
  281. },
  282. ),
  283. CupertinoDialogAction(
  284. child: Text("Use encryption at rest"),
  285. onPressed: () {
  286. Navigator.of(context).pop();
  287. Configuration.instance.setOptInForE2E(false);
  288. Bus.instance.fire(UserAuthenticatedEvent());
  289. },
  290. ),
  291. ],
  292. );
  293. }
  294. }
  295. class PassphraseDialog extends StatefulWidget {
  296. final bool isFreshRegistration;
  297. const PassphraseDialog(
  298. this.isFreshRegistration, {
  299. Key key,
  300. }) : super(key: key);
  301. @override
  302. _PassphraseDialogState createState() => _PassphraseDialogState();
  303. }
  304. class _PassphraseDialogState extends State<PassphraseDialog> {
  305. String _passphrase = "";
  306. @override
  307. Widget build(BuildContext context) {
  308. return CupertinoAlertDialog(
  309. title: Text('Enter passphrase'),
  310. content: Padding(
  311. padding: const EdgeInsets.fromLTRB(0, 16, 0, 0),
  312. child: Column(
  313. children: [
  314. CupertinoTextField(
  315. autofocus: true,
  316. style: Theme.of(context).textTheme.subtitle1,
  317. keyboardType: TextInputType.visiblePassword,
  318. onChanged: (value) {
  319. _passphrase = value;
  320. },
  321. ),
  322. ],
  323. ),
  324. ),
  325. actions: <Widget>[
  326. CupertinoDialogAction(
  327. child: Text('Save'),
  328. onPressed: () async {
  329. Navigator.of(context).pop();
  330. if (widget.isFreshRegistration) {
  331. await Configuration.instance.generateAndSaveKey(_passphrase);
  332. await UserAuthenticator.instance.setEncryptedKeyOnServer();
  333. } else {
  334. await Configuration.instance.decryptEncryptedKey(_passphrase);
  335. }
  336. Bus.instance.fire(UserAuthenticatedEvent());
  337. },
  338. )
  339. ],
  340. );
  341. }
  342. }