yubioath-flutter/lib/fido/views/pin_dialog.dart

195 lines
6.5 KiB
Dart
Raw Normal View History

2022-10-04 13:12:54 +03:00
/*
* Copyright (C) 2022 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.
*/
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';
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';
2022-06-13 17:45:26 +03:00
import '../../desktop/models.dart';
import '../../widgets/responsive_dialog.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;
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> {
String _currentPin = '';
String _newPin = '';
String _confirmPin = '';
String? _currentPinError;
String? _newPinError;
bool _currentIsWrong = false;
bool _newIsWrong = false;
2022-03-17 15:06:48 +03:00
@override
Widget build(BuildContext context) {
final hasPin = widget.state.hasPin;
final isValid = _newPin.isNotEmpty &&
_newPin == _confirmPin &&
(!hasPin || _currentPin.isNotEmpty);
final minPinLength = widget.state.minPinLength;
2022-03-17 15:06:48 +03:00
return ResponsiveDialog(
2022-09-12 16:46:43 +03:00
title: Text(hasPin
? AppLocalizations.of(context)!.fido_change_pin
: AppLocalizations.of(context)!.fido_set_pin),
2022-05-12 09:34:51 +03:00
actions: [
TextButton(
onPressed: isValid ? _submit : null,
2022-09-12 16:46:43 +03:00
child: Text(AppLocalizations.of(context)!.fido_save),
2022-05-12 09:34:51 +03:00
),
],
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 18.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (hasPin) ...[
2022-09-12 16:46:43 +03:00
Text(AppLocalizations.of(context)!.fido_enter_current_pin),
TextFormField(
initialValue: _currentPin,
autofocus: true,
obscureText: true,
decoration: InputDecoration(
border: const OutlineInputBorder(),
2022-09-12 16:46:43 +03:00
labelText: AppLocalizations.of(context)!.fido_current_pin,
errorText: _currentIsWrong ? _currentPinError : null,
errorMaxLines: 3,
prefixIcon: const Icon(Icons.pin_outlined),
),
onChanged: (value) {
setState(() {
_currentIsWrong = false;
_currentPin = value;
});
},
),
],
Text(
2022-09-12 16:46:43 +03:00
AppLocalizations.of(context)!.fido_enter_new_pin(minPinLength)),
// TODO: Set max characters based on UTF-8 bytes
2022-03-17 15:06:48 +03:00
TextFormField(
initialValue: _newPin,
autofocus: !hasPin,
2022-03-17 15:06:48 +03:00
obscureText: true,
decoration: InputDecoration(
border: const OutlineInputBorder(),
2022-09-12 16:46:43 +03:00
labelText: AppLocalizations.of(context)!.fido_new_pin,
enabled: !hasPin || _currentPin.isNotEmpty,
errorText: _newIsWrong ? _newPinError : null,
errorMaxLines: 3,
2022-06-09 12:23:34 +03:00
prefixIcon: const Icon(Icons.pin_outlined),
2022-03-17 15:06:48 +03:00
),
onChanged: (value) {
setState(() {
_newIsWrong = false;
_newPin = value;
2022-03-17 15:06:48 +03:00
});
},
),
TextFormField(
initialValue: _confirmPin,
obscureText: true,
decoration: InputDecoration(
border: const OutlineInputBorder(),
2022-09-12 16:46:43 +03:00
labelText: AppLocalizations.of(context)!.fido_confirm_pin,
prefixIcon: const Icon(Icons.pin_outlined),
enabled:
(!hasPin || _currentPin.isNotEmpty) && _newPin.isNotEmpty,
),
onChanged: (value) {
setState(() {
_confirmPin = value;
});
},
onFieldSubmitted: (_) {
if (isValid) {
_submit();
}
},
2022-03-17 15:06:48 +03:00
),
]
.map((e) => Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: e,
))
.toList(),
),
2022-03-17 15:06:48 +03:00
),
);
}
void _submit() async {
final minPinLength = widget.state.minPinLength;
final oldPin = _currentPin.isNotEmpty ? _currentPin : null;
if (_newPin.length < minPinLength) {
setState(() {
2022-09-12 16:46:43 +03:00
_newPinError =
AppLocalizations.of(context)!.fido_new_pin_chars(minPinLength);
_newIsWrong = true;
});
return;
}
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);
2022-09-12 16:46:43 +03:00
showMessage(context, AppLocalizations.of(context)!.fido_pin_set);
2022-06-13 17:45:26 +03:00
}, failed: (retries, authBlocked) {
setState(() {
if (authBlocked) {
2022-09-12 16:46:43 +03:00
_currentPinError = AppLocalizations.of(context)!.fido_pin_blocked;
2022-06-13 17:45:26 +03:00
_currentIsWrong = true;
} else {
2022-09-12 16:46:43 +03:00
_currentPinError = AppLocalizations.of(context)!
.fido_wrong_pin_retries_remaining(retries);
2022-06-13 17:45:26 +03:00
_currentIsWrong = true;
}
});
});
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();
}
showMessage(
context,
2022-09-12 16:46:43 +03:00
'${AppLocalizations.of(context)!.fido_fail_set_pin}: $errorMessage',
2022-06-13 17:45:26 +03:00
duration: const Duration(seconds: 4),
);
}
}
2022-03-17 15:06:48 +03:00
}