2021-12-03 12:27:29 +03:00
|
|
|
import 'dart:async';
|
|
|
|
|
2021-11-22 11:49:52 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2021-12-03 17:15:00 +03:00
|
|
|
import 'package:flutter/services.dart';
|
2021-11-22 11:49:52 +03:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
|
|
|
|
import '../../widgets/circle_timer.dart';
|
|
|
|
import '../../app/models.dart';
|
|
|
|
import '../models.dart';
|
|
|
|
import '../state.dart';
|
2022-01-21 17:45:04 +03:00
|
|
|
import 'delete_account_dialog.dart';
|
2022-01-24 12:59:09 +03:00
|
|
|
import 'rename_account_dialog.dart';
|
2021-11-22 11:49:52 +03:00
|
|
|
|
2021-12-06 12:26:38 +03:00
|
|
|
final _expireProvider =
|
|
|
|
StateNotifierProvider.autoDispose.family<_ExpireNotifier, bool, int>(
|
|
|
|
(ref, expiry) =>
|
|
|
|
_ExpireNotifier(DateTime.now().millisecondsSinceEpoch, expiry * 1000),
|
|
|
|
);
|
2021-12-03 12:27:29 +03:00
|
|
|
|
2021-12-06 12:26:38 +03:00
|
|
|
class _ExpireNotifier extends StateNotifier<bool> {
|
|
|
|
Timer? _timer;
|
|
|
|
_ExpireNotifier(int now, int expiry) : super(expiry <= now) {
|
|
|
|
if (expiry > now) {
|
|
|
|
_timer = Timer(Duration(milliseconds: expiry - now), () {
|
|
|
|
if (mounted) {
|
|
|
|
state = true;
|
|
|
|
}
|
2021-12-03 12:27:29 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2021-12-06 12:26:38 +03:00
|
|
|
dispose() {
|
|
|
|
_timer?.cancel();
|
2021-12-03 12:27:29 +03:00
|
|
|
super.dispose();
|
|
|
|
}
|
2021-12-06 12:26:38 +03:00
|
|
|
}
|
2021-11-22 11:49:52 +03:00
|
|
|
|
2021-12-06 12:26:38 +03:00
|
|
|
class AccountView extends ConsumerWidget {
|
2022-01-18 17:46:42 +03:00
|
|
|
final YubiKeyData deviceData;
|
2021-12-06 12:26:38 +03:00
|
|
|
final OathCredential credential;
|
|
|
|
final OathCode? code;
|
2022-02-10 14:11:44 +03:00
|
|
|
final FocusNode? focusNode;
|
|
|
|
const AccountView(this.deviceData, this.credential, this.code,
|
|
|
|
{Key? key, this.focusNode})
|
2021-12-06 12:26:38 +03:00
|
|
|
: super(key: key);
|
2021-11-22 11:49:52 +03:00
|
|
|
|
2021-12-06 12:26:38 +03:00
|
|
|
String formatCode() {
|
|
|
|
var value = code?.value;
|
2021-11-22 11:49:52 +03:00
|
|
|
if (value == null) {
|
|
|
|
return '••• •••';
|
|
|
|
} else if (value.length < 6) {
|
|
|
|
return value;
|
|
|
|
} else {
|
|
|
|
var i = value.length ~/ 2;
|
|
|
|
return value.substring(0, i) + ' ' + value.substring(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 13:44:08 +03:00
|
|
|
List<PopupMenuEntry> _buildPopupMenu(
|
|
|
|
BuildContext context, WidgetRef ref, bool trigger) =>
|
|
|
|
[
|
2021-12-03 17:15:00 +03:00
|
|
|
PopupMenuItem(
|
|
|
|
child: const ListTile(
|
|
|
|
leading: Icon(Icons.copy),
|
|
|
|
title: Text('Copy to clipboard'),
|
|
|
|
),
|
|
|
|
onTap: () {
|
2022-02-11 13:44:08 +03:00
|
|
|
_copyToClipboard(context, ref, trigger);
|
2021-12-03 17:15:00 +03:00
|
|
|
},
|
|
|
|
),
|
|
|
|
PopupMenuItem(
|
|
|
|
child: const ListTile(
|
|
|
|
leading: Icon(Icons.star),
|
|
|
|
title: Text('Toggle favorite'),
|
|
|
|
),
|
|
|
|
onTap: () {
|
2021-12-06 12:26:38 +03:00
|
|
|
ref.read(favoritesProvider.notifier).toggleFavorite(credential.id);
|
2021-12-03 17:15:00 +03:00
|
|
|
},
|
|
|
|
),
|
2022-01-18 17:46:42 +03:00
|
|
|
if (deviceData.info.version.major >= 5 &&
|
|
|
|
deviceData.info.version.minor >= 3)
|
2021-12-03 17:15:00 +03:00
|
|
|
PopupMenuItem(
|
|
|
|
child: const ListTile(
|
|
|
|
leading: Icon(Icons.edit),
|
|
|
|
title: Text('Rename account'),
|
|
|
|
),
|
|
|
|
onTap: () {
|
2022-01-24 12:59:09 +03:00
|
|
|
// This ensures the onTap handler finishes before the dialog is shown, otherwise the dialog is immediately closed instead of the popup.
|
|
|
|
Future.delayed(Duration.zero, () {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) =>
|
|
|
|
RenameAccountDialog(deviceData.node, credential),
|
|
|
|
);
|
|
|
|
});
|
2021-12-03 17:15:00 +03:00
|
|
|
},
|
|
|
|
),
|
|
|
|
const PopupMenuDivider(),
|
2022-01-21 17:45:04 +03:00
|
|
|
PopupMenuItem(
|
|
|
|
child: const ListTile(
|
2021-12-03 17:15:00 +03:00
|
|
|
leading: Icon(Icons.delete_forever),
|
|
|
|
title: Text('Delete account'),
|
|
|
|
),
|
2022-01-21 17:45:04 +03:00
|
|
|
onTap: () {
|
|
|
|
// This ensures the onTap handler finishes before the dialog is shown, otherwise the dialog is immediately closed instead of the popup.
|
|
|
|
Future.delayed(Duration.zero, () {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) =>
|
|
|
|
DeleteAccountDialog(deviceData.node, credential),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
2021-12-03 17:15:00 +03:00
|
|
|
),
|
|
|
|
];
|
|
|
|
|
2022-02-11 13:44:08 +03:00
|
|
|
_copyToClipboard(BuildContext context, WidgetRef ref, bool trigger) async {
|
|
|
|
String value;
|
|
|
|
if (trigger) {
|
|
|
|
final updated = await _calculate(context, ref);
|
|
|
|
value = updated.value;
|
|
|
|
} else {
|
|
|
|
value = code!.value;
|
|
|
|
}
|
|
|
|
await Clipboard.setData(ClipboardData(text: value));
|
2021-12-07 14:53:05 +03:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
const SnackBar(
|
|
|
|
content: Text('Code copied'),
|
|
|
|
duration: Duration(seconds: 2),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-11 13:44:08 +03:00
|
|
|
Future<OathCode> _calculate(BuildContext context, WidgetRef ref) async {
|
2021-12-07 14:53:05 +03:00
|
|
|
VoidCallback? close;
|
|
|
|
if (credential.touchRequired) {
|
|
|
|
final sbc = ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
const SnackBar(
|
|
|
|
content: Text('Touch your YubiKey'),
|
|
|
|
duration: Duration(seconds: 30),
|
|
|
|
),
|
2022-01-21 15:50:17 +03:00
|
|
|
);
|
|
|
|
unawaited(sbc.closed.then((_) {
|
|
|
|
close = null;
|
|
|
|
}));
|
2021-12-07 14:53:05 +03:00
|
|
|
close = sbc.close;
|
|
|
|
}
|
|
|
|
try {
|
2022-02-11 13:44:08 +03:00
|
|
|
return await ref
|
2022-01-18 17:46:42 +03:00
|
|
|
.read(credentialListProvider(deviceData.node.path).notifier)
|
2021-12-07 14:53:05 +03:00
|
|
|
.calculate(credential);
|
|
|
|
} finally {
|
|
|
|
close?.call();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 12:56:52 +03:00
|
|
|
Color _iconColor(String label, int shade) {
|
|
|
|
final colors = [
|
|
|
|
Colors.red[shade],
|
|
|
|
Colors.pink[shade],
|
|
|
|
Colors.purple[shade],
|
|
|
|
Colors.deepPurple[shade],
|
|
|
|
Colors.indigo[shade],
|
|
|
|
Colors.blue[shade],
|
|
|
|
Colors.lightBlue[shade],
|
|
|
|
Colors.cyan[shade],
|
|
|
|
Colors.teal[shade],
|
|
|
|
Colors.green[shade],
|
|
|
|
Colors.lightGreen[shade],
|
|
|
|
Colors.lime[shade],
|
|
|
|
Colors.yellow[shade],
|
|
|
|
Colors.amber[shade],
|
|
|
|
Colors.orange[shade],
|
|
|
|
Colors.deepOrange[shade],
|
|
|
|
Colors.brown[shade],
|
|
|
|
Colors.grey[shade],
|
|
|
|
Colors.blueGrey[shade],
|
|
|
|
];
|
|
|
|
return colors[label.hashCode % colors.length]!;
|
|
|
|
}
|
|
|
|
|
2021-11-22 11:49:52 +03:00
|
|
|
@override
|
2021-12-06 12:26:38 +03:00
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
|
|
final code = this.code;
|
|
|
|
final label = credential.issuer != null
|
|
|
|
? '${credential.issuer} (${credential.name})'
|
|
|
|
: credential.name;
|
2022-02-11 13:44:08 +03:00
|
|
|
final expired = ref.watch(_expireProvider(code?.validTo ?? 0));
|
2021-12-07 14:53:05 +03:00
|
|
|
final trigger = code == null ||
|
|
|
|
expired &&
|
|
|
|
(credential.touchRequired || credential.oathType == OathType.hotp);
|
2021-12-06 12:26:38 +03:00
|
|
|
|
2022-02-22 12:56:52 +03:00
|
|
|
final darkMode = Theme.of(context).brightness == Brightness.dark;
|
|
|
|
|
2021-12-06 12:26:38 +03:00
|
|
|
return ListTile(
|
2022-02-10 14:11:44 +03:00
|
|
|
focusNode: focusNode,
|
2021-12-07 14:53:05 +03:00
|
|
|
onTap: () {
|
2022-02-11 13:44:08 +03:00
|
|
|
final focus = focusNode;
|
|
|
|
if (focus != null && focus.hasFocus == false) {
|
|
|
|
focus.requestFocus();
|
2021-12-07 14:53:05 +03:00
|
|
|
} else {
|
2022-02-11 13:44:08 +03:00
|
|
|
_copyToClipboard(context, ref, trigger);
|
2021-12-07 14:53:05 +03:00
|
|
|
}
|
|
|
|
},
|
2021-12-06 12:26:38 +03:00
|
|
|
leading: CircleAvatar(
|
2022-02-22 12:56:52 +03:00
|
|
|
foregroundColor: darkMode ? Colors.black : Colors.white,
|
|
|
|
backgroundColor: _iconColor(label, darkMode ? 300 : 400),
|
2021-12-06 12:26:38 +03:00
|
|
|
child: Text(
|
|
|
|
(credential.issuer ?? credential.name).characters.first.toUpperCase(),
|
|
|
|
style: const TextStyle(fontSize: 18),
|
2021-11-22 11:49:52 +03:00
|
|
|
),
|
2021-12-06 12:26:38 +03:00
|
|
|
),
|
|
|
|
title: Text(
|
|
|
|
formatCode(),
|
|
|
|
style: expired
|
|
|
|
? Theme.of(context)
|
|
|
|
.textTheme
|
|
|
|
.headline5
|
|
|
|
?.copyWith(color: Colors.grey)
|
|
|
|
: Theme.of(context).textTheme.headline5,
|
|
|
|
),
|
|
|
|
subtitle: Text(label, style: Theme.of(context).textTheme.caption),
|
|
|
|
trailing: Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
2021-12-07 14:53:05 +03:00
|
|
|
Column(
|
2021-12-06 12:26:38 +03:00
|
|
|
children: [
|
2021-12-07 14:53:05 +03:00
|
|
|
Align(
|
|
|
|
alignment: AlignmentDirectional.topCenter,
|
|
|
|
child: trigger
|
|
|
|
? const Icon(
|
|
|
|
Icons.touch_app,
|
|
|
|
size: 18,
|
|
|
|
)
|
|
|
|
: SizedBox.square(
|
|
|
|
dimension: 16,
|
|
|
|
child: CircleTimer(
|
|
|
|
code.validFrom * 1000,
|
|
|
|
code.validTo * 1000,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const Spacer(),
|
|
|
|
PopupMenuButton(
|
|
|
|
child: Icon(Icons.adaptive.more),
|
2022-02-11 13:44:08 +03:00
|
|
|
itemBuilder: (context) =>
|
|
|
|
_buildPopupMenu(context, ref, trigger),
|
2021-12-07 14:53:05 +03:00
|
|
|
),
|
2021-12-06 12:26:38 +03:00
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2021-11-22 11:49:52 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|