From 8cc27851c0202b05a9dcdff696e3014239c2efcb Mon Sep 17 00:00:00 2001 From: mvn23 Date: Wed, 4 Jan 2023 02:21:55 +0100 Subject: [PATCH 1/6] feat: add support for EXTERNAL auth without uid --- src/dbus_fast/auth.py | 5 +++++ tests/test_auth.py | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/dbus_fast/auth.py b/src/dbus_fast/auth.py index 5ce353e5..198917a8 100644 --- a/src/dbus_fast/auth.py +++ b/src/dbus_fast/auth.py @@ -68,6 +68,8 @@ def __init__(self, uid: int = None) -> None: def _authentication_start(self, negotiate_unix_fd: bool = False) -> str: self.negotiate_unix_fd = negotiate_unix_fd uid = self.uid + if uid == -1: + return "AUTH EXTERNAL" if uid is None: uid = os.getuid() hex_uid = str(uid).encode().hex() @@ -86,6 +88,9 @@ def _receive_line(self, line: str) -> str: if response is _AuthResponse.AGREE_UNIX_FD: return "BEGIN" + if response is _AuthResponse.DATA and self.uid == -1: + return _AuthResponse.DATA + raise AuthError(f"authentication failed: {response.value}: {args}") diff --git a/tests/test_auth.py b/tests/test_auth.py index 4fec0002..dbd8eeaf 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -9,3 +9,8 @@ def test_uid_is_set(): auth = AuthExternal(uid=999) assert auth._authentication_start() == "AUTH EXTERNAL 393939" + + +def test_no_uid(): + auth = AuthExternal(uid=-1) + assert auth._authentication_start() == "AUTH EXTERNAL" From 536bab9aa0f0b120974fbb3eacce560300fb7dba Mon Sep 17 00:00:00 2001 From: mvn23 Date: Wed, 4 Jan 2023 03:03:46 +0100 Subject: [PATCH 2/6] fix: return bare string in _receive_line() --- src/dbus_fast/auth.py | 2 +- tests/test_auth.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/dbus_fast/auth.py b/src/dbus_fast/auth.py index 198917a8..a4c4b37a 100644 --- a/src/dbus_fast/auth.py +++ b/src/dbus_fast/auth.py @@ -89,7 +89,7 @@ def _receive_line(self, line: str) -> str: return "BEGIN" if response is _AuthResponse.DATA and self.uid == -1: - return _AuthResponse.DATA + return "DATA" raise AuthError(f"authentication failed: {response.value}: {args}") diff --git a/tests/test_auth.py b/tests/test_auth.py index dbd8eeaf..70019bce 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -14,3 +14,4 @@ def test_uid_is_set(): def test_no_uid(): auth = AuthExternal(uid=-1) assert auth._authentication_start() == "AUTH EXTERNAL" + assert auth._receive_line("DATA") == "DATA" From 1b20f0b8f867ebf96edaff8e383b56d8dde01990 Mon Sep 17 00:00:00 2001 From: mvn23 Date: Wed, 4 Jan 2023 03:54:29 +0100 Subject: [PATCH 3/6] fix: use a named constant for an unspecified uid in EXTERNAL auth --- src/dbus_fast/auth.py | 6 ++++-- tests/test_auth.py | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/dbus_fast/auth.py b/src/dbus_fast/auth.py index a4c4b37a..b7462d0f 100644 --- a/src/dbus_fast/auth.py +++ b/src/dbus_fast/auth.py @@ -4,6 +4,8 @@ from .errors import AuthError +UID_NOT_SPECIFIED = -1 + # The auth interface here is unstable. I would like to eventually open this up # for people to define their own custom authentication protocols, but I'm not # familiar with what's needed for that exactly. To work with any message bus @@ -68,7 +70,7 @@ def __init__(self, uid: int = None) -> None: def _authentication_start(self, negotiate_unix_fd: bool = False) -> str: self.negotiate_unix_fd = negotiate_unix_fd uid = self.uid - if uid == -1: + if uid == UID_NOT_SPECIFIED: return "AUTH EXTERNAL" if uid is None: uid = os.getuid() @@ -88,7 +90,7 @@ def _receive_line(self, line: str) -> str: if response is _AuthResponse.AGREE_UNIX_FD: return "BEGIN" - if response is _AuthResponse.DATA and self.uid == -1: + if response is _AuthResponse.DATA and self.uid == UID_NOT_SPECIFIED: return "DATA" raise AuthError(f"authentication failed: {response.value}: {args}") diff --git a/tests/test_auth.py b/tests/test_auth.py index 70019bce..47b25594 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -3,7 +3,7 @@ import pytest -from dbus_fast.auth import AuthExternal +from dbus_fast.auth import AuthExternal, UID_NOT_SPECIFIED def test_uid_is_set(): @@ -12,6 +12,6 @@ def test_uid_is_set(): def test_no_uid(): - auth = AuthExternal(uid=-1) + auth = AuthExternal(uid=UID_NOT_SPECIFIED) assert auth._authentication_start() == "AUTH EXTERNAL" assert auth._receive_line("DATA") == "DATA" From c76662bd8edeaff4afda00534b93c72e15c98d8a Mon Sep 17 00:00:00 2001 From: mvn23 Date: Wed, 4 Jan 2023 13:09:47 +0100 Subject: [PATCH 4/6] fix: sort imports --- tests/test_auth.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_auth.py b/tests/test_auth.py index 47b25594..b5f2a1a1 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -3,7 +3,7 @@ import pytest -from dbus_fast.auth import AuthExternal, UID_NOT_SPECIFIED +from dbus_fast.auth import UID_NOT_SPECIFIED, AuthExternal def test_uid_is_set(): From 603e83e9c4ebc54594544bfc81cf832168c765d1 Mon Sep 17 00:00:00 2001 From: mvn23 Date: Fri, 6 Jan 2023 10:47:19 +0100 Subject: [PATCH 5/6] test: update tests for AuthExternal --- tests/test_auth.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_auth.py b/tests/test_auth.py index b5f2a1a1..8c86056d 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -4,6 +4,7 @@ import pytest from dbus_fast.auth import UID_NOT_SPECIFIED, AuthExternal +from dbus_fast.errors import AuthError def test_uid_is_set(): @@ -11,7 +12,10 @@ def test_uid_is_set(): assert auth._authentication_start() == "AUTH EXTERNAL 393939" -def test_no_uid(): +def test_auth_external_no_uid(): + """Test AuthExternal with UID_NOT_SPECIFIED""" auth = AuthExternal(uid=UID_NOT_SPECIFIED) assert auth._authentication_start() == "AUTH EXTERNAL" assert auth._receive_line("DATA") == "DATA" + with pytest.raises(AuthError): + auth._receive_line("REJECTED") From 8a340084a145472b611bb5e1b7c587d5d2645a40 Mon Sep 17 00:00:00 2001 From: mvn23 Date: Fri, 6 Jan 2023 23:32:41 +0100 Subject: [PATCH 6/6] docs: update docstring for AuthExternal --- src/dbus_fast/auth.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/dbus_fast/auth.py b/src/dbus_fast/auth.py index b7462d0f..5730fa17 100644 --- a/src/dbus_fast/auth.py +++ b/src/dbus_fast/auth.py @@ -59,6 +59,9 @@ class AuthExternal(Authenticator): """An authenticator class for the external auth protocol for use with the :class:`MessageBus `. + :param uid: The uid to use when connecting to the message bus. Use UID_NOT_SPECIFIED to use the uid known to the kernel. + :vartype uid: int + :sealso: https://dbus.freedesktop.org/doc/dbus-specification.html#auth-protocol """