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-17 22:10:10 +03:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2022-09-12 16:46:43 +03:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2022-03-17 22:10:10 +03:00
|
|
|
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() {
|
2023-02-28 21:05:46 +03:00
|
|
|
final l10n = AppLocalizations.of(context)!;
|
2022-03-17 22:10:10 +03:00
|
|
|
final nfc = widget.node.transport == Transport.nfc;
|
|
|
|
switch (_interaction) {
|
|
|
|
case InteractionEvent.remove:
|
2023-02-28 21:05:46 +03:00
|
|
|
return nfc ? l10n.l_remove_yk_from_reader : l10n.l_unplug_yk;
|
2022-03-17 22:10:10 +03:00
|
|
|
case InteractionEvent.insert:
|
2023-02-28 21:05:46 +03:00
|
|
|
return nfc ? l10n.l_replace_yk_on_reader : l10n.l_reinsert_yk;
|
2022-03-17 22:10:10 +03:00
|
|
|
case InteractionEvent.touch:
|
2023-02-28 21:05:46 +03:00
|
|
|
return l10n.l_touch_button_now;
|
2022-03-17 22:10:10 +03:00
|
|
|
case null:
|
2023-02-28 21:05:46 +03:00
|
|
|
return l10n.l_press_reset_to_begin;
|
2022-03-17 22:10:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-02-28 21:05:46 +03:00
|
|
|
final l10n = AppLocalizations.of(context)!;
|
2022-03-17 22:10:10 +03:00
|
|
|
return ResponsiveDialog(
|
2023-03-02 14:45:55 +03:00
|
|
|
title: Text(l10n.s_factory_reset),
|
2022-03-17 22:10:10 +03:00
|
|
|
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();
|
2023-02-28 21:05:46 +03:00
|
|
|
showMessage(context, l10n.l_fido_app_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,
|
2023-03-01 12:30:32 +03:00
|
|
|
l10n.l_reset_failed(errorMessage),
|
2022-06-13 17:45:26 +03:00
|
|
|
duration: const Duration(seconds: 4),
|
|
|
|
);
|
2022-03-18 15:54:50 +03:00
|
|
|
});
|
2022-03-17 22:10:10 +03:00
|
|
|
}
|
|
|
|
: null,
|
2023-03-02 14:45:55 +03:00
|
|
|
child: Text(l10n.s_reset),
|
2022-03-17 22:10:10 +03:00
|
|
|
),
|
|
|
|
],
|
2022-09-07 11:25:22 +03:00
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 18.0),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
2023-02-07 16:38:23 +03:00
|
|
|
Text(
|
2023-02-28 21:05:46 +03:00
|
|
|
l10n.p_warning_deletes_accounts,
|
2023-02-07 16:38:23 +03:00
|
|
|
style: const TextStyle(fontWeight: FontWeight.bold),
|
|
|
|
),
|
2022-09-07 11:25:22 +03:00
|
|
|
Text(
|
2023-02-28 21:05:46 +03:00
|
|
|
l10n.p_warning_disable_accounts,
|
2022-09-07 11:25:22 +03:00
|
|
|
),
|
|
|
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|