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-08-19 10:36:45 +03:00
|
|
|
import 'dart:async';
|
2022-08-16 15:05:53 +03:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
|
2022-08-19 10:36:45 +03:00
|
|
|
import '../app/logging.dart';
|
2022-08-16 15:05:53 +03:00
|
|
|
import '../app/models.dart';
|
|
|
|
import '../core/models.dart';
|
|
|
|
import '../management/models.dart';
|
|
|
|
|
|
|
|
final _log = Logger('android.devices');
|
|
|
|
|
|
|
|
final androidYubikeyProvider =
|
|
|
|
StateNotifierProvider<_YubikeyProvider, AsyncValue<YubiKeyData>>((ref) {
|
2022-08-19 10:36:45 +03:00
|
|
|
return _YubikeyProvider();
|
2022-08-16 15:05:53 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
class _YubikeyProvider extends StateNotifier<AsyncValue<YubiKeyData>> {
|
2022-08-19 10:36:45 +03:00
|
|
|
final _events = const EventChannel('android.devices.deviceInfo');
|
|
|
|
late StreamSubscription sub;
|
|
|
|
_YubikeyProvider() : super(const AsyncValue.loading()) {
|
|
|
|
sub = _events.receiveBroadcastStream().listen((event) {
|
|
|
|
_setDevice(jsonDecode(event));
|
2022-08-16 15:05:53 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2022-08-19 10:36:45 +03:00
|
|
|
sub.cancel();
|
2022-08-16 15:05:53 +03:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _setDevice(Map<String, dynamic>? json) async {
|
|
|
|
if (json == null) {
|
|
|
|
_log.debug('Yubikey was detached.');
|
|
|
|
state = const AsyncValue.loading();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
state = await AsyncValue.guard(() async {
|
|
|
|
DeviceInfo deviceInfo = DeviceInfo.fromJson(json);
|
|
|
|
String name = json['name'];
|
|
|
|
bool isNfc = json['is_nfc'];
|
|
|
|
int? usbPid = json['usb_pid'];
|
|
|
|
|
|
|
|
DeviceNode deviceNode = isNfc
|
|
|
|
? DeviceNode.nfcReader(DevicePath([]), name)
|
|
|
|
: DeviceNode.usbYubiKey(
|
|
|
|
DevicePath([]),
|
|
|
|
name,
|
|
|
|
usbPid != null ? UsbPid.fromValue(usbPid) : UsbPid.yk4OtpFidoCcid,
|
|
|
|
deviceInfo);
|
|
|
|
|
|
|
|
return YubiKeyData(deviceNode, name, deviceInfo);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|