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';
|
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-03-09 19:47:50 +03:00
|
|
|
import '../../app/views/app_failure_screen.dart';
|
|
|
|
import '../../app/views/app_loading_screen.dart';
|
2022-03-25 18:24:15 +03:00
|
|
|
import '../../app/views/app_page.dart';
|
2022-04-05 12:46:22 +03:00
|
|
|
import '../../app/views/message_page.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;
|
|
|
|
const OathScreen(this.devicePath, {Key? key}) : super(key: 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(
|
|
|
|
title: const Text('Authenticator'),
|
|
|
|
centered: true,
|
|
|
|
child: const AppLoadingScreen(),
|
|
|
|
),
|
|
|
|
error: (error, _) => AppPage(
|
|
|
|
title: const Text('Authenticator'),
|
|
|
|
centered: true,
|
|
|
|
child: AppFailureScreen('$error'),
|
|
|
|
),
|
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;
|
|
|
|
|
|
|
|
const _LockedView(this.devicePath, this.oathState, {Key? key})
|
|
|
|
: super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) => AppPage(
|
2022-04-03 12:06:22 +03:00
|
|
|
title: const Text('Authenticator'),
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
const ListTile(title: Text('Unlock')),
|
|
|
|
_UnlockForm(
|
|
|
|
devicePath,
|
2022-04-05 12:46:22 +03:00
|
|
|
oathState,
|
2022-04-03 12:06:22 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
class _UnlockedView extends ConsumerWidget {
|
|
|
|
final DevicePath devicePath;
|
|
|
|
final OathState oathState;
|
|
|
|
|
|
|
|
const _UnlockedView(this.devicePath, this.oathState, {Key? key})
|
|
|
|
: super(key: key);
|
|
|
|
|
|
|
|
@override
|
2022-04-05 12:46:22 +03:00
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
2022-04-05 14:45:11 +03:00
|
|
|
final isEmpty = ref.watch(credentialListProvider(devicePath)
|
|
|
|
.select((value) => value?.isEmpty == true));
|
|
|
|
if (isEmpty) {
|
2022-04-05 12:46:22 +03:00
|
|
|
return MessagePage(
|
|
|
|
title: const Text('Authenticator'),
|
|
|
|
header: 'No accounts',
|
|
|
|
message: 'Follow the instructions on a website to add an account',
|
|
|
|
floatingActionButton: _buildFab(context),
|
2022-03-31 12:41:28 +03:00
|
|
|
);
|
2022-04-05 12:46:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 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) {
|
|
|
|
return TextFormField(
|
|
|
|
initialValue: ref.read(searchProvider),
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
hintText: 'Search accounts',
|
|
|
|
border: InputBorder.none,
|
|
|
|
),
|
|
|
|
onChanged: (value) {
|
|
|
|
ref.read(searchProvider.notifier).setFilter(value);
|
|
|
|
},
|
|
|
|
textInputAction: TextInputAction.next,
|
|
|
|
onFieldSubmitted: (value) {
|
|
|
|
Focus.of(context).focusInDirection(TraversalDirection.down);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
child: AccountList(devicePath, oathState),
|
|
|
|
floatingActionButton: _buildFab(context),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
FloatingActionButton _buildFab(BuildContext context) {
|
|
|
|
final fab = FloatingActionButton.extended(
|
|
|
|
icon: const Icon(Icons.person_add_alt),
|
|
|
|
label: const Text('Setup'),
|
|
|
|
backgroundColor: Theme.of(context).colorScheme.secondary,
|
|
|
|
foregroundColor: Theme.of(context).colorScheme.onSecondary,
|
|
|
|
onPressed: () {
|
|
|
|
showBottomMenu(context, [
|
|
|
|
MenuAction(
|
|
|
|
text: 'Add account',
|
|
|
|
icon: const Icon(Icons.person_add_alt),
|
|
|
|
action: (context) {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => OathAddAccountPage(devicePath),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
MenuAction(
|
|
|
|
text: oathState.hasKey ? 'Manage password' : 'Set password',
|
|
|
|
icon: const Icon(Icons.password),
|
|
|
|
action: (context) {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) =>
|
|
|
|
ManagePasswordDialog(devicePath, oathState),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
MenuAction(
|
|
|
|
text: 'Delete all data',
|
|
|
|
icon: const Icon(Icons.delete_outline),
|
|
|
|
action: (context) {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => ResetDialog(devicePath),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
return fab;
|
|
|
|
}
|
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-04-05 12:46:22 +03:00
|
|
|
final OathState _oathState;
|
2022-02-22 17:22:41 +03:00
|
|
|
final KeystoreState keystore;
|
2022-04-05 12:46:22 +03:00
|
|
|
const _UnlockForm(this._devicePath, this._oathState,
|
|
|
|
{Key? key, required this.keystore})
|
2022-02-22 17:22:41 +03:00
|
|
|
: super(key: key);
|
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-04-03 12:06:22 +03:00
|
|
|
bool _wrong = false;
|
|
|
|
|
|
|
|
void _submit() async {
|
|
|
|
setState(() {
|
|
|
|
_wrong = false;
|
|
|
|
});
|
|
|
|
final result = await ref
|
|
|
|
.read(oathStateProvider(widget._devicePath).notifier)
|
|
|
|
.unlock(_passwordController.text, remember: _remember);
|
|
|
|
if (!result.first) {
|
|
|
|
setState(() {
|
|
|
|
_wrong = true;
|
|
|
|
_passwordController.clear();
|
|
|
|
});
|
|
|
|
showMessage(context, 'Wrong password');
|
|
|
|
} else if (_remember && !result.second) {
|
|
|
|
showMessage(context, 'Failed to remember password');
|
|
|
|
}
|
|
|
|
}
|
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-03-31 12:41:28 +03:00
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 16.0),
|
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: [
|
|
|
|
const Text(
|
2022-04-05 12:46:22 +03:00
|
|
|
'Enter the OATH password for your YubiKey',
|
2022-02-08 14:25:36 +03:00
|
|
|
),
|
2022-04-03 12:06:22 +03:00
|
|
|
const SizedBox(height: 16.0),
|
|
|
|
TextField(
|
|
|
|
controller: _passwordController,
|
|
|
|
autofocus: true,
|
|
|
|
obscureText: true,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
labelText: 'Password',
|
|
|
|
errorText: _wrong ? 'Wrong password' : null,
|
|
|
|
helperText: '', // Prevents resizing when errorText shown
|
2022-03-31 12:41:28 +03:00
|
|
|
),
|
2022-04-03 12:06:22 +03:00
|
|
|
onChanged: (_) => setState(() {}), // Update state on change
|
|
|
|
onSubmitted: (_) => _submit(),
|
2022-02-08 14:25:36 +03:00
|
|
|
),
|
2022-04-05 12:46:22 +03:00
|
|
|
Wrap(
|
|
|
|
spacing: 4.0,
|
|
|
|
runSpacing: 8.0,
|
|
|
|
children: [
|
|
|
|
OutlinedButton.icon(
|
|
|
|
icon: const Icon(Icons.password),
|
|
|
|
label: const Text('Manage password'),
|
|
|
|
onPressed: () {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => ManagePasswordDialog(
|
|
|
|
widget._devicePath, widget._oathState),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
OutlinedButton.icon(
|
2022-04-05 14:20:22 +03:00
|
|
|
icon: const Icon(Icons.delete_outlined),
|
2022-04-05 12:46:22 +03:00
|
|
|
label: const Text('Reset OATH'),
|
|
|
|
onPressed: () {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => ResetDialog(widget._devicePath),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2022-02-08 14:25:36 +03:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2022-04-05 12:46:22 +03:00
|
|
|
const SizedBox(height: 16.0),
|
|
|
|
Row(
|
|
|
|
mainAxisSize: MainAxisSize.max,
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: keystoreFailed
|
|
|
|
? const ListTile(
|
|
|
|
leading: Icon(Icons.warning_amber_rounded),
|
|
|
|
title: Text('OS Keystore unavailable'),
|
|
|
|
dense: true,
|
|
|
|
minLeadingWidth: 0,
|
|
|
|
)
|
|
|
|
: CheckboxListTile(
|
|
|
|
title: const Text('Remember password'),
|
|
|
|
dense: true,
|
|
|
|
controlAffinity: ListTileControlAffinity.leading,
|
|
|
|
value: _remember,
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_remember = value ?? false;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 16.0),
|
|
|
|
child: ElevatedButton(
|
|
|
|
child: const Text('Unlock'),
|
|
|
|
onPressed: _passwordController.text.isNotEmpty ? _submit : null,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
2022-02-08 14:25:36 +03:00
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|