yubioath-flutter/lib/android/command_providers.dart

47 lines
1.3 KiB
Dart
Raw Normal View History

2022-03-03 18:43:36 +03:00
import 'dart:convert';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import '../app/models.dart';
import '../management/models.dart';
2022-03-03 19:24:26 +03:00
final _log = Logger('yubikeyDataCommandProvider');
2022-03-03 18:43:36 +03:00
2022-03-03 19:24:26 +03:00
final androidYubikeyProvider =
StateNotifierProvider<_YubikeyProvider, YubiKeyData?>((ref) {
return _YubikeyProvider(null);
2022-03-03 18:43:36 +03:00
});
2022-03-03 19:24:26 +03:00
class _YubikeyProvider extends StateNotifier<YubiKeyData?> {
_YubikeyProvider(YubiKeyData? yubiKeyData) : super(yubiKeyData);
2022-03-03 18:43:36 +03:00
void setFromString(String input) {
2022-03-03 18:43:36 +03:00
try {
if (input.isEmpty) {
2022-03-04 18:51:56 +03:00
_log.config('Yubikey was detached.');
2022-03-03 18:43:36 +03:00
state = null;
return;
}
var args = jsonDecode(input);
DeviceInfo deviceInfo = DeviceInfo.fromJson(args);
String name = args['name'];
2022-03-16 12:52:22 +03:00
bool isNfc = args['is_nfc'];
2022-03-03 18:43:36 +03:00
2022-03-16 12:52:22 +03:00
DeviceNode deviceNode = isNfc
2022-03-03 18:43:36 +03:00
? DeviceNode.nfcReader(DevicePath([]), name)
2022-03-16 14:43:56 +03:00
: DeviceNode.usbYubiKey(
DevicePath([]),
name,
/*TODO: replace with correct PID*/ UsbPid.yk4OtpFidoCcid,
deviceInfo);
2022-03-03 18:43:36 +03:00
state = YubiKeyData(deviceNode, name, deviceInfo);
} on Exception catch (e) {
2022-03-04 18:51:56 +03:00
_log.config('Invalid data for yubikey: $input. $e');
2022-03-03 18:43:36 +03:00
state = null;
}
}
}