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

188 lines
5.6 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../app/message.dart';
import '../../app/models.dart';
import '../../app/views/app_page.dart';
2022-05-18 15:11:17 +03:00
import '../../app/views/graphics.dart';
import '../../app/views/message_page.dart';
2022-05-20 14:00:07 +03:00
import '../../theme.dart';
import '../models.dart';
import '../state.dart';
import 'pin_dialog.dart';
import 'reset_dialog.dart';
class FidoLockedPage extends ConsumerWidget {
final DeviceNode node;
final FidoState state;
2022-05-12 10:56:55 +03:00
const FidoLockedPage(this.node, this.state, {super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
if (!state.hasPin) {
if (state.bioEnroll != null) {
return MessagePage(
title: const Text('WebAuthn'),
2022-05-20 12:11:20 +03:00
graphic: noFingerprints,
header: 'No fingerprints',
2022-05-20 12:11:20 +03:00
message: 'Set a PIN to register fingerprints.',
2022-05-20 14:00:07 +03:00
actions: _buildActions(context),
);
} else {
return MessagePage(
title: const Text('WebAuthn'),
2022-05-20 12:11:20 +03:00
graphic: noDiscoverable,
header: 'No discoverable accounts',
message:
'Optionally set a PIN to protect access to your YubiKey\nRegister as a Security Key on websites',
2022-05-20 14:00:07 +03:00
actions: _buildActions(context),
);
}
}
return AppPage(
title: const Text('WebAuthn'),
2022-05-20 14:00:07 +03:00
actions: _buildActions(context),
child: Column(
children: [
const ListTile(title: Text('Unlock')),
_PinEntryForm(state, node),
],
),
);
}
2022-05-20 14:00:07 +03:00
List<Widget> _buildActions(BuildContext context) => [
2022-05-20 12:11:20 +03:00
if (!state.hasPin)
2022-05-20 14:00:07 +03:00
OutlinedButton.icon(
style: AppTheme.primaryOutlinedButtonStyle(context),
label: const Text('Set PIN'),
2022-05-18 11:53:22 +03:00
icon: const Icon(Icons.pin),
2022-05-20 14:00:07 +03:00
onPressed: () {
showDialog(
context: context,
builder: (context) => FidoPinDialog(node.path, state),
);
},
),
2022-05-20 14:00:07 +03:00
OutlinedButton.icon(
label: const Text('Options'),
2022-05-20 12:11:20 +03:00
icon: const Icon(Icons.tune),
2022-05-20 14:00:07 +03:00
onPressed: () {
2022-05-20 12:11:20 +03:00
showBottomMenu(context, [
if (state.hasPin)
MenuAction(
text: 'Change PIN',
icon: const Icon(Icons.pin),
action: (context) {
showDialog(
context: context,
builder: (context) => FidoPinDialog(node.path, state),
);
},
),
MenuAction(
text: 'Reset FIDO',
icon: const Icon(Icons.delete),
action: (context) {
showDialog(
context: context,
builder: (context) => ResetDialog(node),
);
},
),
]);
},
),
];
}
class _PinEntryForm extends ConsumerStatefulWidget {
final FidoState _state;
final DeviceNode _deviceNode;
2022-05-12 10:56:55 +03:00
const _PinEntryForm(this._state, this._deviceNode);
@override
ConsumerState<_PinEntryForm> createState() => _PinEntryFormState();
}
class _PinEntryFormState extends ConsumerState<_PinEntryForm> {
final _pinController = TextEditingController();
bool _blocked = false;
int? _retries;
void _submit() async {
final result = await ref
.read(fidoStateProvider(widget._deviceNode.path).notifier)
.unlock(_pinController.text);
result.whenOrNull(failed: (retries, authBlocked) {
setState(() {
_pinController.clear();
_retries = retries;
_blocked = authBlocked;
});
});
}
String? _getErrorText() {
if (_retries == 0) {
return 'PIN is blocked. Factory reset the FIDO application.';
}
if (_blocked) {
return 'PIN temporarily blocked, remove and reinsert your YubiKey.';
}
if (_retries != null) {
return 'Wrong PIN. $_retries attempts remaining.';
}
return null;
}
@override
Widget build(BuildContext context) {
final noFingerprints = widget._state.bioEnroll == false;
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('Enter the FIDO2 PIN for your YubiKey'),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: TextField(
autofocus: true,
obscureText: true,
controller: _pinController,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: 'PIN',
errorText: _getErrorText(),
),
onChanged: (_) => setState(() {}), // Update state on change
onSubmitted: (_) => _submit(),
),
),
ListTile(
leading:
noFingerprints ? const Icon(Icons.warning_amber_rounded) : null,
title: noFingerprints
? const Text(
'No fingerprints have been added',
overflow: TextOverflow.fade,
)
: null,
dense: true,
contentPadding: const EdgeInsets.symmetric(horizontal: 0),
minLeadingWidth: 0,
trailing: ElevatedButton(
onPressed:
_pinController.text.isNotEmpty && !_blocked ? _submit : null,
2022-05-12 09:34:51 +03:00
child: const Text('Unlock'),
),
),
],
),
);
}
}