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

249 lines
8.7 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/choice_filter_chip.dart';
import '../../widgets/responsive_dialog.dart';
import '../models.dart';
import '../state.dart';
import '../keys.dart' as keys;
2023-08-24 10:51:43 +03:00
import 'overwrite_confirm_dialog.dart';
2023-04-27 10:13:38 +03:00
class GenerateKeyDialog extends ConsumerStatefulWidget {
final DevicePath devicePath;
final PivState pivState;
final PivSlot pivSlot;
const GenerateKeyDialog(this.devicePath, this.pivState, this.pivSlot,
{super.key});
@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;
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);
}
@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: textTheme.bodySmall!.color,
);
2023-06-16 18:27:10 +03:00
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,
2023-08-22 12:06:37 +03:00
onPressed: _generating || _invalidSubject
2023-06-16 18:27:10 +03:00
? null
: () 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);
final withContext = ref.read(withContextProvider);
if (!await pivNotifier.validateRfc4514(_subject)) {
setState(() {
_generating = false;
});
_invalidSubject = true;
return;
}
void Function()? close;
2023-06-16 18:27:10 +03:00
final PivGenerateResult result;
try {
2023-08-22 12:06:37 +03:00
close = await withContext<void Function()>(
(context) async => showMessage(
context,
l10n.l_generating_private_key,
duration: const Duration(seconds: 30),
));
result = await pivNotifier.generate(
widget.pivSlot.slot,
_keyType,
parameters: switch (_generateType) {
GenerateType.certificate =>
PivGenerateParameters.certificate(
subject: _subject,
validFrom: _validFrom,
validTo: _validTo),
GenerateType.csr =>
PivGenerateParameters.csr(subject: _subject),
},
2023-06-16 18:27:10 +03:00
);
} finally {
close?.call();
}
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,
);
},
);
},
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-04-27 10:13:38 +03:00
TextField(
autofocus: true,
key: keys.subjectField,
2023-06-05 16:56:13 +03:00
decoration: InputDecoration(
2023-08-22 12:06:37 +03:00
border: const OutlineInputBorder(),
labelText: l10n.s_subject,
errorText: _subject.isNotEmpty && _invalidSubject
? l10n.l_rfc4514_invalid
2023-08-22 12:06:37 +03:00
: null),
2023-04-27 10:13:38 +03:00
textInputAction: TextInputAction.next,
2023-06-16 18:27:10 +03:00
enabled: !_generating,
2023-04-27 10:13:38 +03:00
onChanged: (value) {
setState(() {
_invalidSubject = value.isEmpty;
_subject = value;
2023-04-27 10:13:38 +03:00
});
},
),
Text(
l10n.rfc4514_examples,
style: subtitleStyle,
),
Text(
l10n.s_options,
style: textTheme.bodyLarge,
),
Text(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: KeyType.values,
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;
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)),
2023-06-16 18:27:10 +03:00
onChanged: _generating
? 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
),
]),
]
.map((e) => Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: e,
))
.toList(),
),
),
);
}
}