2022-03-04 15:42:10 +03:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import 'package:collection/collection.dart';
|
|
|
|
|
2022-03-25 17:43:32 +03:00
|
|
|
import '../../app/message.dart';
|
2022-03-04 15:42:10 +03:00
|
|
|
import '../../app/models.dart';
|
2022-03-15 20:04:26 +03:00
|
|
|
import '../../app/views/app_loading_screen.dart';
|
2022-03-16 14:43:56 +03:00
|
|
|
import '../../core/models.dart';
|
2022-06-05 14:49:16 +03:00
|
|
|
import '../../widgets/custom_icons.dart';
|
2022-03-31 12:50:40 +03:00
|
|
|
import '../../widgets/responsive_dialog.dart';
|
2022-03-04 15:42:10 +03:00
|
|
|
import '../models.dart';
|
|
|
|
import '../state.dart';
|
|
|
|
|
|
|
|
final _mapEquals = const DeepCollectionEquality().equals;
|
|
|
|
|
|
|
|
class _CapabilityForm extends StatelessWidget {
|
|
|
|
final int capabilities;
|
|
|
|
final int enabled;
|
|
|
|
final Function(int) onChanged;
|
2022-05-12 10:56:55 +03:00
|
|
|
const _CapabilityForm({
|
|
|
|
required this.capabilities,
|
|
|
|
required this.enabled,
|
|
|
|
required this.onChanged,
|
|
|
|
});
|
2022-03-04 15:42:10 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Wrap(
|
2022-05-20 15:38:23 +03:00
|
|
|
spacing: 8,
|
|
|
|
runSpacing: 16,
|
2022-03-04 15:42:10 +03:00
|
|
|
children: Capability.values
|
|
|
|
.where((c) => capabilities & c.value != 0)
|
|
|
|
.map((c) => FilterChip(
|
|
|
|
showCheckmark: true,
|
|
|
|
selected: enabled & c.value != 0,
|
|
|
|
label: Text(c.name),
|
|
|
|
onSelected: (_) {
|
|
|
|
onChanged(enabled ^ c.value);
|
|
|
|
},
|
|
|
|
))
|
|
|
|
.toList(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 13:40:56 +03:00
|
|
|
class _ModeForm extends StatelessWidget {
|
|
|
|
final int interfaces;
|
|
|
|
final Function(int) onChanged;
|
2022-05-12 10:56:55 +03:00
|
|
|
const _ModeForm(this.interfaces, {required this.onChanged});
|
2022-03-14 12:57:00 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Column(children: [
|
|
|
|
...UsbInterface.values.map(
|
|
|
|
(iface) => CheckboxListTile(
|
|
|
|
title: Text(iface.name.toUpperCase()),
|
2022-05-05 13:40:56 +03:00
|
|
|
value: iface.value & interfaces != 0,
|
2022-03-14 12:57:00 +03:00
|
|
|
onChanged: (_) {
|
2022-05-05 13:40:56 +03:00
|
|
|
onChanged(interfaces ^ iface.value);
|
2022-03-14 12:57:00 +03:00
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
2022-05-05 13:40:56 +03:00
|
|
|
Text(interfaces == 0 ? 'At least one interface must be enabled' : ''),
|
2022-03-14 12:57:00 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 20:04:26 +03:00
|
|
|
class _CapabilitiesForm extends StatelessWidget {
|
|
|
|
final Map<Transport, int> supported;
|
|
|
|
final Map<Transport, int> enabled;
|
|
|
|
final Function(Map<Transport, int> enabled) onChanged;
|
2022-03-04 15:42:10 +03:00
|
|
|
|
2022-03-15 20:04:26 +03:00
|
|
|
const _CapabilitiesForm({
|
|
|
|
required this.onChanged,
|
|
|
|
required this.supported,
|
|
|
|
required this.enabled,
|
2022-05-12 10:56:55 +03:00
|
|
|
});
|
2022-03-04 15:42:10 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-03-15 20:04:26 +03:00
|
|
|
final usbCapabilities = supported[Transport.usb] ?? 0;
|
|
|
|
final nfcCapabilities = supported[Transport.nfc] ?? 0;
|
2022-03-04 15:42:10 +03:00
|
|
|
|
|
|
|
return Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
2022-05-20 15:38:23 +03:00
|
|
|
if (usbCapabilities != 0) ...[
|
2022-03-04 15:42:10 +03:00
|
|
|
const ListTile(
|
|
|
|
leading: Icon(Icons.usb),
|
2022-05-20 15:38:23 +03:00
|
|
|
title: Text('USB'),
|
|
|
|
contentPadding: EdgeInsets.only(bottom: 8),
|
|
|
|
horizontalTitleGap: 0,
|
2022-03-04 15:42:10 +03:00
|
|
|
),
|
2022-05-20 15:38:23 +03:00
|
|
|
_CapabilityForm(
|
|
|
|
capabilities: usbCapabilities,
|
|
|
|
enabled: enabled[Transport.usb] ?? 0,
|
|
|
|
onChanged: (value) {
|
|
|
|
onChanged({...enabled, Transport.usb: value});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
if (nfcCapabilities != 0) ...[
|
|
|
|
if (usbCapabilities != 0)
|
|
|
|
const Padding(
|
|
|
|
padding: EdgeInsets.only(top: 24, bottom: 12),
|
|
|
|
child: Divider(),
|
|
|
|
),
|
2022-06-05 14:49:16 +03:00
|
|
|
ListTile(
|
|
|
|
leading: nfcIcon,
|
|
|
|
title: const Text('NFC'),
|
|
|
|
contentPadding: const EdgeInsets.only(bottom: 8),
|
2022-05-20 15:38:23 +03:00
|
|
|
horizontalTitleGap: 0,
|
2022-03-04 15:42:10 +03:00
|
|
|
),
|
2022-05-20 15:38:23 +03:00
|
|
|
_CapabilityForm(
|
|
|
|
capabilities: nfcCapabilities,
|
|
|
|
enabled: enabled[Transport.nfc] ?? 0,
|
|
|
|
onChanged: (value) {
|
|
|
|
onChanged({...enabled, Transport.nfc: value});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
]
|
2022-03-04 15:42:10 +03:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 20:04:26 +03:00
|
|
|
class ManagementScreen extends ConsumerStatefulWidget {
|
2022-03-04 15:42:10 +03:00
|
|
|
final YubiKeyData deviceData;
|
2022-05-12 10:56:55 +03:00
|
|
|
const ManagementScreen(this.deviceData, {super.key});
|
2022-03-04 15:42:10 +03:00
|
|
|
|
2022-03-15 20:04:26 +03:00
|
|
|
@override
|
|
|
|
ConsumerState<ConsumerStatefulWidget> createState() =>
|
|
|
|
_ManagementScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ManagementScreenState extends ConsumerState<ManagementScreen> {
|
|
|
|
late Map<Transport, int> _enabled;
|
2022-05-05 13:40:56 +03:00
|
|
|
late int _interfaces;
|
2022-03-15 20:04:26 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_enabled = widget.deviceData.info.config.enabledCapabilities;
|
2022-05-12 11:14:11 +03:00
|
|
|
_interfaces = UsbInterface.forCapabilites(
|
2022-05-05 13:40:56 +03:00
|
|
|
widget.deviceData.info.config.enabledCapabilities[Transport.usb] ?? 0);
|
2022-03-15 20:04:26 +03:00
|
|
|
}
|
|
|
|
|
2022-03-14 12:57:00 +03:00
|
|
|
Widget _buildCapabilitiesForm(
|
2022-03-15 20:04:26 +03:00
|
|
|
BuildContext context, WidgetRef ref, DeviceInfo info) {
|
|
|
|
return _CapabilitiesForm(
|
|
|
|
supported: widget.deviceData.info.supportedCapabilities,
|
|
|
|
enabled: _enabled,
|
|
|
|
onChanged: (enabled) {
|
|
|
|
setState(() {
|
|
|
|
_enabled = enabled;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _submitCapabilitiesForm() async {
|
|
|
|
final bool reboot;
|
|
|
|
if (widget.deviceData.node is UsbYubiKeyNode) {
|
|
|
|
// Reboot if USB device descriptor is changed.
|
2022-05-12 11:14:11 +03:00
|
|
|
final oldInterfaces = UsbInterface.forCapabilites(
|
2022-03-15 20:04:26 +03:00
|
|
|
widget.deviceData.info.config.enabledCapabilities[Transport.usb] ??
|
|
|
|
0);
|
|
|
|
final newInterfaces =
|
2022-05-12 11:14:11 +03:00
|
|
|
UsbInterface.forCapabilites(_enabled[Transport.usb] ?? 0);
|
2022-03-15 20:04:26 +03:00
|
|
|
reboot = oldInterfaces != newInterfaces;
|
|
|
|
} else {
|
|
|
|
reboot = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Function()? close;
|
|
|
|
try {
|
|
|
|
if (reboot) {
|
|
|
|
// This will take longer, show a message
|
2022-03-25 17:43:32 +03:00
|
|
|
close = showMessage(
|
|
|
|
context,
|
|
|
|
'Reconfiguring YubiKey...',
|
|
|
|
duration: const Duration(seconds: 8),
|
|
|
|
).close;
|
2022-03-15 20:04:26 +03:00
|
|
|
}
|
|
|
|
await ref
|
|
|
|
.read(managementStateProvider(widget.deviceData.node.path).notifier)
|
|
|
|
.writeConfig(
|
|
|
|
widget.deviceData.info.config
|
|
|
|
.copyWith(enabledCapabilities: _enabled),
|
|
|
|
reboot: reboot,
|
|
|
|
);
|
2022-05-12 09:34:51 +03:00
|
|
|
if (!mounted) return;
|
2022-04-04 20:59:49 +03:00
|
|
|
if (!reboot) Navigator.pop(context);
|
2022-03-25 17:43:32 +03:00
|
|
|
showMessage(context, 'Configuration updated');
|
2022-03-15 20:04:26 +03:00
|
|
|
} finally {
|
|
|
|
close?.call();
|
|
|
|
}
|
|
|
|
}
|
2022-03-14 12:57:00 +03:00
|
|
|
|
|
|
|
Widget _buildModeForm(BuildContext context, WidgetRef ref, DeviceInfo info) =>
|
|
|
|
_ModeForm(
|
2022-05-05 13:40:56 +03:00
|
|
|
_interfaces,
|
|
|
|
onChanged: (interfaces) {
|
|
|
|
setState(() {
|
|
|
|
_interfaces = interfaces;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
void _submitModeForm() async {
|
|
|
|
await ref
|
|
|
|
.read(managementStateProvider(widget.deviceData.node.path).notifier)
|
|
|
|
.setMode(interfaces: _interfaces);
|
2022-05-12 09:34:51 +03:00
|
|
|
if (!mounted) return;
|
2022-05-05 13:40:56 +03:00
|
|
|
showMessage(
|
2022-05-06 09:34:16 +03:00
|
|
|
context,
|
|
|
|
widget.deviceData.node.maybeMap(
|
|
|
|
nfcReader: (_) => 'Configuration updated',
|
|
|
|
orElse: () =>
|
|
|
|
'Configuration updated, remove and reinsert your YubiKey'));
|
|
|
|
Navigator.pop(context);
|
2022-05-05 13:40:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void _submitForm() {
|
|
|
|
if (widget.deviceData.info.version.major > 4) {
|
|
|
|
_submitCapabilitiesForm();
|
|
|
|
} else {
|
|
|
|
_submitModeForm();
|
|
|
|
}
|
|
|
|
}
|
2022-03-14 12:57:00 +03:00
|
|
|
|
2022-03-04 15:42:10 +03:00
|
|
|
@override
|
2022-03-15 20:04:26 +03:00
|
|
|
Widget build(BuildContext context) {
|
2022-05-17 15:02:43 +03:00
|
|
|
var canSave = false;
|
|
|
|
final child =
|
|
|
|
ref.watch(managementStateProvider(widget.deviceData.node.path)).when(
|
|
|
|
loading: () => const AppLoadingScreen(),
|
2022-05-20 18:19:39 +03:00
|
|
|
error: (error, _) => Center(
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
error.toString(),
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2022-05-17 15:02:43 +03:00
|
|
|
data: (info) {
|
|
|
|
bool hasConfig = info.version.major > 4;
|
|
|
|
if (hasConfig) {
|
2022-06-14 17:26:35 +03:00
|
|
|
canSave = _enabled[Transport.usb] != 0 &&
|
|
|
|
!_mapEquals(
|
|
|
|
_enabled,
|
|
|
|
info.config.enabledCapabilities,
|
|
|
|
);
|
2022-05-17 15:02:43 +03:00
|
|
|
} else {
|
|
|
|
canSave = _interfaces != 0 &&
|
|
|
|
_interfaces !=
|
|
|
|
UsbInterface.forCapabilites(widget.deviceData.info
|
|
|
|
.config.enabledCapabilities[Transport.usb] ??
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
return Column(
|
|
|
|
children: [
|
|
|
|
hasConfig
|
|
|
|
? _buildCapabilitiesForm(context, ref, info)
|
|
|
|
: _buildModeForm(context, ref, info),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-03-15 20:04:26 +03:00
|
|
|
return ResponsiveDialog(
|
|
|
|
title: const Text('Toggle applications'),
|
2022-05-12 09:34:51 +03:00
|
|
|
actions: [
|
|
|
|
TextButton(
|
2022-05-17 15:02:43 +03:00
|
|
|
onPressed: canSave ? _submitForm : null,
|
2022-05-12 09:34:51 +03:00
|
|
|
child: const Text('Save'),
|
|
|
|
),
|
|
|
|
],
|
2022-05-17 15:02:43 +03:00
|
|
|
child: child,
|
2022-03-15 20:04:26 +03:00
|
|
|
);
|
|
|
|
}
|
2022-03-04 15:42:10 +03:00
|
|
|
}
|