2022-05-12 09:34:51 +03:00
|
|
|
// ignore_for_file: sort_child_properties_last
|
|
|
|
|
2022-03-23 11:49:20 +03:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
2022-05-03 12:24:25 +03:00
|
|
|
import 'package:yubico_authenticator/app/logging.dart';
|
2022-03-23 11:49:20 +03:00
|
|
|
|
2022-03-25 17:43:32 +03:00
|
|
|
import '../../app/message.dart';
|
2022-06-13 17:45:26 +03:00
|
|
|
import '../../desktop/models.dart';
|
2022-03-31 12:50:40 +03:00
|
|
|
import '../../widgets/responsive_dialog.dart';
|
2022-07-06 16:22:15 +03:00
|
|
|
import '../../widgets/utf8_utils.dart';
|
2022-03-23 11:49:20 +03:00
|
|
|
import '../state.dart';
|
|
|
|
import '../../fido/models.dart';
|
|
|
|
import '../../app/models.dart';
|
|
|
|
|
2022-03-23 19:50:49 +03:00
|
|
|
final _log = Logger('fido.views.add_fingerprint_dialog');
|
2022-03-23 11:49:20 +03:00
|
|
|
|
|
|
|
class AddFingerprintDialog extends ConsumerStatefulWidget {
|
2022-03-28 13:58:45 +03:00
|
|
|
final DevicePath devicePath;
|
2022-05-12 10:56:55 +03:00
|
|
|
const AddFingerprintDialog(this.devicePath, {super.key});
|
2022-03-23 11:49:20 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
ConsumerState<ConsumerStatefulWidget> createState() =>
|
|
|
|
_AddFingerprintDialogState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _AddFingerprintDialogState extends ConsumerState<AddFingerprintDialog>
|
|
|
|
with SingleTickerProviderStateMixin {
|
|
|
|
late FocusNode _nameFocus;
|
|
|
|
|
|
|
|
late AnimationController _animator;
|
|
|
|
late Animation<Color?> _color;
|
|
|
|
late StreamSubscription<FingerprintEvent> _subscription;
|
|
|
|
|
|
|
|
int _samples = 0;
|
|
|
|
int _remaining = 5;
|
|
|
|
Fingerprint? _fingerprint;
|
|
|
|
String _label = '';
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_animator.dispose();
|
|
|
|
_nameFocus.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2022-03-24 14:39:49 +03:00
|
|
|
Animation<Color?> _animateColor(Color color,
|
|
|
|
{Function? atPeak, bool reverse = true}) {
|
2022-03-23 11:49:20 +03:00
|
|
|
final animation =
|
|
|
|
ColorTween(begin: Colors.black, end: color).animate(_animator);
|
|
|
|
_animator.forward().then((_) {
|
2022-03-24 14:39:49 +03:00
|
|
|
if (reverse) {
|
|
|
|
atPeak?.call();
|
|
|
|
_animator.reverse();
|
|
|
|
}
|
2022-03-23 11:49:20 +03:00
|
|
|
});
|
|
|
|
return animation;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
_nameFocus = FocusNode();
|
|
|
|
_animator = AnimationController(
|
|
|
|
vsync: this, duration: const Duration(milliseconds: 250));
|
|
|
|
_color =
|
|
|
|
ColorTween(begin: Colors.black, end: Colors.black).animate(_animator);
|
|
|
|
|
|
|
|
_subscription = ref
|
2022-03-28 13:58:45 +03:00
|
|
|
.read(fingerprintProvider(widget.devicePath).notifier)
|
2022-03-23 11:49:20 +03:00
|
|
|
.registerFingerprint()
|
|
|
|
.listen((event) {
|
|
|
|
setState(() {
|
|
|
|
event.when(capture: (remaining) {
|
|
|
|
_color = _animateColor(Colors.lightGreenAccent, atPeak: () {
|
|
|
|
setState(() {
|
|
|
|
_samples += 1;
|
|
|
|
_remaining = remaining;
|
|
|
|
});
|
2022-03-24 14:39:49 +03:00
|
|
|
}, reverse: remaining > 0);
|
2022-03-23 11:49:20 +03:00
|
|
|
}, complete: (fingerprint) {
|
|
|
|
_remaining = 0;
|
|
|
|
_fingerprint = fingerprint;
|
|
|
|
// This needs a short delay to ensure the field is enabled first
|
|
|
|
Timer(const Duration(milliseconds: 100), _nameFocus.requestFocus);
|
|
|
|
}, error: (code) {
|
2022-05-03 12:24:25 +03:00
|
|
|
_log.debug('Fingerprint capture error (code: $code)');
|
2022-03-23 11:49:20 +03:00
|
|
|
_color = _animateColor(Colors.redAccent);
|
|
|
|
});
|
|
|
|
});
|
2022-03-23 19:50:49 +03:00
|
|
|
}, onError: (error, stacktrace) {
|
2022-05-03 12:24:25 +03:00
|
|
|
_log.error('Error adding fingerprint', error, stacktrace);
|
2022-03-23 11:49:20 +03:00
|
|
|
Navigator.of(context).pop();
|
2022-06-13 17:45:26 +03:00
|
|
|
final String errorMessage;
|
|
|
|
// TODO: Make this cleaner than importing desktop specific RpcError.
|
|
|
|
if (error is RpcError) {
|
|
|
|
errorMessage = error.message;
|
|
|
|
} else {
|
|
|
|
errorMessage = error.toString();
|
|
|
|
}
|
|
|
|
showMessage(
|
|
|
|
context,
|
|
|
|
'Error adding fingerprint: $errorMessage',
|
|
|
|
duration: const Duration(seconds: 4),
|
|
|
|
);
|
2022-03-23 11:49:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
String _getMessage() {
|
|
|
|
if (_samples == 0) {
|
|
|
|
return 'Press your finger against the YubiKey to begin.';
|
|
|
|
}
|
|
|
|
if (_fingerprint == null) {
|
|
|
|
return 'Keep touching your YubiKey repeatedly...';
|
|
|
|
} else {
|
|
|
|
return 'Fingerprint captured successfully!';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-05 12:46:22 +03:00
|
|
|
void _submit() async {
|
2022-06-13 17:45:26 +03:00
|
|
|
try {
|
|
|
|
await ref
|
|
|
|
.read(fingerprintProvider(widget.devicePath).notifier)
|
|
|
|
.renameFingerprint(_fingerprint!, _label);
|
|
|
|
if (!mounted) return;
|
|
|
|
Navigator.of(context).pop(true);
|
|
|
|
showMessage(context, 'Fingerprint added');
|
|
|
|
} catch (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,
|
|
|
|
'Error setting name: $errorMessage',
|
|
|
|
duration: const Duration(seconds: 4),
|
|
|
|
);
|
|
|
|
}
|
2022-04-05 12:46:22 +03:00
|
|
|
}
|
|
|
|
|
2022-03-23 11:49:20 +03:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final progress = _samples == 0 ? 0.0 : _samples / (_samples + _remaining);
|
|
|
|
|
|
|
|
return ResponsiveDialog(
|
|
|
|
title: const Text('Add fingerprint'),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
const Text('Step 1/2: Capture fingerprint'),
|
2022-05-20 15:10:17 +03:00
|
|
|
Column(
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(36.0),
|
|
|
|
child: AnimatedBuilder(
|
2022-03-23 11:49:20 +03:00
|
|
|
animation: _color,
|
|
|
|
builder: (context, _) {
|
|
|
|
return Icon(
|
|
|
|
_fingerprint == null ? Icons.fingerprint : Icons.check,
|
2022-05-20 15:10:17 +03:00
|
|
|
size: 128.0,
|
2022-03-23 11:49:20 +03:00
|
|
|
color: _color.value,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2022-05-20 15:10:17 +03:00
|
|
|
),
|
|
|
|
LinearProgressIndicator(value: progress),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: Text(_getMessage()),
|
|
|
|
),
|
|
|
|
],
|
2022-03-23 11:49:20 +03:00
|
|
|
),
|
|
|
|
const Text('Step 2/2: Name fingerprint'),
|
|
|
|
TextFormField(
|
|
|
|
focusNode: _nameFocus,
|
|
|
|
maxLength: 15,
|
2022-07-05 15:53:21 +03:00
|
|
|
inputFormatters: [limitBytesLength(15)],
|
2022-07-06 16:22:15 +03:00
|
|
|
buildCounter: buildByteCounterFor(_label),
|
2022-03-23 11:49:20 +03:00
|
|
|
autofocus: true,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
enabled: _fingerprint != null,
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
labelText: 'Name',
|
2022-06-09 12:23:34 +03:00
|
|
|
prefixIcon: const Icon(Icons.fingerprint_outlined),
|
2022-03-23 11:49:20 +03:00
|
|
|
),
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_label = value.trim();
|
|
|
|
});
|
|
|
|
},
|
2022-04-05 12:46:22 +03:00
|
|
|
onFieldSubmitted: (_) {
|
|
|
|
_submit();
|
|
|
|
},
|
2022-03-23 11:49:20 +03:00
|
|
|
),
|
|
|
|
]
|
|
|
|
.map((e) => Padding(
|
|
|
|
child: e,
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
|
|
|
))
|
|
|
|
.toList(),
|
|
|
|
),
|
|
|
|
onCancel: () {
|
|
|
|
_subscription.cancel();
|
|
|
|
},
|
|
|
|
actions: [
|
|
|
|
TextButton(
|
2022-04-05 12:46:22 +03:00
|
|
|
onPressed: _fingerprint != null && _label.isNotEmpty ? _submit : null,
|
2022-03-23 11:49:20 +03:00
|
|
|
child: const Text('Save'),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|