feat: allow hardcoding uid in auth (#189)

Closes https://github.com/Bluetooth-Devices/dbus-fast/issues/188
This commit is contained in:
Kai 2022-12-23 13:47:22 -05:00 committed by GitHub
parent bd0e80c4d6
commit 091c262e27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View File

@ -1,6 +1,6 @@
import enum
import os
from typing import List, Tuple
from typing import List, Optional, Tuple
from .errors import AuthError
@ -60,13 +60,17 @@ class AuthExternal(Authenticator):
:sealso: https://dbus.freedesktop.org/doc/dbus-specification.html#auth-protocol
"""
def __init__(self) -> None:
def __init__(self, uid: int = None) -> None:
self.negotiate_unix_fd: bool = False
self.negotiating_fds: bool = False
self.uid: Optional[int] = uid
def _authentication_start(self, negotiate_unix_fd: bool = False) -> str:
self.negotiate_unix_fd = negotiate_unix_fd
hex_uid = str(os.getuid()).encode().hex()
uid = self.uid
if uid is None:
uid = os.getuid()
hex_uid = str(uid).encode().hex()
return f"AUTH EXTERNAL {hex_uid}"
def _receive_line(self, line: str) -> str:

11
tests/test_auth.py Normal file
View File

@ -0,0 +1,11 @@
"""This tests setting a hardcoded UID in AuthExternal"""
import os
import pytest
from dbus_fast.auth import AuthExternal
def test_uid_is_set():
auth = AuthExternal(uid=999)
assert auth._authentication_start() == "AUTH EXTERNAL 393939"