2023-04-27 10:13:38 +03:00
|
|
|
/*
|
2023-11-10 17:24:53 +03:00
|
|
|
* Copyright (C) 2022-2023 Yubico.
|
2023-04-27 10:13:38 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
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/message.dart';
|
|
|
|
import '../../app/models.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';
|
2023-04-27 10:13:38 +03:00
|
|
|
import '../keys.dart' as keys;
|
2024-02-09 13:04:54 +03:00
|
|
|
import '../models.dart';
|
2023-11-10 17:24:53 +03:00
|
|
|
import '../state.dart';
|
2023-04-27 10:13:38 +03:00
|
|
|
|
|
|
|
enum ManageTarget { pin, puk, unblock }
|
|
|
|
|
|
|
|
class ManagePinPukDialog extends ConsumerStatefulWidget {
|
|
|
|
final DevicePath path;
|
2024-02-09 13:04:54 +03:00
|
|
|
final PivState pivState;
|
2023-04-27 10:13:38 +03:00
|
|
|
final ManageTarget target;
|
2024-02-09 13:04:54 +03:00
|
|
|
const ManagePinPukDialog(this.path, this.pivState,
|
2023-04-27 10:13:38 +03:00
|
|
|
{super.key, this.target = ManageTarget.pin});
|
|
|
|
|
|
|
|
@override
|
|
|
|
ConsumerState<ConsumerStatefulWidget> createState() =>
|
|
|
|
_ManagePinPukDialogState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ManagePinPukDialogState extends ConsumerState<ManagePinPukDialog> {
|
2024-02-09 13:04:54 +03:00
|
|
|
final _currentPinController = TextEditingController();
|
2024-03-06 23:16:17 +03:00
|
|
|
final _currentPinFocus = FocusNode();
|
2023-04-27 10:13:38 +03:00
|
|
|
String _newPin = '';
|
|
|
|
String _confirmPin = '';
|
2024-03-06 23:16:17 +03:00
|
|
|
bool _pinIsBlocked = false;
|
2023-04-27 10:13:38 +03:00
|
|
|
bool _currentIsWrong = false;
|
|
|
|
int _attemptsRemaining = -1;
|
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
|
|
|
late final bool _defaultPinUsed;
|
|
|
|
late final bool _defaultPukUsed;
|
|
|
|
late final int _minPinLen;
|
2024-02-09 13:04:54 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
2024-03-06 23:16:17 +03:00
|
|
|
// Old YubiKeys allowed a 4 digit PIN
|
|
|
|
_minPinLen = widget.pivState.version.isAtLeast(4, 3, 1) ? 6 : 4;
|
2024-02-09 13:04:54 +03:00
|
|
|
_defaultPinUsed =
|
|
|
|
widget.pivState.metadata?.pinMetadata.defaultValue ?? false;
|
|
|
|
_defaultPukUsed =
|
|
|
|
widget.pivState.metadata?.pukMetadata.defaultValue ?? false;
|
|
|
|
if (widget.target == ManageTarget.pin && _defaultPinUsed) {
|
|
|
|
_currentPinController.text = defaultPin;
|
|
|
|
}
|
|
|
|
if (widget.target != ManageTarget.pin && _defaultPukUsed) {
|
|
|
|
_currentPinController.text = defaultPuk;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_currentPinController.dispose();
|
2024-03-06 23:16:17 +03:00
|
|
|
_currentPinFocus.dispose();
|
2024-02-09 13:04:54 +03:00
|
|
|
super.dispose();
|
|
|
|
}
|
2023-04-27 10:13:38 +03:00
|
|
|
|
|
|
|
_submit() async {
|
|
|
|
final notifier = ref.read(pivStateProvider(widget.path).notifier);
|
2023-06-13 15:33:23 +03:00
|
|
|
final result = await switch (widget.target) {
|
2024-02-09 13:04:54 +03:00
|
|
|
ManageTarget.pin =>
|
|
|
|
notifier.changePin(_currentPinController.text, _newPin),
|
|
|
|
ManageTarget.puk =>
|
|
|
|
notifier.changePuk(_currentPinController.text, _newPin),
|
|
|
|
ManageTarget.unblock =>
|
|
|
|
notifier.unblockPin(_currentPinController.text, _newPin),
|
2023-06-13 15:33:23 +03:00
|
|
|
};
|
2023-04-27 10:13:38 +03:00
|
|
|
|
|
|
|
result.when(success: () {
|
|
|
|
if (!mounted) return;
|
2023-06-13 15:33:23 +03:00
|
|
|
final l10n = AppLocalizations.of(context)!;
|
2023-04-27 10:13:38 +03:00
|
|
|
Navigator.of(context).pop();
|
2023-06-13 15:33:23 +03:00
|
|
|
showMessage(
|
|
|
|
context,
|
|
|
|
switch (widget.target) {
|
|
|
|
ManageTarget.puk => l10n.s_puk_set,
|
|
|
|
_ => l10n.s_pin_set,
|
|
|
|
});
|
2023-04-27 10:13:38 +03:00
|
|
|
}, failure: (attemptsRemaining) {
|
2024-03-06 23:16:17 +03:00
|
|
|
_currentPinController.selection = TextSelection(
|
|
|
|
baseOffset: 0, extentOffset: _currentPinController.text.length);
|
|
|
|
_currentPinFocus.requestFocus();
|
2023-04-27 10:13:38 +03:00
|
|
|
setState(() {
|
|
|
|
_attemptsRemaining = attemptsRemaining;
|
|
|
|
_currentIsWrong = true;
|
2024-03-06 23:16:17 +03:00
|
|
|
if (_attemptsRemaining == 0) {
|
|
|
|
_pinIsBlocked = true;
|
|
|
|
}
|
2023-04-27 10:13:38 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final l10n = AppLocalizations.of(context)!;
|
2024-02-09 13:04:54 +03:00
|
|
|
final currentPin = _currentPinController.text;
|
2024-03-06 23:16:17 +03:00
|
|
|
final isValid = !_currentIsWrong &&
|
|
|
|
_newPin.isNotEmpty &&
|
|
|
|
_newPin == _confirmPin &&
|
|
|
|
currentPin.isNotEmpty;
|
2023-04-27 10:13:38 +03:00
|
|
|
|
2023-06-13 15:33:23 +03:00
|
|
|
final titleText = switch (widget.target) {
|
|
|
|
ManageTarget.pin => l10n.s_change_pin,
|
|
|
|
ManageTarget.puk => l10n.s_change_puk,
|
|
|
|
ManageTarget.unblock => l10n.s_unblock_pin,
|
|
|
|
};
|
2023-04-27 10:13:38 +03:00
|
|
|
|
2024-02-09 13:04:54 +03:00
|
|
|
final showDefaultPinUsed =
|
|
|
|
widget.target == ManageTarget.pin && _defaultPinUsed;
|
|
|
|
final showDefaultPukUsed =
|
|
|
|
widget.target != ManageTarget.pin && _defaultPukUsed;
|
|
|
|
|
2023-04-27 10:13:38 +03:00
|
|
|
return ResponsiveDialog(
|
|
|
|
title: Text(titleText),
|
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
onPressed: isValid ? _submit : null,
|
|
|
|
key: keys.saveButton,
|
|
|
|
child: Text(l10n.s_save),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 18.0),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
2023-06-13 15:33:23 +03:00
|
|
|
Text(widget.target == ManageTarget.pin
|
|
|
|
? l10n.p_enter_current_pin_or_reset
|
|
|
|
: l10n.p_enter_current_puk_or_reset),
|
2023-11-10 17:24:53 +03:00
|
|
|
AppTextField(
|
2024-02-09 13:04:54 +03:00
|
|
|
autofocus: !(showDefaultPinUsed || showDefaultPukUsed),
|
2023-11-27 17:12:57 +03:00
|
|
|
obscureText: _isObscureCurrent,
|
2023-06-13 15:33:23 +03:00
|
|
|
maxLength: 8,
|
2023-04-27 10:13:38 +03:00
|
|
|
autofillHints: const [AutofillHints.password],
|
|
|
|
key: keys.pinPukField,
|
2024-02-09 13:04:54 +03:00
|
|
|
readOnly: showDefaultPinUsed || showDefaultPukUsed,
|
|
|
|
controller: _currentPinController,
|
2024-03-06 23:16:17 +03:00
|
|
|
focusNode: _currentPinFocus,
|
|
|
|
enabled: !_pinIsBlocked,
|
2023-12-14 18:38:10 +03:00
|
|
|
decoration: AppInputDecoration(
|
|
|
|
border: const OutlineInputBorder(),
|
2024-02-09 13:04:54 +03:00
|
|
|
helperText: showDefaultPinUsed
|
|
|
|
? l10n.l_default_pin_used
|
|
|
|
: showDefaultPukUsed
|
|
|
|
? l10n.l_default_puk_used
|
|
|
|
: null,
|
2023-12-14 18:38:10 +03:00
|
|
|
labelText: widget.target == ManageTarget.pin
|
|
|
|
? l10n.s_current_pin
|
|
|
|
: l10n.s_current_puk,
|
2024-03-06 23:16:17 +03:00
|
|
|
errorText: _pinIsBlocked
|
2023-12-14 18:38:10 +03:00
|
|
|
? (widget.target == ManageTarget.pin
|
2024-03-06 23:16:17 +03:00
|
|
|
? l10n.l_piv_pin_blocked
|
|
|
|
: l10n.l_piv_pin_puk_blocked)
|
|
|
|
: (_currentIsWrong
|
|
|
|
? (widget.target == ManageTarget.pin
|
|
|
|
? l10n.l_wrong_pin_attempts_remaining(
|
|
|
|
_attemptsRemaining)
|
|
|
|
: l10n.l_wrong_puk_attempts_remaining(
|
|
|
|
_attemptsRemaining))
|
|
|
|
: null),
|
2023-12-14 18:38:10 +03:00
|
|
|
errorMaxLines: 3,
|
2024-03-08 11:30:47 +03:00
|
|
|
prefixIcon: const Icon(Symbols.password),
|
2023-12-14 18:38:10 +03:00
|
|
|
suffixIcon: IconButton(
|
|
|
|
icon: Icon(_isObscureCurrent
|
2024-03-08 11:30:47 +03:00
|
|
|
? Symbols.visibility
|
|
|
|
: Symbols.visibility_off),
|
2023-12-14 18:38:10 +03:00
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
_isObscureCurrent = !_isObscureCurrent;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
tooltip: widget.target == ManageTarget.pin
|
|
|
|
? (_isObscureCurrent ? l10n.s_show_pin : l10n.s_hide_pin)
|
|
|
|
: (_isObscureCurrent ? l10n.s_show_puk : l10n.s_hide_puk),
|
|
|
|
),
|
|
|
|
),
|
2023-04-27 10:13:38 +03:00
|
|
|
textInputAction: TextInputAction.next,
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_currentIsWrong = false;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
2023-06-05 16:56:13 +03:00
|
|
|
Text(l10n.p_enter_new_piv_pin_puk(
|
|
|
|
widget.target == ManageTarget.puk ? l10n.s_puk : l10n.s_pin)),
|
2023-11-10 17:24:53 +03:00
|
|
|
AppTextField(
|
2023-04-27 10:13:38 +03:00
|
|
|
key: keys.newPinPukField,
|
2024-02-09 13:04:54 +03:00
|
|
|
autofocus: showDefaultPinUsed || showDefaultPukUsed,
|
2023-11-27 17:12:57 +03:00
|
|
|
obscureText: _isObscureNew,
|
2023-06-13 15:33:23 +03:00
|
|
|
maxLength: 8,
|
2023-04-27 10:13:38 +03:00
|
|
|
autofillHints: const [AutofillHints.newPassword],
|
2023-12-14 18:38:10 +03:00
|
|
|
decoration: AppInputDecoration(
|
2023-04-27 10:13:38 +03:00
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
labelText: widget.target == ManageTarget.puk
|
2023-06-05 16:56:13 +03:00
|
|
|
? l10n.s_new_puk
|
2023-04-27 10:13:38 +03:00
|
|
|
: l10n.s_new_pin,
|
2024-03-08 11:30:47 +03:00
|
|
|
prefixIcon: const Icon(Symbols.password),
|
2023-12-14 18:38:10 +03:00
|
|
|
suffixIcon: IconButton(
|
2024-03-08 11:30:47 +03:00
|
|
|
icon: Icon(_isObscureNew
|
|
|
|
? Symbols.visibility
|
|
|
|
: Symbols.visibility_off),
|
2023-12-14 18:38:10 +03:00
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
_isObscureNew = !_isObscureNew;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
tooltip: widget.target == ManageTarget.pin
|
|
|
|
? (_isObscureNew ? l10n.s_show_pin : l10n.s_hide_pin)
|
|
|
|
: (_isObscureNew ? l10n.s_show_puk : l10n.s_hide_puk),
|
|
|
|
),
|
2024-03-06 23:16:17 +03:00
|
|
|
enabled: currentPin.length >= _minPinLen,
|
2023-04-27 10:13:38 +03:00
|
|
|
),
|
|
|
|
textInputAction: TextInputAction.next,
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_newPin = value;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onSubmitted: (_) {
|
|
|
|
if (isValid) {
|
|
|
|
_submit();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
2023-11-10 17:24:53 +03:00
|
|
|
AppTextField(
|
2023-04-27 10:13:38 +03:00
|
|
|
key: keys.confirmPinPukField,
|
2023-11-27 17:12:57 +03:00
|
|
|
obscureText: _isObscureConfirm,
|
2023-06-13 15:33:23 +03:00
|
|
|
maxLength: 8,
|
2023-04-27 10:13:38 +03:00
|
|
|
autofillHints: const [AutofillHints.newPassword],
|
2023-12-14 18:38:10 +03:00
|
|
|
decoration: AppInputDecoration(
|
2023-04-27 10:13:38 +03:00
|
|
|
border: const OutlineInputBorder(),
|
2023-06-16 18:27:10 +03:00
|
|
|
labelText: widget.target == ManageTarget.puk
|
|
|
|
? l10n.s_confirm_puk
|
|
|
|
: l10n.s_confirm_pin,
|
2024-03-08 11:30:47 +03:00
|
|
|
prefixIcon: const Icon(Symbols.password),
|
2023-12-14 18:38:10 +03:00
|
|
|
suffixIcon: IconButton(
|
|
|
|
icon: Icon(_isObscureConfirm
|
2024-03-08 11:30:47 +03:00
|
|
|
? Symbols.visibility
|
|
|
|
: Symbols.visibility_off),
|
2023-12-14 18:38:10 +03:00
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
_isObscureConfirm = !_isObscureConfirm;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
tooltip: widget.target == ManageTarget.pin
|
|
|
|
? (_isObscureConfirm ? l10n.s_show_pin : l10n.s_hide_pin)
|
|
|
|
: (_isObscureConfirm ? l10n.s_show_puk : l10n.s_hide_puk),
|
|
|
|
),
|
2024-03-06 23:16:17 +03:00
|
|
|
enabled: currentPin.length >= _minPinLen && _newPin.length >= 6,
|
2024-03-07 11:38:38 +03:00
|
|
|
errorText: _newPin.length == _confirmPin.length &&
|
|
|
|
_newPin != _confirmPin
|
|
|
|
? (widget.target == ManageTarget.pin
|
|
|
|
? l10n.l_pin_mismatch
|
|
|
|
: l10n.l_puk_mismatch)
|
|
|
|
: null,
|
|
|
|
helperText: '', // Prevents resizing when errorText shown
|
2023-04-27 10:13:38 +03:00
|
|
|
),
|
|
|
|
textInputAction: TextInputAction.done,
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_confirmPin = value;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onSubmitted: (_) {
|
|
|
|
if (isValid) {
|
|
|
|
_submit();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
]
|
|
|
|
.map((e) => Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
|
|
|
child: e,
|
|
|
|
))
|
|
|
|
.toList(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|