From 14a56dd26f3b9cab7544b93e0e0e6b8d00652491 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B3n=20Bjarni=20Bjarnason?= Date: Wed, 15 Jan 2025 17:53:57 +0000 Subject: [PATCH] optionally pass auth client to dbus client and export it --- lib/dbus.dart | 1 + lib/src/dbus_auth_client.dart | 9 +++++++-- lib/src/dbus_client.dart | 12 ++++++++---- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/dbus.dart b/lib/dbus.dart index b4958131..6b12f97b 100644 --- a/lib/dbus.dart +++ b/lib/dbus.dart @@ -1,4 +1,5 @@ export 'src/dbus_address.dart'; +export 'src/dbus_auth_client.dart'; export 'src/dbus_client.dart'; export 'src/dbus_introspect.dart'; export 'src/dbus_method_call.dart'; diff --git a/lib/src/dbus_auth_client.dart b/lib/src/dbus_auth_client.dart index a6c3f06e..eab1cc9a 100644 --- a/lib/src/dbus_auth_client.dart +++ b/lib/src/dbus_auth_client.dart @@ -15,6 +15,7 @@ class DBusAuthClient { var _unixFdSupported = false; DBusUUID? _uuid; String? _errorMessage; + final String? _uid; /// Stream of requests to send to the authentication server. Stream get requests => _requestsController.stream; @@ -35,7 +36,9 @@ class DBusAuthClient { String? get errorMessage => _errorMessage; /// Creates a new authentication client. - DBusAuthClient({bool requestUnixFd = true}) : _requestUnixFd = requestUnixFd { + DBusAuthClient({bool requestUnixFd = true, String? uid}) + : _requestUnixFd = requestUnixFd, + _uid = uid { // On start, end an empty byte, as this is required if sending the credentials as a socket control message. // We rely on the server using SO_PEERCRED to check out credentials. // Then request the supported mechanisms. @@ -126,7 +129,9 @@ class DBusAuthClient { /// Start authentication using the EXTERNAL mechanism. void _authenticateExternal() { String authId; - if (Platform.isLinux) { + if (_uid != null) { + authId = _uid!; + } else if (Platform.isLinux) { authId = getuid().toString(); } else if (Platform.isWindows) { authId = getsid(); diff --git a/lib/src/dbus_client.dart b/lib/src/dbus_client.dart index f4df2618..1ced1d40 100644 --- a/lib/src/dbus_client.dart +++ b/lib/src/dbus_client.dart @@ -214,7 +214,7 @@ class DBusClient { RawSocket? _socket; var _socketClosed = false; final _readBuffer = DBusReadBuffer(); - final _authClient = DBusAuthClient(); + final DBusAuthClient _authClient; var _authComplete = false; Completer? _connectCompleter; var _lastSerial = 0; @@ -243,11 +243,15 @@ class DBusClient { /// Creates a new DBus client to connect on [address]. /// If [messageBus] is false, then the server is not running a message bus and - /// no adresses or client to client communication is suported. + /// no addresses or client to client communication is supported. + /// If [authClient] is provided, it will be used instead of creating a new one. DBusClient(DBusAddress address, - {this.introspectable = true, bool messageBus = true}) + {this.introspectable = true, + bool messageBus = true, + DBusAuthClient? authClient}) : _address = address, - _messageBus = messageBus; + _messageBus = messageBus, + _authClient = authClient ?? DBusAuthClient(); /// Creates a new DBus client to communicate with the system bus. factory DBusClient.system({bool introspectable = true}) {