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

155 lines
6.1 KiB
Dart
Raw Normal View History

2023-04-27 10:13:38 +03:00
/*
* Copyright (C) 2023 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';
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';
2023-06-09 15:46:16 +03:00
import '../../app/views/action_list.dart';
import '../features.dart' as features;
import '../keys.dart' as keys;
import '../models.dart';
2023-04-27 10:13:38 +03:00
import 'manage_key_dialog.dart';
import 'manage_pin_puk_dialog.dart';
bool pivShowActionsNotifier(PivState state) {
final usingDefaultPin = state.metadata?.pinMetadata.defaultValue == true;
final usingDefaultPuk = state.metadata?.pukMetadata.defaultValue == true;
final usingDefaultMgmtKey =
state.metadata?.managementKeyMetadata.defaultValue == true;
return usingDefaultPin || usingDefaultPuk || usingDefaultMgmtKey;
}
2023-04-27 10:13:38 +03:00
Widget pivBuildActions(BuildContext context, DevicePath devicePath,
PivState pivState, WidgetRef ref) {
2023-06-16 18:27:10 +03:00
final colors = Theme.of(context).buttonTheme.colorScheme ??
Theme.of(context).colorScheme;
2023-04-27 10:13:38 +03:00
final l10n = AppLocalizations.of(context)!;
final usingDefaultPin = pivState.metadata?.pinMetadata.defaultValue == true;
final usingDefaultPuk = pivState.metadata?.pukMetadata.defaultValue == true;
2023-04-27 10:13:38 +03:00
final usingDefaultMgmtKey =
pivState.metadata?.managementKeyMetadata.defaultValue == true;
final pinBlocked = pivState.pinAttempts == 0;
2023-06-05 16:56:13 +03:00
final pukAttempts = pivState.metadata?.pukMetadata.attemptsRemaining;
2024-03-08 11:30:47 +03:00
final alertIcon = Icon(Symbols.warning_amber, color: colors.tertiary);
2023-04-27 10:13:38 +03:00
2024-01-09 14:50:26 +03:00
return Column(
children: [
ActionListSection(
l10n.s_manage,
2023-12-20 17:09:31 +03:00
children: [
2024-01-09 14:50:26 +03:00
ActionListItem(
key: keys.managePinAction,
feature: features.actionsPin,
2024-03-06 16:09:12 +03:00
title: l10n.s_change_pin,
2024-01-09 14:50:26 +03:00
subtitle: pinBlocked
? (pukAttempts != 0
? l10n.l_piv_pin_blocked
: l10n.l_piv_pin_puk_blocked)
: usingDefaultPin
? '${l10n.l_attempts_remaining(pivState.pinAttempts)}\n${l10n.l_warning_default_pin}'
: l10n.l_attempts_remaining(pivState.pinAttempts),
2024-03-08 11:30:47 +03:00
icon: const Icon(Symbols.pin),
trailing: pinBlocked || usingDefaultPin ? alertIcon : null,
2024-01-09 14:50:26 +03:00
onTap: !(pinBlocked && pukAttempts == 0)
? (context) {
Navigator.of(context).popUntil((route) => route.isFirst);
showBlurDialog(
context: context,
builder: (context) => ManagePinPukDialog(
devicePath,
pivState,
2024-01-09 14:50:26 +03:00
target: pinBlocked
? ManageTarget.unblock
: ManageTarget.pin,
),
);
}
: null),
ActionListItem(
key: keys.managePukAction,
feature: features.actionsPuk,
2024-03-06 16:09:12 +03:00
title: l10n.s_change_puk,
2024-01-09 14:50:26 +03:00
subtitle: pukAttempts != null
? (pukAttempts == 0
? l10n.l_piv_pin_puk_blocked
: usingDefaultPuk
? '${l10n.l_attempts_remaining(pukAttempts)}\n${l10n.l_warning_default_puk}'
: l10n.l_attempts_remaining(pukAttempts))
: usingDefaultPuk
? l10n.l_warning_default_puk
: null,
2024-03-08 11:30:47 +03:00
icon: const Icon(Symbols.pin),
trailing: pukAttempts == 0 || usingDefaultPuk ? alertIcon : null,
2024-01-09 14:50:26 +03:00
onTap: pukAttempts != 0
? (context) {
Navigator.of(context).popUntil((route) => route.isFirst);
showBlurDialog(
context: context,
builder: (context) => ManagePinPukDialog(
devicePath, pivState,
2024-01-09 14:50:26 +03:00
target: ManageTarget.puk),
);
}
: null),
ActionListItem(
key: keys.manageManagementKeyAction,
feature: features.actionsManagementKey,
title: l10n.s_management_key,
subtitle: usingDefaultMgmtKey
? l10n.l_warning_default_key
: (pivState.protectedKey
? l10n.l_pin_protected_key
: l10n.l_change_management_key),
2024-03-08 11:30:47 +03:00
icon: const Icon(Symbols.key),
2024-01-09 14:50:26 +03:00
trailing: usingDefaultMgmtKey ? alertIcon : null,
onTap: (context) {
Navigator.of(context).popUntil((route) => route.isFirst);
showBlurDialog(
context: context,
builder: (context) => ManageKeyDialog(devicePath, pivState),
);
}),
2023-12-20 17:09:31 +03:00
],
),
2024-01-09 14:50:26 +03:00
// TODO
/*
if (false == true) ...[
KeyActionTitle(l10n.s_setup),
KeyActionItem(
key: keys.setupMacOsAction,
title: Text('Setup for macOS'),
subtitle: Text('Create certificates for macOS login'),
leading: CircleAvatar(
backgroundColor: theme.secondary,
foregroundColor: theme.onSecondary,
2024-03-08 11:30:47 +03:00
child: const Icon(Symbols.laptop),
2024-01-09 14:50:26 +03:00
),
onTap: () async {
Navigator.of(context).pop();
}),
],
*/
],
2023-04-27 10:13:38 +03:00
);
}