yubioath-flutter/lib/app/models.dart

138 lines
4.0 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-05-12 12:06:28 +03:00
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
2021-11-19 17:05:57 +03:00
import 'package:freezed_annotation/freezed_annotation.dart';
2023-02-28 13:34:29 +03:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2022-05-12 12:06:28 +03:00
2021-11-19 17:05:57 +03:00
import '../../management/models.dart';
2022-03-16 14:43:56 +03:00
import '../core/models.dart';
2021-11-19 17:05:57 +03:00
part 'models.freezed.dart';
2022-03-16 14:43:56 +03:00
const _listEquality = ListEquality();
enum Availability { enabled, disabled, unsupported }
enum Application {
2023-02-28 13:34:29 +03:00
oath,
fido,
otp,
piv,
openpgp,
hsmauth,
management;
2023-02-28 13:34:29 +03:00
const Application();
2023-05-22 12:52:49 +03:00
bool _inCapabilities(int capabilities) => switch (this) {
Application.oath => Capability.oath.value & capabilities != 0,
Application.fido =>
(Capability.u2f.value | Capability.fido2.value) & capabilities != 0,
Application.otp => Capability.otp.value & capabilities != 0,
Application.piv => Capability.piv.value & capabilities != 0,
Application.openpgp => Capability.openpgp.value & capabilities != 0,
Application.hsmauth => Capability.hsmauth.value & capabilities != 0,
Application.management => true,
};
String getDisplayName(AppLocalizations l10n) => switch (this) {
Application.oath => l10n.s_authenticator,
Application.fido => l10n.s_webauthn,
2023-06-05 16:56:13 +03:00
Application.piv => l10n.s_piv,
2023-05-22 12:52:49 +03:00
_ => name.substring(0, 1).toUpperCase() + name.substring(1),
};
2023-02-28 13:34:29 +03:00
Availability getAvailability(YubiKeyData data) {
if (this == Application.management) {
final version = data.info.version;
final available = (version.major > 4 || // YK5 and up
(version.major == 4 && version.minor >= 1) || // YK4.1 and up
version.major == 3); // NEO
// Management can't be disabled
return available ? Availability.enabled : Availability.unsupported;
}
final int supported =
data.info.supportedCapabilities[data.node.transport] ?? 0;
final int enabled =
data.info.config.enabledCapabilities[data.node.transport] ?? 0;
return _inCapabilities(supported)
? (_inCapabilities(enabled)
? Availability.enabled
: Availability.disabled)
: Availability.unsupported;
}
}
2022-01-12 14:49:04 +03:00
@freezed
class YubiKeyData with _$YubiKeyData {
factory YubiKeyData(DeviceNode node, String name, DeviceInfo info) =
_YubiKeyData;
}
class DevicePath {
final List<String> segments;
DevicePath(List<String> path) : segments = List.unmodifiable(path);
@override
bool operator ==(Object other) =>
other is DevicePath && _listEquality.equals(segments, other.segments);
@override
int get hashCode => Object.hashAll(segments);
String get key => segments.join('/');
@override
String toString() => key;
}
2022-01-12 14:49:04 +03:00
@freezed
class DeviceNode with _$DeviceNode {
2022-03-07 11:57:29 +03:00
const DeviceNode._();
2022-01-12 14:49:04 +03:00
factory DeviceNode.usbYubiKey(
2022-03-16 14:43:56 +03:00
DevicePath path, String name, UsbPid pid, DeviceInfo? info) =
UsbYubiKeyNode;
factory DeviceNode.nfcReader(DevicePath path, String name) = NfcReaderNode;
2022-03-07 11:57:29 +03:00
Transport get transport =>
map(usbYubiKey: (_) => Transport.usb, nfcReader: (_) => Transport.nfc);
2022-01-12 14:49:04 +03:00
}
@freezed
class MenuAction with _$MenuAction {
2022-09-28 18:31:17 +03:00
factory MenuAction({
required String text,
required Widget icon,
String? trailing,
2023-02-10 19:37:42 +03:00
Intent? intent,
2022-09-28 18:31:17 +03:00
}) = _MenuAction;
}
@freezed
class WindowState with _$WindowState {
factory WindowState({
required bool focused,
required bool visible,
required bool active,
2023-02-24 16:10:39 +03:00
@Default(false) bool hidden,
}) = _WindowState;
}