yubioath-flutter/lib/piv/views/piv_screen.dart

335 lines
13 KiB
Dart
Raw Normal View History

2023-04-27 10:13:38 +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.
*/
2024-01-09 16:26:52 +03:00
import 'dart:async';
2023-04-27 10:13:38 +03:00
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
2024-03-08 11:30:47 +03:00
import 'package:material_symbols_icons/symbols.dart';
2023-04-27 10:13:38 +03:00
import '../../app/message.dart';
import '../../app/models.dart';
import '../../app/shortcuts.dart';
2024-01-09 14:50:26 +03:00
import '../../app/views/action_list.dart';
2023-04-27 10:13:38 +03:00
import '../../app/views/app_failure_page.dart';
2023-06-15 18:39:17 +03:00
import '../../app/views/app_list_item.dart';
2023-04-27 10:13:38 +03:00
import '../../app/views/app_page.dart';
import '../../app/views/message_page.dart';
import '../../core/state.dart';
import '../../management/models.dart';
2023-06-15 18:39:17 +03:00
import '../../widgets/list_title.dart';
import '../features.dart' as features;
2023-11-28 13:30:22 +03:00
import '../keys.dart';
2023-04-27 10:13:38 +03:00
import '../models.dart';
import '../state.dart';
2023-06-15 18:39:17 +03:00
import 'actions.dart';
2024-01-09 14:50:26 +03:00
import 'cert_info_view.dart';
2023-04-27 10:13:38 +03:00
import 'key_actions.dart';
import 'slot_dialog.dart';
2024-01-10 22:47:54 +03:00
class PivScreen extends ConsumerStatefulWidget {
2023-04-27 10:13:38 +03:00
final DevicePath devicePath;
2024-01-10 22:47:54 +03:00
PivScreen(this.devicePath) : super(key: ObjectKey(devicePath));
2023-04-27 10:13:38 +03:00
@override
2024-01-10 22:47:54 +03:00
ConsumerState<ConsumerStatefulWidget> createState() => _PivScreenState();
}
class _PivScreenState extends ConsumerState<PivScreen> {
SlotId? _selected;
@override
Widget build(BuildContext context) {
2023-04-27 10:13:38 +03:00
final l10n = AppLocalizations.of(context)!;
final hasFeature = ref.watch(featureProvider);
2024-01-10 22:47:54 +03:00
return ref.watch(pivStateProvider(widget.devicePath)).when(
loading: () => MessagePage(
title: l10n.s_certificates,
capabilities: const [Capability.piv],
centered: true,
graphic: const CircularProgressIndicator(),
2023-04-27 10:13:38 +03:00
delayedContent: true,
),
error: (error, _) => AppFailurePage(
cause: error,
),
data: (pivState) {
2024-01-10 22:47:54 +03:00
final pivSlots =
ref.watch(pivSlotsProvider(widget.devicePath)).asData;
final selected = _selected != null
? pivSlots?.value.firstWhere((e) => e.slot == _selected)
: null;
2024-03-20 17:42:17 +03:00
final normalSlots = pivSlots?.value
.where((element) => !element.slot.isRetired)
.toList() ??
[];
final shownRetiredSlots = pivSlots?.value
.where((element) =>
element.slot.isRetired &&
(element.certInfo != null || element.metadata != null))
2024-03-20 17:42:17 +03:00
.toList() ??
[];
2024-01-09 14:50:26 +03:00
final theme = Theme.of(context);
final textTheme = theme.textTheme;
// This is what ListTile uses for subtitle
final subtitleStyle = textTheme.bodyMedium!.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
2024-01-09 14:50:26 +03:00
);
return PivActions(
devicePath: widget.devicePath,
pivState: pivState,
builder: (context) => Actions(
actions: {
EscapeIntent:
CallbackAction<EscapeIntent>(onInvoke: (intent) {
if (selected != null) {
setState(() {
_selected = null;
});
} else {
Actions.invoke(context, intent);
}
return false;
}),
OpenIntent<PivSlot>: CallbackAction<OpenIntent<PivSlot>>(
onInvoke: (intent) async {
await showBlurDialog(
context: context,
barrierColor: Colors.transparent,
builder: (context) => SlotDialog(intent.target.slot),
);
return null;
},
),
},
child: AppPage(
2024-01-15 17:58:40 +03:00
title: l10n.s_certificates,
2024-01-26 13:59:37 +03:00
capabilities: const [Capability.piv],
detailViewBuilder: selected != null
? (context) => Column(
2024-01-10 19:52:27 +03:00
crossAxisAlignment: CrossAxisAlignment.stretch,
2024-01-09 14:50:26 +03:00
children: [
2024-01-10 19:52:27 +03:00
ListTitle(l10n.s_details),
Padding(
padding: const EdgeInsets.only(left: 16.0),
child: Card(
elevation: 0.0,
color: Theme.of(context).hoverColor,
child: Padding(
2024-04-25 16:26:33 +03:00
padding: const EdgeInsets.symmetric(
horizontal: 16, vertical: 24),
child: Column(
children: [
Text(
selected.slot.getDisplayName(l10n),
style: textTheme.headlineSmall,
softWrap: true,
textAlign: TextAlign.center,
),
const SizedBox(height: 16),
if (selected.certInfo != null ||
selected.metadata != null) ...[
CertInfoTable(
selected.certInfo,
selected.metadata,
alwaysIncludePrivate:
pivState.supportsMetadata,
),
2024-04-25 16:26:33 +03:00
if (selected.certInfo == null)
const SizedBox(height: 16)
],
if (selected.certInfo == null)
Text(
l10n.l_no_certificate,
softWrap: true,
textAlign: TextAlign.center,
style: subtitleStyle,
),
],
),
2024-01-09 14:50:26 +03:00
),
),
2024-01-10 19:52:27 +03:00
),
2024-01-09 14:50:26 +03:00
ActionListSection.fromMenuActions(
context,
l10n.s_actions,
2024-03-18 13:32:03 +03:00
actions:
buildSlotActions(pivState, selected, l10n),
2024-01-09 14:50:26 +03:00
),
],
)
: null,
keyActionsBuilder: hasFeature(features.actions)
? (context) => pivBuildActions(
context, widget.devicePath, pivState, ref)
: null,
keyActionsBadge: pivShowActionsNotifier(pivState),
builder: (context, expanded) {
// De-select if window is resized to be non-expanded.
if (!expanded && _selected != null) {
Timer.run(() {
setState(() {
_selected = null;
});
2024-01-10 22:47:54 +03:00
});
}
return Actions(
actions: {
if (expanded)
OpenIntent<PivSlot>:
CallbackAction<OpenIntent<PivSlot>>(
onInvoke: (intent) async {
setState(() {
_selected = intent.target.slot;
});
return null;
}),
},
child: Column(
children: [
2024-03-20 17:42:17 +03:00
...normalSlots.map(
(e) => _CertificateListItem(
pivState,
e,
expanded: expanded,
selected: e == selected,
),
),
...shownRetiredSlots.map(
(e) => _CertificateListItem(
pivState,
e,
expanded: expanded,
selected: e == selected,
),
)
],
),
);
},
),
2023-04-27 10:13:38 +03:00
),
);
},
);
}
}
class _CertificateListItem extends ConsumerWidget {
2024-03-18 13:32:03 +03:00
final PivState pivState;
2023-04-27 10:13:38 +03:00
final PivSlot pivSlot;
2024-01-09 16:26:52 +03:00
final bool expanded;
2024-01-10 22:47:54 +03:00
final bool selected;
2023-11-28 13:30:22 +03:00
2024-03-18 13:32:03 +03:00
const _CertificateListItem(this.pivState, this.pivSlot,
2024-01-10 22:47:54 +03:00
{required this.expanded, required this.selected});
2023-04-27 10:13:38 +03:00
@override
Widget build(BuildContext context, WidgetRef ref) {
2023-04-27 10:13:38 +03:00
final slot = pivSlot.slot;
final certInfo = pivSlot.certInfo;
final l10n = AppLocalizations.of(context)!;
final colorScheme = Theme.of(context).colorScheme;
final hasFeature = ref.watch(featureProvider);
2023-06-15 18:39:17 +03:00
2024-01-09 16:26:52 +03:00
return AppListItem(
pivSlot,
2024-01-09 16:26:52 +03:00
selected: selected,
key: _getAppListItemKey(slot),
leading: CircleAvatar(
foregroundColor: colorScheme.onSecondary,
backgroundColor: colorScheme.secondary,
2024-03-27 13:31:50 +03:00
child: Text(pivSlot.slot.hexId),
2024-01-09 16:26:52 +03:00
),
2024-03-27 13:31:50 +03:00
title: slot.getSlotName(l10n),
2024-01-09 16:26:52 +03:00
subtitle: certInfo != null
// Simplify subtitle by stripping "CN=", etc.
? certInfo.subject.replaceAll(RegExp(r'[A-Z]+='), ' ').trimLeft()
: pivSlot.metadata != null
2024-01-09 16:26:52 +03:00
? l10n.l_key_no_certificate
: l10n.l_no_certificate,
trailing: expanded
? null
: OutlinedButton(
key: _getMeatballKey(slot),
onPressed: Actions.handler(context, OpenIntent(pivSlot)),
2024-03-08 11:30:47 +03:00
child: const Icon(Symbols.more_horiz),
2024-01-09 16:26:52 +03:00
),
tapIntent: isDesktop && !expanded ? null : OpenIntent(pivSlot),
doubleTapIntent: isDesktop && !expanded ? OpenIntent(pivSlot) : null,
2024-01-09 16:26:52 +03:00
buildPopupActions: hasFeature(features.slots)
2024-03-18 13:32:03 +03:00
? (context) => buildSlotActions(pivState, pivSlot, l10n)
2024-01-09 16:26:52 +03:00
: null,
);
2023-04-27 10:13:38 +03:00
}
2023-11-28 13:30:22 +03:00
Key _getMeatballKey(SlotId slotId) => switch (slotId) {
SlotId.authentication => meatballButton9a,
SlotId.signature => meatballButton9c,
SlotId.keyManagement => meatballButton9d,
SlotId.cardAuth => meatballButton9e,
2024-03-20 16:35:17 +03:00
SlotId.retired1 => meatballButton82,
SlotId.retired2 => meatballButton83,
SlotId.retired3 => meatballButton84,
SlotId.retired4 => meatballButton85,
SlotId.retired5 => meatballButton86,
SlotId.retired6 => meatballButton87,
SlotId.retired7 => meatballButton88,
SlotId.retired8 => meatballButton89,
SlotId.retired9 => meatballButton8a,
SlotId.retired10 => meatballButton8b,
SlotId.retired11 => meatballButton8c,
SlotId.retired12 => meatballButton8d,
SlotId.retired13 => meatballButton8e,
SlotId.retired14 => meatballButton8f,
SlotId.retired15 => meatballButton90,
SlotId.retired16 => meatballButton91,
SlotId.retired17 => meatballButton92,
SlotId.retired18 => meatballButton93,
SlotId.retired19 => meatballButton94,
SlotId.retired20 => meatballButton95
2023-11-28 13:30:22 +03:00
};
Key _getAppListItemKey(SlotId slotId) => switch (slotId) {
SlotId.authentication => appListItem9a,
SlotId.signature => appListItem9c,
SlotId.keyManagement => appListItem9d,
2024-03-20 16:35:17 +03:00
SlotId.cardAuth => appListItem9e,
SlotId.retired1 => appListItem82,
SlotId.retired2 => appListItem83,
SlotId.retired3 => appListItem84,
SlotId.retired4 => appListItem85,
SlotId.retired5 => appListItem86,
SlotId.retired6 => appListItem87,
SlotId.retired7 => appListItem88,
SlotId.retired8 => appListItem89,
SlotId.retired9 => appListItem8a,
SlotId.retired10 => appListItem8b,
SlotId.retired11 => appListItem8c,
SlotId.retired12 => appListItem8d,
SlotId.retired13 => appListItem8e,
SlotId.retired14 => appListItem8f,
SlotId.retired15 => appListItem90,
SlotId.retired16 => appListItem91,
SlotId.retired17 => appListItem92,
SlotId.retired18 => appListItem93,
SlotId.retired19 => appListItem94,
SlotId.retired20 => appListItem95
2023-11-28 13:30:22 +03:00
};
2023-04-27 10:13:38 +03:00
}