Add token input, cookie storage, logout

This commit is contained in:
2026-07-23 11:38:10 +04:00
parent 51f4508c1d
commit 6a84257533
2 changed files with 57 additions and 13 deletions
+46 -11
View File
@@ -1,4 +1,4 @@
from flask import Blueprint, current_app, render_template from flask import Blueprint, current_app, render_template, request, make_response
from api.http_client import HttpClient from api.http_client import HttpClient
from operations.get_instances import get_organization from operations.get_instances import get_organization
@@ -6,16 +6,51 @@ from operations.get_instances import get_organization
bp = Blueprint("main", __name__) bp = Blueprint("main", __name__)
@bp.route("/") def _mask(s):
if not s or len(s) < 8:
return ""
return s[:4] + "*" * (len(s) - 8) + s[-4:]
@bp.route("/", methods=["GET", "POST"])
def index(): def index():
env_token = current_app.config["NUBES_API_TOKEN"]
user_token = request.cookies.get("token") or request.form.get("token") or ""
active_token = user_token or env_token
org = None org = None
error = None error = None
try: action = request.form.get("action", "")
client = HttpClient(
current_app.config["NUBES_API_ENDPOINT"], if action == "clear":
current_app.config["NUBES_API_TOKEN"], resp = make_response(render_template("index.html",
) organization=None, error=None,
org = get_organization(client) env_token_masked=_mask(env_token),
except Exception as e: has_user_token=False))
error = str(e) resp.delete_cookie("token")
return render_template("index.html", organization=org, error=error) return resp
if action == "save":
user_token = request.form.get("token", "")
active_token = user_token or env_token
resp = make_response()
resp.set_cookie("token", user_token, max_age=60*60*24*365)
# redirect to GET
resp.headers["Location"] = "/"
resp.status_code = 302
return resp
if active_token:
try:
client = HttpClient(
current_app.config["NUBES_API_ENDPOINT"],
active_token,
)
org = get_organization(client)
except Exception as e:
error = str(e)
return render_template("index.html",
organization=org, error=error,
env_token_masked=_mask(env_token),
has_user_token=bool(user_token))
+11 -2
View File
@@ -9,7 +9,16 @@
</head> </head>
<body> <body>
<div class="card" style="max-width: 780px; margin: 32px auto;"> <div class="card" style="max-width: 780px; margin: 32px auto;">
<div class="card-header">Организация</div> <div class="card-header" style="justify-content: space-between;">
<span>Организация</span>
<form method="post" style="display:flex;align-items:center;gap:6px;">
<input type="password" name="token" placeholder="env: {{ env_token_masked }}" value="{{ '' if not has_user_token else '••••••••' }}" style="height:28px;width:200px;font-size:12px;border:1px solid var(--brand-gray);border-radius:4px;padding:0 6px;" />
<button type="submit" name="action" value="save" class="btn" style="height:28px;font-size:12px;padding:0 8px;">OK</button>
{% if has_user_token %}
<button type="submit" name="action" value="clear" class="btn btn-danger" style="height:28px;font-size:12px;padding:0 8px;">Выйти</button>
{% endif %}
</form>
</div>
<div class="card-body"> <div class="card-body">
{% if organization %} {% if organization %}
<table> <table>
@@ -31,7 +40,7 @@
</tbody> </tbody>
</table> </table>
{% else %} {% else %}
<p style="color: var(--muted);">{% if error %}Ошибка: {{ error }}{% else %}Организация не найдена.{% endif %}</p> <p style="color: var(--muted);">{% if error %}Ошибка: {{ error }}{% else %}Введите токен{% endif %}</p>
{% endif %} {% endif %}
</div> </div>
</div> </div>