mirror of
https://github.com/Yubico/yubioath-flutter.git
synced 2024-12-23 02:01:36 +03:00
Add QrScanner via provider.
This commit is contained in:
parent
1f15640bfa
commit
d41a300d48
@ -126,3 +126,11 @@ final menuActionsProvider = Provider.autoDispose<List<MenuAction>>((ref) {
|
||||
}
|
||||
return [];
|
||||
});
|
||||
|
||||
abstract class QrScanner {
|
||||
Future<String> scanQr();
|
||||
}
|
||||
|
||||
final qrScannerProvider = Provider<QrScanner?>(
|
||||
(ref) => null,
|
||||
);
|
||||
|
@ -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
20
lib/desktop/qr_scanner.dart
Executable 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)),
|
||||
);
|
@ -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',
|
||||
|
Loading…
Reference in New Issue
Block a user