2022-03-04 15:42:10 +03:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:yubico_authenticator/management/models.dart';
|
|
|
|
|
|
|
|
import '../../app/models.dart';
|
|
|
|
import '../../app/state.dart';
|
2022-03-09 19:47:50 +03:00
|
|
|
import '../../core/models.dart';
|
2022-03-04 15:42:10 +03:00
|
|
|
import '../../management/state.dart';
|
|
|
|
import '../rpc.dart';
|
|
|
|
import '../state.dart';
|
|
|
|
|
|
|
|
final _log = Logger('desktop.management.state');
|
|
|
|
|
|
|
|
final _sessionProvider =
|
|
|
|
Provider.autoDispose.family<RpcNodeSession, DevicePath>(
|
2022-03-09 19:47:50 +03:00
|
|
|
(ref, devicePath) => RpcNodeSession(ref.watch(rpcProvider), devicePath, []),
|
2022-03-04 15:42:10 +03:00
|
|
|
);
|
|
|
|
|
2022-03-09 19:47:50 +03:00
|
|
|
final desktopManagementState = StateNotifierProvider.autoDispose.family<
|
|
|
|
ManagementStateNotifier, ApplicationStateResult<DeviceInfo>, DevicePath>(
|
2022-03-04 15:42:10 +03:00
|
|
|
(ref, devicePath) {
|
2022-03-09 19:47:50 +03:00
|
|
|
// Make sure to rebuild if currentDevice changes (as on reboot)
|
|
|
|
ref.watch(currentDeviceProvider);
|
2022-03-04 15:42:10 +03:00
|
|
|
final session = ref.watch(_sessionProvider(devicePath));
|
2022-03-07 11:57:29 +03:00
|
|
|
final notifier = _DesktopManagementStateNotifier(ref, session);
|
2022-03-04 15:42:10 +03:00
|
|
|
session.setErrorHandler('state-reset', (_) async {
|
|
|
|
ref.refresh(_sessionProvider(devicePath));
|
|
|
|
});
|
|
|
|
ref.onDispose(() {
|
2022-03-15 19:16:14 +03:00
|
|
|
session.unsetErrorHandler('state-reset');
|
2022-03-04 15:42:10 +03:00
|
|
|
});
|
|
|
|
return notifier..refresh();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
class _DesktopManagementStateNotifier extends ManagementStateNotifier {
|
2022-03-07 11:57:29 +03:00
|
|
|
final Ref _ref;
|
2022-03-04 15:42:10 +03:00
|
|
|
final RpcNodeSession _session;
|
2022-03-09 19:47:50 +03:00
|
|
|
List<String> _subpath = [];
|
2022-03-07 11:57:29 +03:00
|
|
|
_DesktopManagementStateNotifier(this._ref, this._session) : super();
|
2022-03-04 15:42:10 +03:00
|
|
|
|
|
|
|
void refresh() async {
|
2022-03-09 19:47:50 +03:00
|
|
|
try {
|
|
|
|
final result = await _session.command('get');
|
|
|
|
final info = DeviceInfo.fromJson(result['data']['info']);
|
|
|
|
final interfaces = (result['children'] as Map).keys.toSet();
|
|
|
|
for (final iface in [
|
|
|
|
// This is the preferred order
|
|
|
|
UsbInterface.ccid,
|
|
|
|
UsbInterface.otp,
|
|
|
|
UsbInterface.fido,
|
|
|
|
]) {
|
|
|
|
if (interfaces.contains(iface.name)) {
|
|
|
|
final path = [iface.name, 'management'];
|
|
|
|
try {
|
|
|
|
await _session.command('get', target: path);
|
|
|
|
_subpath = path;
|
|
|
|
_log.config('Using transport $iface for management');
|
|
|
|
setState(info);
|
|
|
|
return;
|
|
|
|
} catch (e) {
|
|
|
|
_log.warning('Failed connecting to management via $iface');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setFailure('Failed connecting over all interfaces');
|
|
|
|
} catch (error) {
|
|
|
|
_log.severe('Failed getting device info');
|
|
|
|
setFailure('Failed to connect');
|
2022-03-04 15:42:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> setMode(int mode,
|
|
|
|
{int challengeResponseTimeout = 0, int autoEjectTimeout = 0}) async {
|
2022-03-09 19:47:50 +03:00
|
|
|
await _session.command('set_mode', target: _subpath, params: {
|
2022-03-04 15:42:10 +03:00
|
|
|
'mode': mode,
|
|
|
|
'challenge_response_timeout': challengeResponseTimeout,
|
|
|
|
'auto_eject_timeout': autoEjectTimeout,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> writeConfig(DeviceConfig config,
|
|
|
|
{String currentLockCode = '',
|
|
|
|
String newLockCode = '',
|
|
|
|
bool reboot = false}) async {
|
|
|
|
if (reboot) {
|
2022-03-09 19:47:50 +03:00
|
|
|
unsetState();
|
2022-03-04 15:42:10 +03:00
|
|
|
}
|
2022-03-09 19:47:50 +03:00
|
|
|
await _session.command('configure', target: _subpath, params: {
|
2022-03-04 15:42:10 +03:00
|
|
|
...config.toJson(),
|
|
|
|
'cur_lock_code': currentLockCode,
|
|
|
|
'new_lock_code': newLockCode,
|
|
|
|
'reboot': reboot,
|
|
|
|
});
|
2022-03-07 11:57:29 +03:00
|
|
|
_ref.read(attachedDevicesProvider.notifier).refresh();
|
2022-03-04 15:42:10 +03:00
|
|
|
}
|
|
|
|
}
|