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

168 lines
5.5 KiB
Dart
Raw Normal View History

2022-03-25 16:40:45 +03:00
import 'package:flutter/material.dart';
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 '../../app/state.dart';
import '../../widgets/responsive_dialog.dart';
2022-03-25 16:40:45 +03:00
import '../models.dart';
import '../state.dart';
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
ConsumerState<ConsumerStatefulWidget> createState() =>
_ManagePasswordDialogState();
}
class _ManagePasswordDialogState extends ConsumerState<ManagePasswordDialog> {
String _currentPassword = '';
String _newPassword = '';
String _confirmPassword = '';
bool _currentIsWrong = false;
_submit() async {
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, 'Password set');
} else {
setState(() {
_currentIsWrong = true;
});
}
}
2022-03-25 16:40:45 +03:00
@override
Widget build(BuildContext context) {
final isValid = _newPassword.isNotEmpty &&
_newPassword == _confirmPassword &&
(!widget.state.hasKey || _currentPassword.isNotEmpty);
2022-03-25 16:40:45 +03:00
return ResponsiveDialog(
title: const Text('Manage password'),
2022-05-12 09:34:51 +03:00
actions: [
TextButton(
onPressed: isValid ? _submit : null,
child: const Text('Save'),
)
],
2022-03-25 16:40:45 +03:00
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (widget.state.hasKey) ...[
const Text(
"Enter your current password. If you don't know your password, you'll need to reset the YubiKey."),
2022-03-25 16:40:45 +03:00
TextField(
autofocus: true,
obscureText: true,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: 'Current password',
2022-06-09 12:23:34 +03:00
prefixIcon: const Icon(Icons.password_outlined),
errorText: _currentIsWrong ? 'Wrong password' : null,
errorMaxLines: 3),
2022-03-25 16:40:45 +03:00
onChanged: (value) {
setState(() {
_currentIsWrong = false;
2022-03-25 16:40:45 +03:00
_currentPassword = value;
});
},
),
Wrap(
spacing: 4.0,
runSpacing: 8.0,
2022-03-25 16:40:45 +03:00
children: [
OutlinedButton(
onPressed: _currentPassword.isNotEmpty
? () async {
final result = await ref
.read(oathStateProvider(widget.path).notifier)
.unsetPassword(_currentPassword);
if (result) {
2022-05-12 09:34:51 +03:00
if (!mounted) return;
2022-03-25 16:40:45 +03:00
Navigator.of(context).pop();
2022-03-25 17:43:32 +03:00
showMessage(context, 'Password removed');
2022-03-25 16:40:45 +03:00
} else {
setState(() {
_currentIsWrong = true;
});
}
}
: null,
2022-05-12 09:34:51 +03:00
child: const Text('Remove password'),
2022-03-25 16:40:45 +03:00
),
if (widget.state.remembered)
OutlinedButton(
child: const Text('Clear saved password'),
onPressed: () async {
await ref
.read(oathStateProvider(widget.path).notifier)
.forgetPassword();
2022-05-12 09:34:51 +03:00
if (!mounted) return;
2022-03-25 16:40:45 +03:00
Navigator.of(context).pop();
2022-03-25 17:43:32 +03:00
showMessage(context, 'Password forgotten');
2022-03-25 16:40:45 +03:00
},
),
],
),
],
const Text(
'Enter your new password. A password may contain letters, numbers and special characters.'),
2022-03-25 16:40:45 +03:00
TextField(
autofocus: !widget.state.hasKey,
obscureText: true,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: 'New password',
2022-06-09 12:23:34 +03:00
prefixIcon: const Icon(Icons.password_outlined),
2022-03-25 16:40:45 +03:00
enabled: !widget.state.hasKey || _currentPassword.isNotEmpty,
),
onChanged: (value) {
setState(() {
_newPassword = value;
});
},
onSubmitted: (_) {
if (isValid) {
_submit();
}
},
2022-03-25 16:40:45 +03:00
),
TextField(
obscureText: true,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: 'Confirm password',
2022-06-09 12:23:34 +03:00
prefixIcon: const Icon(Icons.password_outlined),
enabled: (!widget.state.hasKey || _currentPassword.isNotEmpty) &&
_newPassword.isNotEmpty,
2022-03-25 16:40:45 +03:00
),
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(),
),
);
}
}