2022-04-26 17:05:59 +03:00
|
|
|
import 'dart:io';
|
|
|
|
|
2021-11-22 11:49:52 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2022-03-25 18:24:15 +03:00
|
|
|
import 'package:flutter/services.dart';
|
2022-09-06 15:30:18 +03:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2021-11-22 11:49:52 +03:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
|
2022-03-25 17:43:32 +03:00
|
|
|
import '../../app/message.dart';
|
2021-11-22 11:49:52 +03:00
|
|
|
import '../../app/models.dart';
|
2022-06-10 14:49:02 +03:00
|
|
|
import '../../app/shortcuts.dart';
|
2022-05-20 18:19:39 +03:00
|
|
|
import '../../app/views/app_failure_page.dart';
|
2022-03-09 19:47:50 +03:00
|
|
|
import '../../app/views/app_loading_screen.dart';
|
2022-03-25 18:24:15 +03:00
|
|
|
import '../../app/views/app_page.dart';
|
2022-05-18 15:11:17 +03:00
|
|
|
import '../../app/views/graphics.dart';
|
2022-04-05 12:46:22 +03:00
|
|
|
import '../../app/views/message_page.dart';
|
2022-07-07 21:20:04 +03:00
|
|
|
import '../../widgets/menu_list_tile.dart';
|
2022-03-25 18:24:15 +03:00
|
|
|
import '../models.dart';
|
2021-11-22 11:49:52 +03:00
|
|
|
import '../state.dart';
|
|
|
|
import 'account_list.dart';
|
2022-03-31 12:41:28 +03:00
|
|
|
import 'add_account_page.dart';
|
|
|
|
import 'manage_password_dialog.dart';
|
|
|
|
import 'reset_dialog.dart';
|
2021-11-22 11:49:52 +03:00
|
|
|
|
|
|
|
class OathScreen extends ConsumerWidget {
|
2022-04-03 12:06:22 +03:00
|
|
|
final DevicePath devicePath;
|
2022-05-12 10:56:55 +03:00
|
|
|
const OathScreen(this.devicePath, {super.key});
|
2021-11-22 11:49:52 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
2022-04-03 12:06:22 +03:00
|
|
|
return ref.watch(oathStateProvider(devicePath)).when(
|
|
|
|
loading: () => AppPage(
|
2022-09-06 15:30:18 +03:00
|
|
|
title: Text(AppLocalizations.of(context)!.oath_authenticator),
|
2022-04-03 12:06:22 +03:00
|
|
|
centered: true,
|
|
|
|
child: const AppLoadingScreen(),
|
|
|
|
),
|
2022-05-20 18:19:39 +03:00
|
|
|
error: (error, _) => AppFailurePage(
|
2022-09-06 15:30:18 +03:00
|
|
|
title: Text(AppLocalizations.of(context)!.oath_authenticator),
|
2022-05-20 18:19:39 +03:00
|
|
|
cause: error,
|
2022-04-03 12:06:22 +03:00
|
|
|
),
|
2022-03-31 12:41:28 +03:00
|
|
|
data: (oathState) => oathState.locked
|
2022-04-03 12:06:22 +03:00
|
|
|
? _LockedView(devicePath, oathState)
|
|
|
|
: _UnlockedView(devicePath, oathState),
|
2022-03-31 12:41:28 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _LockedView extends ConsumerWidget {
|
|
|
|
final DevicePath devicePath;
|
|
|
|
final OathState oathState;
|
|
|
|
|
2022-05-12 10:56:55 +03:00
|
|
|
const _LockedView(this.devicePath, this.oathState);
|
2022-03-31 12:41:28 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) => AppPage(
|
2022-09-06 15:30:18 +03:00
|
|
|
title: Text(AppLocalizations.of(context)!.oath_authenticator),
|
2022-07-07 21:20:04 +03:00
|
|
|
keyActions: [
|
|
|
|
buildMenuItem(
|
2022-09-06 15:30:18 +03:00
|
|
|
title: Text(AppLocalizations.of(context)!.oath_manage_password),
|
2022-07-07 21:20:04 +03:00
|
|
|
leading: const Icon(Icons.password),
|
|
|
|
action: () {
|
|
|
|
showBlurDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) =>
|
|
|
|
ManagePasswordDialog(devicePath, oathState),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
buildMenuItem(
|
2022-09-06 15:30:18 +03:00
|
|
|
title: Text(AppLocalizations.of(context)!.oath_reset_oath),
|
2022-07-07 21:20:04 +03:00
|
|
|
leading: const Icon(Icons.delete),
|
|
|
|
action: () {
|
|
|
|
showBlurDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => ResetDialog(devicePath),
|
|
|
|
);
|
2022-05-23 12:02:37 +03:00
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
2022-04-03 12:06:22 +03:00
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
_UnlockForm(
|
|
|
|
devicePath,
|
|
|
|
keystore: oathState.keystore,
|
2022-03-31 12:41:28 +03:00
|
|
|
),
|
2022-04-03 12:06:22 +03:00
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
2022-03-31 12:41:28 +03:00
|
|
|
}
|
|
|
|
|
2022-06-09 12:52:54 +03:00
|
|
|
class _UnlockedView extends ConsumerStatefulWidget {
|
2022-03-31 12:41:28 +03:00
|
|
|
final DevicePath devicePath;
|
|
|
|
final OathState oathState;
|
|
|
|
|
2022-05-12 10:56:55 +03:00
|
|
|
const _UnlockedView(this.devicePath, this.oathState);
|
2022-03-31 12:41:28 +03:00
|
|
|
|
|
|
|
@override
|
2022-06-09 12:52:54 +03:00
|
|
|
ConsumerState<ConsumerStatefulWidget> createState() => _UnlockedViewState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _UnlockedViewState extends ConsumerState<_UnlockedView> {
|
|
|
|
late FocusNode searchFocus;
|
|
|
|
late TextEditingController searchController;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
searchFocus = FocusNode();
|
|
|
|
searchController = TextEditingController(text: ref.read(searchProvider));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
searchFocus.dispose();
|
|
|
|
searchController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-07-08 10:12:31 +03:00
|
|
|
final credentials = ref.watch(credentialsProvider);
|
2022-07-07 21:20:04 +03:00
|
|
|
if (credentials?.isEmpty == true) {
|
2022-04-05 12:46:22 +03:00
|
|
|
return MessagePage(
|
2022-09-06 15:30:18 +03:00
|
|
|
title: Text(AppLocalizations.of(context)!.oath_authenticator),
|
2022-05-20 12:11:20 +03:00
|
|
|
graphic: noAccounts,
|
2022-09-06 15:30:18 +03:00
|
|
|
header: AppLocalizations.of(context)!.oath_no_accounts,
|
2022-07-07 21:20:04 +03:00
|
|
|
keyActions: _buildActions(
|
|
|
|
context,
|
2022-09-05 16:02:36 +03:00
|
|
|
credentials: null,
|
2022-07-07 21:20:04 +03:00
|
|
|
),
|
2022-03-31 12:41:28 +03:00
|
|
|
);
|
2022-04-05 12:46:22 +03:00
|
|
|
}
|
2022-06-10 14:49:02 +03:00
|
|
|
return Actions(
|
|
|
|
actions: {
|
|
|
|
SearchIntent: CallbackAction(onInvoke: (_) {
|
2022-06-09 12:52:54 +03:00
|
|
|
searchController.selection = TextSelection(
|
|
|
|
baseOffset: 0, extentOffset: searchController.text.length);
|
|
|
|
searchFocus.requestFocus();
|
2022-06-10 14:49:02 +03:00
|
|
|
return null;
|
|
|
|
}),
|
2022-06-09 12:52:54 +03:00
|
|
|
},
|
2022-07-07 13:40:54 +03:00
|
|
|
child: AppPage(
|
|
|
|
title: Focus(
|
|
|
|
canRequestFocus: false,
|
|
|
|
onKeyEvent: (node, event) {
|
|
|
|
if (event.logicalKey == LogicalKeyboardKey.arrowDown) {
|
|
|
|
node.focusInDirection(TraversalDirection.down);
|
|
|
|
return KeyEventResult.handled;
|
|
|
|
}
|
|
|
|
return KeyEventResult.ignored;
|
|
|
|
},
|
|
|
|
child: Builder(builder: (context) {
|
2022-09-06 16:02:24 +03:00
|
|
|
final textTheme = Theme.of(context).textTheme;
|
2022-07-07 13:40:54 +03:00
|
|
|
return TextFormField(
|
|
|
|
key: const Key('search_accounts'),
|
|
|
|
controller: searchController,
|
|
|
|
focusNode: searchFocus,
|
2022-09-06 16:02:24 +03:00
|
|
|
// Use the default style, but with a smaller font size:
|
|
|
|
style: textTheme.subtitle1
|
|
|
|
?.copyWith(fontSize: textTheme.titleSmall?.fontSize),
|
2022-09-06 15:30:18 +03:00
|
|
|
decoration: InputDecoration(
|
|
|
|
hintText: AppLocalizations.of(context)!.oath_search_accounts,
|
2022-07-07 13:40:54 +03:00
|
|
|
isDense: true,
|
2022-09-06 15:30:18 +03:00
|
|
|
prefixIcon: const Icon(Icons.search_outlined),
|
|
|
|
prefixIconConstraints: const BoxConstraints(
|
2022-07-07 13:40:54 +03:00
|
|
|
minHeight: 30,
|
|
|
|
minWidth: 30,
|
2022-06-09 12:52:54 +03:00
|
|
|
),
|
2022-07-07 13:40:54 +03:00
|
|
|
border: InputBorder.none,
|
|
|
|
),
|
|
|
|
onChanged: (value) {
|
|
|
|
ref.read(searchProvider.notifier).setFilter(value);
|
|
|
|
},
|
|
|
|
textInputAction: TextInputAction.next,
|
|
|
|
onFieldSubmitted: (value) {
|
|
|
|
Focus.of(context).focusInDirection(TraversalDirection.down);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}),
|
2022-06-09 12:52:54 +03:00
|
|
|
),
|
2022-07-07 21:20:04 +03:00
|
|
|
keyActions: _buildActions(
|
|
|
|
context,
|
2022-09-05 16:02:36 +03:00
|
|
|
credentials: credentials,
|
2022-07-07 21:20:04 +03:00
|
|
|
),
|
2022-07-07 13:40:54 +03:00
|
|
|
child: AccountList(widget.devicePath, widget.oathState),
|
2022-04-05 12:46:22 +03:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-05 16:02:36 +03:00
|
|
|
List<PopupMenuEntry> _buildActions(
|
|
|
|
BuildContext context, {
|
|
|
|
required List<OathCredential>? credentials,
|
|
|
|
}) {
|
|
|
|
final used = credentials?.length ?? 0;
|
|
|
|
final capacity = widget.oathState.version.isAtLeast(4) ? 32 : null;
|
2022-05-20 12:11:20 +03:00
|
|
|
return [
|
2022-09-09 13:36:53 +03:00
|
|
|
buildMenuItem(
|
|
|
|
title: Text(AppLocalizations.of(context)!.oath_add_account,
|
|
|
|
key: const Key('add oath account'),),
|
|
|
|
leading: const Icon(Icons.person_add_alt_1),
|
2022-07-07 21:20:04 +03:00
|
|
|
trailing: capacity != null ? '$used/$capacity' : null,
|
|
|
|
action: capacity == null || capacity > used
|
|
|
|
? () {
|
2022-07-04 13:56:31 +03:00
|
|
|
showBlurDialog(
|
2022-05-20 12:11:20 +03:00
|
|
|
context: context,
|
2022-07-07 21:20:04 +03:00
|
|
|
builder: (context) => OathAddAccountPage(
|
|
|
|
widget.devicePath,
|
|
|
|
widget.oathState,
|
2022-09-05 16:02:36 +03:00
|
|
|
credentials: credentials,
|
2022-07-07 21:20:04 +03:00
|
|
|
openQrScanner: Platform.isAndroid,
|
|
|
|
),
|
2022-05-20 12:11:20 +03:00
|
|
|
);
|
2022-07-07 21:20:04 +03:00
|
|
|
}
|
|
|
|
: null,
|
2022-05-20 12:11:20 +03:00
|
|
|
),
|
2022-07-07 21:20:04 +03:00
|
|
|
buildMenuItem(
|
2022-09-06 15:30:18 +03:00
|
|
|
title: Text(widget.oathState.hasKey
|
|
|
|
? AppLocalizations.of(context)!.oath_manage_password
|
2022-09-09 13:36:53 +03:00
|
|
|
: AppLocalizations.of(context)!.oath_set_password, key: const Key('set or manage oath password')),
|
2022-07-07 21:20:04 +03:00
|
|
|
leading: const Icon(Icons.password),
|
|
|
|
action: () {
|
|
|
|
showBlurDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) =>
|
|
|
|
ManagePasswordDialog(widget.devicePath, widget.oathState),
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
buildMenuItem(
|
2022-09-09 13:36:53 +03:00
|
|
|
title: Text(AppLocalizations.of(context)!.oath_reset_oath, key: const Key('reset oath app')),
|
2022-07-07 21:20:04 +03:00
|
|
|
leading: const Icon(Icons.delete),
|
|
|
|
action: () {
|
|
|
|
showBlurDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => ResetDialog(widget.devicePath),
|
|
|
|
);
|
|
|
|
}),
|
2022-05-20 12:11:20 +03:00
|
|
|
];
|
2022-04-05 12:46:22 +03:00
|
|
|
}
|
2021-11-22 11:49:52 +03:00
|
|
|
}
|
2022-02-08 14:25:36 +03:00
|
|
|
|
2022-04-03 12:06:22 +03:00
|
|
|
class _UnlockForm extends ConsumerStatefulWidget {
|
|
|
|
final DevicePath _devicePath;
|
2022-02-22 17:22:41 +03:00
|
|
|
final KeystoreState keystore;
|
2022-05-20 12:11:20 +03:00
|
|
|
const _UnlockForm(this._devicePath, {required this.keystore});
|
2022-02-08 14:25:36 +03:00
|
|
|
|
|
|
|
@override
|
2022-04-03 12:06:22 +03:00
|
|
|
ConsumerState<_UnlockForm> createState() => _UnlockFormState();
|
2022-02-08 14:25:36 +03:00
|
|
|
}
|
|
|
|
|
2022-04-03 12:06:22 +03:00
|
|
|
class _UnlockFormState extends ConsumerState<_UnlockForm> {
|
|
|
|
final _passwordController = TextEditingController();
|
2022-02-08 14:25:36 +03:00
|
|
|
bool _remember = false;
|
2022-06-08 17:23:19 +03:00
|
|
|
bool _passwordIsWrong = false;
|
|
|
|
bool _isObscure = true;
|
2022-04-03 12:06:22 +03:00
|
|
|
|
|
|
|
void _submit() async {
|
|
|
|
setState(() {
|
2022-06-08 17:23:19 +03:00
|
|
|
_passwordIsWrong = false;
|
2022-04-03 12:06:22 +03:00
|
|
|
});
|
|
|
|
final result = await ref
|
|
|
|
.read(oathStateProvider(widget._devicePath).notifier)
|
|
|
|
.unlock(_passwordController.text, remember: _remember);
|
2022-05-12 09:34:51 +03:00
|
|
|
if (!mounted) return;
|
2022-04-03 12:06:22 +03:00
|
|
|
if (!result.first) {
|
|
|
|
setState(() {
|
2022-06-08 17:23:19 +03:00
|
|
|
_passwordIsWrong = true;
|
2022-04-03 12:06:22 +03:00
|
|
|
_passwordController.clear();
|
|
|
|
});
|
|
|
|
} else if (_remember && !result.second) {
|
2022-09-06 15:30:18 +03:00
|
|
|
showMessage(
|
|
|
|
context, AppLocalizations.of(context)!.oath_failed_remember_pw);
|
2022-04-03 12:06:22 +03:00
|
|
|
}
|
|
|
|
}
|
2022-02-08 14:25:36 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-02-23 14:37:19 +03:00
|
|
|
final keystoreFailed = widget.keystore == KeystoreState.failed;
|
2022-02-08 14:25:36 +03:00
|
|
|
return Column(
|
|
|
|
children: [
|
|
|
|
Padding(
|
2022-06-08 17:23:19 +03:00
|
|
|
padding: const EdgeInsets.only(left: 18.0, right: 18, top: 32),
|
2022-02-08 14:25:36 +03:00
|
|
|
child: Column(
|
2022-04-05 12:46:22 +03:00
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
2022-02-08 14:25:36 +03:00
|
|
|
children: [
|
2022-09-06 15:30:18 +03:00
|
|
|
Text(
|
|
|
|
AppLocalizations.of(context)!.oath_enter_oath_pw,
|
2022-02-08 14:25:36 +03:00
|
|
|
),
|
2022-04-03 12:06:22 +03:00
|
|
|
const SizedBox(height: 16.0),
|
|
|
|
TextField(
|
2022-07-21 16:42:47 +03:00
|
|
|
key: const Key('oath password'),
|
2022-04-03 12:06:22 +03:00
|
|
|
controller: _passwordController,
|
|
|
|
autofocus: true,
|
2022-06-08 17:23:19 +03:00
|
|
|
obscureText: _isObscure,
|
2022-04-03 12:06:22 +03:00
|
|
|
decoration: InputDecoration(
|
|
|
|
border: const OutlineInputBorder(),
|
2022-09-06 15:30:18 +03:00
|
|
|
labelText: AppLocalizations.of(context)!.oath_password,
|
|
|
|
errorText: _passwordIsWrong
|
|
|
|
? AppLocalizations.of(context)!.oath_wrong_password
|
|
|
|
: null,
|
2022-04-03 12:06:22 +03:00
|
|
|
helperText: '', // Prevents resizing when errorText shown
|
2022-06-09 12:23:34 +03:00
|
|
|
prefixIcon: const Icon(Icons.password_outlined),
|
2022-06-08 17:23:19 +03:00
|
|
|
suffixIcon: IconButton(
|
|
|
|
icon: Icon(
|
|
|
|
_isObscure ? Icons.visibility : Icons.visibility_off,
|
2022-09-01 15:00:33 +03:00
|
|
|
color: IconTheme.of(context).color,
|
2022-06-08 17:23:19 +03:00
|
|
|
),
|
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
_isObscure = !_isObscure;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
2022-03-31 12:41:28 +03:00
|
|
|
),
|
2022-06-08 17:23:19 +03:00
|
|
|
onChanged: (_) => setState(() {
|
|
|
|
_passwordIsWrong = false;
|
|
|
|
}), // Update state on change
|
2022-04-03 12:06:22 +03:00
|
|
|
onSubmitted: (_) => _submit(),
|
2022-02-08 14:25:36 +03:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2022-06-08 17:23:19 +03:00
|
|
|
const SizedBox(height: 8.0),
|
|
|
|
keystoreFailed
|
2022-09-06 15:30:18 +03:00
|
|
|
? ListTile(
|
|
|
|
leading: const Icon(Icons.warning_amber_rounded),
|
|
|
|
title: Text(
|
|
|
|
AppLocalizations.of(context)!.oath_keystore_unavailable),
|
2022-06-08 17:23:19 +03:00
|
|
|
dense: true,
|
|
|
|
minLeadingWidth: 0,
|
|
|
|
)
|
|
|
|
: CheckboxListTile(
|
2022-09-06 15:30:18 +03:00
|
|
|
title:
|
|
|
|
Text(AppLocalizations.of(context)!.oath_remember_password),
|
2022-06-08 17:23:19 +03:00
|
|
|
dense: true,
|
|
|
|
controlAffinity: ListTileControlAffinity.leading,
|
|
|
|
value: _remember,
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_remember = value ?? false;
|
|
|
|
});
|
|
|
|
},
|
2022-04-05 12:46:22 +03:00
|
|
|
),
|
2022-06-08 17:23:19 +03:00
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(top: 12.0, right: 18.0, bottom: 4.0),
|
|
|
|
child: Align(
|
|
|
|
alignment: Alignment.centerRight,
|
2022-09-01 12:35:39 +03:00
|
|
|
child: ElevatedButton.icon(
|
2022-07-21 16:42:47 +03:00
|
|
|
key: const Key('oath unlock'),
|
2022-09-06 15:30:18 +03:00
|
|
|
label: Text(AppLocalizations.of(context)!.oath_unlock),
|
2022-09-01 12:35:39 +03:00
|
|
|
icon: const Icon(Icons.lock_open),
|
2022-06-08 17:23:19 +03:00
|
|
|
onPressed: _passwordController.text.isNotEmpty ? _submit : null,
|
|
|
|
),
|
|
|
|
),
|
2022-02-08 14:25:36 +03:00
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|