yubioath-flutter/lib/fido/views/unlocked_page.dart

221 lines
7.6 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.
*/
import 'package:flutter/material.dart';
2022-09-12 16:46:43 +03:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../app/message.dart';
import '../../app/models.dart';
import '../../app/shortcuts.dart';
2023-06-15 18:39:17 +03:00
import '../../app/views/app_list_item.dart';
import '../../app/views/app_page.dart';
import '../../app/views/message_page.dart';
2023-10-06 11:49:01 +03:00
import '../../core/state.dart';
import '../../widgets/list_title.dart';
import '../features.dart' as features;
import '../models.dart';
2022-04-04 12:15:38 +03:00
import '../state.dart';
2023-06-15 18:39:17 +03:00
import 'actions.dart';
2023-05-04 17:20:50 +03:00
import 'credential_dialog.dart';
2023-06-15 18:39:17 +03:00
import 'delete_credential_dialog.dart';
import 'delete_fingerprint_dialog.dart';
import 'fingerprint_dialog.dart';
import 'key_actions.dart';
2023-06-15 18:39:17 +03:00
import 'rename_fingerprint_dialog.dart';
class FidoUnlockedPage extends ConsumerWidget {
final DeviceNode node;
final FidoState state;
2022-05-12 10:56:55 +03:00
const FidoUnlockedPage(this.node, this.state, {super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
2023-02-28 21:05:46 +03:00
final l10n = AppLocalizations.of(context)!;
2023-10-06 11:49:01 +03:00
final hasFeature = ref.watch(featureProvider);
List<Widget> children = [];
2023-10-06 11:49:01 +03:00
if (state.credMgmt) {
final data = ref.watch(credentialProvider(node.path)).asData;
if (data == null) {
2022-09-12 17:14:58 +03:00
return _buildLoadingPage(context);
}
final creds = data.value;
if (creds.isNotEmpty) {
2023-05-04 17:20:50 +03:00
children.add(ListTitle(l10n.s_passkeys));
children.addAll(creds.map((cred) => Actions(
actions: {
2023-06-15 18:39:17 +03:00
OpenIntent: CallbackAction<OpenIntent>(
onInvoke: (_) => showBlurDialog(
context: context,
2023-08-16 17:52:49 +03:00
barrierColor: Colors.transparent,
2023-06-15 18:39:17 +03:00
builder: (context) => CredentialDialog(cred),
)),
2023-10-06 11:49:01 +03:00
if (hasFeature(features.credentialsDelete))
DeleteIntent: CallbackAction<DeleteIntent>(
onInvoke: (_) => showBlurDialog(
context: context,
builder: (context) => DeleteCredentialDialog(
node.path,
cred,
),
2023-06-15 18:39:17 +03:00
),
),
2023-05-04 17:20:50 +03:00
},
child: _CredentialListItem(cred),
)));
}
}
int nFingerprints = 0;
if (state.bioEnroll != null) {
final data = ref.watch(fingerprintProvider(node.path)).asData;
if (data == null) {
2022-09-12 17:14:58 +03:00
return _buildLoadingPage(context);
}
final fingerprints = data.value;
if (fingerprints.isNotEmpty) {
nFingerprints = fingerprints.length;
children.add(ListTitle(l10n.s_fingerprints));
children.addAll(fingerprints.map((fp) => Actions(
actions: {
2023-06-15 18:39:17 +03:00
OpenIntent: CallbackAction<OpenIntent>(
onInvoke: (_) => showBlurDialog(
context: context,
2023-08-16 17:52:49 +03:00
barrierColor: Colors.transparent,
2023-06-15 18:39:17 +03:00
builder: (context) => FingerprintDialog(fp),
)),
2023-10-06 11:49:01 +03:00
if (hasFeature(features.fingerprintsEdit))
EditIntent: CallbackAction<EditIntent>(
onInvoke: (_) => showBlurDialog(
context: context,
builder: (context) => RenameFingerprintDialog(
node.path,
fp,
),
)),
if (hasFeature(features.fingerprintsDelete))
DeleteIntent: CallbackAction<DeleteIntent>(
onInvoke: (_) => showBlurDialog(
context: context,
builder: (context) => DeleteFingerprintDialog(
node.path,
fp,
),
)),
},
child: _FingerprintListItem(fp),
)));
}
}
2023-10-06 11:49:01 +03:00
final hasActions = ref.watch(featureProvider)(features.actions);
if (children.isNotEmpty) {
return AppPage(
title: Text(l10n.s_webauthn),
2023-10-06 11:49:01 +03:00
keyActionsBuilder: hasActions
? (context) => fidoBuildActions(context, node, state, nFingerprints)
: null,
keyActionsBadge: fidoShowActionsNotifier(state),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start, children: children),
);
}
if (state.bioEnroll != null) {
return MessagePage(
title: Text(l10n.s_webauthn),
2023-12-20 17:09:31 +03:00
graphic: Icon(Icons.fingerprint,
size: 96, color: Theme.of(context).colorScheme.primary),
header: l10n.s_no_fingerprints,
2023-02-28 21:05:46 +03:00
message: l10n.l_add_one_or_more_fps,
2023-10-06 11:49:01 +03:00
keyActionsBuilder: hasActions
? (context) => fidoBuildActions(context, node, state, 0)
: null,
keyActionsBadge: fidoShowActionsNotifier(state),
);
}
return MessagePage(
title: Text(l10n.s_webauthn),
2023-12-20 17:09:31 +03:00
graphic: Icon(Icons.security,
size: 96, color: Theme.of(context).colorScheme.primary),
2023-02-28 21:05:46 +03:00
header: l10n.l_no_discoverable_accounts,
message: l10n.l_register_sk_on_websites,
2023-10-06 11:49:01 +03:00
keyActionsBuilder: hasActions
? (context) => fidoBuildActions(context, node, state, 0)
: null,
keyActionsBadge: fidoShowActionsNotifier(state),
);
}
2022-09-12 17:14:58 +03:00
Widget _buildLoadingPage(BuildContext context) => AppPage(
title: Text(AppLocalizations.of(context)!.s_webauthn),
centered: true,
delayedContent: true,
child: const CircularProgressIndicator(),
);
}
2023-05-04 17:20:50 +03:00
class _CredentialListItem extends StatelessWidget {
final FidoCredential credential;
const _CredentialListItem(this.credential);
@override
Widget build(BuildContext context) {
2023-06-15 18:39:17 +03:00
return AppListItem(
2023-05-04 17:20:50 +03:00
leading: CircleAvatar(
foregroundColor: Theme.of(context).colorScheme.onPrimary,
backgroundColor: Theme.of(context).colorScheme.primary,
child: const Icon(Icons.person),
),
2023-06-15 18:39:17 +03:00
title: credential.userName,
subtitle: credential.rpId,
2023-05-04 17:20:50 +03:00
trailing: OutlinedButton(
2023-06-15 18:39:17 +03:00
onPressed: Actions.handler(context, const OpenIntent()),
2023-05-04 17:20:50 +03:00
child: const Icon(Icons.more_horiz),
),
2023-06-15 18:39:17 +03:00
buildPopupActions: (context) =>
buildCredentialActions(AppLocalizations.of(context)!),
2023-05-04 17:20:50 +03:00
);
}
}
class _FingerprintListItem extends StatelessWidget {
final Fingerprint fingerprint;
const _FingerprintListItem(this.fingerprint);
@override
Widget build(BuildContext context) {
2023-06-15 18:39:17 +03:00
return AppListItem(
leading: CircleAvatar(
foregroundColor: Theme.of(context).colorScheme.onSecondary,
backgroundColor: Theme.of(context).colorScheme.secondary,
child: const Icon(Icons.fingerprint),
),
2023-06-15 18:39:17 +03:00
title: fingerprint.label,
trailing: OutlinedButton(
2023-06-15 18:39:17 +03:00
onPressed: Actions.handler(context, const OpenIntent()),
child: const Icon(Icons.more_horiz),
),
2023-06-15 18:39:17 +03:00
buildPopupActions: (context) =>
buildFingerprintActions(AppLocalizations.of(context)!),
);
}
}