2022-10-04 13:12:54 +03:00
|
|
|
/*
|
2024-01-04 19:09:59 +03:00
|
|
|
* Copyright (C) 2022-2024 Yubico.
|
2022-10-04 13:12:54 +03:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-03-17 15:06:48 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2022-09-12 16:46:43 +03:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2022-03-17 15:06:48 +03:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2022-06-13 17:45:26 +03:00
|
|
|
import 'package:logging/logging.dart';
|
2024-03-08 11:30:47 +03:00
|
|
|
import 'package:material_symbols_icons/symbols.dart';
|
2022-03-17 15:06:48 +03:00
|
|
|
|
2022-06-13 17:45:26 +03:00
|
|
|
import '../../app/logging.dart';
|
2022-03-25 17:43:32 +03:00
|
|
|
import '../../app/message.dart';
|
2022-03-17 15:06:48 +03:00
|
|
|
import '../../app/models.dart';
|
2023-08-21 16:43:17 +03:00
|
|
|
import '../../app/state.dart';
|
2022-06-13 17:45:26 +03:00
|
|
|
import '../../desktop/models.dart';
|
2024-01-03 17:35:22 +03:00
|
|
|
import '../../exception/cancellation_exception.dart';
|
2023-12-14 19:40:29 +03:00
|
|
|
import '../../widgets/app_input_decoration.dart';
|
2023-11-10 17:24:53 +03:00
|
|
|
import '../../widgets/app_text_form_field.dart';
|
2023-11-27 13:41:05 +03:00
|
|
|
import '../../widgets/responsive_dialog.dart';
|
2024-02-08 18:06:39 +03:00
|
|
|
import '../keys.dart';
|
2022-03-17 15:06:48 +03:00
|
|
|
import '../models.dart';
|
|
|
|
import '../state.dart';
|
|
|
|
|
2022-06-13 17:45:26 +03:00
|
|
|
final _log = Logger('fido.views.pin_dialog');
|
|
|
|
|
2022-03-17 15:06:48 +03:00
|
|
|
class FidoPinDialog extends ConsumerStatefulWidget {
|
|
|
|
final DevicePath devicePath;
|
|
|
|
final FidoState state;
|
2024-01-05 10:41:54 +03:00
|
|
|
|
2022-05-12 10:56:55 +03:00
|
|
|
const FidoPinDialog(this.devicePath, this.state, {super.key});
|
2022-03-17 15:06:48 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
ConsumerState<ConsumerStatefulWidget> createState() => _FidoPinDialogState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _FidoPinDialogState extends ConsumerState<FidoPinDialog> {
|
2024-03-06 23:16:17 +03:00
|
|
|
final _currentPinController = TextEditingController();
|
|
|
|
final _currentPinFocus = FocusNode();
|
2022-03-17 15:06:48 +03:00
|
|
|
String _newPin = '';
|
|
|
|
String _confirmPin = '';
|
|
|
|
String? _currentPinError;
|
|
|
|
String? _newPinError;
|
2022-06-08 17:23:19 +03:00
|
|
|
bool _currentIsWrong = false;
|
|
|
|
bool _newIsWrong = false;
|
2023-11-27 17:12:57 +03:00
|
|
|
bool _isObscureCurrent = true;
|
|
|
|
bool _isObscureNew = true;
|
|
|
|
bool _isObscureConfirm = true;
|
2024-03-06 23:16:17 +03:00
|
|
|
bool _isBlocked = false;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_currentPinController.dispose();
|
|
|
|
_currentPinFocus.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
2022-03-17 15:06:48 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-02-28 21:05:46 +03:00
|
|
|
final l10n = AppLocalizations.of(context)!;
|
2022-03-17 15:06:48 +03:00
|
|
|
final hasPin = widget.state.hasPin;
|
2022-06-09 16:30:27 +03:00
|
|
|
final minPinLength = widget.state.minPinLength;
|
2024-03-06 23:22:31 +03:00
|
|
|
final currentMinPinLen = !hasPin
|
|
|
|
? 0
|
|
|
|
// N.B. current PIN may be shorter than minimum if set before the minimum was increased
|
|
|
|
: (widget.state.forcePinChange ? 4 : widget.state.minPinLength);
|
|
|
|
final currentPinLenOk =
|
|
|
|
_currentPinController.text.length >= currentMinPinLen;
|
2024-03-06 23:16:17 +03:00
|
|
|
final newPinLenOk = _newPin.length >= minPinLength;
|
|
|
|
final isValid = currentPinLenOk && newPinLenOk && _newPin == _confirmPin;
|
2022-03-17 15:06:48 +03:00
|
|
|
|
|
|
|
return ResponsiveDialog(
|
2023-03-02 14:45:55 +03:00
|
|
|
title: Text(hasPin ? l10n.s_change_pin : l10n.s_set_pin),
|
2022-05-12 09:34:51 +03:00
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
onPressed: isValid ? _submit : null,
|
2024-02-09 22:56:07 +03:00
|
|
|
key: saveButton,
|
2023-03-02 14:45:55 +03:00
|
|
|
child: Text(l10n.s_save),
|
2022-05-12 09:34:51 +03:00
|
|
|
),
|
|
|
|
],
|
2022-09-07 11:25:22 +03:00
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 18.0),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
if (hasPin) ...[
|
2023-09-27 16:12:31 +03:00
|
|
|
Text(l10n.p_enter_current_pin_or_reset_no_puk),
|
2023-11-10 17:24:53 +03:00
|
|
|
AppTextFormField(
|
2024-02-08 18:06:39 +03:00
|
|
|
key: currentPin,
|
2024-03-06 23:16:17 +03:00
|
|
|
controller: _currentPinController,
|
|
|
|
focusNode: _currentPinFocus,
|
2022-09-07 11:25:22 +03:00
|
|
|
autofocus: true,
|
2023-11-27 17:12:57 +03:00
|
|
|
obscureText: _isObscureCurrent,
|
2023-03-14 17:24:41 +03:00
|
|
|
autofillHints: const [AutofillHints.password],
|
2023-12-14 19:21:46 +03:00
|
|
|
decoration: AppInputDecoration(
|
2024-03-06 23:16:17 +03:00
|
|
|
enabled: !_isBlocked,
|
2023-12-14 19:21:46 +03:00
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
labelText: l10n.s_current_pin,
|
|
|
|
errorText: _currentIsWrong ? _currentPinError : null,
|
|
|
|
errorMaxLines: 3,
|
2024-03-08 11:30:47 +03:00
|
|
|
prefixIcon: const Icon(Symbols.pin),
|
2023-12-14 19:21:46 +03:00
|
|
|
suffixIcon: IconButton(
|
|
|
|
icon: Icon(_isObscureCurrent
|
2024-03-08 11:30:47 +03:00
|
|
|
? Symbols.visibility
|
|
|
|
: Symbols.visibility_off),
|
2023-12-14 19:21:46 +03:00
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
_isObscureCurrent = !_isObscureCurrent;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
tooltip:
|
|
|
|
_isObscureCurrent ? l10n.s_show_pin : l10n.s_hide_pin,
|
|
|
|
),
|
|
|
|
),
|
2022-09-07 11:25:22 +03:00
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_currentIsWrong = false;
|
|
|
|
});
|
|
|
|
},
|
2024-03-07 17:01:46 +03:00
|
|
|
).init(),
|
2022-09-07 11:25:22 +03:00
|
|
|
],
|
2023-02-28 21:05:46 +03:00
|
|
|
Text(l10n.p_enter_new_fido2_pin(minPinLength)),
|
2022-09-07 11:25:22 +03:00
|
|
|
// TODO: Set max characters based on UTF-8 bytes
|
2023-11-10 17:24:53 +03:00
|
|
|
AppTextFormField(
|
2024-02-08 18:06:39 +03:00
|
|
|
key: newPin,
|
2022-09-07 11:25:22 +03:00
|
|
|
initialValue: _newPin,
|
|
|
|
autofocus: !hasPin,
|
2023-11-27 17:12:57 +03:00
|
|
|
obscureText: _isObscureNew,
|
2023-03-14 17:24:41 +03:00
|
|
|
autofillHints: const [AutofillHints.password],
|
2023-12-14 19:21:46 +03:00
|
|
|
decoration: AppInputDecoration(
|
2022-03-17 15:06:48 +03:00
|
|
|
border: const OutlineInputBorder(),
|
2023-03-02 14:45:55 +03:00
|
|
|
labelText: l10n.s_new_pin,
|
2024-03-06 23:16:17 +03:00
|
|
|
enabled: !_isBlocked && currentPinLenOk,
|
2022-09-07 11:25:22 +03:00
|
|
|
errorText: _newIsWrong ? _newPinError : null,
|
2022-06-08 17:23:19 +03:00
|
|
|
errorMaxLines: 3,
|
2024-03-08 11:30:47 +03:00
|
|
|
prefixIcon: const Icon(Symbols.pin),
|
2023-12-14 19:21:46 +03:00
|
|
|
suffixIcon: IconButton(
|
2024-03-08 11:30:47 +03:00
|
|
|
icon: Icon(_isObscureNew
|
|
|
|
? Symbols.visibility
|
|
|
|
: Symbols.visibility_off),
|
2023-12-14 19:21:46 +03:00
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
_isObscureNew = !_isObscureNew;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
tooltip: _isObscureNew ? l10n.s_show_pin : l10n.s_hide_pin,
|
|
|
|
),
|
2022-03-17 15:06:48 +03:00
|
|
|
),
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
2022-09-07 11:25:22 +03:00
|
|
|
_newIsWrong = false;
|
|
|
|
_newPin = value;
|
2022-03-17 15:06:48 +03:00
|
|
|
});
|
|
|
|
},
|
2024-03-07 17:01:46 +03:00
|
|
|
).init(),
|
2023-11-10 17:24:53 +03:00
|
|
|
AppTextFormField(
|
2024-02-08 18:06:39 +03:00
|
|
|
key: confirmPin,
|
2022-09-07 11:25:22 +03:00
|
|
|
initialValue: _confirmPin,
|
2023-11-27 17:12:57 +03:00
|
|
|
obscureText: _isObscureConfirm,
|
2023-03-14 17:24:41 +03:00
|
|
|
autofillHints: const [AutofillHints.password],
|
2023-12-14 19:21:46 +03:00
|
|
|
decoration: AppInputDecoration(
|
2022-09-07 11:25:22 +03:00
|
|
|
border: const OutlineInputBorder(),
|
2023-03-02 14:45:55 +03:00
|
|
|
labelText: l10n.s_confirm_pin,
|
2024-03-08 11:30:47 +03:00
|
|
|
prefixIcon: const Icon(Symbols.pin),
|
2023-12-14 19:21:46 +03:00
|
|
|
suffixIcon: IconButton(
|
|
|
|
icon: Icon(_isObscureConfirm
|
2024-03-08 11:30:47 +03:00
|
|
|
? Symbols.visibility
|
|
|
|
: Symbols.visibility_off),
|
2023-12-14 19:21:46 +03:00
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
_isObscureConfirm = !_isObscureConfirm;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
tooltip:
|
|
|
|
_isObscureConfirm ? l10n.s_show_pin : l10n.s_hide_pin,
|
2023-11-27 17:12:57 +03:00
|
|
|
),
|
2024-03-06 23:16:17 +03:00
|
|
|
enabled: !_isBlocked && currentPinLenOk && newPinLenOk,
|
2024-03-07 11:38:38 +03:00
|
|
|
errorText: _newPin.length == _confirmPin.length &&
|
|
|
|
_newPin != _confirmPin
|
|
|
|
? l10n.l_pin_mismatch
|
|
|
|
: null,
|
|
|
|
helperText: '', // Prevents resizing when errorText shown
|
2022-09-07 11:25:22 +03:00
|
|
|
),
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_confirmPin = value;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onFieldSubmitted: (_) {
|
|
|
|
if (isValid) {
|
|
|
|
_submit();
|
|
|
|
}
|
|
|
|
},
|
2024-03-07 17:01:46 +03:00
|
|
|
).init(),
|
2022-09-07 11:25:22 +03:00
|
|
|
]
|
|
|
|
.map((e) => Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
|
|
|
child: e,
|
|
|
|
))
|
|
|
|
.toList(),
|
|
|
|
),
|
2022-03-17 15:06:48 +03:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2022-04-05 12:46:22 +03:00
|
|
|
|
|
|
|
void _submit() async {
|
2023-02-28 21:05:46 +03:00
|
|
|
final l10n = AppLocalizations.of(context)!;
|
2024-03-06 23:16:17 +03:00
|
|
|
final oldPin = _currentPinController.text.isNotEmpty
|
|
|
|
? _currentPinController.text
|
|
|
|
: null;
|
2022-06-13 17:45:26 +03:00
|
|
|
try {
|
|
|
|
final result = await ref
|
|
|
|
.read(fidoStateProvider(widget.devicePath).notifier)
|
|
|
|
.setPin(_newPin, oldPin: oldPin);
|
|
|
|
result.when(success: () {
|
|
|
|
Navigator.of(context).pop(true);
|
2023-03-02 14:45:55 +03:00
|
|
|
showMessage(context, l10n.s_pin_set);
|
2022-06-13 17:45:26 +03:00
|
|
|
}, failed: (retries, authBlocked) {
|
|
|
|
setState(() {
|
2024-03-06 23:16:17 +03:00
|
|
|
_currentPinController.selection = TextSelection(
|
|
|
|
baseOffset: 0, extentOffset: _currentPinController.text.length);
|
|
|
|
_currentPinFocus.requestFocus();
|
2022-06-13 17:45:26 +03:00
|
|
|
if (authBlocked) {
|
2023-02-28 21:05:46 +03:00
|
|
|
_currentPinError = l10n.l_pin_soft_locked;
|
2022-06-13 17:45:26 +03:00
|
|
|
_currentIsWrong = true;
|
2024-03-06 23:16:17 +03:00
|
|
|
_isBlocked = true;
|
2022-06-13 17:45:26 +03:00
|
|
|
} else {
|
2023-02-28 21:05:46 +03:00
|
|
|
_currentPinError = l10n.l_wrong_pin_attempts_remaining(retries);
|
2022-06-13 17:45:26 +03:00
|
|
|
_currentIsWrong = true;
|
|
|
|
}
|
|
|
|
});
|
2022-04-05 12:46:22 +03:00
|
|
|
});
|
2024-01-05 10:41:54 +03:00
|
|
|
} on CancellationException catch (_) {
|
2024-01-03 17:35:22 +03:00
|
|
|
// ignored
|
2022-06-13 17:45:26 +03:00
|
|
|
} catch (e) {
|
|
|
|
_log.error('Failed to set PIN', e);
|
|
|
|
final String errorMessage;
|
|
|
|
// TODO: Make this cleaner than importing desktop specific RpcError.
|
|
|
|
if (e is RpcError) {
|
|
|
|
errorMessage = e.message;
|
|
|
|
} else {
|
|
|
|
errorMessage = e.toString();
|
|
|
|
}
|
2023-08-21 16:43:17 +03:00
|
|
|
await ref.read(withContextProvider)(
|
|
|
|
(context) async {
|
|
|
|
showMessage(
|
|
|
|
context,
|
|
|
|
l10n.l_set_pin_failed(errorMessage),
|
|
|
|
duration: const Duration(seconds: 4),
|
|
|
|
);
|
|
|
|
},
|
2022-06-13 17:45:26 +03:00
|
|
|
);
|
|
|
|
}
|
2022-04-05 12:46:22 +03:00
|
|
|
}
|
2022-03-17 15:06:48 +03:00
|
|
|
}
|