Add QrScanner via provider.

This commit is contained in:
Dain Nilsson 2022-02-10 15:24:28 +01:00
parent 1f15640bfa
commit d41a300d48
No known key found for this signature in database
GPG Key ID: F04367096FBA95E8
4 changed files with 52 additions and 0 deletions

View File

@ -126,3 +126,11 @@ final menuActionsProvider = Provider.autoDispose<List<MenuAction>>((ref) {
}
return [];
});
abstract class QrScanner {
Future<String> scanQr();
}
final qrScannerProvider = Provider<QrScanner?>(
(ref) => null,
);

View File

@ -7,6 +7,7 @@ import 'package:logging/logging.dart';
import 'package:window_manager/window_manager.dart';
import 'package:yubico_authenticator/desktop/devices.dart';
import 'package:yubico_authenticator/desktop/oath/state.dart';
import 'package:yubico_authenticator/desktop/qr_scanner.dart';
import 'package:yubico_authenticator/desktop/state.dart';
import 'package:yubico_authenticator/oath/state.dart';
@ -52,5 +53,6 @@ Future<List<Override>> initializeAndGetOverrides() async {
oathStateProvider.overrideWithProvider(desktopOathState),
credentialListProvider
.overrideWithProvider(desktopOathCredentialListProvider),
qrScannerProvider.overrideWithProvider(desktopQrScannerProvider),
];
}

20
lib/desktop/qr_scanner.dart Executable file
View File

@ -0,0 +1,20 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:yubico_authenticator/app/state.dart';
import 'package:yubico_authenticator/desktop/state.dart';
import 'rpc.dart';
class RpcQrScanner implements QrScanner {
final RpcSession _rpc;
RpcQrScanner(this._rpc);
@override
Future<String> scanQr() async {
final result = await _rpc.command('qr', []);
return result['result'];
}
}
final desktopQrScannerProvider = Provider<QrScanner?>(
(ref) => RpcQrScanner(ref.watch(rpcProvider)),
);

View File

@ -12,8 +12,30 @@ List<MenuAction> buildOathMenuActions(AutoDisposeProviderRef ref) {
final device = ref.watch(currentDeviceProvider);
if (device != null) {
final state = ref.watch(oathStateProvider(device.path));
final qrScanner = ref.watch(qrScannerProvider);
if (state != null) {
return [
if (!state.locked && qrScanner != null)
MenuAction(
text: 'Scan for QR code',
icon: const Icon(Icons.qr_code),
action: (context) async {
var messenger = ScaffoldMessenger.of(context);
// TODO: Go to add credential page.
String message;
try {
final otpauth = await qrScanner.scanQr();
message = 'Captured: $otpauth';
} catch (e) {
message = 'Unable to capture QR code';
}
messenger.showSnackBar(
SnackBar(
content: Text(message),
duration: const Duration(seconds: 2),
),
);
}),
if (!state.locked)
MenuAction(
text: 'Add credential',