ente/lib/ui/account/password_entry_page.dart

460 lines
17 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2021-03-29 15:09:12 +00:00
import 'package:flutter/services.dart';
2021-03-29 18:35:46 +00:00
import 'package:logging/logging.dart';
2022-05-29 09:43:21 +00:00
import 'package:password_strength/password_strength.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';
import 'package:photos/ui/account/recovery_key_page.dart';
2022-07-03 10:09:01 +00:00
import 'package:photos/ui/common/dynamic_fab.dart';
import 'package:photos/ui/common/web_page.dart';
import 'package:photos/ui/payment/subscription.dart';
import 'package:photos/utils/dialog_util.dart';
import 'package:photos/utils/navigation_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
const PasswordEntryPage({this.mode = PasswordEntryMode.set, Key key})
2021-04-01 14:26:08 +00:00
: super(key: key);
@override
2022-07-03 09:45:00 +00:00
State<PasswordEntryPage> createState() => _PasswordEntryPageState();
}
2021-01-05 14:27:02 +00:00
class _PasswordEntryPageState extends State<PasswordEntryPage> {
2022-05-29 09:43:21 +00:00
static const kMildPasswordStrengthThreshold = 0.4;
static const kStrongPasswordStrengthThreshold = 0.7;
2022-05-29 09:43:21 +00:00
final _logger = Logger((_PasswordEntryPageState).toString());
2021-01-05 14:27:02 +00:00
final _passwordController1 = TextEditingController(),
_passwordController2 = TextEditingController();
2022-07-04 06:02:17 +00:00
final Color _validFieldValueColor = const Color.fromRGBO(45, 194, 98, 0.2);
String _volatilePassword;
String _passwordInInputBox = '';
2022-06-09 09:59:10 +00:00
String _passwordInInputConfirmationBox = '';
2022-05-29 09:43:21 +00:00
double _passwordStrength = 0.0;
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
bool _passwordsMatch = false;
2022-05-29 09:43:21 +00:00
bool _isPasswordValid = false;
2021-05-08 23:59:55 +00:00
@override
void initState() {
super.initState();
_volatilePassword = Configuration.instance.getVolatilePassword();
if (_volatilePassword != null) {
Future.delayed(
2022-06-11 08:23:52 +00:00
Duration.zero,
() => _showRecoveryCodeDialog(_volatilePassword),
);
2021-05-08 23:59:55 +00:00
}
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) {
2022-06-15 06:48:23 +00:00
final isKeypadOpen = MediaQuery.of(context).viewInsets.bottom > 100;
FloatingActionButtonLocation fabLocation() {
if (isKeypadOpen) {
return null;
} else {
return FloatingActionButtonLocation.centerFloat;
}
}
2022-03-12 14:16:04 +00:00
String title = "Set password";
2021-04-01 14:26:08 +00:00
if (widget.mode == PasswordEntryMode.update) {
2022-03-12 14:16:04 +00:00
title = "Change password";
2021-04-01 14:26:08 +00:00
} else if (widget.mode == PasswordEntryMode.reset) {
2022-03-12 14:16:04 +00:00
title = "Reset password";
} else if (_volatilePassword != null) {
2022-03-12 14:16:04 +00:00
title = "Encryption keys";
2021-04-01 14:26:08 +00:00
}
return Scaffold(
2022-06-15 06:48:23 +00:00
resizeToAvoidBottomInset: isKeypadOpen,
appBar: AppBar(
leading: widget.mode == PasswordEntryMode.reset
2022-05-29 09:43:21 +00:00
? Container()
: IconButton(
2022-07-04 06:02:17 +00:00
icon: const Icon(Icons.arrow_back),
color: Theme.of(context).iconTheme.color,
onPressed: () {
Navigator.of(context).pop();
},
),
elevation: 0,
),
2021-04-01 14:26:08 +00:00
body: _getBody(title),
floatingActionButton: DynamicFAB(
2022-06-11 08:23:52 +00:00
isKeypadOpen: isKeypadOpen,
isFormValid: _passwordsMatch && _isPasswordValid,
2022-06-11 08:23:52 +00:00
buttonText: title,
onPressedFunction: () {
if (widget.mode == PasswordEntryMode.set) {
_showRecoveryCodeDialog(_passwordController1.text);
} else {
_updatePassword();
}
FocusScope.of(context).unfocus();
2022-06-11 08:23:52 +00:00
},
),
floatingActionButtonLocation: fabLocation(),
2022-05-30 15:43:33 +00:00
floatingActionButtonAnimator: NoScalingAnimation(),
);
}
Widget _getBody(String buttonTextAndHeading) {
2022-05-29 09:43:21 +00:00
final email = Configuration.instance.getEmail();
var passwordStrengthText = 'Weak';
var passwordStrengthColor = Colors.redAccent;
if (_passwordStrength > kStrongPasswordStrengthThreshold) {
passwordStrengthText = 'Strong';
passwordStrengthColor = Colors.greenAccent;
} else if (_passwordStrength > kMildPasswordStrengthThreshold) {
passwordStrengthText = 'Moderate';
passwordStrengthColor = Colors.orangeAccent;
}
if (_volatilePassword != null) {
2021-05-08 23:59:55 +00:00
return Container();
}
2021-01-05 14:40:43 +00:00
return Column(
children: [
Expanded(
child: AutofillGroup(
child: ListView(
children: [
Padding(
padding:
const EdgeInsets.symmetric(vertical: 30, horizontal: 20),
2022-06-11 08:23:52 +00:00
child: Text(
buttonTextAndHeading,
style: Theme.of(context).textTheme.headline4,
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Text(
"Enter a" +
2021-11-11 10:54:52 +00:00
(widget.mode != PasswordEntryMode.set ? " new " : " ") +
"password we can use to encrypt your data",
textAlign: TextAlign.start,
style: Theme.of(context)
.textTheme
.subtitle1
.copyWith(fontSize: 14),
2021-01-05 14:40:43 +00:00
),
),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.all(8)),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: RichText(
2022-06-11 08:23:52 +00:00
text: TextSpan(
style: Theme.of(context)
.textTheme
.subtitle1
.copyWith(fontSize: 14),
children: [
2022-07-04 06:02:17 +00:00
const TextSpan(
2022-06-11 08:23:52 +00:00
text:
"We don't store this password, so if you forget, ",
),
TextSpan(
text: "we cannot decrypt your data",
style: Theme.of(context).textTheme.subtitle1.copyWith(
2022-06-11 08:23:52 +00:00
fontSize: 14,
decoration: TextDecoration.underline,
),
),
2022-06-11 08:23:52 +00:00
],
),
),
),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.all(12)),
2022-05-29 09:43:21 +00:00
Visibility(
// hidden textForm for suggesting auto-fill service for saving
// password
visible: false,
child: TextFormField(
autofillHints: const [
AutofillHints.email,
],
autocorrect: false,
keyboardType: TextInputType.emailAddress,
initialValue: email,
textInputAction: TextInputAction.next,
),
),
Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
child: TextFormField(
2022-05-29 09:43:21 +00:00
autofillHints: const [AutofillHints.newPassword],
decoration: InputDecoration(
2022-05-29 09:43:21 +00:00
fillColor:
_isPasswordValid ? _validFieldValueColor : null,
filled: true,
2022-05-29 09:43:21 +00:00
hintText: "Password",
2022-07-04 06:02:17 +00:00
contentPadding: const EdgeInsets.all(20),
border: UnderlineInputBorder(
2022-06-11 08:23:52 +00:00
borderSide: BorderSide.none,
borderRadius: BorderRadius.circular(6),
),
suffixIcon: _password1InFocus
? IconButton(
icon: Icon(
_password1Visible
? Icons.visibility
: Icons.visibility_off,
color: Theme.of(context).iconTheme.color,
size: 20,
),
onPressed: () {
setState(() {
_password1Visible = !_password1Visible;
});
},
)
2022-05-29 09:43:21 +00:00
: _isPasswordValid
? Icon(
Icons.check,
color: Theme.of(context)
.inputDecorationTheme
.focusedBorder
.borderSide
.color,
)
: null,
2021-11-11 10:54:52 +00:00
),
obscureText: !_password1Visible,
controller: _passwordController1,
autofocus: false,
autocorrect: false,
keyboardType: TextInputType.visiblePassword,
onChanged: (password) {
setState(() {
_passwordInInputBox = password;
2022-05-29 09:43:21 +00:00
_passwordStrength = estimatePasswordStrength(password);
_isPasswordValid =
_passwordStrength >= kMildPasswordStrengthThreshold;
2022-06-09 09:59:10 +00:00
_passwordsMatch = _passwordInInputBox ==
_passwordInInputConfirmationBox;
});
},
textInputAction: TextInputAction.next,
focusNode: _password1FocusNode,
2021-01-05 14:40:43 +00:00
),
),
const SizedBox(height: 8),
Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
child: TextFormField(
keyboardType: TextInputType.visiblePassword,
controller: _passwordController2,
obscureText: !_password2Visible,
autofillHints: const [AutofillHints.newPassword],
onEditingComplete: () => TextInput.finishAutofillContext(),
decoration: InputDecoration(
fillColor: _passwordsMatch ? _validFieldValueColor : null,
filled: true,
hintText: "Confirm password",
2022-07-04 06:02:17 +00:00
contentPadding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 20,
),
suffixIcon: _password2InFocus
? IconButton(
icon: Icon(
_password2Visible
? Icons.visibility
: Icons.visibility_off,
color: Theme.of(context).iconTheme.color,
size: 20,
),
onPressed: () {
setState(() {
_password2Visible = !_password2Visible;
});
},
)
: _passwordsMatch
? Icon(
Icons.check,
color: Theme.of(context)
.inputDecorationTheme
.focusedBorder
.borderSide
.color,
)
: null,
border: UnderlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.circular(6),
2022-06-11 08:23:52 +00:00
),
2021-11-11 10:54:52 +00:00
),
focusNode: _password2FocusNode,
onChanged: (cnfPassword) {
setState(() {
_passwordInInputConfirmationBox = cnfPassword;
if (_passwordInInputBox != null ||
_passwordInInputBox != '') {
_passwordsMatch = _passwordInInputBox ==
_passwordInInputConfirmationBox;
}
});
},
),
),
Opacity(
opacity:
(_passwordInInputBox != '') && _password1InFocus ? 1 : 0,
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 20, vertical: 8),
child: Text(
'Password Strength: $passwordStrengthText',
style: TextStyle(
color: passwordStrengthColor,
),
),
),
),
const SizedBox(height: 8),
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
2022-07-04 06:02:17 +00:00
return const WebPage(
2022-06-11 08:23:52 +00:00
"How it works",
"https://ente.io/architecture",
);
},
),
);
},
child: Container(
2022-07-04 06:02:17 +00:00
padding: const EdgeInsets.symmetric(horizontal: 20),
child: RichText(
text: TextSpan(
2022-06-11 08:23:52 +00:00
text: "How it works",
style: Theme.of(context).textTheme.subtitle1.copyWith(
fontSize: 14,
2022-06-11 08:23:52 +00:00
decoration: TextDecoration.underline,
),
),
2021-11-11 10:54:52 +00:00
),
),
2021-03-28 11:39:26 +00:00
),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.all(20)),
],
),
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 _updatePassword() async {
final dialog =
2022-05-30 11:08:15 +00:00
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();
2022-06-10 14:29:56 +00:00
showShortToast(context, "Password changed successfully");
2021-04-01 14:26:08 +00:00
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 =
2022-05-29 09:43:21 +00:00
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 {
2022-05-17 11:38:21 +00:00
final dialog = createProgressDialog(context, "Please wait...");
2021-03-29 18:35:46 +00:00
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
}
routeToPage(
2022-06-11 08:23:52 +00:00
context,
RecoveryKeyPage(
result.privateKeyAttributes.recoveryKey,
"Continue",
showAppBar: false,
isDismissible: false,
onDone: onDone,
showProgressBar: true,
),
);
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) {
2022-06-11 08:23:52 +00:00
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
}
}
}