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';
|
|
|
|
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_field.dart';
|
2023-11-27 13:41:05 +03:00
|
|
|
import '../../widgets/responsive_dialog.dart';
|
2024-03-11 18:24:38 +03:00
|
|
|
import '../../widgets/utf8_utils.dart';
|
2023-04-27 10:13:38 +03:00
|
|
|
import '../keys.dart' as keys;
|
2023-11-10 17:24:53 +03:00
|
|
|
import '../state.dart';
|
2023-04-27 10:13:38 +03:00
|
|
|
|
|
|
|
class PinDialog extends ConsumerStatefulWidget {
|
|
|
|
final DevicePath devicePath;
|
|
|
|
const PinDialog(this.devicePath, {super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
ConsumerState<ConsumerStatefulWidget> createState() => _PinDialogState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _PinDialogState extends ConsumerState<PinDialog> {
|
2023-08-22 15:22:32 +03:00
|
|
|
final _pinController = TextEditingController();
|
2024-03-06 23:16:17 +03:00
|
|
|
final _pinFocus = FocusNode();
|
2023-04-27 10:13:38 +03:00
|
|
|
bool _pinIsWrong = false;
|
|
|
|
int _attemptsRemaining = -1;
|
2023-08-22 15:22:32 +03:00
|
|
|
bool _isObscure = true;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_pinController.dispose();
|
2024-03-06 23:16:17 +03:00
|
|
|
_pinFocus.dispose();
|
2023-08-22 15:22:32 +03:00
|
|
|
super.dispose();
|
|
|
|
}
|
2023-04-27 10:13:38 +03:00
|
|
|
|
|
|
|
Future<void> _submit() async {
|
|
|
|
final navigator = Navigator.of(context);
|
|
|
|
try {
|
|
|
|
final status = await ref
|
|
|
|
.read(pivStateProvider(widget.devicePath).notifier)
|
2023-08-22 15:22:32 +03:00
|
|
|
.verifyPin(_pinController.text);
|
2023-04-27 10:13:38 +03:00
|
|
|
status.when(
|
|
|
|
success: () {
|
|
|
|
navigator.pop(true);
|
|
|
|
},
|
|
|
|
failure: (attemptsRemaining) {
|
2024-03-06 23:16:17 +03:00
|
|
|
_pinController.selection = TextSelection(
|
|
|
|
baseOffset: 0, extentOffset: _pinController.text.length);
|
|
|
|
_pinFocus.requestFocus();
|
2023-04-27 10:13:38 +03:00
|
|
|
setState(() {
|
|
|
|
_attemptsRemaining = attemptsRemaining;
|
|
|
|
_pinIsWrong = true;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} on CancellationException catch (_) {
|
|
|
|
navigator.pop(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final l10n = AppLocalizations.of(context)!;
|
2024-03-06 23:16:17 +03:00
|
|
|
final version = ref.watch(pivStateProvider(widget.devicePath)).valueOrNull;
|
|
|
|
final minPinLen = version?.version.isAtLeast(4, 3, 1) == true ? 6 : 4;
|
2024-03-11 18:24:38 +03:00
|
|
|
final currentPinLen = byteLength(_pinController.text);
|
2023-04-27 10:13:38 +03:00
|
|
|
return ResponsiveDialog(
|
2023-06-05 16:56:13 +03:00
|
|
|
title: Text(l10n.s_pin_required),
|
2023-04-27 10:13:38 +03:00
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
key: keys.unlockButton,
|
2024-03-11 18:24:38 +03:00
|
|
|
onPressed: currentPinLen >= minPinLen ? _submit : 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_pin_required_desc),
|
2023-11-10 17:24:53 +03:00
|
|
|
AppTextField(
|
2023-04-27 10:13:38 +03:00
|
|
|
autofocus: true,
|
2023-08-22 15:22:32 +03:00
|
|
|
obscureText: _isObscure,
|
2023-06-13 15:33:23 +03:00
|
|
|
maxLength: 8,
|
2024-03-11 18:24:38 +03:00
|
|
|
inputFormatters: [limitBytesLength(8)],
|
|
|
|
buildCounter: buildByteCounterFor(_pinController.text),
|
2023-04-27 10:13:38 +03:00
|
|
|
autofillHints: const [AutofillHints.password],
|
|
|
|
key: keys.managementKeyField,
|
2023-08-22 15:22:32 +03:00
|
|
|
controller: _pinController,
|
2024-03-06 23:16:17 +03:00
|
|
|
focusNode: _pinFocus,
|
2023-12-14 18:38:10 +03:00
|
|
|
decoration: AppInputDecoration(
|
2023-08-22 15:22:32 +03:00
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
labelText: l10n.s_pin,
|
|
|
|
errorText: _pinIsWrong
|
|
|
|
? l10n.l_wrong_pin_attempts_remaining(_attemptsRemaining)
|
|
|
|
: null,
|
|
|
|
errorMaxLines: 3,
|
2024-03-08 11:30:47 +03:00
|
|
|
prefixIcon: const Icon(Symbols.pin),
|
2023-12-14 18:38:10 +03:00
|
|
|
suffixIcon: IconButton(
|
|
|
|
icon: Icon(
|
2024-03-08 11:30:47 +03:00
|
|
|
_isObscure ? Symbols.visibility : Symbols.visibility_off),
|
2023-12-14 18:38:10 +03:00
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
_isObscure = !_isObscure;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
tooltip: _isObscure ? l10n.s_show_pin : l10n.s_hide_pin,
|
2023-08-22 15:22:32 +03:00
|
|
|
),
|
|
|
|
),
|
2023-04-27 10:13:38 +03:00
|
|
|
textInputAction: TextInputAction.next,
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_pinIsWrong = false;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onSubmitted: (_) => _submit(),
|
2024-03-07 17:01:46 +03:00
|
|
|
).init(),
|
2023-04-27 10:13:38 +03:00
|
|
|
]
|
|
|
|
.map((e) => Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
|
|
|
child: e,
|
|
|
|
))
|
|
|
|
.toList(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|