Add token input, cookie storage, logout
This commit is contained in:
+39
-4
@@ -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 operations.get_instances import get_organization
|
||||
@@ -6,16 +6,51 @@ from operations.get_instances import get_organization
|
||||
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():
|
||||
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
|
||||
error = None
|
||||
action = request.form.get("action", "")
|
||||
|
||||
if action == "clear":
|
||||
resp = make_response(render_template("index.html",
|
||||
organization=None, error=None,
|
||||
env_token_masked=_mask(env_token),
|
||||
has_user_token=False))
|
||||
resp.delete_cookie("token")
|
||||
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"],
|
||||
current_app.config["NUBES_API_TOKEN"],
|
||||
active_token,
|
||||
)
|
||||
org = get_organization(client)
|
||||
except Exception as e:
|
||||
error = str(e)
|
||||
return render_template("index.html", organization=org, error=error)
|
||||
|
||||
return render_template("index.html",
|
||||
organization=org, error=error,
|
||||
env_token_masked=_mask(env_token),
|
||||
has_user_token=bool(user_token))
|
||||
|
||||
@@ -9,7 +9,16 @@
|
||||
</head>
|
||||
<body>
|
||||
<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">
|
||||
{% if organization %}
|
||||
<table>
|
||||
@@ -31,7 +40,7 @@
|
||||
</tbody>
|
||||
</table>
|
||||
{% else %}
|
||||
<p style="color: var(--muted);">{% if error %}Ошибка: {{ error }}{% else %}Организация не найдена.{% endif %}</p>
|
||||
<p style="color: var(--muted);">{% if error %}Ошибка: {{ error }}{% else %}Введите токен{% endif %}</p>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user