yubioath-flutter/lib/desktop/devices.dart

307 lines
8.6 KiB
Dart
Raw Normal View History

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.
*/
import 'dart:async';
import 'dart:convert';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import '../app/logging.dart';
import '../app/models.dart';
import '../app/state.dart';
2022-03-16 14:43:56 +03:00
import '../core/models.dart';
import '../management/models.dart';
import 'models.dart';
import 'rpc.dart';
import 'state.dart';
const _usbPollDelay = Duration(milliseconds: 500);
const _nfcPollDelay = Duration(milliseconds: 2500);
const _nfcAttachPollDelay = Duration(seconds: 1);
const _nfcDetachPollDelay = Duration(seconds: 5);
final _log = Logger('desktop.devices');
final _usbDevicesProvider =
StateNotifierProvider<UsbDeviceNotifier, List<UsbYubiKeyNode>>((ref) {
2022-12-20 16:14:14 +03:00
final notifier = UsbDeviceNotifier(ref.watch(rpcProvider).valueOrNull);
ref.listen<WindowState>(windowStateProvider, (_, windowState) {
notifier._notifyWindowState(windowState);
}, fireImmediately: true);
return notifier;
});
class UsbDeviceNotifier extends StateNotifier<List<UsbYubiKeyNode>> {
2022-12-20 16:14:14 +03:00
final RpcSession? _rpc;
Timer? _pollTimer;
int _usbState = -1;
UsbDeviceNotifier(this._rpc) : super([]);
2022-03-07 11:57:29 +03:00
void refresh() {
2022-10-19 11:12:51 +03:00
_log.debug('Refreshing all USB devices');
2022-03-07 11:57:29 +03:00
_usbState = -1;
_pollDevices();
}
void _notifyWindowState(WindowState windowState) {
if (windowState.active) {
_pollDevices();
} else {
_pollTimer?.cancel();
// Release any held device
2022-12-20 16:14:14 +03:00
_rpc?.command('get', ['usb']);
}
}
@override
void dispose() {
_pollTimer?.cancel();
super.dispose();
}
void _pollDevices() async {
_pollTimer?.cancel();
2022-12-20 16:14:14 +03:00
final rpc = _rpc;
if (rpc == null) {
return;
}
try {
2022-12-20 16:14:14 +03:00
var scan = await rpc.command('scan', ['usb']);
2023-01-02 20:02:32 +03:00
if (!mounted) {
return;
}
final pids = {
for (var e in (scan['pids'] as Map).entries)
UsbPid.fromValue(int.parse(e.key)): e.value as int
};
final numDevices = pids.values.fold<int>(0, (a, b) => a + b);
2022-02-22 15:05:45 +03:00
if (_usbState != scan['state'] || state.length != numDevices) {
2022-12-20 16:14:14 +03:00
var usbResult = await rpc.command('get', ['usb']);
_log.info('USB state change', jsonEncode(usbResult));
_usbState = usbResult['data']['state'];
List<UsbYubiKeyNode> usbDevices = [];
for (String id in (usbResult['children'] as Map).keys) {
final path = ['usb', id];
2022-12-20 16:14:14 +03:00
final deviceResult = await rpc.command('get', path);
final deviceData = deviceResult['data'];
final pid = UsbPid.fromValue(deviceData['pid'] as int);
usbDevices.add(DeviceNode.usbYubiKey(
DevicePath(path),
deviceData['name'],
pid,
DeviceInfo.fromJson(deviceData['info']),
) as UsbYubiKeyNode);
pids.update(pid, (value) => value - 1);
}
pids.removeWhere((_, value) => value == 0);
if (pids.isNotEmpty) {
pids.forEach((pid, count) {
for (var i = 0; i < count; i++) {
usbDevices.add(DeviceNode.usbYubiKey(
DevicePath(['pid', pid.value.toString(), i.toString()]),
2022-03-16 14:43:56 +03:00
pid.displayName,
pid,
null) as UsbYubiKeyNode);
}
});
}
_log.info('USB state updated, unaccounted for: $pids');
if (mounted) {
state = usbDevices;
}
}
} on RpcError catch (e) {
2022-05-03 12:24:25 +03:00
_log.error('Error polling USB', jsonEncode(e));
}
if (mounted) {
_pollTimer = Timer(_usbPollDelay, _pollDevices);
}
}
}
final _nfcDevicesProvider =
StateNotifierProvider<NfcDeviceNotifier, List<NfcReaderNode>>((ref) {
2022-12-20 16:14:14 +03:00
final notifier = NfcDeviceNotifier(ref.watch(rpcProvider).valueOrNull);
ref.listen<WindowState>(windowStateProvider, (_, windowState) {
notifier._notifyWindowState(windowState);
}, fireImmediately: true);
return notifier;
});
class NfcDeviceNotifier extends StateNotifier<List<NfcReaderNode>> {
2022-12-20 16:14:14 +03:00
final RpcSession? _rpc;
Timer? _pollTimer;
String _nfcState = '';
NfcDeviceNotifier(this._rpc) : super([]);
void _notifyWindowState(WindowState windowState) {
if (windowState.active) {
_pollReaders();
} else {
_pollTimer?.cancel();
// Release any held device
2022-12-20 16:14:14 +03:00
_rpc?.command('get', ['nfc']);
}
}
@override
void dispose() {
_pollTimer?.cancel();
super.dispose();
}
void _pollReaders() async {
_pollTimer?.cancel();
2022-12-20 16:14:14 +03:00
final rpc = _rpc;
if (rpc == null) {
return;
}
try {
2022-12-20 16:14:14 +03:00
var children = await rpc.command('scan', ['nfc']);
var newState = children.keys.join(':');
if (mounted && newState != _nfcState) {
_log.info('NFC state change', jsonEncode(children));
_nfcState = newState;
state = children.entries
.map((e) => DeviceNode.nfcReader(
DevicePath(['nfc', e.key]), e.value['name'] as String)
as NfcReaderNode)
.toList();
}
} on RpcError catch (e) {
2022-05-03 12:24:25 +03:00
_log.error('Error polling NFC', jsonEncode(e));
}
if (mounted) {
_pollTimer = Timer(_nfcPollDelay, _pollReaders);
}
}
}
class DesktopDevicesNotifier extends AttachedDevicesNotifier {
@override
List<DeviceNode> build() {
final usbDevices = ref.watch(_usbDevicesProvider).toList();
final nfcDevices = ref.watch(_nfcDevicesProvider).toList();
usbDevices.sort((a, b) => a.name.compareTo(b.name));
nfcDevices.sort((a, b) => a.name.compareTo(b.name));
return [...usbDevices, ...nfcDevices];
}
2022-03-07 11:57:29 +03:00
@override
refresh() {
ref.read(_usbDevicesProvider.notifier).refresh();
2022-03-07 11:57:29 +03:00
}
}
final _desktopDeviceDataProvider =
StateNotifierProvider<CurrentDeviceDataNotifier, AsyncValue<YubiKeyData>>(
(ref) {
final notifier = CurrentDeviceDataNotifier(
2022-12-20 16:14:14 +03:00
ref.watch(rpcProvider).valueOrNull,
ref.watch(currentDeviceProvider),
);
if (notifier._deviceNode is NfcReaderNode) {
// If this is an NFC reader, listen on WindowState.
ref.listen<WindowState>(windowStateProvider, (_, windowState) {
notifier._notifyWindowState(windowState);
}, fireImmediately: true);
}
return notifier;
});
final desktopDeviceDataProvider = Provider<AsyncValue<YubiKeyData>>(
(ref) {
return ref.watch(_desktopDeviceDataProvider);
},
2022-03-07 11:57:29 +03:00
);
class CurrentDeviceDataNotifier extends StateNotifier<AsyncValue<YubiKeyData>> {
2022-12-20 16:14:14 +03:00
final RpcSession? _rpc;
final DeviceNode? _deviceNode;
Timer? _pollTimer;
CurrentDeviceDataNotifier(this._rpc, this._deviceNode)
: super(const AsyncValue.loading()) {
final dev = _deviceNode;
if (dev is UsbYubiKeyNode) {
final info = dev.info;
if (info != null) {
state = AsyncValue.data(YubiKeyData(dev, dev.name, info));
} else {
2022-10-19 11:12:25 +03:00
state = AsyncValue.error('device-inaccessible', StackTrace.current);
}
}
}
void _notifyWindowState(WindowState windowState) {
if (windowState.active) {
_pollReader();
} else {
_pollTimer?.cancel();
// TODO: Should we clear the key here?
/*if (mounted) {
state = null;
}*/
}
}
@override
void dispose() {
_pollTimer?.cancel();
super.dispose();
}
void _pollReader() async {
_pollTimer?.cancel();
final node = _deviceNode!;
try {
2022-05-03 12:24:25 +03:00
_log.debug('Polling for USB device changes...');
2022-12-20 16:14:14 +03:00
var result = await _rpc?.command('get', node.path.segments);
if (mounted && result != null) {
if (result['data']['present']) {
state = AsyncValue.data(YubiKeyData(node, result['data']['name'],
DeviceInfo.fromJson(result['data']['info'])));
} else {
2022-11-08 17:06:39 +03:00
final status = result['data']['status'];
// Only update if status is not changed
if (state.asError?.error != status) {
state = AsyncValue.error(status, StackTrace.current);
}
}
}
} on RpcError catch (e) {
2022-05-03 12:24:25 +03:00
_log.error('Error polling NFC', jsonEncode(e));
}
if (mounted) {
_pollTimer = Timer(
state is AsyncData ? _nfcDetachPollDelay : _nfcAttachPollDelay,
_pollReader);
}
}
}