Add project-key support to Python/Node/Java AuthClient equivalents
Mirrors Go's authclient.WithProjectKey: each client now accepts a project_key/projectKey and sends it as X-Project-Key on every request, so AuthGuard can enforce per-user project-access restriction for consumers written in these languages too. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5364588ba0
commit
f58579290d
4 files changed
+71
-4
No files matched your search
@@ -61,6 +61,12 @@ DEFAULT_TIMEOUT = 5.0
|
||||
|
||||
_BEARER_PREFIX = "Bearer "
|
||||
|
||||
# PROJECT_KEY_HEADER is the header AuthClient attaches to every request when
|
||||
# constructed with project_key, identifying the calling project to AuthGuard
|
||||
# so it can enforce per-user project-access restriction (see AuthGuard's
|
||||
# project-scope admin UI). Mirrors Go's authclient.ProjectKeyHeader.
|
||||
PROJECT_KEY_HEADER = "X-Project-Key"
|
||||
|
||||
|
||||
def _normalize_base_url(base_url: str) -> str:
|
||||
trimmed = str(base_url or "").strip().rstrip("/")
|
||||
@@ -205,6 +211,7 @@ class AuthClient:
|
||||
*,
|
||||
cookie_name: str = DEFAULT_COOKIE_NAME,
|
||||
timeout: float = DEFAULT_TIMEOUT,
|
||||
project_key: str = "",
|
||||
) -> None:
|
||||
"""base_url is AuthGuard's base URL, e.g. "https://auth.example.com"
|
||||
(no trailing slash required). May be given without a scheme (e.g.
|
||||
@@ -221,6 +228,11 @@ class AuthClient:
|
||||
self.base_url = _normalize_base_url(base_url)
|
||||
self.cookie_name = cookie_name
|
||||
self.timeout = timeout
|
||||
# project_key identifies this project to AuthGuard on every request
|
||||
# (see PROJECT_KEY_HEADER), enabling per-user project-access
|
||||
# restriction. Normally a project's own lowercase identifier,
|
||||
# matching the key ProjectConfigurator registers it under.
|
||||
self.project_key = (project_key or "").strip().lower()
|
||||
|
||||
# --- session validation (the original, minimal surface) ----------------
|
||||
|
||||
@@ -238,9 +250,12 @@ class AuthClient:
|
||||
return await asyncio.to_thread(self._validate_sync, trimmed)
|
||||
|
||||
def _validate_sync(self, token: str) -> AuthGuardUser:
|
||||
headers = {"Authorization": f"{_BEARER_PREFIX}{token}"}
|
||||
if self.project_key:
|
||||
headers[PROJECT_KEY_HEADER] = self.project_key
|
||||
request = urllib.request.Request(
|
||||
f"{self.base_url}/api/session/validate",
|
||||
headers={"Authorization": f"{_BEARER_PREFIX}{token}"},
|
||||
headers=headers,
|
||||
)
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=self.timeout) as response:
|
||||
@@ -301,6 +316,8 @@ class AuthClient:
|
||||
) -> tuple[dict[str, Any], list[str]]:
|
||||
data = None
|
||||
headers: dict[str, str] = {}
|
||||
if self.project_key:
|
||||
headers[PROJECT_KEY_HEADER] = self.project_key
|
||||
if body is not None:
|
||||
data = json.dumps(body).encode()
|
||||
headers["Content-Type"] = "application/json"
|
||||
|
||||
Reference in new issue
Block a user