pysxLearn by building

Build and operate · Lesson 24 of 28

Authentication and authorization

Apply current host authorization to an operation and keep login generations separate.

Authentication identifies the current principal and login session. Authorization decides whether that principal may perform a particular operation on a target. A hidden button is not an authorization check; the handler must still enforce policy.

This course has explicitly labeled local demonstration roles. The login links change a demo cookie and create a new authentication generation. They are not a production credential provider and do not ask you for a real password.

Understand the moving parts

The host resolves identity for HTTP and socket operations. require_access checks the current policy at the write boundary. The reader may edit its own private UI draft while a protected commit remains denied.

Identity changes must not import another login's delivered state. Recovery tests need both a same-generation restoration control and a changed-generation rejection; a system that never restores anything would otherwise appear secure by passing only the negative case.

Walk through the code

from pysx import (
    AccessDenied,
    Fragment,
    RequestContext,
    SecurityPolicy,
    current_request,
    pysx,
    require_access,
    signal,
)


def authorize(request: RequestContext, action: str, target: str) -> bool:
    writer = request.principal == "writer"

    return action != "write" or (writer and target == "course-demo")


def app() -> Fragment:
    user = current_request().principal or "learner"
    count = signal(0)
    result = signal("No write attempted")

    def write(_: object) -> None:
        try:
            require_access("write", "course-demo")
        except AccessDenied:
            result.set("Denied by current host policy")

            return
        count.update(lambda value: value + 1)
        result.set("Allowed by current host policy")

    return pysx(t"""
        p: "Local demonstration identity: "
        output(id="auth-principal"): {user}
        div:
            a(id="auth-reader" href="/demo-login/reader"): "Sign in as reader"
            a(id="auth-writer" href="/demo-login/writer"): "Sign in as writer"
            a(id="auth-logout" href="/demo-logout"): "Sign out"
        button(id="auth-write" onClick={write}): "Attempt protected write"
        output(id="auth-count"): {count}
        output(id="auth-result" role="status"): {result}
        p: "These local roles are not a production credential provider."
    """)


policy = SecurityPolicy(authorize=authorize)

authorize permits a protected write only for writer and the course-demo target. The handler calls require_access before incrementing; denied writes produce the visible denial. The course host installs this policy and resolves the role cookie from its sign- in links; those demo links are not a production identity provider.

Try it live

Sign in as reader and attempt a protected write. Repeat as writer, then sign out and inspect fresh state.

What to observe

Reader receives a visible denial; writer succeeds. A new login generation starts its own authorized state.

Local demonstration identity:

learner0No write attempted

These local roles are not a production credential provider.

Take this with you

Check authorization at the operation, and bind continuity to the current login session.