Authentication

Caspian provides a robust, session-based authentication system built on FastAPI's security utilities. It is secure by default (HttpOnly cookies), supports Role-Based Access Control (RBAC), and integrates seamlessly with the async RPC layer.

Configuration

Auth settings are located in utils/auth.py. You can configure global route protection, define roles, and set redirect paths here.

Global Protection Switch

By default, routes are public. To make your application private by default, toggle this flag. You then explicitly whitelist public routes (like landing pages).

class AuthConfig:
    # Set to True to require login for ALL pages by default
    IS_ALL_ROUTES_PRIVATE = False 

    # Where to go after login/logout
    DEFAULT_SIGNIN_REDIRECT = "/dashboard"
    DEFAULT_SIGNOUT_REDIRECT = "/signin"

    # Exceptions to the rule
    PUBLIC_ROUTES = ["/", "/about"]
    AUTH_ROUTES = ["/signin", "/signup"] # Redirects to dashboard if already logged in

The Auth Object

The global auth object manages the session lifecycle. It abstracts FastAPI's response and cookie logic.

auth.sign_in(data, redirect_to?)

Creates a session. data is a dict stored in the secure session cookie. Returns a response object handling the cookie set.

auth.sign_out(redirect_to?)

Destroys the session and clears HttpOnly cookies.

auth.is_authenticated()

Returns True if the current session is valid.

auth.get_payload()

Retrieves the user data stored during sign-in.

Implementation Example

A complete async sign-in flow. The backend handles verification using Prisma (Async), while the frontend handles the form submission via RPC.

Protecting Routes

You can protect individual actions using the @rpc decorator, or protect entire pages.

Action Level (RPC)

Best for securing specific buttons or form submissions. The client receives a 401/403 error.

@rpc(require_auth=True)
async def delete_account():
    # Only runs if authenticated
    await prisma.user.delete(...)

Page Level

Best for securing entire views. Redirects unauthenticated users to the sign-in page.

from casp.auth import require_auth

@require_auth
def page():
    return load_page(__file__)