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

199 lines
6.7 KiB
Dart
Raw Normal View History

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';
import '../../app/message.dart';
import '../../app/models.dart';
2023-11-10 17:24:53 +03:00
import '../../widgets/app_text_field.dart';
import '../../widgets/responsive_dialog.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
enum ManageTarget { pin, puk, unblock }
class ManagePinPukDialog extends ConsumerStatefulWidget {
final DevicePath path;
final ManageTarget target;
const ManagePinPukDialog(this.path,
{super.key, this.target = ManageTarget.pin});
@override
ConsumerState<ConsumerStatefulWidget> createState() =>
_ManagePinPukDialogState();
}
class _ManagePinPukDialogState extends ConsumerState<ManagePinPukDialog> {
String _currentPin = '';
String _newPin = '';
String _confirmPin = '';
bool _currentIsWrong = false;
int _attemptsRemaining = -1;
_submit() async {
final notifier = ref.read(pivStateProvider(widget.path).notifier);
2023-06-13 15:33:23 +03:00
final result = await switch (widget.target) {
ManageTarget.pin => notifier.changePin(_currentPin, _newPin),
ManageTarget.puk => notifier.changePuk(_currentPin, _newPin),
ManageTarget.unblock => notifier.unblockPin(_currentPin, _newPin),
};
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) {
setState(() {
_attemptsRemaining = attemptsRemaining;
_currentIsWrong = true;
_currentPin = '';
});
});
}
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
final isValid =
_newPin.isNotEmpty && _newPin == _confirmPin && _currentPin.isNotEmpty;
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
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
//TODO fix string
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(
2023-04-27 10:13:38 +03:00
autofocus: true,
obscureText: true,
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,
decoration: InputDecoration(
2023-11-24 16:37:37 +03:00
border: const OutlineInputBorder(),
labelText: widget.target == ManageTarget.pin
? l10n.s_current_pin
: l10n.s_current_puk,
errorText: _currentIsWrong
? (widget.target == ManageTarget.pin
? l10n
.l_wrong_pin_attempts_remaining(_attemptsRemaining)
: l10n
.l_wrong_puk_attempts_remaining(_attemptsRemaining))
: null,
errorMaxLines: 3,
prefixIcon: const Icon(Icons.password_outlined),
suffixIcon: _currentIsWrong ? const Icon(Icons.error) : null,
),
2023-04-27 10:13:38 +03:00
textInputAction: TextInputAction.next,
onChanged: (value) {
setState(() {
_currentIsWrong = false;
_currentPin = value;
});
},
),
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,
obscureText: true,
2023-06-13 15:33:23 +03:00
maxLength: 8,
2023-04-27 10:13:38 +03:00
autofillHints: const [AutofillHints.newPassword],
decoration: InputDecoration(
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,
prefixIcon: const Icon(Icons.password_outlined),
2023-06-13 15:33:23 +03:00
// Old YubiKeys allowed a 4 digit PIN
enabled: _currentPin.length >= 4,
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,
obscureText: true,
2023-06-13 15:33:23 +03:00
maxLength: 8,
2023-04-27 10:13:38 +03:00
autofillHints: const [AutofillHints.newPassword],
decoration: InputDecoration(
border: const OutlineInputBorder(),
2023-06-16 18:27:10 +03:00
labelText: widget.target == ManageTarget.puk
? l10n.s_confirm_puk
: l10n.s_confirm_pin,
2023-04-27 10:13:38 +03:00
prefixIcon: const Icon(Icons.password_outlined),
2023-06-13 15:33:23 +03:00
enabled: _currentPin.length >= 4 && _newPin.length >= 6,
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(),
),
),
);
}
}