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 'dart:io';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
|
2023-08-30 15:13:08 +03:00
|
|
|
import '../../app/message.dart';
|
2023-04-27 10:13:38 +03:00
|
|
|
import '../../app/models.dart';
|
2023-08-30 15:13:08 +03:00
|
|
|
import '../../app/state.dart';
|
2023-11-10 17:24:53 +03:00
|
|
|
import '../../widgets/app_text_field.dart';
|
2023-11-27 13:41:05 +03:00
|
|
|
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-23 16:53:30 +03:00
|
|
|
import 'cert_info_view.dart';
|
2023-08-24 10:51:43 +03:00
|
|
|
import 'overwrite_confirm_dialog.dart';
|
2023-04-27 10:13:38 +03:00
|
|
|
|
|
|
|
class ImportFileDialog extends ConsumerStatefulWidget {
|
|
|
|
final DevicePath devicePath;
|
|
|
|
final PivState pivState;
|
|
|
|
final PivSlot pivSlot;
|
|
|
|
final File file;
|
|
|
|
const ImportFileDialog(
|
|
|
|
this.devicePath, this.pivState, this.pivSlot, this.file,
|
|
|
|
{super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
ConsumerState<ConsumerStatefulWidget> createState() =>
|
|
|
|
_ImportFileDialogState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ImportFileDialogState extends ConsumerState<ImportFileDialog> {
|
|
|
|
late String _data;
|
|
|
|
PivExamineResult? _state;
|
|
|
|
String _password = '';
|
|
|
|
bool _passwordIsWrong = false;
|
2023-08-30 15:13:08 +03:00
|
|
|
bool _importing = false;
|
2023-11-27 17:12:57 +03:00
|
|
|
bool _isObscure = true;
|
2023-04-27 10:13:38 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _init() async {
|
|
|
|
final bytes = await widget.file.readAsBytes();
|
|
|
|
_data = bytes.map((e) => e.toRadixString(16).padLeft(2, '0')).join();
|
|
|
|
_examine();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _examine() async {
|
|
|
|
setState(() {
|
|
|
|
_state = null;
|
|
|
|
});
|
|
|
|
final result = await ref
|
|
|
|
.read(pivSlotsProvider(widget.devicePath).notifier)
|
|
|
|
.examine(_data, password: _password.isNotEmpty ? _password : null);
|
|
|
|
setState(() {
|
|
|
|
_state = result;
|
|
|
|
_passwordIsWrong = result.maybeWhen(
|
|
|
|
invalidPassword: () => _password.isNotEmpty,
|
|
|
|
orElse: () => true,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final l10n = AppLocalizations.of(context)!;
|
2023-08-23 16:53:30 +03:00
|
|
|
final textTheme = Theme.of(context).textTheme;
|
|
|
|
// This is what ListTile uses for subtitle
|
|
|
|
final subtitleStyle = textTheme.bodyMedium!.copyWith(
|
|
|
|
color: textTheme.bodySmall!.color,
|
|
|
|
);
|
2023-04-27 10:13:38 +03:00
|
|
|
final state = _state;
|
|
|
|
if (state == null) {
|
|
|
|
return ResponsiveDialog(
|
2023-06-05 16:56:13 +03:00
|
|
|
title: Text(l10n.l_import_file),
|
2023-04-27 10:13:38 +03:00
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
key: keys.unlockButton,
|
|
|
|
onPressed: null,
|
|
|
|
child: Text(l10n.s_unlock),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
child: const Padding(
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 18.0),
|
|
|
|
child: Center(
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return state.when(
|
|
|
|
invalidPassword: () => ResponsiveDialog(
|
2023-06-05 16:56:13 +03:00
|
|
|
title: Text(l10n.l_import_file),
|
2023-04-27 10:13:38 +03:00
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
key: keys.unlockButton,
|
|
|
|
onPressed: () => _examine(),
|
|
|
|
child: Text(l10n.s_unlock),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 18.0),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
2023-06-05 16:56:13 +03:00
|
|
|
Text(l10n.p_password_protected_file),
|
2023-11-10 17:24:53 +03:00
|
|
|
AppTextField(
|
2023-04-27 10:13:38 +03:00
|
|
|
autofocus: true,
|
2023-11-27 17:12:57 +03:00
|
|
|
obscureText: _isObscure,
|
2023-04-27 10:13:38 +03:00
|
|
|
autofillHints: const [AutofillHints.password],
|
|
|
|
key: keys.managementKeyField,
|
|
|
|
decoration: InputDecoration(
|
2023-11-24 16:37:37 +03:00
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
labelText: l10n.s_password,
|
|
|
|
errorText: _passwordIsWrong ? l10n.s_wrong_password : null,
|
|
|
|
errorMaxLines: 3,
|
|
|
|
prefixIcon: const Icon(Icons.password_outlined),
|
2023-11-27 17:12:57 +03:00
|
|
|
suffixIcon: Wrap(
|
|
|
|
crossAxisAlignment: WrapCrossAlignment.center,
|
|
|
|
children: [
|
|
|
|
IconButton(
|
|
|
|
icon: Icon(_isObscure
|
|
|
|
? Icons.visibility
|
|
|
|
: Icons.visibility_off),
|
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
_isObscure = !_isObscure;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
tooltip: _isObscure
|
|
|
|
? l10n.s_show_password
|
|
|
|
: l10n.s_hide_password),
|
|
|
|
if (_passwordIsWrong) ...[
|
|
|
|
const Icon(Icons.error_outlined),
|
|
|
|
const SizedBox(
|
|
|
|
width: 8.0,
|
|
|
|
)
|
|
|
|
]
|
|
|
|
]),
|
2023-11-24 16:37:37 +03:00
|
|
|
),
|
2023-04-27 10:13:38 +03:00
|
|
|
textInputAction: TextInputAction.next,
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_passwordIsWrong = false;
|
|
|
|
_password = value;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onSubmitted: (_) => _examine(),
|
|
|
|
),
|
|
|
|
]
|
|
|
|
.map((e) => Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
|
|
|
child: e,
|
|
|
|
))
|
|
|
|
.toList(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2023-08-23 16:53:30 +03:00
|
|
|
result: (_, keyType, certInfo) => ResponsiveDialog(
|
2023-06-05 16:56:13 +03:00
|
|
|
title: Text(l10n.l_import_file),
|
2023-04-27 10:13:38 +03:00
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
key: keys.unlockButton,
|
2023-08-30 15:13:08 +03:00
|
|
|
onPressed: (keyType == null && certInfo == null) || _importing
|
2023-08-24 10:51:43 +03:00
|
|
|
? null
|
|
|
|
: () async {
|
2023-08-30 15:13:08 +03:00
|
|
|
final withContext = ref.read(withContextProvider);
|
2023-08-24 10:51:43 +03:00
|
|
|
|
|
|
|
if (!await confirmOverwrite(
|
|
|
|
context,
|
|
|
|
widget.pivSlot,
|
|
|
|
writeKey: keyType != null,
|
|
|
|
writeCert: certInfo != null,
|
|
|
|
)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-30 15:13:08 +03:00
|
|
|
setState(() {
|
|
|
|
_importing = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
void Function()? close;
|
2023-08-24 10:51:43 +03:00
|
|
|
try {
|
2023-08-30 15:13:08 +03:00
|
|
|
close = await withContext<void Function()>(
|
|
|
|
(context) async => showMessage(
|
|
|
|
context,
|
|
|
|
l10n.l_importing_file,
|
|
|
|
duration: const Duration(seconds: 30),
|
|
|
|
));
|
2023-08-24 10:51:43 +03:00
|
|
|
await ref
|
|
|
|
.read(pivSlotsProvider(widget.devicePath).notifier)
|
|
|
|
.import(widget.pivSlot.slot, _data,
|
|
|
|
password:
|
|
|
|
_password.isNotEmpty ? _password : null);
|
2023-08-30 15:13:08 +03:00
|
|
|
await withContext(
|
|
|
|
(context) async {
|
|
|
|
Navigator.of(context).pop(true);
|
|
|
|
showMessage(context, l10n.s_file_imported);
|
|
|
|
},
|
|
|
|
);
|
2023-08-24 10:51:43 +03:00
|
|
|
} catch (err) {
|
|
|
|
// TODO: More error cases
|
|
|
|
setState(() {
|
|
|
|
_passwordIsWrong = true;
|
2023-08-30 15:13:08 +03:00
|
|
|
_importing = false;
|
2023-08-24 10:51:43 +03:00
|
|
|
});
|
2023-08-30 15:13:08 +03:00
|
|
|
} finally {
|
|
|
|
close?.call();
|
2023-08-24 10:51:43 +03:00
|
|
|
}
|
|
|
|
},
|
2023-06-05 16:56:13 +03:00
|
|
|
child: Text(l10n.s_import),
|
2023-04-27 10:13:38 +03:00
|
|
|
),
|
|
|
|
],
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 18.0),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
2023-06-05 16:56:13 +03:00
|
|
|
Text(l10n.p_import_items_desc(
|
|
|
|
widget.pivSlot.slot.getDisplayName(l10n))),
|
2023-08-23 16:53:30 +03:00
|
|
|
if (keyType != null) ...[
|
|
|
|
Text(
|
|
|
|
l10n.s_private_key,
|
|
|
|
style: textTheme.bodyLarge,
|
|
|
|
softWrap: true,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
),
|
|
|
|
Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
Text(l10n.s_algorithm),
|
|
|
|
const SizedBox(width: 8),
|
|
|
|
Text(
|
|
|
|
keyType.name.toUpperCase(),
|
|
|
|
style: subtitleStyle,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
],
|
|
|
|
if (certInfo != null) ...[
|
|
|
|
Text(
|
|
|
|
l10n.s_certificate,
|
|
|
|
style: textTheme.bodyLarge,
|
|
|
|
softWrap: true,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
),
|
|
|
|
SizedBox(
|
|
|
|
height: 120, // Needed for layout, adapt if text sizes changes
|
|
|
|
child: CertInfoTable(certInfo),
|
|
|
|
),
|
|
|
|
]
|
2023-04-27 10:13:38 +03:00
|
|
|
]
|
|
|
|
.map((e) => Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
|
|
|
child: e,
|
|
|
|
))
|
|
|
|
.toList(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|