2021-11-22 11:49:52 +03:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2022-02-22 17:22:41 +03:00
|
|
|
import 'package:yubico_authenticator/oath/models.dart';
|
2021-11-22 11:49:52 +03:00
|
|
|
|
|
|
|
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);
|
2021-11-22 11:49:52 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
2022-01-18 17:46:42 +03:00
|
|
|
final state = ref.watch(oathStateProvider(deviceData.node.path));
|
2021-11-22 11:49:52 +03:00
|
|
|
|
|
|
|
if (state == null) {
|
2021-12-07 16:22:28 +03:00
|
|
|
return Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: const [
|
|
|
|
Center(child: CircularProgressIndicator()),
|
|
|
|
],
|
|
|
|
);
|
2021-11-22 11:49:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state.locked) {
|
2022-02-08 14:25:36 +03:00
|
|
|
return ListView(
|
|
|
|
children: [
|
|
|
|
_UnlockForm(
|
2022-02-22 17:22:41 +03:00
|
|
|
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);
|
2022-02-22 17:22:41 +03:00
|
|
|
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),
|
|
|
|
),
|
|
|
|
);
|
2022-02-22 17:22:41 +03:00
|
|
|
} 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
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
2021-11-22 11:49:52 +03:00
|
|
|
);
|
|
|
|
} else {
|
2022-01-18 17:46:42 +03:00
|
|
|
final accounts = ref.watch(credentialListProvider(deviceData.node.path));
|
2021-11-22 11:49:52 +03:00
|
|
|
if (accounts == null) {
|
|
|
|
return Column(
|
2021-12-07 16:22:28 +03:00
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
2021-11-22 11:49:52 +03:00
|
|
|
children: const [
|
2021-12-07 16:22:28 +03:00
|
|
|
Center(child: CircularProgressIndicator()),
|
2021-11-22 11:49:52 +03:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
2021-12-02 13:44:17 +03:00
|
|
|
return AccountList(
|
2022-01-18 17:46:42 +03:00
|
|
|
deviceData,
|
2021-12-02 13:44:17 +03:00
|
|
|
ref.watch(filteredCredentialsProvider(accounts)),
|
2021-12-03 17:15:00 +03:00
|
|
|
ref.watch(favoritesProvider),
|
2021-11-22 11:49:52 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-08 14:25:36 +03:00
|
|
|
|
|
|
|
class _UnlockForm extends StatefulWidget {
|
2022-02-22 17:22:41 +03:00
|
|
|
final KeystoreState keystore;
|
2022-02-08 14:25:36 +03:00
|
|
|
final Function(String, bool) onSubmit;
|
2022-02-22 17:22:41 +03:00
|
|
|
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> {
|
2022-02-22 17:22:41 +03:00
|
|
|
// 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,
|
2022-02-22 17:22:41 +03:00
|
|
|
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);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|