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 ' ;
2022-06-13 17:45:26 +03:00
import ' ../../desktop/models.dart ' ;
2022-03-31 12:50:40 +03:00
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 ( )
2022-03-18 15:54:50 +03:00
. 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-06-13 17:45:26 +03:00
final String errorMessage ;
// TODO: Make this cleaner than importing desktop specific RpcError.
if ( e is RpcError ) {
errorMessage = e . message ;
} else {
errorMessage = e . toString ( ) ;
}
showMessage (
context ,
' Error performing reset: $ errorMessage ' ,
duration: const Duration ( seconds: 4 ) ,
) ;
2022-03-18 15:54:50 +03:00
} ) ;
2022-03-17 22:10:10 +03:00
}
: null ,
child: const Text ( ' Reset ' ) ,
) ,
] ,
2022-09-07 11:25:22 +03:00
child: Padding (
padding: const EdgeInsets . symmetric ( horizontal: 18.0 ) ,
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 ( ) ,
style: Theme . of ( context ) . textTheme . titleLarge ) ,
) ,
]
. map ( ( e ) = > Padding (
padding: const EdgeInsets . symmetric ( vertical: 8.0 ) ,
child: e ,
) )
. toList ( ) ,
) ,
2022-05-12 09:34:51 +03:00
) ,
2022-03-17 22:10:10 +03:00
) ;
}
}