Skip to content

Commit 118b25d

Browse files
committed
chore: trimmed comments
1 parent 4c1a173 commit 118b25d

4 files changed

Lines changed: 25 additions & 80 deletions

File tree

src/auth0_server_python/auth_server/anonymous_client.py

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ def _map_anonymous_error(
199199

200200
if code in ("session_expired", "invalid_session_token"):
201201
return _AnonymousSessionExpired(description)
202-
# Distinguishes DPoP-mandated clients from a plain client-not-enabled block.
203202
if status_code == 400 and "Proof-of-Possession" in description:
204203
return AnonymousClientNotSupportedError(description, error_data)
205204
if code == "feature_not_enabled":
@@ -273,10 +272,6 @@ def _encrypt_context(self, context: AnonymousSessionContext) -> str:
273272
def _decrypt_context(self, stored: Any) -> AnonymousSessionContext:
274273
"""Decrypt and validate a stored anonymous session record.
275274
276-
A crypto-library failure and a validation failure both mean the
277-
record is unusable, and both must convert to the same internal
278-
signal instead of an untyped exception reaching the caller.
279-
280275
Args:
281276
stored: The raw record read from the anonymous store.
282277
@@ -313,8 +308,6 @@ async def _create_session_at(
313308
) -> AnonymousSession:
314309
"""Create a fresh anonymous session against a resolved domain.
315310
316-
Shared by create_session() and every renewal-ladder fallback.
317-
318311
Args:
319312
domain: The resolved tenant domain.
320313
audience: Audience for the new session, or None.
@@ -344,9 +337,7 @@ async def _create_session_at(
344337
try:
345338
response = await client.post(f"{base_url}/anonymous/token", json=body)
346339
except httpx.HTTPError as e:
347-
raise AnonymousCreateError(
348-
"Failed to reach the anonymous token endpoint"
349-
) from e
340+
raise AnonymousCreateError("Failed to reach the anonymous token endpoint") from e
350341

351342
if response.status_code != 200:
352343
error_data = self._parse_anonymous_error_body(response)
@@ -359,9 +350,7 @@ async def _create_session_at(
359350
try:
360351
token_response = AnonymousTokenResponse.model_validate(response.json())
361352
except (json.JSONDecodeError, ValueError, ValidationError) as e:
362-
raise AnonymousCreateError(
363-
"Failed to parse anonymous token response"
364-
) from e
353+
raise AnonymousCreateError("Failed to parse anonymous token response") from e
365354

366355
if not token_response.session_token:
367356
raise AnonymousCreateError("Anonymous token response missing required fields")
@@ -408,9 +397,6 @@ async def _remint(
408397
) -> AnonymousSession:
409398
"""Re-mint an access token using the stored session token.
410399
411-
Retries once by minting a brand-new session if the stored session
412-
token is itself rejected as expired or invalid.
413-
414400
Args:
415401
context: The current decrypted session context.
416402
store_options: Options passed to the anonymous store.
@@ -425,7 +411,10 @@ async def _remint(
425411
"""
426412
domain = context.domain or await self._resolve_domain(store_options)
427413
base_url = f"https://{domain}"
428-
body: dict[str, Any] = {"client_id": self._client_id, "session_token": context.session_token}
414+
body: dict[str, Any] = {
415+
"client_id": self._client_id,
416+
"session_token": context.session_token,
417+
}
429418
if self._client_secret:
430419
body["client_secret"] = self._client_secret
431420

@@ -456,7 +445,6 @@ async def _remint(
456445

457446
now = int(time.time())
458447
new_context = AnonymousSessionContext(
459-
# Rewrite when a fresh session_token is present, else keep the old one.
460448
session_token=token_response.session_token or context.session_token,
461449
sub=token_response.sub or context.sub,
462450
session_id=token_response.session_id or context.session_id,
@@ -495,18 +483,14 @@ async def _remint(
495483
async def get_session_token_for_injection(
496484
self, store_options: Optional[dict[str, Any]] = None
497485
) -> Optional[str]:
498-
"""Read the active session token for login injection.
499-
500-
Does not trigger the renewal ladder. Never raises: no configured
501-
store, no active session, and an undecryptable record all return
502-
None, so malformed linking state denies the link instead of
503-
aborting the login.
486+
"""Read the active session token for login injection without renewing.
504487
505488
Args:
506489
store_options: Options passed to the anonymous store.
507490
508491
Returns:
509-
The raw session token, or None.
492+
The raw session token, or None when there is no store, no active
493+
session, or the stored record cannot be decrypted.
510494
"""
511495
if self._anonymous_store is None:
512496
return None
@@ -577,16 +561,9 @@ async def create_session(
577561
domain, audience=audience, scope=scope, metadata=metadata, store_options=store_options
578562
)
579563

580-
async def get_token(
581-
self, store_options: Optional[dict[str, Any]] = None
582-
) -> AnonymousSession:
564+
async def get_token(self, store_options: Optional[dict[str, Any]] = None) -> AnonymousSession:
583565
"""Return a valid anonymous access token, renewing or re-minting as needed.
584566
585-
The renewal ladder: a fresh cached token is returned as-is. An
586-
expired one is re-minted from the stored session token. A session
587-
token that is itself expired or invalid silently mints a brand-new
588-
session, once. Any other error is raised, never swallowed or retried.
589-
590567
Args:
591568
store_options: Options passed to the anonymous store.
592569
@@ -606,7 +583,6 @@ async def get_token(
606583
try:
607584
context = self._decrypt_context(stored)
608585
except _AnonymousSessionExpired:
609-
# No audience/scope to recover, fall back to configured defaults.
610586
domain = await self._resolve_domain(store_options)
611587
return await self._create_session_at(
612588
domain,
@@ -648,11 +624,7 @@ async def get_token(
648624
async def introspect(
649625
self, store_options: Optional[dict[str, Any]] = None
650626
) -> AnonymousSessionIntrospection:
651-
"""Return the current anonymous session status without mutating it.
652-
653-
Never triggers the renewal ladder and never writes to the store. An
654-
unreadable stored context is a hard failure here, not a silent
655-
re-mint. Uses the cached access token as a Bearer credential.
627+
"""Return the current anonymous session status without mutating the store.
656628
657629
Args:
658630
store_options: Options passed to the anonymous store.
@@ -706,12 +678,7 @@ async def introspect(
706678
) from e
707679

708680
async def logout(self, store_options: Optional[dict[str, Any]] = None) -> None:
709-
"""Clear the locally-held anonymous session.
710-
711-
No server-side revocation exists: access tokens already issued
712-
remain valid until natural expiry. The remote POST is best-effort
713-
only. The local store clear is what actually ends the session from
714-
this SDK's perspective.
681+
"""Clear the locally-held anonymous session without revoking issued tokens.
715682
716683
Args:
717684
store_options: Options passed to the anonymous store.

src/auth0_server_python/auth_server/server_client.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def __init__(
225225
headers=self._telemetry_headers,
226226
)
227227

228-
# Deliberately given its own store, never self._state_store.
228+
# Its own store, never self._state_store, so anonymous state stays isolated.
229229
self._anonymous_client = AnonymousClient(
230230
domain=domain,
231231
client_id=self._client_id,
@@ -568,11 +568,9 @@ async def start_interactive_login(
568568
if options.invitation:
569569
auth_params["invitation"] = options.invitation
570570

571-
# session_token is sourced only from the SDK's own encrypted anonymous
572-
# store, never from a caller. INTERNAL_AUTHORIZE_PARAMS alone isn't
573-
# enough, since auth_params is seeded unfiltered from the constructor
574-
# defaults above, so a caller-supplied value would survive that filter.
575-
# Suppressed entirely on the PAR branch below (unsupported there).
571+
# session_token comes only from the SDK's own encrypted anonymous
572+
# store, never a caller. The pop strips any value seeded from the
573+
# constructor defaults, closing a session-fixation vector.
576574
auth_params.pop("session_token", None)
577575
anonymous_session_token = None
578576
if not self._pushed_authorization_requests:

src/auth0_server_python/auth_types/__init__.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -871,14 +871,8 @@ class PasskeyTokenResponse(BaseModel):
871871

872872

873873
class AnonymousSession(BaseModel):
874-
"""
875-
Public result of create_session() / the renewal ladder.
876-
877-
Never exposes the raw session token, which stays inside the encrypted
878-
AnonymousSessionContext, server-side only.
879-
"""
874+
"""Public result of create_session() and the renewal ladder."""
880875

881-
# Optional: the platform's /anonymous/token response doesn't always include these.
882876
sub: Optional[str] = None
883877
session_id: Optional[str] = None
884878
access_token: str
@@ -889,11 +883,7 @@ class AnonymousSession(BaseModel):
889883

890884

891885
class AnonymousSessionIntrospection(BaseModel):
892-
"""
893-
Result of introspect(). Deliberately minimal and lenient, since the
894-
platform's /anonymous/userinfo response shape is unconfirmed.
895-
Unrecognized fields are ignored rather than rejected.
896-
"""
886+
"""Result of introspect(), lenient to unrecognized response fields."""
897887

898888
model_config = ConfigDict(extra="ignore")
899889
sub: str
@@ -915,25 +905,21 @@ class AnonymousTokenResponse(BaseModel):
915905

916906

917907
class AnonymousSessionContext(BaseModel):
918-
"""
919-
Internal context stored inside the encrypted anonymous session record.
908+
"""Internal context stored inside the encrypted anonymous session record.
920909
921-
No `extra` config, so decrypt fails closed on a tampered or malformed
922-
payload rather than silently yielding a partial object.
910+
Rejects extra fields so a tampered payload fails closed on decrypt.
923911
"""
924912

925913
session_token: str
926-
# sub/session_id: optional for the same reason as AnonymousSession above.
927914
sub: Optional[str] = None
928915
session_id: Optional[str] = None
929916
access_token: str
930917
expires_at: int
931918
session_expires_at: Optional[int] = None
932919
metadata: Optional[dict[str, Any]] = None
933920
created_at: int
934-
# Resolved domain at creation time. Gated on in resolver/MCD mode so a
935-
# session minted against tenant A cannot be read back for tenant B.
936-
# None when the client uses a static domain.
921+
# Resolved domain at creation, gated on in resolver/MCD mode so a session
922+
# minted for one tenant cannot be read back for another.
937923
domain: Optional[str] = None
938924
audience: Optional[str] = None
939925
scope: Optional[str] = None

src/auth0_server_python/error/__init__.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,7 @@ def __init__(self, message: str, cause: Optional[dict] = None):
404404

405405

406406
class AnonymousIntrospectError(AnonymousApiError):
407-
"""
408-
Error thrown when introspect() fails.
409-
410-
Only raised on a genuine HTTP/auth failure, never on an unknown or
411-
missing response field.
412-
"""
407+
"""Error thrown when introspect() fails on an HTTP or auth failure."""
413408

414409
def __init__(self, message: str, cause: Optional[dict] = None):
415410
super().__init__("anonymous_introspect_error", message, cause)
@@ -451,10 +446,9 @@ def __init__(self, message: str, cause: Optional[dict] = None):
451446

452447

453448
class _AnonymousSessionExpired(Auth0Error):
454-
"""
455-
Internal-only signal that the stored session token is expired or invalid.
449+
"""Internal-only signal that the stored session token is expired or invalid.
456450
457-
Drives the silent re-mint in the renewal ladder. Never raised to SDK callers.
451+
Never raised to SDK callers.
458452
"""
459453

460454
def __init__(self, message: str = "The anonymous session token is expired or invalid."):

0 commit comments

Comments
 (0)