yubioath-flutter/lib/android/command_providers.dart

54 lines
1.6 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 'package:yubico_authenticator/android/oath/command_providers.dart';
2022-03-03 18:43:36 +03:00
import '../app/models.dart';
import '../core/models.dart';
2022-03-03 18:43:36 +03:00
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, ref);
2022-03-03 18:43:36 +03:00
});
2022-03-03 19:24:26 +03:00
class _YubikeyProvider extends StateNotifier<YubiKeyData?> {
final Ref _ref;
_YubikeyProvider(YubiKeyData? yubiKeyData, this._ref) : 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;
// reset other providers when YubiKey is removed
_ref.read(androidStateProvider.notifier).reset();
_ref.read(androidCredentialsProvider.notifier).reset();
2022-03-03 18:43:36 +03:00
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;
}
}
}