This commit is contained in:
Dain Nilsson 2022-03-25 08:57:11 +01:00
commit 687df015d4
No known key found for this signature in database
GPG Key ID: F04367096FBA95E8
4 changed files with 90 additions and 95 deletions

View File

@ -3,8 +3,12 @@ import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../app/app.dart';
import '../app/models.dart'; import '../app/models.dart';
import '../app/views/main_page.dart';
import '../core/state.dart';
import '../management/state.dart'; import '../management/state.dart';
import 'management/state.dart'; import 'management/state.dart';
import 'oath/state.dart'; import 'oath/state.dart';
@ -15,7 +19,7 @@ import '../oath/state.dart';
final _log = Logger('android.init'); final _log = Logger('android.init');
initializeLogging() { Future<Widget> initialize() async {
Logger.root.onRecord.listen((record) { Logger.root.onRecord.listen((record) {
if (record.level >= Logger.root.level) { if (record.level >= Logger.root.level) {
debugPrint('[${record.loggerName}] ${record.level}: ${record.message}'); debugPrint('[${record.loggerName}] ${record.level}: ${record.message}');
@ -25,24 +29,27 @@ initializeLogging() {
} }
}); });
_log.info('Logging initialized, outputting to stderr'); _log.info('Logging initialized, outputting to stderr');
}
Future<List<Override>> initializeAndGetOverrides() async {
/// initializes global handler for dialogs /// initializes global handler for dialogs
TapRequestDialog.initialize(); TapRequestDialog.initialize();
return [ return ProviderScope(
supportedAppsProvider.overrideWithValue([ overrides: [
Application.oath, supportedAppsProvider.overrideWithValue([
Application.management, Application.oath,
]), Application.management,
attachedDevicesProvider ]),
.overrideWithProvider(androidAttachedDevicesProvider), prefProvider.overrideWithValue(await SharedPreferences.getInstance()),
currentDeviceDataProvider.overrideWithProvider(androidDeviceDataProvider), attachedDevicesProvider
oathStateProvider.overrideWithProvider(androidOathStateProvider), .overrideWithProvider(androidAttachedDevicesProvider),
credentialListProvider.overrideWithProvider(androidCredentialListProvider), currentDeviceDataProvider.overrideWithProvider(androidDeviceDataProvider),
currentAppProvider.overrideWithProvider(androidSubPageProvider), oathStateProvider.overrideWithProvider(androidOathStateProvider),
managementStateProvider.overrideWithProvider(androidManagementState), credentialListProvider
currentDeviceProvider.overrideWithProvider(androidCurrentDeviceProvider) .overrideWithProvider(androidCredentialListProvider),
]; currentAppProvider.overrideWithProvider(androidSubPageProvider),
managementStateProvider.overrideWithProvider(androidManagementState),
currentDeviceProvider.overrideWithProvider(androidCurrentDeviceProvider)
],
child: const YubicoAuthenticatorApp(page: MainPage()),
);
} }

View File

@ -1,12 +1,15 @@
import 'dart:async'; import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:window_manager/window_manager.dart'; import 'package:window_manager/window_manager.dart';
import '../app/app.dart';
import '../app/views/main_page.dart';
import '../core/state.dart';
import '../fido/state.dart'; import '../fido/state.dart';
import '../oath/state.dart'; import '../oath/state.dart';
import '../app/models.dart'; import '../app/models.dart';
@ -24,16 +27,6 @@ final _log = Logger('desktop.init');
const String _keyWidth = 'DESKTOP_WINDOW_WIDTH'; const String _keyWidth = 'DESKTOP_WINDOW_WIDTH';
const String _keyHeight = 'DESKTOP_WINDOW_HEIGHT'; const String _keyHeight = 'DESKTOP_WINDOW_HEIGHT';
initializeLogging() {
Logger.root.onRecord.listen((record) {
stderr.writeln('[${record.loggerName}] ${record.level}: ${record.message}');
if (record.error != null) {
stderr.writeln(record.error);
}
});
_log.info('Logging initialized, outputting to stderr');
}
class _WindowResizeListener extends WindowListener { class _WindowResizeListener extends WindowListener {
final SharedPreferences _prefs; final SharedPreferences _prefs;
_WindowResizeListener(this._prefs); _WindowResizeListener(this._prefs);
@ -46,9 +39,17 @@ class _WindowResizeListener extends WindowListener {
} }
} }
Future<List<Override>> initializeAndGetOverrides( Future<Widget> initialize() async {
SharedPreferences prefs) async { Logger.root.onRecord.listen((record) {
stderr.writeln('[${record.loggerName}] ${record.level}: ${record.message}');
if (record.error != null) {
stderr.writeln(record.error);
}
});
_log.info('Logging initialized, outputting to stderr');
await windowManager.ensureInitialized(); await windowManager.ensureInitialized();
final prefs = await SharedPreferences.getInstance();
unawaited(windowManager.waitUntilReadyToShow().then((_) async { unawaited(windowManager.waitUntilReadyToShow().then((_) async {
await windowManager.setMinimumSize(const Size(270, 0)); await windowManager.setMinimumSize(const Size(270, 0));
@ -78,31 +79,43 @@ Future<List<Override>> initializeAndGetOverrides(
} }
_log.info('Starting subprocess: $exe'); _log.info('Starting subprocess: $exe');
var rpc = await RpcSession.launch(exe!); final rpc = await RpcSession.launch(exe!);
_log.info('ykman-rpc process started', exe); _log.info('ykman-rpc process started', exe);
rpc.setLogLevel(Logger.root.level); rpc.setLogLevel(Logger.root.level);
return [ return ProviderScope(
supportedAppsProvider.overrideWithValue([ overrides: [
Application.oath, supportedAppsProvider.overrideWithValue([
Application.fido, Application.oath,
Application.otp, Application.fido,
Application.piv, Application.otp,
Application.management, Application.piv,
]), Application.management,
rpcProvider.overrideWithValue(rpc), ]),
windowStateProvider.overrideWithProvider(desktopWindowStateProvider), prefProvider.overrideWithValue(prefs),
attachedDevicesProvider.overrideWithProvider(desktopDevicesProvider), rpcProvider.overrideWithValue(rpc),
currentDeviceDataProvider.overrideWithProvider(desktopDeviceDataProvider), windowStateProvider.overrideWithProvider(desktopWindowStateProvider),
oathStateProvider.overrideWithProvider(desktopOathState), attachedDevicesProvider.overrideWithProvider(desktopDevicesProvider),
credentialListProvider currentDeviceDataProvider.overrideWithProvider(desktopDeviceDataProvider),
.overrideWithProvider(desktopOathCredentialListProvider), oathStateProvider.overrideWithProvider(desktopOathState),
qrScannerProvider.overrideWithProvider(desktopQrScannerProvider), credentialListProvider
managementStateProvider.overrideWithProvider(desktopManagementState), .overrideWithProvider(desktopOathCredentialListProvider),
fidoStateProvider.overrideWithProvider(desktopFidoState), qrScannerProvider.overrideWithProvider(desktopQrScannerProvider),
fidoPinProvider.overrideWithProvider(desktopFidoPinProvider), managementStateProvider.overrideWithProvider(desktopManagementState),
fingerprintProvider.overrideWithProvider(desktopFingerprintProvider), fidoStateProvider.overrideWithProvider(desktopFidoState),
credentialProvider.overrideWithProvider(desktopCredentialProvider), fidoPinProvider.overrideWithProvider(desktopFidoPinProvider),
currentDeviceProvider.overrideWithProvider(desktopCurrentDeviceProvider) fingerprintProvider.overrideWithProvider(desktopFingerprintProvider),
]; credentialProvider.overrideWithProvider(desktopCredentialProvider),
currentDeviceProvider.overrideWithProvider(desktopCurrentDeviceProvider)
],
child: YubicoAuthenticatorApp(page: Consumer(
builder: (context, ref, child) {
// Keep RPC log level synced with main app.
ref.listen<Level>(logLevelProvider, (_, level) {
rpc.setLogLevel(level);
});
return const MainPage();
},
)),
);
} }

View File

@ -20,13 +20,7 @@ final rpcProvider = Provider<RpcSession>((ref) {
}); });
final rpcStateProvider = StateNotifierProvider<_RpcStateNotifier, RpcState>( final rpcStateProvider = StateNotifierProvider<_RpcStateNotifier, RpcState>(
(ref) { (ref) => _RpcStateNotifier(ref.watch(rpcProvider)),
final rpc = ref.watch(rpcProvider);
ref.listen<Level>(logLevelProvider, (_, level) {
rpc.setLogLevel(level);
}, fireImmediately: true);
return _RpcStateNotifier(rpc);
},
); );
class _RpcStateNotifier extends StateNotifier<RpcState> { class _RpcStateNotifier extends StateNotifier<RpcState> {

View File

@ -1,7 +1,5 @@
import 'dart:developer' as developer; import 'dart:developer' as developer;
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
@ -9,7 +7,6 @@ import 'package:shared_preferences/shared_preferences.dart';
import 'android/init.dart' as android; import 'android/init.dart' as android;
import 'app/app.dart'; import 'app/app.dart';
import 'app/views/main_page.dart';
import 'core/state.dart'; import 'core/state.dart';
import 'desktop/init.dart' as desktop; import 'desktop/init.dart' as desktop;
import 'error_page.dart'; import 'error_page.dart';
@ -17,46 +14,30 @@ import 'error_page.dart';
final _log = Logger('main'); final _log = Logger('main');
void main() async { void main() async {
// Set YUBIOATH_DEV_LOG=true to use the developer log.
if (kDebugMode && Platform.environment['YUBIOATH_DEV_LOG'] == 'true') {
_initializeDebugLogging();
Logger.root.level = Level.INFO;
_log.info('Logging to debug console');
} else {
if (isDesktop) {
desktop.initializeLogging();
}
if (isAndroid) {
android.initializeLogging();
}
}
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
var prefs = await SharedPreferences.getInstance();
List<Override> overrides = [
prefProvider.overrideWithValue(prefs),
];
Widget page;
try { try {
// Platform specific initialization final Widget initializedApp;
if (isDesktop) { if (isDesktop) {
_log.config('Initializing desktop platform.'); initializedApp = await desktop.initialize();
overrides.addAll(await desktop.initializeAndGetOverrides(prefs));
} else if (isAndroid) { } else if (isAndroid) {
_log.config('Initializing Android platform.'); initializedApp = await android.initialize();
overrides.addAll(await android.initializeAndGetOverrides()); } else {
_initializeDebugLogging();
throw UnimplementedError('Platform not supported');
} }
page = const MainPage(); runApp(initializedApp);
} catch (e) { } catch (e) {
_log.warning('Platform initialization failed: $e'); _log.warning('Platform initialization failed: $e');
page = ErrorPage(error: e.toString()); runApp(
ProviderScope(
child: YubicoAuthenticatorApp(page: ErrorPage(error: e.toString())),
overrides: [
prefProvider.overrideWithValue(await SharedPreferences.getInstance())
],
),
);
} }
runApp(ProviderScope(
overrides: overrides,
child: YubicoAuthenticatorApp(page: page),
));
} }
void _initializeDebugLogging() { void _initializeDebugLogging() {