Skip to content

Commit

Permalink
More variable naming consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
Archmonger committed Dec 25, 2024
1 parent 56c9b3d commit 7e3f489
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 27 deletions.
39 changes: 20 additions & 19 deletions src/reactpy_django/auth/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ async def rerender():


@component
def session_manager():
"""This component can force the client (browser) to switch HTTP sessions,
making it match the websocket session, by using a authentication token.
def auth_manager():
"""This component uses a client-side component alongside an authentication token
to make the client (browser) to switch the HTTP auth session, to make it match the websocket session.
Used to force persistent authentication between Django's websocket and HTTP stack."""
from reactpy_django import config
Expand All @@ -52,24 +52,24 @@ def session_manager():
def setup_asgi_scope():
"""Store trigger functions in the websocket scope so that ReactPy-Django's hooks can command
any relevant actions."""
scope["reactpy"]["synchronize_session"] = synchronize_session
scope["reactpy"]["synchronize_auth"] = synchronize_auth

@hooks.use_effect(dependencies=[synchronize_requested])
async def synchronize_session_watchdog():
"""Detected if the client has taken too long to request a session synchronization.
async def synchronize_auth_watchdog():
"""Detected if the client has taken too long to request a auth session synchronization.
This effect will automatically be cancelled if the session is successfully
switched (via effect dependencies)."""
synchronized (via effect dependencies)."""
if synchronize_requested:
await asyncio.sleep(config.REACTPY_AUTH_TOKEN_TIMEOUT + 0.1)
await asyncio.to_thread(
_logger.warning,
f"Client did not switch sessions within {config.REACTPY_AUTH_TOKEN_TIMEOUT} (REACTPY_AUTH_TOKEN_TIMEOUT) seconds.",
f"Client did not switch authentication sessions within {config.REACTPY_AUTH_TOKEN_TIMEOUT} (REACTPY_AUTH_TOKEN_TIMEOUT) seconds.",
)
set_synchronize_requested(False)

async def synchronize_session():
"""Event that can command the client to switch HTTP sessions (to match the websocket session)."""
async def synchronize_auth():
"""Event that can command the client to switch HTTP auth sessions (to match the websocket session)."""
session: SessionBase | None = scope.get("session")
if not session or not session.session_key:
return
Expand All @@ -85,30 +85,31 @@ async def synchronize_session():
# Create a fresh token
token.set_current(str(uuid4()))

# Begin the process of synchronizing HTTP and websocket sessions
# Begin the process of synchronizing HTTP and websocket auth sessions
obj = await AuthToken.objects.acreate(value=token.current, session_key=session.session_key)
await obj.asave()
set_synchronize_requested(True)

async def synchronize_session_callback(status_code: int, response: str):
async def synchronize_auth_callback(status_code: int, response: str):
"""This callback acts as a communication bridge, allowing the client to notify the server
of the status of session switch."""
of the status of auth session switch."""
set_synchronize_requested(False)
if status_code >= 300 or status_code < 200:
await asyncio.to_thread(
_logger.warning,
f"Client returned unexpected HTTP status code ({status_code}) while trying to sychronize sessions.",
_logger.error,
f"Client returned unexpected HTTP status code ({status_code}) while trying to synchronize authentication sessions.",
)

# If needed, synchronize sessions by configuring all relevant session cookies.
# This is achieved by commanding the client to perform a HTTP request to our session manager endpoint.
# If needed, synchronize authenication sessions by configuring all relevant session cookies.
# This is achieved by commanding the client to perform a HTTP request to our session manager endpoint,
# which will set any required cookies.
if synchronize_requested:
return HttpRequest(
{
"method": "GET",
"url": reverse("reactpy:session_manager", args=[token.current]),
"url": reverse("reactpy:auth_manager", args=[token.current]),
"body": None,
"callback": synchronize_session_callback,
"callback": synchronize_auth_callback,
},
)

Expand Down
2 changes: 1 addition & 1 deletion src/reactpy_django/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ async def login(user: AbstractUser, rerender: bool = True):
await channels_auth.login(scope, user, backend=config.REACTPY_AUTH_BACKEND)
session_save_method = getattr(scope["session"], "asave", scope["session"].save)
await ensure_async(session_save_method)()
await scope["reactpy"]["synchronize_session"]()
await scope["reactpy"]["synchronize_auth"]()

if rerender:
await scope["reactpy"]["rerender"]()
Expand Down
6 changes: 3 additions & 3 deletions src/reactpy_django/http/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
name="view_to_iframe",
),
path(
"session/<uuid:uuid>",
views.session_manager,
name="session_manager",
"auth/<uuid:uuid>",
views.auth_manager,
name="auth_manager",
),
]
4 changes: 2 additions & 2 deletions src/reactpy_django/http/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ async def view_to_iframe(request: HttpRequest, dotted_path: str) -> HttpResponse
return response


async def session_manager(request: HttpRequest, uuid: str) -> HttpResponse:
"""Switches the client's active session to match ReactPy.
async def auth_manager(request: HttpRequest, uuid: str) -> HttpResponse:
"""Switches the client's active auth session to match ReactPy's session.
This view exists because ReactPy is rendered via WebSockets, and browsers do not
allow active WebSocket connections to modify cookies. Django's authentication
Expand Down
4 changes: 2 additions & 2 deletions src/reactpy_django/websocket/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ async def run_dispatcher(self):
"""Runs the main loop that performs component rendering tasks."""
# TODO: Figure out why exceptions raised in this method are not being printed to the console.
from reactpy_django import models
from reactpy_django.auth.components import root_manager, session_manager
from reactpy_django.auth.components import auth_manager, root_manager
from reactpy_django.config import (
REACTPY_REGISTERED_COMPONENTS,
REACTPY_SESSION_MAX_AGE,
Expand Down Expand Up @@ -214,7 +214,7 @@ async def run_dispatcher(self):
await serve_layout(
Layout( # type: ignore
ConnectionContext(
session_manager(),
auth_manager(),
root_manager(root_component),
value=connection,
)
Expand Down

0 comments on commit 7e3f489

Please sign in to comment.