ente/lib/ui/password_entry_page.dart

346 lines
12 KiB
Dart
Raw Normal View History

2021-03-29 15:09:12 +00:00
import 'dart:ui';
import 'package:flutter/material.dart';
2021-03-29 15:09:12 +00:00
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
2021-01-05 14:27:02 +00:00
import 'package:flutter_password_strength/flutter_password_strength.dart';
2021-03-29 18:35:46 +00:00
import 'package:logging/logging.dart';
2021-03-29 15:09:12 +00:00
import 'package:photos/core/configuration.dart';
2021-04-01 14:40:32 +00:00
import 'package:photos/core/event_bus.dart';
import 'package:photos/events/account_configured_event.dart';
2021-04-01 14:40:32 +00:00
import 'package:photos/events/subscription_purchased_event.dart';
2020-10-03 17:56:18 +00:00
import 'package:photos/services/user_service.dart';
2020-10-24 21:07:12 +00:00
import 'package:photos/ui/common_elements.dart';
import 'package:photos/ui/payment/subscription.dart';
import 'package:photos/ui/recovery_key_dialog.dart';
import 'package:photos/ui/payment/subscription_page.dart';
2021-03-28 11:39:26 +00:00
import 'package:photos/ui/web_page.dart';
import 'package:photos/utils/dialog_util.dart';
2021-04-01 14:26:08 +00:00
import 'package:photos/utils/toast_util.dart';
enum PasswordEntryMode {
set,
update,
reset,
}
2021-01-05 14:27:02 +00:00
class PasswordEntryPage extends StatefulWidget {
2021-04-01 14:26:08 +00:00
final PasswordEntryMode mode;
2021-03-26 16:13:32 +00:00
2021-04-01 14:26:08 +00:00
PasswordEntryPage({this.mode = PasswordEntryMode.set, Key key})
: super(key: key);
@override
2021-01-05 14:27:02 +00:00
_PasswordEntryPageState createState() => _PasswordEntryPageState();
}
2021-01-05 14:27:02 +00:00
class _PasswordEntryPageState extends State<PasswordEntryPage> {
static const kPasswordStrengthThreshold = 0.4;
2021-04-01 18:29:37 +00:00
final _logger = Logger("PasswordEntry");
2021-01-05 14:27:02 +00:00
final _passwordController1 = TextEditingController(),
_passwordController2 = TextEditingController();
double _passwordStrength = 0;
2021-05-08 23:59:55 +00:00
String _password;
2021-05-12 16:37:14 +00:00
bool _password1Visible = false;
bool _password2Visible = false;
2021-07-26 14:18:03 +00:00
final _password1FocusNode = FocusNode();
final _password2FocusNode = FocusNode();
2021-05-12 16:37:14 +00:00
bool _password1InFocus = false;
bool _password2InFocus = false;
2021-05-08 23:59:55 +00:00
@override
void initState() {
super.initState();
_password = Configuration.instance.getVolatilePassword();
if (_password != null) {
Future.delayed(Duration.zero, () => _showRecoveryCodeDialog(_password));
}
2021-05-12 16:37:14 +00:00
_password1FocusNode.addListener(() {
setState(() {
_password1InFocus = _password1FocusNode.hasFocus;
});
});
_password2FocusNode.addListener(() {
setState(() {
_password2InFocus = _password2FocusNode.hasFocus;
});
});
2021-05-08 23:59:55 +00:00
}
@override
Widget build(BuildContext context) {
2021-04-01 14:26:08 +00:00
String title = "set password";
if (widget.mode == PasswordEntryMode.update) {
title = "change password";
} else if (widget.mode == PasswordEntryMode.reset) {
title = "reset password";
2021-05-08 23:59:55 +00:00
} else if (_password != null) {
title = "encryption keys";
2021-04-01 14:26:08 +00:00
}
return Scaffold(
appBar: AppBar(
2021-04-01 14:26:08 +00:00
title: Text(title),
leading:
widget.mode == PasswordEntryMode.reset ? Icon(Icons.lock) : null,
),
2021-04-01 14:26:08 +00:00
body: _getBody(title),
2021-03-28 11:54:04 +00:00
resizeToAvoidBottomInset: false,
);
}
2021-04-01 14:26:08 +00:00
Widget _getBody(String buttonText) {
2021-05-08 23:59:55 +00:00
if (_password != null) {
return Container();
}
2021-01-05 14:40:43 +00:00
return Column(
children: [
FlutterPasswordStrength(
password: _passwordController1.text,
2021-05-12 16:37:14 +00:00
backgroundColor: Colors.white.withOpacity(0.1),
2021-01-05 14:40:43 +00:00
strengthCallback: (strength) {
_passwordStrength = strength;
2021-01-05 14:40:43 +00:00
},
2021-05-12 16:37:14 +00:00
strengthColors: passwordStrengthColors,
2021-01-05 14:40:43 +00:00
),
2021-03-28 11:39:26 +00:00
SingleChildScrollView(
child: Container(
padding: EdgeInsets.fromLTRB(16, 36, 16, 16),
child: Column(
children: [
Padding(padding: EdgeInsets.all(12)),
Text(
2021-03-28 12:43:35 +00:00
"enter a" +
2021-04-01 14:26:08 +00:00
(widget.mode != PasswordEntryMode.set ? " new " : " ") +
2021-03-28 12:43:35 +00:00
"password we can use to encrypt your data",
2021-03-28 11:39:26 +00:00
textAlign: TextAlign.center,
style: TextStyle(
height: 1.3,
2021-01-05 14:40:43 +00:00
),
2021-03-28 11:39:26 +00:00
),
Padding(padding: EdgeInsets.all(8)),
Text("we don't store this password, so if you forget, "),
Text.rich(
TextSpan(
text: "we cannot decrypt your data",
style: TextStyle(
decoration: TextDecoration.underline,
fontWeight: FontWeight.bold,
)),
style: TextStyle(
height: 1.3,
2021-01-05 14:40:43 +00:00
),
2021-03-28 11:39:26 +00:00
textAlign: TextAlign.center,
),
Padding(padding: EdgeInsets.all(12)),
Padding(
padding: const EdgeInsets.fromLTRB(32, 0, 32, 0),
child: TextFormField(
decoration: InputDecoration(
hintText: "password",
contentPadding: EdgeInsets.all(20),
2021-05-12 16:37:14 +00:00
suffixIcon: _password1InFocus
? IconButton(
icon: Icon(
_password1Visible
? Icons.visibility
: Icons.visibility_off,
color: Colors.white.withOpacity(0.5),
size: 20,
),
onPressed: () {
setState(() {
_password1Visible = !_password1Visible;
});
},
)
: null,
2021-01-05 14:40:43 +00:00
),
2021-05-12 16:37:14 +00:00
obscureText: !_password1Visible,
2021-03-28 11:39:26 +00:00
controller: _passwordController1,
autofocus: false,
autocorrect: false,
keyboardType: TextInputType.visiblePassword,
onChanged: (_) {
setState(() {});
},
2021-05-12 16:37:14 +00:00
textInputAction: TextInputAction.next,
focusNode: _password1FocusNode,
2021-01-05 14:40:43 +00:00
),
2021-03-28 11:39:26 +00:00
),
Padding(padding: EdgeInsets.all(8)),
Padding(
padding: const EdgeInsets.fromLTRB(32, 0, 32, 0),
child: TextFormField(
decoration: InputDecoration(
hintText: "password again",
contentPadding: EdgeInsets.all(20),
2021-05-12 16:37:14 +00:00
suffixIcon: _password2InFocus
? IconButton(
icon: Icon(
_password2Visible
? Icons.visibility
: Icons.visibility_off,
color: Colors.white.withOpacity(0.5),
size: 20,
),
onPressed: () {
setState(() {
_password2Visible = !_password2Visible;
});
},
)
: null,
2021-01-05 14:40:43 +00:00
),
2021-05-12 16:37:14 +00:00
obscureText: !_password2Visible,
2021-03-28 11:39:26 +00:00
controller: _passwordController2,
autofocus: false,
autocorrect: false,
keyboardType: TextInputType.visiblePassword,
onChanged: (_) {
setState(() {});
},
2021-05-12 16:37:14 +00:00
focusNode: _password2FocusNode,
2021-01-05 14:40:43 +00:00
),
2021-03-28 11:39:26 +00:00
),
Padding(padding: EdgeInsets.all(20)),
Container(
width: double.infinity,
height: 64,
padding: EdgeInsets.fromLTRB(40, 0, 40, 0),
child: button(
2021-04-01 14:26:08 +00:00
buttonText,
2021-03-28 11:39:26 +00:00
fontSize: 18,
onPressed: _passwordController1.text.isNotEmpty &&
_passwordController2.text.isNotEmpty
2021-04-01 14:26:08 +00:00
? _onButtonPress
2021-03-28 11:39:26 +00:00
: null,
),
),
],
),
),
),
Expanded(child: Container()),
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
2021-08-11 08:23:43 +00:00
return WebPage("how it works", "https://ente.io/architecture");
2021-03-28 11:39:26 +00:00
},
),
);
},
child: Container(
padding: EdgeInsets.all(40),
child: RichText(
text: TextSpan(
text: "how it works",
style: TextStyle(
color: Colors.blue,
fontFamily: 'Ubuntu',
),
),
),
2021-01-05 14:40:43 +00:00
),
),
2021-01-05 14:40:43 +00:00
],
);
}
2021-04-01 14:26:08 +00:00
void _onButtonPress() {
if (_passwordController1.text != _passwordController2.text) {
showErrorDialog(
context, "uhm...", "the passwords you entered don't match");
} else if (_passwordStrength < kPasswordStrengthThreshold) {
showErrorDialog(context, "weak password",
"the password you have chosen is too simple, please choose another one");
} else {
if (widget.mode == PasswordEntryMode.set) {
2021-05-08 23:59:55 +00:00
_showRecoveryCodeDialog(_passwordController1.text);
2021-04-01 14:26:08 +00:00
} else {
_updatePassword();
}
}
}
void _updatePassword() async {
final dialog =
createProgressDialog(context, "generating encryption keys...");
2021-04-01 14:26:08 +00:00
await dialog.show();
try {
final keyAttributes = await Configuration.instance
.updatePassword(_passwordController1.text);
await UserService.instance.updateKeyAttributes(keyAttributes);
await dialog.hide();
showToast("password changed successfully");
Navigator.of(context).pop();
2021-04-01 14:40:32 +00:00
if (widget.mode == PasswordEntryMode.reset) {
Bus.instance.fire(SubscriptionPurchasedEvent());
Navigator.of(context).popUntil((route) => route.isFirst);
2021-04-01 14:40:32 +00:00
}
2021-04-01 18:29:37 +00:00
} catch (e, s) {
_logger.severe(e, s);
2021-04-01 14:26:08 +00:00
await dialog.hide();
showGenericErrorDialog(context);
}
}
2021-05-08 23:59:55 +00:00
Future<void> _showRecoveryCodeDialog(String password) async {
2021-03-29 18:35:46 +00:00
final dialog =
createProgressDialog(context, "generating encryption keys...");
2021-03-29 15:09:12 +00:00
await dialog.show();
try {
2021-05-08 23:59:55 +00:00
final result = await Configuration.instance.generateKey(password);
Configuration.instance.setVolatilePassword(null);
2021-03-29 15:09:12 +00:00
await dialog.hide();
2021-07-26 14:18:03 +00:00
onDone() async {
2021-03-29 18:35:46 +00:00
final dialog = createProgressDialog(context, "please wait...");
await dialog.show();
try {
2021-04-01 14:26:08 +00:00
await UserService.instance.setAttributes(result);
await dialog.hide();
Bus.instance.fire(AccountConfiguredEvent());
2021-04-01 14:26:08 +00:00
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (BuildContext context) {
return getSubscriptionPage(isOnBoarding: true);
2021-04-01 14:26:08 +00:00
},
),
(route) => route.isFirst,
);
2021-03-29 18:35:46 +00:00
} catch (e, s) {
2021-05-08 23:59:55 +00:00
_logger.severe(e, s);
2021-03-29 18:35:46 +00:00
await dialog.hide();
showGenericErrorDialog(context);
}
2021-07-26 14:18:03 +00:00
}
2021-03-29 18:35:46 +00:00
showDialog(
context: context,
builder: (BuildContext context) {
2021-03-30 08:07:31 +00:00
return RecoveryKeyDialog(
2021-05-08 23:59:55 +00:00
result.privateKeyAttributes.recoveryKey,
"continue",
onDone,
isDismissible: false,
);
2021-03-29 18:35:46 +00:00
},
barrierColor: Colors.black.withOpacity(0.85),
barrierDismissible: false,
);
2021-03-29 15:09:12 +00:00
} catch (e) {
_logger.severe(e);
2021-03-29 15:09:12 +00:00
await dialog.hide();
if (e is UnsupportedError) {
showErrorDialog(context, "insecure device",
"sorry, we could not generate secure keys on this device.\n\nplease sign up from a different device.");
} else {
showGenericErrorDialog(context);
}
2021-03-29 15:09:12 +00:00
}
}
}