yubioath-flutter/lib/fido/views/reset_dialog.dart

102 lines
3.3 KiB
Dart
Raw Normal View History

2022-03-17 22:10:10 +03:00
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
2022-03-18 17:56:07 +03:00
import 'package:logging/logging.dart';
2022-05-03 12:24:25 +03:00
import 'package:yubico_authenticator/app/logging.dart';
2022-03-17 22:10:10 +03:00
2022-03-25 17:43:32 +03:00
import '../../app/message.dart';
2022-03-18 18:10:05 +03:00
import '../../core/models.dart';
import '../../widgets/responsive_dialog.dart';
2022-03-17 22:10:10 +03:00
import '../state.dart';
import '../../fido/models.dart';
import '../../app/models.dart';
2022-03-18 17:56:07 +03:00
final _log = Logger('fido.views.reset_dialog');
2022-03-17 22:10:10 +03:00
class ResetDialog extends ConsumerStatefulWidget {
final DeviceNode node;
2022-05-12 10:56:55 +03:00
const ResetDialog(this.node, {super.key});
2022-03-17 22:10:10 +03:00
@override
ConsumerState<ConsumerStatefulWidget> createState() => _ResetDialogState();
}
class _ResetDialogState extends ConsumerState<ResetDialog> {
StreamSubscription<InteractionEvent>? _subscription;
InteractionEvent? _interaction;
String _getMessage() {
final nfc = widget.node.transport == Transport.nfc;
switch (_interaction) {
case InteractionEvent.remove:
return nfc
? 'Remove your YubiKey from the NFC reader'
: 'Unplug your YubiKey';
case InteractionEvent.insert:
return nfc
? 'Place your YubiKey back on the reader'
: 'Re-insert your YubiKey';
case InteractionEvent.touch:
return 'Touch your YubiKey now';
case null:
return 'Press reset to begin...';
}
}
@override
Widget build(BuildContext context) {
return ResponsiveDialog(
title: const Text('Factory reset'),
onCancel: () {
_subscription?.cancel();
},
actions: [
TextButton(
onPressed: _subscription == null
? () async {
_subscription = ref
.read(fidoStateProvider(widget.node.path).notifier)
.reset()
.listen((event) {
setState(() {
_interaction = event;
});
}, onDone: () {
_subscription = null;
Navigator.of(context).pop();
2022-03-25 17:43:32 +03:00
showMessage(context, 'FIDO application reset');
2022-03-18 17:56:07 +03:00
}, onError: (e) {
2022-05-03 12:24:25 +03:00
_log.error('Error performing FIDO reset', e);
2022-03-18 17:56:07 +03:00
Navigator.of(context).pop();
2022-03-25 17:43:32 +03:00
showMessage(context, 'Error performing reset');
});
2022-03-17 22:10:10 +03:00
}
: null,
child: const Text('Reset'),
),
],
2022-05-12 09:34:51 +03:00
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Warning! This will irrevocably delete all U2F and FIDO2 accounts from your YubiKey.'),
Text(
'Your credentials, as well as any PIN set, will be removed from this YubiKey. Make sure to first disable these from their respective web sites to avoid being locked out of your accounts.',
style: Theme.of(context).textTheme.bodyText1,
),
Center(
child: Text(_getMessage(),
2022-05-18 11:30:50 +03:00
style: Theme.of(context).textTheme.titleLarge),
2022-05-12 09:34:51 +03:00
),
]
.map((e) => Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: e,
))
.toList(),
),
2022-03-17 22:10:10 +03:00
);
}
}