yubioath-flutter/lib/oath/views/manage_password_dialog.dart

196 lines
6.8 KiB
Dart
Raw Normal View History

2022-10-04 13:12:54 +03:00
/*
* Copyright (C) 2022 Yubico.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2022-03-25 16:40:45 +03:00
import 'package:flutter/material.dart';
2022-09-05 16:10:44 +03:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2022-03-25 16:40:45 +03:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
2022-03-25 17:43:32 +03:00
import '../../app/message.dart';
2022-03-25 16:40:45 +03:00
import '../../app/models.dart';
import '../../widgets/responsive_dialog.dart';
2022-03-25 16:40:45 +03:00
import '../models.dart';
import '../state.dart';
2022-09-12 13:58:17 +03:00
import '../keys.dart' as keys;
2022-03-25 16:40:45 +03:00
class ManagePasswordDialog extends ConsumerStatefulWidget {
final DevicePath path;
final OathState state;
2022-05-12 10:56:55 +03:00
const ManagePasswordDialog(this.path, this.state, {super.key});
2022-03-25 16:40:45 +03:00
@override
2022-09-09 13:55:56 +03:00
ConsumerState<ConsumerStatefulWidget> createState() =>
_ManagePasswordDialogState();
2022-03-25 16:40:45 +03:00
}
class _ManagePasswordDialogState extends ConsumerState<ManagePasswordDialog> {
String _currentPassword = '';
String _newPassword = '';
String _confirmPassword = '';
bool _currentIsWrong = false;
_submit() async {
2022-09-09 13:55:56 +03:00
final result = await ref
.read(oathStateProvider(widget.path).notifier)
.setPassword(_currentPassword, _newPassword);
if (result) {
2022-05-12 09:34:51 +03:00
if (!mounted) return;
Navigator.of(context).pop();
showMessage(context, AppLocalizations.of(context)!.s_password_set);
} else {
setState(() {
_currentIsWrong = true;
});
}
}
2022-03-25 16:40:45 +03:00
@override
Widget build(BuildContext context) {
2023-02-28 21:05:46 +03:00
final l10n = AppLocalizations.of(context)!;
final isValid = _newPassword.isNotEmpty &&
_newPassword == _confirmPassword &&
(!widget.state.hasKey || _currentPassword.isNotEmpty);
2022-03-25 16:40:45 +03:00
return ResponsiveDialog(
title: Text(l10n.s_manage_password),
2022-05-12 09:34:51 +03:00
actions: [
TextButton(
onPressed: isValid ? _submit : null,
2022-09-12 13:58:17 +03:00
key: keys.savePasswordButton,
child: Text(l10n.s_save),
2022-05-12 09:34:51 +03:00
)
],
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 18.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (widget.state.hasKey) ...[
2023-02-28 21:05:46 +03:00
Text(l10n.p_enter_current_password_or_reset),
TextField(
autofocus: true,
obscureText: true,
2022-09-12 13:58:17 +03:00
key: keys.currentPasswordField,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: l10n.s_current_password,
prefixIcon: const Icon(Icons.password_outlined),
errorText: _currentIsWrong ? l10n.s_wrong_password : null,
errorMaxLines: 3),
textInputAction: TextInputAction.next,
onChanged: (value) {
setState(() {
_currentIsWrong = false;
_currentPassword = value;
});
},
),
Wrap(
spacing: 4.0,
runSpacing: 8.0,
children: [
OutlinedButton(
2022-09-12 13:58:17 +03:00
key: keys.removePasswordButton,
onPressed: _currentPassword.isNotEmpty
? () async {
final result = await ref
.read(oathStateProvider(widget.path).notifier)
.unsetPassword(_currentPassword);
if (result) {
if (!mounted) return;
Navigator.of(context).pop();
showMessage(context, l10n.s_password_removed);
} else {
setState(() {
_currentIsWrong = true;
});
}
}
: null,
child: Text(l10n.s_remove_password),
),
if (widget.state.remembered)
OutlinedButton(
child: Text(l10n.s_clear_saved_password),
onPressed: () async {
await ref
.read(oathStateProvider(widget.path).notifier)
.forgetPassword();
if (!mounted) return;
Navigator.of(context).pop();
showMessage(context, l10n.s_password_forgotten);
},
),
],
),
],
2023-02-28 21:05:46 +03:00
Text(l10n.p_enter_new_password),
2022-03-25 16:40:45 +03:00
TextField(
2022-09-12 13:58:17 +03:00
key: keys.newPasswordField,
autofocus: !widget.state.hasKey,
2022-03-25 16:40:45 +03:00
obscureText: true,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: l10n.s_new_password,
prefixIcon: const Icon(Icons.password_outlined),
enabled: !widget.state.hasKey || _currentPassword.isNotEmpty,
),
textInputAction: TextInputAction.next,
2022-03-25 16:40:45 +03:00
onChanged: (value) {
setState(() {
_newPassword = value;
2022-03-25 16:40:45 +03:00
});
},
onSubmitted: (_) {
if (isValid) {
_submit();
}
},
2022-03-25 16:40:45 +03:00
),
TextField(
2022-09-12 13:58:17 +03:00
key: keys.confirmPasswordField,
obscureText: true,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: l10n.s_confirm_password,
prefixIcon: const Icon(Icons.password_outlined),
enabled:
(!widget.state.hasKey || _currentPassword.isNotEmpty) &&
_newPassword.isNotEmpty,
),
textInputAction: TextInputAction.done,
onChanged: (value) {
setState(() {
_confirmPassword = value;
});
},
onSubmitted: (_) {
if (isValid) {
_submit();
}
},
2022-03-25 16:40:45 +03:00
),
]
.map((e) => Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: e,
))
.toList(),
),
2022-03-25 16:40:45 +03:00
),
);
}
}