ente/lib/ui/password_entry_page.dart

494 lines
20 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';
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';
2022-04-26 11:56:49 +00:00
import 'package:photos/ui/common/dynamicFAB.dart';
import 'package:photos/ui/payment/subscription.dart';
import 'package:photos/ui/recovery_key_page.dart';
2021-03-28 11:39:26 +00:00
import 'package:photos/ui/web_page.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
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> {
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-05-29 09:43:21 +00:00
final Color _validFieldValueColor = Color.fromRGBO(45, 194, 98, 0.2);
String _volatilePassword;
2021-05-08 23:59:55 +00:00
String _password;
String _passwordInInputBox = '';
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(
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) {
final isKeypadOpen = MediaQuery.of(context).viewInsets.bottom != 0;
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(
appBar: AppBar(
leading: widget.mode == PasswordEntryMode.reset
2022-05-29 09:43:21 +00:00
? Container()
: IconButton(
icon: 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(
isKeypadOpen: isKeypadOpen,
isFormValid: _passwordsMatch,
buttonText: title,
onPressedFunction: () {
if (widget.mode == PasswordEntryMode.set) {
_showRecoveryCodeDialog(_passwordController1.text);
} else {
_updatePassword();
}
}),
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 isKeypadOpen = MediaQuery.of(context).viewInsets.bottom != 0;
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),
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
),
),
Padding(padding: EdgeInsets.all(8)),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: RichText(
text: TextSpan(
style: Theme.of(context)
.textTheme
.subtitle1
.copyWith(fontSize: 14),
children: [
TextSpan(
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(
fontSize: 14,
decoration: TextDecoration.underline),
),
])),
),
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",
contentPadding: EdgeInsets.all(20),
border: UnderlineInputBorder(
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;
});
},
textInputAction: TextInputAction.next,
focusNode: _password1FocusNode,
2021-01-05 14:40:43 +00:00
),
),
Padding(padding: EdgeInsets.all(4)),
Stack(
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
child: TextFormField(
keyboardType: TextInputType.visiblePassword,
controller: _passwordController2,
obscureText: !_password2Visible,
2022-05-29 09:43:21 +00:00
autofillHints: const [AutofillHints.newPassword],
onEditingComplete: () =>
TextInput.finishAutofillContext(),
decoration: InputDecoration(
2022-05-29 09:43:21 +00:00
fillColor:
_passwordsMatch ? _validFieldValueColor : null,
filled: true,
2022-05-29 09:43:21 +00:00
hintText: "Confirm password",
contentPadding: 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)),
),
focusNode: _password2FocusNode,
onChanged: (cnfPassword) {
setState(() {
if (_password != null || _password != '') {
2022-05-29 09:43:21 +00:00
_passwordsMatch = _password == cnfPassword;
}
});
},
),
2021-01-05 14:40:43 +00:00
),
Visibility(
2022-05-29 09:43:21 +00:00
visible:
((_passwordInInputBox != '') && _password1InFocus),
child: Positioned(
2022-05-29 09:43:21 +00:00
bottom: 24,
child: Row(
children: [
SizedBox(
width: MediaQuery.of(context).size.width,
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20),
child: Container(
decoration: BoxDecoration(
2022-04-22 11:32:20 +00:00
boxShadow: [
BoxShadow(
spreadRadius: 0.5,
color: Theme.of(context).hintColor,
2022-04-22 11:32:20 +00:00
offset: Offset(0, -0.325),
),
],
borderRadius: BorderRadius.only(
topLeft: Radius.zero,
topRight: Radius.zero,
bottomLeft: Radius.circular(5),
bottomRight: Radius.circular(5),
),
color: Theme.of(context)
.dialogTheme
.backgroundColor,
),
width: double.infinity,
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(
2022-05-29 09:43:21 +00:00
4.0, 4, 4.0, 4.0),
child: Row(
children: [
Padding(
2022-05-29 09:43:21 +00:00
padding:
EdgeInsets.symmetric(
horizontal: 10,
vertical: 5),
child: Text(
'Password Strength: $passwordStrengthText',
style: TextStyle(
color:
passwordStrengthColor),
)),
],
),
),
]),
),
2021-11-11 10:54:52 +00:00
),
),
SizedBox(
width: 20,
),
],
)),
2021-11-11 10:54:52 +00:00
),
],
clipBehavior: Clip.none,
),
SizedBox(
height: 50,
),
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return WebPage(
2022-05-29 09:43:21 +00:00
"How it works", "https://ente.io/architecture");
},
),
);
},
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20),
child: RichText(
text: TextSpan(
2022-05-29 09:43:21 +00:00
text: "How it works",
style: Theme.of(context).textTheme.subtitle1.copyWith(
fontSize: 14,
decoration: TextDecoration.underline)),
2021-11-11 10:54:52 +00:00
),
),
2021-03-28 11:39:26 +00:00
),
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-05-30 11:08:15 +00:00
showShortToast("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(
context,
RecoveryKeyPage(
2021-05-08 23:59:55 +00:00
result.privateKeyAttributes.recoveryKey,
"Continue",
showAppBar: false,
2021-05-08 23:59:55 +00:00
isDismissible: false,
onDone: onDone,
));
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-05-29 09:43:21 +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
}
}
}