29 lines
1.2 KiB
Python
29 lines
1.2 KiB
Python
"""Fail closed when the operator explicitly enables Internet access."""
|
|
import asyncio
|
|
from sqlalchemy import select
|
|
from .auth import verify_password
|
|
from .models import User
|
|
|
|
|
|
async def require_public_ready(db, settings):
|
|
if not settings.public_access:
|
|
return
|
|
if not settings.cookie_secure:
|
|
raise ValueError("PUBLIC_ACCESS requires COOKIE_SECURE and HTTPS")
|
|
if (settings.sqlalchemy_url.username or '').lower()=='root':
|
|
raise ValueError("PUBLIC_ACCESS requires a dedicated MySQL application user")
|
|
async with db.session() as session:
|
|
admins = (await session.scalars(select(User).where(
|
|
User.role == "admin", User.disabled.is_(False)))).all()
|
|
if not admins:
|
|
raise ValueError("Public access requires an enabled administrator")
|
|
initial_passwords = {"123456"}
|
|
if settings.admin_password:
|
|
initial_passwords.add(settings.admin_password)
|
|
for user in admins:
|
|
unchanged = user.must_change_password
|
|
for password in initial_passwords:
|
|
unchanged |= await asyncio.to_thread(verify_password, password, user.password_hash)
|
|
if unchanged:
|
|
raise ValueError("Change the initial administrator password in private mode before PUBLIC_ACCESS")
|