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

154 lines
5.2 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';
2022-03-09 19:47:50 +03:00
import '../../app/views/app_failure_screen.dart';
import '../../app/views/app_loading_screen.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-03-09 19:47:50 +03:00
return ref.watch(oathStateProvider(deviceData.node.path)).when(
none: () => const AppLoadingScreen(),
failure: (reason) => AppFailureScreen(reason),
success: (oathState) {
if (oathState.locked) {
return ListView(
children: [
_UnlockForm(
keystore: oathState.keystore,
onSubmit: (password, remember) async {
final result = await ref
.read(
oathStateProvider(deviceData.node.path).notifier)
.unlock(password, remember: remember);
if (!result.first) {
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-03-09 19:47:50 +03:00
],
);
} else {
final accounts =
ref.watch(credentialListProvider(deviceData.node.path));
if (accounts == null) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Center(child: CircularProgressIndicator()),
],
);
2022-02-08 14:25:36 +03:00
}
2022-03-09 19:47:50 +03:00
return AccountList(
deviceData,
ref.watch(filteredCredentialsProvider(accounts)),
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) {
2022-02-23 14:37:19 +03:00
final keystoreFailed = widget.keystore == KeystoreState.failed;
2022-02-08 14:25:36 +03:00
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'),
2022-02-23 14:37:19 +03:00
subtitle: Text(keystoreFailed
? 'The OS keychain is not available.'
: 'Uses the OS keychain to protect access to this YubiKey.'),
2022-02-08 14:25:36 +03:00
controlAffinity: ListTileControlAffinity.leading,
value: _remember,
2022-02-23 14:37:19 +03:00
onChanged: keystoreFailed
? 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);
},
),
),
],
);
}
}