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

175 lines
6.3 KiB
Dart
Raw Permalink 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/models.dart';
2023-12-13 21:35:17 +03:00
import '../../core/models.dart';
2023-04-27 10:13:38 +03:00
import '../../exception/cancellation_exception.dart';
import '../../widgets/app_input_decoration.dart';
2023-11-10 17:24:53 +03:00
import '../../widgets/app_text_field.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';
class AuthenticationDialog extends ConsumerStatefulWidget {
final DevicePath devicePath;
final PivState pivState;
const AuthenticationDialog(this.devicePath, this.pivState, {super.key});
@override
ConsumerState<ConsumerStatefulWidget> createState() =>
_AuthenticationDialogState();
}
class _AuthenticationDialogState extends ConsumerState<AuthenticationDialog> {
bool _defaultKeyUsed = false;
2023-04-27 10:13:38 +03:00
bool _keyIsWrong = false;
2023-11-24 16:37:37 +03:00
bool _keyFormatInvalid = false;
final _keyController = TextEditingController();
final _keyFocus = FocusNode();
@override
void dispose() {
_keyController.dispose();
_keyFocus.dispose();
super.dispose();
}
2023-04-27 10:13:38 +03:00
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
final hasMetadata = widget.pivState.metadata != null;
2023-06-13 15:33:23 +03:00
final keyLen = (widget.pivState.metadata?.managementKeyMetadata.keyType ??
ManagementKeyType.tdes)
.keyLength *
2;
2023-11-24 16:37:37 +03:00
final keyFormatInvalid = !Format.hex.isValid(_keyController.text);
2023-04-27 10:13:38 +03:00
return ResponsiveDialog(
2023-06-05 16:56:13 +03:00
title: Text(l10n.l_unlock_piv_management),
2023-04-27 10:13:38 +03:00
actions: [
TextButton(
key: keys.unlockButton,
onPressed: !_keyIsWrong && _keyController.text.length == keyLen
2023-06-13 15:33:23 +03:00
? () async {
2023-11-24 16:37:37 +03:00
if (keyFormatInvalid) {
setState(() {
_keyFormatInvalid = true;
});
return;
}
2023-06-13 15:33:23 +03:00
final navigator = Navigator.of(context);
try {
final status = await ref
.read(pivStateProvider(widget.devicePath).notifier)
.authenticate(_keyController.text);
2023-06-13 15:33:23 +03:00
if (status) {
navigator.pop(true);
} else {
_keyController.selection = TextSelection(
baseOffset: 0,
extentOffset: _keyController.text.length);
_keyFocus.requestFocus();
2023-06-13 15:33:23 +03:00
setState(() {
_keyIsWrong = true;
});
}
} on CancellationException catch (_) {
navigator.pop(false);
} catch (_) {
_keyController.selection = TextSelection(
baseOffset: 0,
extentOffset: _keyController.text.length);
_keyFocus.requestFocus();
2023-06-13 15:33:23 +03:00
// TODO: More error cases
setState(() {
_keyIsWrong = true;
});
}
}
: null,
2023-04-27 10:13:38 +03:00
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_unlock_piv_management_desc),
2023-11-10 17:24:53 +03:00
AppTextField(
2023-06-13 15:33:23 +03:00
key: keys.managementKeyField,
2023-04-27 10:13:38 +03:00
autofocus: true,
autofillHints: const [AutofillHints.password],
controller: _keyController,
focusNode: _keyFocus,
readOnly: _defaultKeyUsed,
maxLength: !_defaultKeyUsed ? keyLen : null,
2023-12-14 18:38:10 +03:00
decoration: AppInputDecoration(
2023-06-13 15:33:23 +03:00
border: const OutlineInputBorder(),
labelText: l10n.s_management_key,
helperText: _defaultKeyUsed ? l10n.l_default_key_used : null,
2023-11-24 16:37:37 +03:00
errorText: _keyIsWrong
? l10n.l_wrong_key
: _keyFormatInvalid
? l10n.l_invalid_format_allowed_chars(
Format.hex.allowedCharacters)
: null,
errorMaxLines: 3,
2024-03-08 11:30:47 +03:00
prefixIcon: const Icon(Symbols.key),
2023-12-15 11:37:34 +03:00
suffixIcon: hasMetadata
? null
2023-12-15 11:37:34 +03:00
: IconButton(
2024-03-08 11:30:47 +03:00
icon: Icon(Symbols.auto_awesome,
fill: _defaultKeyUsed ? 1.0 : 0.0),
2023-12-15 11:37:34 +03:00
tooltip: l10n.s_use_default,
onPressed: () {
setState(() {
_keyFormatInvalid = false;
_defaultKeyUsed = !_defaultKeyUsed;
if (_defaultKeyUsed) {
_keyController.text = defaultManagementKey;
} else {
_keyController.clear();
}
});
},
),
2023-06-13 15:33:23 +03:00
),
2023-04-27 10:13:38 +03:00
textInputAction: TextInputAction.next,
onChanged: (value) {
setState(() {
_keyIsWrong = false;
2023-11-24 16:37:37 +03:00
_keyFormatInvalid = false;
2023-04-27 10:13:38 +03:00
});
},
).init(),
2023-04-27 10:13:38 +03:00
]
.map((e) => Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: e,
))
.toList(),
),
),
);
}
}