Authentication
DyUI can put a shared password in front of an app, and always keeps each browser’s conversations private. Read this page before you put an app on the internet.
Shared password
Section titled “Shared password”import osapp = dyui.App(title="Internal Analyst", password=os.environ["APP_PASSWORD"])With password set:
- the app shows a password screen first;
POST /api/loginchecks the password with a constant-time comparison and sets a signed,HttpOnly,SameSite=Laxdyui_authcookie that lasts 30 days (markedSecurewhen the request arrived over HTTPS);- after 10 failed attempts within 5 minutes from one client address, login answers
429 Too many attemptsuntil the window passes, and a successful login resets the count; - every other
/api/*route (except/api/configand/api/logo) returns401without that cookie.
The throttle is kept in memory per process and keys on the client address the server sees. Behind a reverse proxy, run Uvicorn with --proxy-headers (and --forwarded-allow-ips set to your proxy). Otherwise every user shares the proxy’s address, and HTTPS isn’t detected for the Secure flag.
The cookie is an HMAC of the password, signed with a secret. The secret comes from the DYUI_SECRET environment variable, or, when that isn’t set, from a random key generated once in <data_dir>/secret. Changing the password signs everyone out. Set DYUI_SECRET explicitly when you run several instances, so they all accept the same cookie.
Anonymous identity
Section titled “Anonymous identity”Each browser gets a random dyui_uid cookie, and threads, messages, uploaded files and actions are scoped to it. One browser can’t read or act on another browser’s conversations, even with the ids. Clearing cookies starts a fresh, empty history.
Your own auth
Section titled “Your own auth”DyUI’s app is a standard ASGI application, so you can put it behind any gateway that authenticates users: an SSO proxy, your platform’s access control, or ASGI middleware you wrap around it. OAuth providers are on the roadmap.
Complete example
Section titled “Complete example”An app behind a shared password read from the environment. It needs no API key, only the password.
"""An internal app behind a shared password.
Put APP_PASSWORD=... in a .env file next to this one (dyui run loads it).Save as app.py, then run: dyui run app.py"""import os
import dyuifrom dyui import ui
app = dyui.App(title="Internal Analyst", password=os.environ["APP_PASSWORD"])
@app.on_messageasync def reply(msg: dyui.Message, chat: dyui.Chat): yield "You're signed in. Each browser still sees only its own conversations." yield ui.KeyValue({"Browser id": chat.user_id[:8] + "…"})-
Save it as
app.pyin a new, empty file, or download app.py. -
Put
APP_PASSWORD=choose-a-long-passphrasein a.envfile next to it. -
Run it:
Terminal window pip install dyuidyui run app.py -
Open http://localhost:8000 and sign in with the password from
.env.
See also
Section titled “See also”- Behind a reverse proxy: HTTPS and real client addresses.
- API keys & environment:
.envfiles and secrets in deployment.