123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:logging/logging.dart';
- import 'package:password_strength/password_strength.dart';
- import 'package:photos/core/configuration.dart';
- import 'package:photos/core/event_bus.dart';
- import 'package:photos/events/account_configured_event.dart';
- import 'package:photos/events/subscription_purchased_event.dart';
- import 'package:photos/services/user_service.dart';
- import 'package:photos/ui/common/dynamicFAB.dart';
- import 'package:photos/ui/payment/subscription.dart';
- import 'package:photos/ui/recovery_key_page.dart';
- import 'package:photos/ui/web_page.dart';
- import 'package:photos/utils/dialog_util.dart';
- import 'package:photos/utils/navigation_util.dart';
- import 'package:photos/utils/toast_util.dart';
- enum PasswordEntryMode {
- set,
- update,
- reset,
- }
- class PasswordEntryPage extends StatefulWidget {
- final PasswordEntryMode mode;
- PasswordEntryPage({this.mode = PasswordEntryMode.set, Key key})
- : super(key: key);
- @override
- _PasswordEntryPageState createState() => _PasswordEntryPageState();
- }
- class _PasswordEntryPageState extends State<PasswordEntryPage> {
- static const kMildPasswordStrengthThreshold = 0.4;
- static const kStrongPasswordStrengthThreshold = 0.7;
- final _logger = Logger((_PasswordEntryPageState).toString());
- final _passwordController1 = TextEditingController(),
- _passwordController2 = TextEditingController();
- final Color _validFieldValueColor = Color.fromRGBO(45, 194, 98, 0.2);
- String _volatilePassword;
- String _passwordInInputBox = '';
- String _passwordInInputConfirmationBox = '';
- double _passwordStrength = 0.0;
- bool _password1Visible = false;
- bool _password2Visible = false;
- final _password1FocusNode = FocusNode();
- final _password2FocusNode = FocusNode();
- bool _password1InFocus = false;
- bool _password2InFocus = false;
- bool _passwordsMatch = false;
- bool _isPasswordValid = false;
- @override
- void initState() {
- super.initState();
- _volatilePassword = Configuration.instance.getVolatilePassword();
- if (_volatilePassword != null) {
- Future.delayed(
- Duration.zero,
- () => _showRecoveryCodeDialog(_volatilePassword),
- );
- }
- _password1FocusNode.addListener(() {
- setState(() {
- _password1InFocus = _password1FocusNode.hasFocus;
- });
- });
- _password2FocusNode.addListener(() {
- setState(() {
- _password2InFocus = _password2FocusNode.hasFocus;
- });
- });
- }
- @override
- Widget build(BuildContext context) {
- final isKeypadOpen = MediaQuery.of(context).viewInsets.bottom > 125;
- FloatingActionButtonLocation fabLocation() {
- if (isKeypadOpen) {
- return null;
- } else {
- return FloatingActionButtonLocation.centerFloat;
- }
- }
- String title = "Set password";
- if (widget.mode == PasswordEntryMode.update) {
- title = "Change password";
- } else if (widget.mode == PasswordEntryMode.reset) {
- title = "Reset password";
- } else if (_volatilePassword != null) {
- title = "Encryption keys";
- }
- return Scaffold(
- appBar: AppBar(
- leading: widget.mode == PasswordEntryMode.reset
- ? Container()
- : IconButton(
- icon: Icon(Icons.arrow_back),
- color: Theme.of(context).iconTheme.color,
- onPressed: () {
- Navigator.of(context).pop();
- },
- ),
- elevation: 0,
- ),
- body: _getBody(title),
- floatingActionButton: DynamicFAB(
- isKeypadOpen: isKeypadOpen,
- isFormValid: _passwordsMatch,
- buttonText: title,
- onPressedFunction: () {
- if (widget.mode == PasswordEntryMode.set) {
- _showRecoveryCodeDialog(_passwordController1.text);
- } else {
- _updatePassword();
- }
- },
- ),
- floatingActionButtonLocation: fabLocation(),
- floatingActionButtonAnimator: NoScalingAnimation(),
- );
- }
- Widget _getBody(String buttonTextAndHeading) {
- 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) {
- return Container();
- }
- 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" +
- (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),
- ),
- ),
- 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)),
- 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(
- autofillHints: const [AutofillHints.newPassword],
- decoration: InputDecoration(
- fillColor:
- _isPasswordValid ? _validFieldValueColor : null,
- filled: true,
- 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;
- });
- },
- )
- : _isPasswordValid
- ? Icon(
- Icons.check,
- color: Theme.of(context)
- .inputDecorationTheme
- .focusedBorder
- .borderSide
- .color,
- )
- : null,
- ),
- obscureText: !_password1Visible,
- controller: _passwordController1,
- autofocus: false,
- autocorrect: false,
- keyboardType: TextInputType.visiblePassword,
- onChanged: (password) {
- setState(() {
- _passwordInInputBox = password;
- _passwordStrength = estimatePasswordStrength(password);
- _isPasswordValid =
- _passwordStrength >= kMildPasswordStrengthThreshold;
- _passwordsMatch = _passwordInInputBox ==
- _passwordInInputConfirmationBox;
- });
- },
- textInputAction: TextInputAction.next,
- focusNode: _password1FocusNode,
- ),
- ),
- 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",
- 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(() {
- _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) {
- return WebPage(
- "How it works",
- "https://ente.io/architecture",
- );
- },
- ),
- );
- },
- child: Container(
- padding: EdgeInsets.symmetric(horizontal: 20),
- child: RichText(
- text: TextSpan(
- text: "How it works",
- style: Theme.of(context).textTheme.subtitle1.copyWith(
- fontSize: 14,
- decoration: TextDecoration.underline,
- ),
- ),
- ),
- ),
- ),
- Padding(padding: EdgeInsets.all(20)),
- ],
- ),
- ),
- ),
- ],
- );
- }
- void _updatePassword() async {
- final dialog =
- createProgressDialog(context, "Generating encryption keys...");
- await dialog.show();
- try {
- final keyAttributes = await Configuration.instance
- .updatePassword(_passwordController1.text);
- await UserService.instance.updateKeyAttributes(keyAttributes);
- await dialog.hide();
- showShortToast(context, "Password changed successfully");
- Navigator.of(context).pop();
- if (widget.mode == PasswordEntryMode.reset) {
- Bus.instance.fire(SubscriptionPurchasedEvent());
- Navigator.of(context).popUntil((route) => route.isFirst);
- }
- } catch (e, s) {
- _logger.severe(e, s);
- await dialog.hide();
- showGenericErrorDialog(context);
- }
- }
- Future<void> _showRecoveryCodeDialog(String password) async {
- final dialog =
- createProgressDialog(context, "Generating encryption keys...");
- await dialog.show();
- try {
- final result = await Configuration.instance.generateKey(password);
- Configuration.instance.setVolatilePassword(null);
- await dialog.hide();
- onDone() async {
- final dialog = createProgressDialog(context, "Please wait...");
- await dialog.show();
- try {
- await UserService.instance.setAttributes(result);
- await dialog.hide();
- Bus.instance.fire(AccountConfiguredEvent());
- Navigator.of(context).pushAndRemoveUntil(
- MaterialPageRoute(
- builder: (BuildContext context) {
- return getSubscriptionPage(isOnBoarding: true);
- },
- ),
- (route) => route.isFirst,
- );
- } catch (e, s) {
- _logger.severe(e, s);
- await dialog.hide();
- showGenericErrorDialog(context);
- }
- }
- routeToPage(
- context,
- RecoveryKeyPage(
- result.privateKeyAttributes.recoveryKey,
- "Continue",
- showAppBar: false,
- isDismissible: false,
- onDone: onDone,
- showProgressBar: true,
- ),
- );
- } catch (e) {
- _logger.severe(e);
- 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);
- }
- }
- }
- }
|