Sync shared snapshot from de9b599

This commit is contained in:
Gabriel20xx committed 2026-09-02 20:50:21 +02:00
1 parent eb113a7029
commit 49e3c7d561
9 files changed
+697 -44

No files matched your search

@@ -61,6 +61,15 @@ DEFAULT_TIMEOUT = 5.0
_BEARER_PREFIX = "Bearer "
def _normalize_base_url(base_url: str) -> str:
trimmed = str(base_url or "").strip().rstrip("/")
if not trimmed:
return trimmed
if trimmed.lower().startswith(("http://", "https://")):
return trimmed
return f"https://{trimmed}"
class InvalidSessionError(Exception):
"""Raised by validate() / resolve_user() when AuthGuard reports the
token/cookie as not currently valid (expired, unknown, or missing)."""
@@ -170,11 +179,15 @@ class AuthClient:
timeout: float = DEFAULT_TIMEOUT,
) -> None:
"""base_url is AuthGuard's base URL, e.g. "https://auth.example.com"
(no trailing slash required). cookie_name is the cookie
(no trailing slash required). May be given without a scheme (e.g.
"auth.example.com" or "192.168.1.50:8080") -- "https://" is assumed
in that case, so a deployment only needs to name the host, not repeat
the scheme; an explicit "http://" (for a plain-HTTP local/LAN
AuthGuard) is left as-is. cookie_name is the cookie
token_from_request looks for when there is no Authorization header;
it must match AuthGuard's SessionCookieName. timeout is the request
timeout in seconds for the underlying blocking HTTP call."""
self.base_url = str(base_url or "").strip().rstrip("/")
self.base_url = _normalize_base_url(base_url)
self.cookie_name = cookie_name
self.timeout = timeout
@@ -49,6 +49,24 @@ def _fake_http_error(status: int, payload: dict) -> urllib.error.HTTPError:
)
class AuthClientBaseUrlTests(unittest.TestCase):
def test_bare_host_assumes_https(self) -> None:
self.assertEqual(AuthClient("auth.example.com").base_url, "https://auth.example.com")
self.assertEqual(AuthClient("192.168.1.50:8080").base_url, "https://192.168.1.50:8080")
def test_explicit_scheme_left_alone(self) -> None:
self.assertEqual(AuthClient("http://auth.example.com").base_url, "http://auth.example.com")
self.assertEqual(AuthClient("https://auth.example.com").base_url, "https://auth.example.com")
self.assertEqual(AuthClient("HTTP://auth.example.com").base_url, "HTTP://auth.example.com")
def test_trailing_slash_stripped_before_scheme_check(self) -> None:
self.assertEqual(AuthClient("auth.example.com/").base_url, "https://auth.example.com")
def test_whitespace_trimmed_and_empty_stays_empty(self) -> None:
self.assertEqual(AuthClient(" auth.example.com ").base_url, "https://auth.example.com")
self.assertEqual(AuthClient("").base_url, "")
class AuthClientValidateTests(unittest.IsolatedAsyncioTestCase):
def setUp(self) -> None:
self.client = AuthClient("https://authguard.example")