feat(auth): session login with per-domain scope
Cookie session with /auth/login, /auth/logout and /auth/me. Four accounts come from WEBAPI_USUARIOS (admin plus one per domain) and a second before_request answers 403 when an account asks for another domain. The data suites run with WEBAPI_AUTH_REQUIRED=0; test_auth.py turns the lock back on and covers both hooks. The Dockerfile normalises the entrypoint so a Windows checkout still starts the container.
This commit is contained in:
+30
-2
@@ -1,10 +1,13 @@
|
||||
import datetime as _dt
|
||||
import logging
|
||||
import os
|
||||
import secrets
|
||||
from decimal import Decimal
|
||||
|
||||
from flask import Flask, jsonify
|
||||
from flask.json.provider import DefaultJSONProvider
|
||||
|
||||
from webapi.blueprints.auth import auth_bp, exigir_dominio, exigir_sesion
|
||||
from webapi.blueprints.cacu import cacu_bp
|
||||
from webapi.blueprints.data import data_bp
|
||||
from webapi.blueprints.governance import governance_bp
|
||||
@@ -39,13 +42,37 @@ def create_app() -> Flask:
|
||||
# File sources travel inside the request; cap them per request.
|
||||
app.config["MAX_CONTENT_LENGTH"] = int(os.environ.get("WEBAPI_MAX_UPLOAD_MB", "200")) * 1024 * 1024
|
||||
|
||||
# Session cookie: the login lives in it, so it needs a stable key. Without
|
||||
# WEBAPI_SECRET_KEY each process would sign with its own random key and a
|
||||
# login would break on the next gunicorn worker (the Dockerfile sets one);
|
||||
# in that case the session dies when the process restarts.
|
||||
clave = os.environ.get("WEBAPI_SECRET_KEY")
|
||||
if not clave:
|
||||
clave = secrets.token_hex(32)
|
||||
logging.getLogger(__name__).warning(
|
||||
"WEBAPI_SECRET_KEY sin definir: la sesión no sobrevive a un reinicio "
|
||||
"ni es válida entre varios workers de gunicorn"
|
||||
)
|
||||
app.secret_key = clave
|
||||
app.config["SESSION_COOKIE_HTTPONLY"] = True
|
||||
app.config["SESSION_COOKIE_SAMESITE"] = "Lax"
|
||||
# Eight hours: a working day, no re-login in the middle of a session.
|
||||
app.config["PERMANENT_SESSION_LIFETIME"] = _dt.timedelta(hours=8)
|
||||
|
||||
# The web client goes through a proxy; browser clients on other origins
|
||||
# are allowed with WEBAPI_CORS_ORIGINS (comma-separated).
|
||||
# are allowed with WEBAPI_CORS_ORIGINS (comma-separated). Credentials so
|
||||
# that a cross-origin client can also carry the session cookie.
|
||||
cors_origins = os.environ.get("WEBAPI_CORS_ORIGINS")
|
||||
if cors_origins:
|
||||
from flask_cors import CORS
|
||||
|
||||
CORS(app, origins=[origin.strip() for origin in cors_origins.split(",")])
|
||||
CORS(app, origins=[origin.strip() for origin in cors_origins.split(",")], supports_credentials=True)
|
||||
|
||||
# One hook for the whole application: every route answers 401 without a
|
||||
# session, except the login routes and `health`/`api` (see blueprints.auth).
|
||||
app.before_request(exigir_sesion)
|
||||
# And 403 when the account asks for a domain that is not its own.
|
||||
app.before_request(exigir_dominio)
|
||||
|
||||
@app.get("/health")
|
||||
def health():
|
||||
@@ -73,6 +100,7 @@ def create_app() -> Flask:
|
||||
limit_mb = app.config["MAX_CONTENT_LENGTH"] // (1024 * 1024)
|
||||
return jsonify({"error": f"la solicitud excede {limit_mb} MB"}), 413
|
||||
|
||||
app.register_blueprint(auth_bp, url_prefix="/auth")
|
||||
app.register_blueprint(cacu_bp, url_prefix="/cacu")
|
||||
app.register_blueprint(imports_bp, url_prefix="/imports")
|
||||
app.register_blueprint(data_bp, url_prefix="/data")
|
||||
|
||||
Reference in New Issue
Block a user