yubioath-flutter/lib/oath/views/oath_screen.dart

151 lines
4.6 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:yubico_authenticator/oath/models.dart';
import '../../app/models.dart';
import '../state.dart';
import 'account_list.dart';
class OathScreen extends ConsumerWidget {
2022-01-18 17:46:42 +03:00
final YubiKeyData deviceData;
const OathScreen(this.deviceData, {Key? key}) : super(key: key);
@override
Widget build(BuildContext context, WidgetRef ref) {
2022-01-18 17:46:42 +03:00
final state = ref.watch(oathStateProvider(deviceData.node.path));
if (state == null) {
2021-12-07 16:22:28 +03:00
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Center(child: CircularProgressIndicator()),
],
);
}
if (state.locked) {
2022-02-08 14:25:36 +03:00
return ListView(
children: [
_UnlockForm(
keystore: state.keystore,
2022-02-08 14:25:36 +03:00
onSubmit: (password, remember) async {
final result = await ref
.read(oathStateProvider(deviceData.node.path).notifier)
.unlock(password, remember: remember);
if (!result.first) {
2022-02-08 14:25:36 +03:00
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Wrong password'),
duration: Duration(seconds: 1),
),
);
} else if (remember && !result.second) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Failed to remember password'),
duration: Duration(seconds: 1),
),
);
2022-02-08 14:25:36 +03:00
}
},
),
],
);
} else {
2022-01-18 17:46:42 +03:00
final accounts = ref.watch(credentialListProvider(deviceData.node.path));
if (accounts == null) {
return Column(
2021-12-07 16:22:28 +03:00
mainAxisAlignment: MainAxisAlignment.center,
children: const [
2021-12-07 16:22:28 +03:00
Center(child: CircularProgressIndicator()),
],
);
}
return AccountList(
2022-01-18 17:46:42 +03:00
deviceData,
ref.watch(filteredCredentialsProvider(accounts)),
2021-12-03 17:15:00 +03:00
ref.watch(favoritesProvider),
);
}
}
}
2022-02-08 14:25:36 +03:00
class _UnlockForm extends StatefulWidget {
final KeystoreState keystore;
2022-02-08 14:25:36 +03:00
final Function(String, bool) onSubmit;
const _UnlockForm({Key? key, required this.keystore, required this.onSubmit})
: super(key: key);
2022-02-08 14:25:36 +03:00
@override
State<StatefulWidget> createState() => _UnlockFormState();
}
class _UnlockFormState extends State<_UnlockForm> {
// TODO: Use a TextEditingController so we can clear it on wrong entry
2022-02-08 14:25:36 +03:00
String _password = '';
bool _remember = false;
@override
Widget build(BuildContext context) {
return Column(
//mainAxisAlignment: MainAxisAlignment.center,
//crossAxisAlignment: CrossAxisAlignment.end,
children: [
Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.symmetric(vertical: 24.0),
child: Text(
'Unlock YubiKey',
style: Theme.of(context).textTheme.headline5,
),
),
const Text(
'Enter the password for your YubiKey. If you don\'t know your password, you\'ll need to reset the YubiKey.',
),
TextField(
autofocus: true,
obscureText: true,
decoration: const InputDecoration(labelText: 'Password'),
onChanged: (value) {
setState(() {
_password = value;
});
},
onSubmitted: (value) {
widget.onSubmit(value, _remember);
},
),
],
),
),
CheckboxListTile(
title: const Text('Remember password'),
controlAffinity: ListTileControlAffinity.leading,
value: _remember,
onChanged: widget.keystore == KeystoreState.failed
? null
: (value) {
setState(() {
_remember = value ?? false;
});
},
2022-02-08 14:25:36 +03:00
),
Container(
padding: const EdgeInsets.all(16.0),
alignment: Alignment.centerRight,
child: ElevatedButton(
child: const Text('Unlock'),
onPressed: () {
widget.onSubmit(_password, _remember);
},
),
),
],
);
}
}