yubioath-flutter/lib/otp/views/configure_chalresp_dialog.dart

206 lines
7.3 KiB
Dart
Raw Normal View History

2023-11-09 16:09:59 +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:math';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
2024-03-08 11:30:47 +03:00
import 'package:material_symbols_icons/symbols.dart';
2023-11-09 16:09:59 +03:00
2023-12-13 21:35:17 +03:00
import '../../app/logging.dart';
2023-11-09 16:09:59 +03:00
import '../../app/message.dart';
import '../../app/models.dart';
import '../../app/state.dart';
2023-12-13 21:35:17 +03:00
import '../../core/models.dart';
import '../../core/state.dart';
import '../../widgets/app_input_decoration.dart';
import '../../widgets/app_text_field.dart';
2023-11-09 16:09:59 +03:00
import '../../widgets/responsive_dialog.dart';
2023-12-13 21:35:17 +03:00
import '../keys.dart' as keys;
2023-11-09 16:09:59 +03:00
import '../models.dart';
import '../state.dart';
2024-03-27 18:42:56 +03:00
import 'access_code_dialog.dart';
2023-11-09 16:09:59 +03:00
import 'overwrite_confirm_dialog.dart';
2023-12-14 18:37:24 +03:00
final _log = Logger('otp.view.configure_chalresp_dialog');
2023-11-09 16:09:59 +03:00
class ConfigureChalrespDialog extends ConsumerStatefulWidget {
final DevicePath devicePath;
final OtpSlot otpSlot;
const ConfigureChalrespDialog(this.devicePath, this.otpSlot, {super.key});
@override
ConsumerState<ConsumerStatefulWidget> createState() =>
_ConfigureChalrespDialogState();
}
class _ConfigureChalrespDialogState
extends ConsumerState<ConfigureChalrespDialog> {
2023-11-17 15:02:51 +03:00
final _secretController = TextEditingController();
2023-11-24 16:37:37 +03:00
bool _validateSecret = false;
2023-11-09 16:09:59 +03:00
bool _requireTouch = false;
2023-11-17 15:02:51 +03:00
final int secretMaxLength = 40;
2023-11-09 16:09:59 +03:00
@override
void dispose() {
2023-11-17 15:02:51 +03:00
_secretController.dispose();
2023-11-09 16:09:59 +03:00
super.dispose();
}
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
2023-11-23 18:25:11 +03:00
final secret = _secretController.text;
final secretLengthValid = secret.isNotEmpty &&
secret.length % 2 == 0 &&
secret.length <= secretMaxLength;
final secretFormatValid = Format.hex.isValid(secret);
2023-11-09 16:09:59 +03:00
return ResponsiveDialog(
title: Text(l10n.s_challenge_response),
actions: [
TextButton(
key: keys.saveButton,
2023-11-24 16:37:37 +03:00
onPressed: !_validateSecret
2023-11-17 15:02:51 +03:00
? () async {
2023-11-24 16:37:37 +03:00
if (!secretLengthValid || !secretFormatValid) {
2023-11-09 16:09:59 +03:00
setState(() {
2023-11-24 16:37:37 +03:00
_validateSecret = true;
2023-11-23 18:25:11 +03:00
});
return;
}
2023-11-09 16:09:59 +03:00
if (!await confirmOverwrite(context, widget.otpSlot)) {
return;
}
final otpNotifier =
ref.read(otpStateProvider(widget.devicePath).notifier);
2024-03-27 18:42:56 +03:00
final configuration = SlotConfiguration.chalresp(
key: secret,
options: SlotConfigurationOptions(
requireTouch: _requireTouch));
bool configurationSucceded = false;
2023-11-09 16:09:59 +03:00
try {
await otpNotifier.configureSlot(widget.otpSlot.slot,
2024-03-27 18:42:56 +03:00
configuration: configuration);
configurationSucceded = true;
2023-11-09 16:09:59 +03:00
} catch (e) {
_log.error('Failed to program credential', e);
2024-03-27 18:42:56 +03:00
// Access code required
2023-11-09 16:09:59 +03:00
await ref.read(withContextProvider)((context) async {
2024-03-27 18:42:56 +03:00
final result = await showBlurDialog(
context: context,
builder: (context) => AccessCodeDialog(
devicePath: widget.devicePath,
otpSlot: widget.otpSlot,
action: (accessCode) async {
await otpNotifier.configureSlot(
widget.otpSlot.slot,
configuration: configuration,
accessCode: accessCode);
},
));
configurationSucceded = result ?? false;
2023-11-09 16:09:59 +03:00
});
}
2024-03-27 18:42:56 +03:00
await ref.read(withContextProvider)((context) async {
Navigator.of(context).pop();
if (configurationSucceded) {
showMessage(
context,
l10n.l_slot_credential_configured(
l10n.s_challenge_response));
}
});
2023-11-17 15:02:51 +03:00
}
: null,
2023-11-09 16:09:59 +03:00
child: Text(l10n.s_save),
)
],
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 18.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
AppTextField(
2023-11-09 16:09:59 +03:00
key: keys.secretField,
autofocus: true,
2023-11-17 15:02:51 +03:00
controller: _secretController,
2023-11-09 16:09:59 +03:00
autofillHints: isAndroid ? [] : const [AutofillHints.password],
2023-11-17 15:02:51 +03:00
maxLength: secretMaxLength,
2023-12-14 18:38:10 +03:00
decoration: AppInputDecoration(
border: const OutlineInputBorder(),
labelText: l10n.s_secret_key,
errorText: _validateSecret && !secretFormatValid
? l10n.l_invalid_format_allowed_chars(
Format.hex.allowedCharacters)
: _validateSecret && !secretLengthValid
? l10n.s_invalid_length
2023-12-14 18:38:10 +03:00
: null,
2024-03-08 11:30:47 +03:00
prefixIcon: const Icon(Symbols.key),
2023-12-14 18:38:10 +03:00
suffixIcon: IconButton(
2024-02-08 22:47:26 +03:00
key: keys.generateSecretKey,
2024-03-08 11:30:47 +03:00
icon: const Icon(Symbols.refresh),
2023-12-14 18:38:10 +03:00
onPressed: () {
setState(() {
final random = Random.secure();
final key = List.generate(
20,
(_) => random
.nextInt(256)
.toRadixString(16)
.padLeft(2, '0')).join();
2023-11-24 16:37:37 +03:00
setState(() {
2023-12-14 18:38:10 +03:00
_secretController.text = key;
2023-11-24 16:37:37 +03:00
});
2023-12-14 18:38:10 +03:00
});
},
tooltip: l10n.s_generate_random,
)),
2023-11-09 16:09:59 +03:00
textInputAction: TextInputAction.next,
onChanged: (value) {
setState(() {
2023-11-24 16:37:37 +03:00
_validateSecret = false;
2023-11-09 16:09:59 +03:00
});
},
).init(),
2023-11-09 16:09:59 +03:00
FilterChip(
label: Text(l10n.s_require_touch),
selected: _requireTouch,
onSelected: (value) {
setState(() {
_requireTouch = value;
});
},
)
]
.map((e) => Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: e,
))
.toList(),
),
),
);
}
}