Skip to content

Commit

Permalink
feat: allow hardcoding uid in auth (#189)
Browse files Browse the repository at this point in the history
Closes #188
  • Loading branch information
kiwiz authored Dec 23, 2022
1 parent bd0e80c commit 091c262
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/dbus_fast/auth.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import enum
import os
from typing import List, Tuple
from typing import List, Optional, Tuple

from .errors import AuthError

Expand Down Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 091c262

Please sign in to comment.