yubioath-flutter/lib/desktop/management/state.dart

117 lines
3.7 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.
*/
2022-03-04 15:42:10 +03:00
import 'dart:async';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import '../../app/logging.dart';
2022-03-04 15:42:10 +03:00
import '../../app/models.dart';
import '../../app/state.dart';
2022-03-09 19:47:50 +03:00
import '../../core/models.dart';
import '../../management/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-12-20 16:14:14 +03:00
(ref, devicePath) =>
RpcNodeSession(ref.watch(rpcProvider).requireValue, devicePath, []),
2022-03-04 15:42:10 +03:00
);
final desktopManagementState = AsyncNotifierProvider.autoDispose
.family<ManagementStateNotifier, DeviceInfo, DevicePath>(
_DesktopManagementStateNotifier.new);
class _DesktopManagementStateNotifier extends ManagementStateNotifier {
late RpcNodeSession _session;
List<String> _subpath = [];
_DesktopManagementStateNotifier() : super();
@override
FutureOr<DeviceInfo> build(DevicePath devicePath) async {
2022-03-09 19:47:50 +03:00
// Make sure to rebuild if currentDevice changes (as on reboot)
ref.watch(currentDeviceProvider);
_session = ref.watch(_sessionProvider(devicePath));
_session.setErrorHandler('state-reset', (_) async {
ref.invalidate(_sessionProvider(devicePath));
2022-03-04 15:42:10 +03:00
});
ref.onDispose(() {
_session.unsetErrorHandler('state-reset');
2022-03-04 15:42:10 +03:00
});
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.debug('Using transport $iface for management');
return info;
} catch (e) {
_log.warning('Failed connecting to management via $iface');
2022-03-09 19:47:50 +03:00
}
}
}
throw 'Failed connection over all interfaces';
}
2022-03-04 15:42:10 +03:00
@override
2022-05-05 13:40:56 +03:00
Future<void> setMode(
{required int interfaces,
int challengeResponseTimeout = 0,
int? autoEjectTimeout}) async {
2022-03-09 19:47:50 +03:00
await _session.command('set_mode', target: _subpath, params: {
2022-05-05 13:40:56 +03:00
'interfaces': interfaces,
2022-03-04 15:42:10 +03:00
'challenge_response_timeout': challengeResponseTimeout,
'auto_eject_timeout': autoEjectTimeout,
});
ref.read(attachedDevicesProvider.notifier).refresh();
2022-03-04 15:42:10 +03:00
}
@override
Future<void> writeConfig(DeviceConfig config,
{String currentLockCode = '',
String newLockCode = '',
bool reboot = false}) async {
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,
});
ref.read(attachedDevicesProvider.notifier).refresh();
2022-03-04 15:42:10 +03:00
}
2024-02-02 15:52:22 +03:00
@override
Future<void> deviceReset() async {
await _session.command('device_reset', target: ['ccid', 'management']);
}
2022-03-04 15:42:10 +03:00
}