dev server uses admin login too: localhost shows the public view

Drop open_access; run() prints a one-off PHYSCOM_ADMIN_TOKEN when none is set.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2026-09-25 17:27:55 -05:00
parent 8171fc023e
commit af401d6bc2
6 changed files with 25 additions and 26 deletions

View File

@@ -44,7 +44,7 @@ def _secret_key() -> str:
def is_admin() -> bool:
from flask import current_app
return current_app.config["OPEN_ACCESS"] or session.get("admin", False)
return session.get("admin", False)
def get_repo() -> Repository:
@@ -61,16 +61,13 @@ def close_db(exc: BaseException | None = None) -> None:
repo.conn.close()
def create_app(open_access: bool = False) -> Flask:
"""open_access=True (the localhost dev server) skips the admin lock
unless PHYSCOM_ADMIN_TOKEN is set. Anything else -- gunicorn in Docker --
is read-only to visitors; writes need a login with PHYSCOM_ADMIN_TOKEN
def create_app() -> Flask:
"""Read-only to visitors; writes need a login with PHYSCOM_ADMIN_TOKEN
(no token set = no one can write)."""
app = Flask(__name__)
app.secret_key = _secret_key()
admin_token = os.environ.get("PHYSCOM_ADMIN_TOKEN", "")
app.config.update(
OPEN_ACCESS=open_access and not admin_token,
SESSION_COOKIE_SAMESITE="Lax",
SESSION_COOKIE_HTTPONLY=True,
SESSION_COOKIE_SECURE=os.environ.get("PHYSCOM_SECURE_COOKIES") == "1",
@@ -153,8 +150,13 @@ def create_app(open_access: bool = False) -> Flask:
def run() -> None:
"""Entry point for `physcom-web` script."""
app = create_app(open_access=True)
"""Entry point for `physcom-web` script. Same access rules as production,
so localhost shows the public view; without PHYSCOM_ADMIN_TOKEN a one-off
token is generated and printed for /login."""
if not os.environ.get("PHYSCOM_ADMIN_TOKEN"):
os.environ["PHYSCOM_ADMIN_TOKEN"] = secrets.token_urlsafe(16)
print(f" * Admin login token (this run only): {os.environ['PHYSCOM_ADMIN_TOKEN']}")
app = create_app()
app.run(
host=os.environ.get("HOST", "127.0.0.1"),
debug=os.environ.get("FLASK_DEBUG") == "1",

View File

@@ -21,10 +21,8 @@
{% if is_admin() %}
<li><a href="{{ url_for('pipeline.pipeline_form') }}">Pipeline</a></li>
<li><a href="{{ url_for('admin.admin_index') }}">Admin</a></li>
{% if not config.OPEN_ACCESS %}
<li><form method="post" action="{{ url_for('logout') }}" class="nav-logout"><button type="submit">Log out</button></form></li>
{% endif %}
{% endif %}
</ul>
</nav>