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

282 lines
10 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';
2023-06-16 18:27:10 +03:00
import '../../app/message.dart';
2023-04-27 10:13:38 +03:00
import '../../app/models.dart';
2023-06-16 18:27:10 +03:00
import '../../app/state.dart';
2023-04-27 10:13:38 +03:00
import '../../core/models.dart';
import '../../widgets/app_input_decoration.dart';
import '../../widgets/app_text_field.dart';
2023-04-27 10:13:38 +03:00
import '../../widgets/choice_filter_chip.dart';
import '../../widgets/responsive_dialog.dart';
2023-11-10 17:24:53 +03:00
import '../keys.dart' as keys;
2023-04-27 10:13:38 +03:00
import '../models.dart';
import '../state.dart';
2023-08-24 10:51:43 +03:00
import 'overwrite_confirm_dialog.dart';
import 'utils.dart';
2023-04-27 10:13:38 +03:00
class GenerateKeyDialog extends ConsumerStatefulWidget {
final DevicePath devicePath;
final PivState pivState;
final PivSlot pivSlot;
2024-08-16 15:36:57 +03:00
final bool showMatch;
GenerateKeyDialog(this.devicePath, this.pivState, this.pivSlot, {super.key})
: showMatch = pivSlot.slot != SlotId.cardAuth && pivState.supportsBio;
2023-04-27 10:13:38 +03:00
@override
ConsumerState<ConsumerStatefulWidget> createState() =>
_GenerateKeyDialogState();
}
class _GenerateKeyDialogState extends ConsumerState<GenerateKeyDialog> {
String _subject = '';
2023-08-22 12:06:37 +03:00
bool _invalidSubject = true;
2023-04-27 10:13:38 +03:00
GenerateType _generateType = defaultGenerateType;
KeyType _keyType = defaultKeyType;
late DateTime _validFrom;
late DateTime _validTo;
late DateTime _validToDefault;
late DateTime _validToMax;
2024-08-16 15:36:57 +03:00
late bool _allowMatch;
2023-06-16 18:27:10 +03:00
bool _generating = false;
2023-04-27 10:13:38 +03:00
@override
void initState() {
super.initState();
final now = DateTime.now();
_validFrom = DateTime.utc(now.year, now.month, now.day);
_validToDefault = DateTime.utc(now.year + 1, now.month, now.day);
_validTo = _validToDefault;
_validToMax = DateTime.utc(now.year + 10, now.month, now.day);
2024-08-16 15:36:57 +03:00
_allowMatch = widget.showMatch;
2023-04-27 10:13:38 +03:00
}
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
final textTheme = Theme.of(context).textTheme;
// This is what ListTile uses for subtitle
final subtitleStyle = textTheme.bodyMedium!.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
);
2023-06-16 18:27:10 +03:00
final isFips =
ref.watch(currentDeviceDataProvider).valueOrNull?.info.isFips ?? false;
final canSave = !_generating &&
(!_invalidSubject || _generateType == GenerateType.publicKey);
2023-04-27 10:13:38 +03:00
return ResponsiveDialog(
2023-06-16 18:27:10 +03:00
allowCancel: !_generating,
2023-06-05 16:56:13 +03:00
title: Text(l10n.s_generate_key),
2023-04-27 10:13:38 +03:00
actions: [
TextButton(
key: keys.saveButton,
onPressed: canSave
? () async {
2023-08-24 10:51:43 +03:00
if (!await confirmOverwrite(
context,
widget.pivSlot,
writeKey: true,
writeCert: _generateType == GenerateType.certificate,
)) {
return;
}
2023-06-16 18:27:10 +03:00
setState(() {
_generating = true;
});
2023-08-22 12:06:37 +03:00
final pivNotifier =
ref.read(pivSlotsProvider(widget.devicePath).notifier);
if (!(_generateType == GenerateType.publicKey ||
await pivNotifier.validateRfc4514(_subject))) {
2023-08-22 12:06:37 +03:00
setState(() {
_generating = false;
_invalidSubject = true;
2023-08-22 12:06:37 +03:00
});
return;
}
2024-02-02 15:52:22 +03:00
final result = await pivNotifier.generate(
widget.pivSlot.slot,
_keyType,
2024-08-16 15:36:57 +03:00
pinPolicy: getPinPolicy(widget.pivSlot.slot, _allowMatch),
2024-02-02 15:52:22 +03:00
parameters: switch (_generateType) {
GenerateType.publicKey =>
PivGenerateParameters.publicKey(),
2024-02-02 15:52:22 +03:00
GenerateType.certificate =>
PivGenerateParameters.certificate(
subject: _subject,
validFrom: _validFrom,
validTo: _validTo),
GenerateType.csr =>
PivGenerateParameters.csr(subject: _subject),
},
);
2023-04-27 10:13:38 +03:00
2023-06-16 18:27:10 +03:00
await ref.read(withContextProvider)(
(context) async {
Navigator.of(context).pop(result);
showMessage(
context,
l10n.s_private_key_generated,
);
},
);
}
: null,
2023-04-27 10:13:38 +03:00
child: Text(l10n.s_save),
),
],
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 18.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2023-08-24 11:14:35 +03:00
Text(
l10n.p_generate_desc(widget.pivSlot.slot.getDisplayName(l10n))),
Text(
l10n.s_subject,
style: textTheme.bodyLarge,
),
Text(l10n.p_subject_desc),
2023-11-10 17:24:53 +03:00
AppTextField(
2023-04-27 10:13:38 +03:00
autofocus: true,
key: keys.subjectField,
2023-12-14 18:38:10 +03:00
decoration: AppInputDecoration(
2023-11-24 16:37:37 +03:00
border: const OutlineInputBorder(),
labelText: l10n.s_subject,
errorText: _subject.isNotEmpty && _invalidSubject
? l10n.l_rfc4514_invalid
: null,
),
2023-04-27 10:13:38 +03:00
textInputAction: TextInputAction.next,
enabled: !_generating && _generateType != GenerateType.publicKey,
2023-04-27 10:13:38 +03:00
onChanged: (value) {
setState(() {
_invalidSubject = value.isEmpty;
_subject = value;
2023-04-27 10:13:38 +03:00
});
},
).init(),
Text(
l10n.rfc4514_examples,
style: subtitleStyle,
),
Text(
l10n.s_options,
style: textTheme.bodyLarge,
),
2024-08-16 15:36:57 +03:00
Text(widget.showMatch
? l10n.p_cert_options_bio_desc
: l10n.p_cert_options_desc),
2023-04-27 10:13:38 +03:00
Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
spacing: 4.0,
runSpacing: 8.0,
children: [
ChoiceFilterChip<KeyType>(
items:
getSupportedKeyTypes(widget.pivState.version, isFips),
value: _keyType,
selected: _keyType != defaultKeyType,
2023-04-27 10:13:38 +03:00
itemBuilder: (value) => Text(value.getDisplayName(l10n)),
2023-06-16 18:27:10 +03:00
onChanged: _generating
? null
: (value) {
setState(() {
_keyType = value;
if (value == KeyType.x25519) {
_generateType = GenerateType.publicKey;
}
2023-06-16 18:27:10 +03:00
});
},
2023-04-27 10:13:38 +03:00
),
ChoiceFilterChip<GenerateType>(
items: GenerateType.values,
value: _generateType,
selected: _generateType != defaultGenerateType,
2023-04-27 10:13:38 +03:00
itemBuilder: (value) => Text(value.getDisplayName(l10n)),
onChanged: _generating || _keyType == KeyType.x25519
2023-06-16 18:27:10 +03:00
? null
: (value) {
setState(() {
_generateType = value;
2023-06-16 18:27:10 +03:00
});
},
2023-04-27 10:13:38 +03:00
),
if (_generateType == GenerateType.certificate)
FilterChip(
label: Text(dateFormatter.format(_validTo)),
2023-06-16 18:27:10 +03:00
onSelected: _generating
? null
: (value) async {
final selected = await showDatePicker(
context: context,
initialDate: _validTo,
firstDate: _validFrom,
lastDate: _validToMax,
);
if (selected != null) {
setState(() {
_validTo = selected;
});
}
},
2023-04-27 10:13:38 +03:00
),
2024-08-16 15:36:57 +03:00
if (widget.showMatch)
FilterChip(
label: Text(l10n.s_allow_fingerprint),
selected: _allowMatch,
onSelected: _generating
? null
: (value) {
setState(() {
_allowMatch = value;
});
},
2024-08-16 15:36:57 +03:00
),
2023-04-27 10:13:38 +03:00
]),
2024-02-02 15:52:22 +03:00
Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Visibility(
visible: _generating,
maintainSize: true,
maintainAnimation: true,
maintainState: true,
child: const LinearProgressIndicator(),
),
),
2023-04-27 10:13:38 +03:00
]
.map((e) => Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: e,
))
.toList(),
),
),
);
}
}