diff --git a/lib/android/command_providers.dart b/lib/android/command_providers.dart index 6b633fe0..243b8ca8 100644 --- a/lib/android/command_providers.dart +++ b/lib/android/command_providers.dart @@ -32,7 +32,11 @@ class _YubikeyProvider extends StateNotifier { DeviceNode deviceNode = isNfc ? DeviceNode.nfcReader(DevicePath([]), name) - : DeviceNode.usbYubiKey(DevicePath([]), name, -1, deviceInfo); + : DeviceNode.usbYubiKey( + DevicePath([]), + name, + /*TODO: replace with correct PID*/ UsbPid.yk4OtpFidoCcid, + deviceInfo); state = YubiKeyData(deviceNode, name, deviceInfo); } on Exception catch (e) { _log.config('Invalid data for yubikey: $input. $e'); diff --git a/lib/app/models.dart b/lib/app/models.dart index 97b810cc..ef1b9e45 100755 --- a/lib/app/models.dart +++ b/lib/app/models.dart @@ -1,9 +1,12 @@ import 'package:flutter/material.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; import '../../management/models.dart'; +import '../core/models.dart'; part 'models.freezed.dart'; +const _listEquality = ListEquality(); + enum Availability { enabled, disabled, unsupported } enum Application { oath, fido, otp, piv, openpgp, hsmauth, management } @@ -71,14 +74,19 @@ extension Applications on Application { } } +@freezed +class ApplicationStateResult with _$ApplicationStateResult { + factory ApplicationStateResult.none() = _None; + factory ApplicationStateResult.failure(String reason) = _Failure; + factory ApplicationStateResult.success(T state) = _Success; +} + @freezed class YubiKeyData with _$YubiKeyData { factory YubiKeyData(DeviceNode node, String name, DeviceInfo info) = _YubiKeyData; } -const _listEquality = ListEquality(); - class DevicePath { final List segments; @@ -96,7 +104,8 @@ class DevicePath { class DeviceNode with _$DeviceNode { const DeviceNode._(); factory DeviceNode.usbYubiKey( - DevicePath path, String name, int pid, DeviceInfo? info) = UsbYubiKeyNode; + DevicePath path, String name, UsbPid pid, DeviceInfo? info) = + UsbYubiKeyNode; factory DeviceNode.nfcReader(DevicePath path, String name) = NfcReaderNode; Transport get transport => diff --git a/lib/app/models.freezed.dart b/lib/app/models.freezed.dart index a6011f8f..faf4b24b 100755 --- a/lib/app/models.freezed.dart +++ b/lib/app/models.freezed.dart @@ -13,6 +13,482 @@ T _$identity(T value) => value; final _privateConstructorUsedError = UnsupportedError( 'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more informations: https://github.com/rrousselGit/freezed#custom-getters-and-methods'); +/// @nodoc +class _$ApplicationStateResultTearOff { + const _$ApplicationStateResultTearOff(); + + _None none() { + return _None(); + } + + _Failure failure(String reason) { + return _Failure( + reason, + ); + } + + _Success success(T state) { + return _Success( + state, + ); + } +} + +/// @nodoc +const $ApplicationStateResult = _$ApplicationStateResultTearOff(); + +/// @nodoc +mixin _$ApplicationStateResult { + @optionalTypeArgs + TResult when({ + required TResult Function() none, + required TResult Function(String reason) failure, + required TResult Function(T state) success, + }) => + throw _privateConstructorUsedError; + @optionalTypeArgs + TResult? whenOrNull({ + TResult Function()? none, + TResult Function(String reason)? failure, + TResult Function(T state)? success, + }) => + throw _privateConstructorUsedError; + @optionalTypeArgs + TResult maybeWhen({ + TResult Function()? none, + TResult Function(String reason)? failure, + TResult Function(T state)? success, + required TResult orElse(), + }) => + throw _privateConstructorUsedError; + @optionalTypeArgs + TResult map({ + required TResult Function(_None value) none, + required TResult Function(_Failure value) failure, + required TResult Function(_Success value) success, + }) => + throw _privateConstructorUsedError; + @optionalTypeArgs + TResult? mapOrNull({ + TResult Function(_None value)? none, + TResult Function(_Failure value)? failure, + TResult Function(_Success value)? success, + }) => + throw _privateConstructorUsedError; + @optionalTypeArgs + TResult maybeMap({ + TResult Function(_None value)? none, + TResult Function(_Failure value)? failure, + TResult Function(_Success value)? success, + required TResult orElse(), + }) => + throw _privateConstructorUsedError; +} + +/// @nodoc +abstract class $ApplicationStateResultCopyWith { + factory $ApplicationStateResultCopyWith(ApplicationStateResult value, + $Res Function(ApplicationStateResult) then) = + _$ApplicationStateResultCopyWithImpl; +} + +/// @nodoc +class _$ApplicationStateResultCopyWithImpl + implements $ApplicationStateResultCopyWith { + _$ApplicationStateResultCopyWithImpl(this._value, this._then); + + final ApplicationStateResult _value; + // ignore: unused_field + final $Res Function(ApplicationStateResult) _then; +} + +/// @nodoc +abstract class _$NoneCopyWith { + factory _$NoneCopyWith(_None value, $Res Function(_None) then) = + __$NoneCopyWithImpl; +} + +/// @nodoc +class __$NoneCopyWithImpl + extends _$ApplicationStateResultCopyWithImpl + implements _$NoneCopyWith { + __$NoneCopyWithImpl(_None _value, $Res Function(_None) _then) + : super(_value, (v) => _then(v as _None)); + + @override + _None get _value => super._value as _None; +} + +/// @nodoc + +class _$_None implements _None { + _$_None(); + + @override + String toString() { + return 'ApplicationStateResult<$T>.none()'; + } + + @override + bool operator ==(dynamic other) { + return identical(this, other) || + (other.runtimeType == runtimeType && other is _None); + } + + @override + int get hashCode => runtimeType.hashCode; + + @override + @optionalTypeArgs + TResult when({ + required TResult Function() none, + required TResult Function(String reason) failure, + required TResult Function(T state) success, + }) { + return none(); + } + + @override + @optionalTypeArgs + TResult? whenOrNull({ + TResult Function()? none, + TResult Function(String reason)? failure, + TResult Function(T state)? success, + }) { + return none?.call(); + } + + @override + @optionalTypeArgs + TResult maybeWhen({ + TResult Function()? none, + TResult Function(String reason)? failure, + TResult Function(T state)? success, + required TResult orElse(), + }) { + if (none != null) { + return none(); + } + return orElse(); + } + + @override + @optionalTypeArgs + TResult map({ + required TResult Function(_None value) none, + required TResult Function(_Failure value) failure, + required TResult Function(_Success value) success, + }) { + return none(this); + } + + @override + @optionalTypeArgs + TResult? mapOrNull({ + TResult Function(_None value)? none, + TResult Function(_Failure value)? failure, + TResult Function(_Success value)? success, + }) { + return none?.call(this); + } + + @override + @optionalTypeArgs + TResult maybeMap({ + TResult Function(_None value)? none, + TResult Function(_Failure value)? failure, + TResult Function(_Success value)? success, + required TResult orElse(), + }) { + if (none != null) { + return none(this); + } + return orElse(); + } +} + +abstract class _None implements ApplicationStateResult { + factory _None() = _$_None; +} + +/// @nodoc +abstract class _$FailureCopyWith { + factory _$FailureCopyWith( + _Failure value, $Res Function(_Failure) then) = + __$FailureCopyWithImpl; + $Res call({String reason}); +} + +/// @nodoc +class __$FailureCopyWithImpl + extends _$ApplicationStateResultCopyWithImpl + implements _$FailureCopyWith { + __$FailureCopyWithImpl(_Failure _value, $Res Function(_Failure) _then) + : super(_value, (v) => _then(v as _Failure)); + + @override + _Failure get _value => super._value as _Failure; + + @override + $Res call({ + Object? reason = freezed, + }) { + return _then(_Failure( + reason == freezed + ? _value.reason + : reason // ignore: cast_nullable_to_non_nullable + as String, + )); + } +} + +/// @nodoc + +class _$_Failure implements _Failure { + _$_Failure(this.reason); + + @override + final String reason; + + @override + String toString() { + return 'ApplicationStateResult<$T>.failure(reason: $reason)'; + } + + @override + bool operator ==(dynamic other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _Failure && + const DeepCollectionEquality().equals(other.reason, reason)); + } + + @override + int get hashCode => + Object.hash(runtimeType, const DeepCollectionEquality().hash(reason)); + + @JsonKey(ignore: true) + @override + _$FailureCopyWith> get copyWith => + __$FailureCopyWithImpl>(this, _$identity); + + @override + @optionalTypeArgs + TResult when({ + required TResult Function() none, + required TResult Function(String reason) failure, + required TResult Function(T state) success, + }) { + return failure(reason); + } + + @override + @optionalTypeArgs + TResult? whenOrNull({ + TResult Function()? none, + TResult Function(String reason)? failure, + TResult Function(T state)? success, + }) { + return failure?.call(reason); + } + + @override + @optionalTypeArgs + TResult maybeWhen({ + TResult Function()? none, + TResult Function(String reason)? failure, + TResult Function(T state)? success, + required TResult orElse(), + }) { + if (failure != null) { + return failure(reason); + } + return orElse(); + } + + @override + @optionalTypeArgs + TResult map({ + required TResult Function(_None value) none, + required TResult Function(_Failure value) failure, + required TResult Function(_Success value) success, + }) { + return failure(this); + } + + @override + @optionalTypeArgs + TResult? mapOrNull({ + TResult Function(_None value)? none, + TResult Function(_Failure value)? failure, + TResult Function(_Success value)? success, + }) { + return failure?.call(this); + } + + @override + @optionalTypeArgs + TResult maybeMap({ + TResult Function(_None value)? none, + TResult Function(_Failure value)? failure, + TResult Function(_Success value)? success, + required TResult orElse(), + }) { + if (failure != null) { + return failure(this); + } + return orElse(); + } +} + +abstract class _Failure implements ApplicationStateResult { + factory _Failure(String reason) = _$_Failure; + + String get reason; + @JsonKey(ignore: true) + _$FailureCopyWith> get copyWith => + throw _privateConstructorUsedError; +} + +/// @nodoc +abstract class _$SuccessCopyWith { + factory _$SuccessCopyWith( + _Success value, $Res Function(_Success) then) = + __$SuccessCopyWithImpl; + $Res call({T state}); +} + +/// @nodoc +class __$SuccessCopyWithImpl + extends _$ApplicationStateResultCopyWithImpl + implements _$SuccessCopyWith { + __$SuccessCopyWithImpl(_Success _value, $Res Function(_Success) _then) + : super(_value, (v) => _then(v as _Success)); + + @override + _Success get _value => super._value as _Success; + + @override + $Res call({ + Object? state = freezed, + }) { + return _then(_Success( + state == freezed + ? _value.state + : state // ignore: cast_nullable_to_non_nullable + as T, + )); + } +} + +/// @nodoc + +class _$_Success implements _Success { + _$_Success(this.state); + + @override + final T state; + + @override + String toString() { + return 'ApplicationStateResult<$T>.success(state: $state)'; + } + + @override + bool operator ==(dynamic other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _Success && + const DeepCollectionEquality().equals(other.state, state)); + } + + @override + int get hashCode => + Object.hash(runtimeType, const DeepCollectionEquality().hash(state)); + + @JsonKey(ignore: true) + @override + _$SuccessCopyWith> get copyWith => + __$SuccessCopyWithImpl>(this, _$identity); + + @override + @optionalTypeArgs + TResult when({ + required TResult Function() none, + required TResult Function(String reason) failure, + required TResult Function(T state) success, + }) { + return success(state); + } + + @override + @optionalTypeArgs + TResult? whenOrNull({ + TResult Function()? none, + TResult Function(String reason)? failure, + TResult Function(T state)? success, + }) { + return success?.call(state); + } + + @override + @optionalTypeArgs + TResult maybeWhen({ + TResult Function()? none, + TResult Function(String reason)? failure, + TResult Function(T state)? success, + required TResult orElse(), + }) { + if (success != null) { + return success(state); + } + return orElse(); + } + + @override + @optionalTypeArgs + TResult map({ + required TResult Function(_None value) none, + required TResult Function(_Failure value) failure, + required TResult Function(_Success value) success, + }) { + return success(this); + } + + @override + @optionalTypeArgs + TResult? mapOrNull({ + TResult Function(_None value)? none, + TResult Function(_Failure value)? failure, + TResult Function(_Success value)? success, + }) { + return success?.call(this); + } + + @override + @optionalTypeArgs + TResult maybeMap({ + TResult Function(_None value)? none, + TResult Function(_Failure value)? failure, + TResult Function(_Success value)? success, + required TResult orElse(), + }) { + if (success != null) { + return success(this); + } + return orElse(); + } +} + +abstract class _Success implements ApplicationStateResult { + factory _Success(T state) = _$_Success; + + T get state; + @JsonKey(ignore: true) + _$SuccessCopyWith> get copyWith => + throw _privateConstructorUsedError; +} + /// @nodoc class _$YubiKeyDataTearOff { const _$YubiKeyDataTearOff(); @@ -205,7 +681,7 @@ class _$DeviceNodeTearOff { const _$DeviceNodeTearOff(); UsbYubiKeyNode usbYubiKey( - DevicePath path, String name, int pid, DeviceInfo? info) { + DevicePath path, String name, UsbPid pid, DeviceInfo? info) { return UsbYubiKeyNode( path, name, @@ -233,21 +709,23 @@ mixin _$DeviceNode { @optionalTypeArgs TResult when({ required TResult Function( - DevicePath path, String name, int pid, DeviceInfo? info) + DevicePath path, String name, UsbPid pid, DeviceInfo? info) usbYubiKey, required TResult Function(DevicePath path, String name) nfcReader, }) => throw _privateConstructorUsedError; @optionalTypeArgs TResult? whenOrNull({ - TResult Function(DevicePath path, String name, int pid, DeviceInfo? info)? + TResult Function( + DevicePath path, String name, UsbPid pid, DeviceInfo? info)? usbYubiKey, TResult Function(DevicePath path, String name)? nfcReader, }) => throw _privateConstructorUsedError; @optionalTypeArgs TResult maybeWhen({ - TResult Function(DevicePath path, String name, int pid, DeviceInfo? info)? + TResult Function( + DevicePath path, String name, UsbPid pid, DeviceInfo? info)? usbYubiKey, TResult Function(DevicePath path, String name)? nfcReader, required TResult orElse(), @@ -319,7 +797,7 @@ abstract class $UsbYubiKeyNodeCopyWith<$Res> UsbYubiKeyNode value, $Res Function(UsbYubiKeyNode) then) = _$UsbYubiKeyNodeCopyWithImpl<$Res>; @override - $Res call({DevicePath path, String name, int pid, DeviceInfo? info}); + $Res call({DevicePath path, String name, UsbPid pid, DeviceInfo? info}); $DeviceInfoCopyWith<$Res>? get info; } @@ -353,7 +831,7 @@ class _$UsbYubiKeyNodeCopyWithImpl<$Res> extends _$DeviceNodeCopyWithImpl<$Res> pid == freezed ? _value.pid : pid // ignore: cast_nullable_to_non_nullable - as int, + as UsbPid, info == freezed ? _value.info : info // ignore: cast_nullable_to_non_nullable @@ -383,7 +861,7 @@ class _$UsbYubiKeyNode extends UsbYubiKeyNode { @override final String name; @override - final int pid; + final UsbPid pid; @override final DeviceInfo? info; @@ -420,7 +898,7 @@ class _$UsbYubiKeyNode extends UsbYubiKeyNode { @optionalTypeArgs TResult when({ required TResult Function( - DevicePath path, String name, int pid, DeviceInfo? info) + DevicePath path, String name, UsbPid pid, DeviceInfo? info) usbYubiKey, required TResult Function(DevicePath path, String name) nfcReader, }) { @@ -430,7 +908,8 @@ class _$UsbYubiKeyNode extends UsbYubiKeyNode { @override @optionalTypeArgs TResult? whenOrNull({ - TResult Function(DevicePath path, String name, int pid, DeviceInfo? info)? + TResult Function( + DevicePath path, String name, UsbPid pid, DeviceInfo? info)? usbYubiKey, TResult Function(DevicePath path, String name)? nfcReader, }) { @@ -440,7 +919,8 @@ class _$UsbYubiKeyNode extends UsbYubiKeyNode { @override @optionalTypeArgs TResult maybeWhen({ - TResult Function(DevicePath path, String name, int pid, DeviceInfo? info)? + TResult Function( + DevicePath path, String name, UsbPid pid, DeviceInfo? info)? usbYubiKey, TResult Function(DevicePath path, String name)? nfcReader, required TResult orElse(), @@ -485,7 +965,7 @@ class _$UsbYubiKeyNode extends UsbYubiKeyNode { abstract class UsbYubiKeyNode extends DeviceNode { factory UsbYubiKeyNode( - DevicePath path, String name, int pid, DeviceInfo? info) = + DevicePath path, String name, UsbPid pid, DeviceInfo? info) = _$UsbYubiKeyNode; UsbYubiKeyNode._() : super._(); @@ -493,7 +973,7 @@ abstract class UsbYubiKeyNode extends DeviceNode { DevicePath get path; @override String get name; - int get pid; + UsbPid get pid; DeviceInfo? get info; @override @JsonKey(ignore: true) @@ -578,7 +1058,7 @@ class _$NfcReaderNode extends NfcReaderNode { @optionalTypeArgs TResult when({ required TResult Function( - DevicePath path, String name, int pid, DeviceInfo? info) + DevicePath path, String name, UsbPid pid, DeviceInfo? info) usbYubiKey, required TResult Function(DevicePath path, String name) nfcReader, }) { @@ -588,7 +1068,8 @@ class _$NfcReaderNode extends NfcReaderNode { @override @optionalTypeArgs TResult? whenOrNull({ - TResult Function(DevicePath path, String name, int pid, DeviceInfo? info)? + TResult Function( + DevicePath path, String name, UsbPid pid, DeviceInfo? info)? usbYubiKey, TResult Function(DevicePath path, String name)? nfcReader, }) { @@ -598,7 +1079,8 @@ class _$NfcReaderNode extends NfcReaderNode { @override @optionalTypeArgs TResult maybeWhen({ - TResult Function(DevicePath path, String name, int pid, DeviceInfo? info)? + TResult Function( + DevicePath path, String name, UsbPid pid, DeviceInfo? info)? usbYubiKey, TResult Function(DevicePath path, String name)? nfcReader, required TResult orElse(), diff --git a/lib/app/views/device_images.dart b/lib/app/views/device_images.dart index b719028a..7e150aed 100755 --- a/lib/app/views/device_images.dart +++ b/lib/app/views/device_images.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; +import '../../core/models.dart'; import '../../management/models.dart'; const _imagesForName = { diff --git a/lib/core/models.dart b/lib/core/models.dart index bb9a870c..0dc6b320 100644 --- a/lib/core/models.dart +++ b/lib/core/models.dart @@ -1,6 +1,117 @@ +import 'dart:convert'; + import 'package:freezed_annotation/freezed_annotation.dart'; +import '../management/models.dart'; + part 'models.freezed.dart'; +part 'models.g.dart'; + +enum Transport { usb, nfc } + +enum UsbInterface { otp, fido, ccid } + +@JsonEnum(alwaysCreate: true) +enum UsbPid { + @JsonValue(0x0010) + yksOtp, + @JsonValue(0x0110) + neoOtp, + @JsonValue(0x0111) + neoOtpCcid, + @JsonValue(0x0112) + neoCcid, + @JsonValue(0x0113) + neoFido, + @JsonValue(0x0114) + neoOtpFido, + @JsonValue(0x0115) + neoFidoCcid, + @JsonValue(0x0116) + neoOtpFidoCcid, + @JsonValue(0x0120) + skyFido, + @JsonValue(0x0401) + yk4Otp, + @JsonValue(0x0402) + yk4Fido, + @JsonValue(0x0403) + yk4OtpFido, + @JsonValue(0x0404) + yk4Ccid, + @JsonValue(0x0405) + yk4OtpCcid, + @JsonValue(0x0406) + yk4FidoCcid, + @JsonValue(0x0407) + yk4OtpFidoCcid, + @JsonValue(0x0410) + ykpOtpFido, +} + +extension UsbPids on UsbPid { + int get value => _$UsbPidEnumMap[this]!; + + String get displayName { + switch (this) { + case UsbPid.yksOtp: + return 'YubiKey Standard'; + case UsbPid.ykpOtpFido: + return 'YubiKey Plus'; + case UsbPid.skyFido: + return 'Security Key by Yubico'; + default: + final prefix = name.startsWith('neo') ? 'YubiKey NEO' : 'YubiKey'; + final suffix = UsbInterface.values + .where((e) => e.value & usbInterfaces != 0) + .map((e) => e.name.toUpperCase()) + .join(' '); + return '$prefix $suffix'; + } + } + + int get usbInterfaces => UsbInterface.values + .where( + (e) => name.contains(e.name[0].toUpperCase() + e.name.substring(1))) + .map((e) => e.value) + .sum; + + static UsbPid fromValue(int value) { + return UsbPid.values.firstWhere((pid) => pid.value == value); + } +} + +extension UsbInterfaces on UsbInterface { + int get value { + switch (this) { + case UsbInterface.otp: + return 0x01; + case UsbInterface.fido: + return 0x02; + case UsbInterface.ccid: + return 0x04; + } + } + + static int forCapabilites(int capabilities) { + var interfaces = 0; + if (capabilities & Capability.otp.value != 0) { + interfaces |= UsbInterface.otp.value; + } + if (capabilities & (Capability.u2f.value | Capability.fido2.value) != 0) { + interfaces |= UsbInterface.fido.value; + } + if (capabilities & + (Capability.openpgp.value | + Capability.piv.value | + Capability.oath.value | + Capability.hsmauth.value) != + 0) { + interfaces |= UsbInterface.ccid.value; + } + return interfaces; + } +} @freezed class Version with _$Version { @@ -23,10 +134,3 @@ class Version with _$Version { class Pair with _$Pair { factory Pair(T1 first, T2 second) = _Pair; } - -@freezed -class ApplicationStateResult with _$ApplicationStateResult { - factory ApplicationStateResult.none() = _None; - factory ApplicationStateResult.failure(String reason) = _Failure; - factory ApplicationStateResult.success(T state) = _Success; -} diff --git a/lib/core/models.freezed.dart b/lib/core/models.freezed.dart index 1211c6bf..1764149d 100755 --- a/lib/core/models.freezed.dart +++ b/lib/core/models.freezed.dart @@ -311,479 +311,3 @@ abstract class _Pair implements Pair { _$PairCopyWith> get copyWith => throw _privateConstructorUsedError; } - -/// @nodoc -class _$ApplicationStateResultTearOff { - const _$ApplicationStateResultTearOff(); - - _None none() { - return _None(); - } - - _Failure failure(String reason) { - return _Failure( - reason, - ); - } - - _Success success(T state) { - return _Success( - state, - ); - } -} - -/// @nodoc -const $ApplicationStateResult = _$ApplicationStateResultTearOff(); - -/// @nodoc -mixin _$ApplicationStateResult { - @optionalTypeArgs - TResult when({ - required TResult Function() none, - required TResult Function(String reason) failure, - required TResult Function(T state) success, - }) => - throw _privateConstructorUsedError; - @optionalTypeArgs - TResult? whenOrNull({ - TResult Function()? none, - TResult Function(String reason)? failure, - TResult Function(T state)? success, - }) => - throw _privateConstructorUsedError; - @optionalTypeArgs - TResult maybeWhen({ - TResult Function()? none, - TResult Function(String reason)? failure, - TResult Function(T state)? success, - required TResult orElse(), - }) => - throw _privateConstructorUsedError; - @optionalTypeArgs - TResult map({ - required TResult Function(_None value) none, - required TResult Function(_Failure value) failure, - required TResult Function(_Success value) success, - }) => - throw _privateConstructorUsedError; - @optionalTypeArgs - TResult? mapOrNull({ - TResult Function(_None value)? none, - TResult Function(_Failure value)? failure, - TResult Function(_Success value)? success, - }) => - throw _privateConstructorUsedError; - @optionalTypeArgs - TResult maybeMap({ - TResult Function(_None value)? none, - TResult Function(_Failure value)? failure, - TResult Function(_Success value)? success, - required TResult orElse(), - }) => - throw _privateConstructorUsedError; -} - -/// @nodoc -abstract class $ApplicationStateResultCopyWith { - factory $ApplicationStateResultCopyWith(ApplicationStateResult value, - $Res Function(ApplicationStateResult) then) = - _$ApplicationStateResultCopyWithImpl; -} - -/// @nodoc -class _$ApplicationStateResultCopyWithImpl - implements $ApplicationStateResultCopyWith { - _$ApplicationStateResultCopyWithImpl(this._value, this._then); - - final ApplicationStateResult _value; - // ignore: unused_field - final $Res Function(ApplicationStateResult) _then; -} - -/// @nodoc -abstract class _$NoneCopyWith { - factory _$NoneCopyWith(_None value, $Res Function(_None) then) = - __$NoneCopyWithImpl; -} - -/// @nodoc -class __$NoneCopyWithImpl - extends _$ApplicationStateResultCopyWithImpl - implements _$NoneCopyWith { - __$NoneCopyWithImpl(_None _value, $Res Function(_None) _then) - : super(_value, (v) => _then(v as _None)); - - @override - _None get _value => super._value as _None; -} - -/// @nodoc - -class _$_None implements _None { - _$_None(); - - @override - String toString() { - return 'ApplicationStateResult<$T>.none()'; - } - - @override - bool operator ==(dynamic other) { - return identical(this, other) || - (other.runtimeType == runtimeType && other is _None); - } - - @override - int get hashCode => runtimeType.hashCode; - - @override - @optionalTypeArgs - TResult when({ - required TResult Function() none, - required TResult Function(String reason) failure, - required TResult Function(T state) success, - }) { - return none(); - } - - @override - @optionalTypeArgs - TResult? whenOrNull({ - TResult Function()? none, - TResult Function(String reason)? failure, - TResult Function(T state)? success, - }) { - return none?.call(); - } - - @override - @optionalTypeArgs - TResult maybeWhen({ - TResult Function()? none, - TResult Function(String reason)? failure, - TResult Function(T state)? success, - required TResult orElse(), - }) { - if (none != null) { - return none(); - } - return orElse(); - } - - @override - @optionalTypeArgs - TResult map({ - required TResult Function(_None value) none, - required TResult Function(_Failure value) failure, - required TResult Function(_Success value) success, - }) { - return none(this); - } - - @override - @optionalTypeArgs - TResult? mapOrNull({ - TResult Function(_None value)? none, - TResult Function(_Failure value)? failure, - TResult Function(_Success value)? success, - }) { - return none?.call(this); - } - - @override - @optionalTypeArgs - TResult maybeMap({ - TResult Function(_None value)? none, - TResult Function(_Failure value)? failure, - TResult Function(_Success value)? success, - required TResult orElse(), - }) { - if (none != null) { - return none(this); - } - return orElse(); - } -} - -abstract class _None implements ApplicationStateResult { - factory _None() = _$_None; -} - -/// @nodoc -abstract class _$FailureCopyWith { - factory _$FailureCopyWith( - _Failure value, $Res Function(_Failure) then) = - __$FailureCopyWithImpl; - $Res call({String reason}); -} - -/// @nodoc -class __$FailureCopyWithImpl - extends _$ApplicationStateResultCopyWithImpl - implements _$FailureCopyWith { - __$FailureCopyWithImpl(_Failure _value, $Res Function(_Failure) _then) - : super(_value, (v) => _then(v as _Failure)); - - @override - _Failure get _value => super._value as _Failure; - - @override - $Res call({ - Object? reason = freezed, - }) { - return _then(_Failure( - reason == freezed - ? _value.reason - : reason // ignore: cast_nullable_to_non_nullable - as String, - )); - } -} - -/// @nodoc - -class _$_Failure implements _Failure { - _$_Failure(this.reason); - - @override - final String reason; - - @override - String toString() { - return 'ApplicationStateResult<$T>.failure(reason: $reason)'; - } - - @override - bool operator ==(dynamic other) { - return identical(this, other) || - (other.runtimeType == runtimeType && - other is _Failure && - const DeepCollectionEquality().equals(other.reason, reason)); - } - - @override - int get hashCode => - Object.hash(runtimeType, const DeepCollectionEquality().hash(reason)); - - @JsonKey(ignore: true) - @override - _$FailureCopyWith> get copyWith => - __$FailureCopyWithImpl>(this, _$identity); - - @override - @optionalTypeArgs - TResult when({ - required TResult Function() none, - required TResult Function(String reason) failure, - required TResult Function(T state) success, - }) { - return failure(reason); - } - - @override - @optionalTypeArgs - TResult? whenOrNull({ - TResult Function()? none, - TResult Function(String reason)? failure, - TResult Function(T state)? success, - }) { - return failure?.call(reason); - } - - @override - @optionalTypeArgs - TResult maybeWhen({ - TResult Function()? none, - TResult Function(String reason)? failure, - TResult Function(T state)? success, - required TResult orElse(), - }) { - if (failure != null) { - return failure(reason); - } - return orElse(); - } - - @override - @optionalTypeArgs - TResult map({ - required TResult Function(_None value) none, - required TResult Function(_Failure value) failure, - required TResult Function(_Success value) success, - }) { - return failure(this); - } - - @override - @optionalTypeArgs - TResult? mapOrNull({ - TResult Function(_None value)? none, - TResult Function(_Failure value)? failure, - TResult Function(_Success value)? success, - }) { - return failure?.call(this); - } - - @override - @optionalTypeArgs - TResult maybeMap({ - TResult Function(_None value)? none, - TResult Function(_Failure value)? failure, - TResult Function(_Success value)? success, - required TResult orElse(), - }) { - if (failure != null) { - return failure(this); - } - return orElse(); - } -} - -abstract class _Failure implements ApplicationStateResult { - factory _Failure(String reason) = _$_Failure; - - String get reason; - @JsonKey(ignore: true) - _$FailureCopyWith> get copyWith => - throw _privateConstructorUsedError; -} - -/// @nodoc -abstract class _$SuccessCopyWith { - factory _$SuccessCopyWith( - _Success value, $Res Function(_Success) then) = - __$SuccessCopyWithImpl; - $Res call({T state}); -} - -/// @nodoc -class __$SuccessCopyWithImpl - extends _$ApplicationStateResultCopyWithImpl - implements _$SuccessCopyWith { - __$SuccessCopyWithImpl(_Success _value, $Res Function(_Success) _then) - : super(_value, (v) => _then(v as _Success)); - - @override - _Success get _value => super._value as _Success; - - @override - $Res call({ - Object? state = freezed, - }) { - return _then(_Success( - state == freezed - ? _value.state - : state // ignore: cast_nullable_to_non_nullable - as T, - )); - } -} - -/// @nodoc - -class _$_Success implements _Success { - _$_Success(this.state); - - @override - final T state; - - @override - String toString() { - return 'ApplicationStateResult<$T>.success(state: $state)'; - } - - @override - bool operator ==(dynamic other) { - return identical(this, other) || - (other.runtimeType == runtimeType && - other is _Success && - const DeepCollectionEquality().equals(other.state, state)); - } - - @override - int get hashCode => - Object.hash(runtimeType, const DeepCollectionEquality().hash(state)); - - @JsonKey(ignore: true) - @override - _$SuccessCopyWith> get copyWith => - __$SuccessCopyWithImpl>(this, _$identity); - - @override - @optionalTypeArgs - TResult when({ - required TResult Function() none, - required TResult Function(String reason) failure, - required TResult Function(T state) success, - }) { - return success(state); - } - - @override - @optionalTypeArgs - TResult? whenOrNull({ - TResult Function()? none, - TResult Function(String reason)? failure, - TResult Function(T state)? success, - }) { - return success?.call(state); - } - - @override - @optionalTypeArgs - TResult maybeWhen({ - TResult Function()? none, - TResult Function(String reason)? failure, - TResult Function(T state)? success, - required TResult orElse(), - }) { - if (success != null) { - return success(state); - } - return orElse(); - } - - @override - @optionalTypeArgs - TResult map({ - required TResult Function(_None value) none, - required TResult Function(_Failure value) failure, - required TResult Function(_Success value) success, - }) { - return success(this); - } - - @override - @optionalTypeArgs - TResult? mapOrNull({ - TResult Function(_None value)? none, - TResult Function(_Failure value)? failure, - TResult Function(_Success value)? success, - }) { - return success?.call(this); - } - - @override - @optionalTypeArgs - TResult maybeMap({ - TResult Function(_None value)? none, - TResult Function(_Failure value)? failure, - TResult Function(_Success value)? success, - required TResult orElse(), - }) { - if (success != null) { - return success(this); - } - return orElse(); - } -} - -abstract class _Success implements ApplicationStateResult { - factory _Success(T state) = _$_Success; - - T get state; - @JsonKey(ignore: true) - _$SuccessCopyWith> get copyWith => - throw _privateConstructorUsedError; -} diff --git a/lib/core/models.g.dart b/lib/core/models.g.dart new file mode 100755 index 00000000..1e741574 --- /dev/null +++ b/lib/core/models.g.dart @@ -0,0 +1,27 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'models.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +const _$UsbPidEnumMap = { + UsbPid.yksOtp: 16, + UsbPid.neoOtp: 272, + UsbPid.neoOtpCcid: 273, + UsbPid.neoCcid: 274, + UsbPid.neoFido: 275, + UsbPid.neoOtpFido: 276, + UsbPid.neoFidoCcid: 277, + UsbPid.neoOtpFidoCcid: 278, + UsbPid.skyFido: 288, + UsbPid.yk4Otp: 1025, + UsbPid.yk4Fido: 1026, + UsbPid.yk4OtpFido: 1027, + UsbPid.yk4Ccid: 1028, + UsbPid.yk4OtpCcid: 1029, + UsbPid.yk4FidoCcid: 1030, + UsbPid.yk4OtpFidoCcid: 1031, + UsbPid.ykpOtpFido: 1040, +}; diff --git a/lib/core/state.dart b/lib/core/state.dart index 1e4f2ef6..16264841 100644 --- a/lib/core/state.dart +++ b/lib/core/state.dart @@ -5,7 +5,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:logging/logging.dart'; import 'package:shared_preferences/shared_preferences.dart'; -import 'models.dart'; +import '../app/models.dart'; final isDesktop = Platform.isWindows || Platform.isMacOS || Platform.isLinux; final isAndroid = Platform.isAndroid; diff --git a/lib/desktop/devices.dart b/lib/desktop/devices.dart index 335ee9c4..fc84b1a9 100755 --- a/lib/desktop/devices.dart +++ b/lib/desktop/devices.dart @@ -6,6 +6,7 @@ import 'package:logging/logging.dart'; import '../app/models.dart'; import '../app/state.dart'; +import '../core/models.dart'; import '../management/models.dart'; import 'models.dart'; import 'rpc.dart'; @@ -63,7 +64,7 @@ class UsbDeviceNotifier extends StateNotifier> { var scan = await _rpc.command('scan', ['usb']); final pids = { for (var e in (scan['pids'] as Map).entries) - int.parse(e.key): e.value as int + UsbPids.fromValue(int.parse(e.key)): e.value as int }; final numDevices = pids.values.fold(0, (a, b) => a + b); if (_usbState != scan['state'] || state.length != numDevices) { @@ -76,7 +77,7 @@ class UsbDeviceNotifier extends StateNotifier> { final path = ['usb', id]; final deviceResult = await _rpc.command('get', path); final deviceData = deviceResult['data']; - final pid = deviceData['pid'] as int; + final pid = UsbPids.fromValue(deviceData['pid'] as int); usbDevices.add(DeviceNode.usbYubiKey( DevicePath(path), deviceData['name'], @@ -92,7 +93,7 @@ class UsbDeviceNotifier extends StateNotifier> { for (var i = 0; i < count; i++) { usbDevices.add(DeviceNode.usbYubiKey( DevicePath(['invalid', '$pid-$i']), - 'YubiKey ($pid)', + pid.displayName, pid, null) as UsbYubiKeyNode); } diff --git a/lib/management/models.dart b/lib/management/models.dart index dbf2a96f..c4d35b91 100755 --- a/lib/management/models.dart +++ b/lib/management/models.dart @@ -5,8 +5,6 @@ import '../core/models.dart'; part 'models.freezed.dart'; part 'models.g.dart'; -enum Transport { usb, nfc } - enum FormFactor { @JsonValue(0) unknown, @@ -26,40 +24,6 @@ enum FormFactor { usbCBio, } -enum UsbInterface { otp, fido, ccid } - -extension UsbInterfaces on UsbInterface { - int get value { - switch (this) { - case UsbInterface.otp: - return 0x01; - case UsbInterface.fido: - return 0x02; - case UsbInterface.ccid: - return 0x04; - } - } - - static int forCapabilites(int capabilities) { - var interfaces = 0; - if (capabilities & Capability.otp.value != 0) { - interfaces |= UsbInterface.otp.value; - } - if (capabilities & (Capability.u2f.value | Capability.fido2.value) != 0) { - interfaces |= UsbInterface.fido.value; - } - if (capabilities & - (Capability.openpgp.value | - Capability.piv.value | - Capability.oath.value | - Capability.hsmauth.value) != - 0) { - interfaces |= UsbInterface.ccid.value; - } - return interfaces; - } -} - enum Capability { otp, piv, oath, openpgp, hsmauth, u2f, fido2 } extension CapabilityExtension on Capability { diff --git a/lib/management/views/management_screen.dart b/lib/management/views/management_screen.dart index d169f1cd..0b57de9b 100755 --- a/lib/management/views/management_screen.dart +++ b/lib/management/views/management_screen.dart @@ -7,6 +7,7 @@ import '../../app/state.dart'; import '../../app/views/app_failure_screen.dart'; import '../../app/views/app_loading_screen.dart'; import '../../app/views/responsive_dialog.dart'; +import '../../core/models.dart'; import '../models.dart'; import '../state.dart';