revert(auth): remove the session login from the API
The API no longer authenticates: no /auth routes, no session cookie and no before_request lock. An external service that reads and writes data was hitting the 401, so the endpoint is open again. The interface keeps its login and the per-account domains, now resolved in the browser. WEBAPI_USUARIO(S) and WEBAPI_SECRET_KEY are gone from the Dockerfile and .env.example.
This commit is contained in:
+2
-30
@@ -1,13 +1,10 @@
|
||||
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
|
||||
@@ -42,37 +39,13 @@ 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). Credentials so
|
||||
# that a cross-origin client can also carry the session cookie.
|
||||
# are allowed with WEBAPI_CORS_ORIGINS (comma-separated).
|
||||
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(",")], 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)
|
||||
CORS(app, origins=[origin.strip() for origin in cors_origins.split(",")])
|
||||
|
||||
@app.get("/health")
|
||||
def health():
|
||||
@@ -100,7 +73,6 @@ 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