feat: desktop — connexion compte central + environnement prod/dev (#230)
Liaison d'appareil sur og/og-dev, validation du jeton draco et ouverture de la session de l'admin local. Sélecteur prod/dev (Configuration + premier lancement), admin local par défaut admin/chab30, sans impact sur le web Docker. Inclut BUG-126 (runtime python-embed/_pth + copie de backend/frontend + signature updater opt-in) et BUG-127 (retour /liaison même hôte).
This commit is contained in:
+224
-1
@@ -4,6 +4,12 @@ import { safeCreateIcons, escapeHtml } from './utils.js';
|
||||
import { showToast, closeHeaderMenu, closeMobileSidebar } from './ui.js';
|
||||
import { t, getLocale, setLocale } from './i18n.js';
|
||||
import { showWelcome } from './viewer.js';
|
||||
import {
|
||||
isTauriEnv,
|
||||
getDesktopSettings,
|
||||
desktopEnvironmentLabel,
|
||||
openExternalUrl,
|
||||
} from './desktop.js';
|
||||
|
||||
// ── Global logout handler for header dropdown onclick ────────────────────
|
||||
// Defined at module top level so it's always available, even if
|
||||
@@ -135,8 +141,14 @@ const AuthManager = {
|
||||
const u = new URL(rt);
|
||||
const okHost = u.hostname === "dracodev.net"
|
||||
|| u.hostname.endsWith(".dracodev.net");
|
||||
const sameHost = u.hostname === window.location.hostname;
|
||||
// #230 : la page publique /liaison (approbation d'appareil) renvoie
|
||||
// vers elle-même après connexion (`return_to` même hôte). On l'honore
|
||||
// pour que la liaison desktop reprenne après le login central ; les
|
||||
// autres retours même-hôte restent ignorés (pas de boucle).
|
||||
const sameHostLiaison = sameHost && u.pathname.startsWith("/liaison");
|
||||
if (u.protocol === "https:" && okHost
|
||||
&& u.hostname !== window.location.hostname) {
|
||||
&& (!sameHost || sameHostLiaison)) {
|
||||
window.location.replace(rt);
|
||||
return true;
|
||||
}
|
||||
@@ -922,6 +934,210 @@ function showRecoveryChallenge(username, rememberMe, loginForm, mfaSection) {
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Desktop — connexion avec un compte central (#230)
|
||||
// ---------------------------------------------------------------------------
|
||||
// Le backend desktop démarre une liaison d'appareil sur le site central
|
||||
// (og / og-dev), l'utilisateur approuve dans son navigateur, puis le backend
|
||||
// valide le jeton draco et ouvre la session de l'admin local. Aucun de ces
|
||||
// appels n'existe sur le web : tout est conditionné par isTauriEnv().
|
||||
|
||||
let _centralPollTimer = null;
|
||||
let _centralCancelled = false;
|
||||
|
||||
async function _jsonOrEmpty(res) {
|
||||
try { return await res.json(); } catch (e) { return {}; }
|
||||
}
|
||||
|
||||
function _centralSetStatus(text, isError) {
|
||||
const el = document.getElementById("login-central-status");
|
||||
if (!el) return;
|
||||
el.textContent = text || "";
|
||||
el.classList.toggle("error", !!isError);
|
||||
}
|
||||
|
||||
function _centralShowError(text) {
|
||||
_centralSetStatus(text, true);
|
||||
const el = document.getElementById("login-error");
|
||||
if (el) {
|
||||
el.textContent = text || "";
|
||||
if (text) el.classList.remove("hidden");
|
||||
else el.classList.add("hidden");
|
||||
}
|
||||
}
|
||||
|
||||
function _centralResetPanel() {
|
||||
if (_centralPollTimer) {
|
||||
clearTimeout(_centralPollTimer);
|
||||
_centralPollTimer = null;
|
||||
}
|
||||
_centralCancelled = true;
|
||||
const panel = document.getElementById("login-central-panel");
|
||||
if (panel) panel.remove();
|
||||
const btn = document.getElementById("login-central-btn");
|
||||
if (btn) btn.disabled = false;
|
||||
}
|
||||
|
||||
async function _centralPollOnce(state) {
|
||||
if (_centralCancelled) return;
|
||||
let res;
|
||||
try {
|
||||
res = await fetch("/api/desktop/sso/poll", {
|
||||
method: "POST",
|
||||
credentials: "include",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
demande_id: state.demande_id,
|
||||
code_appareil: state.code_appareil,
|
||||
remember_me: state.rememberMe,
|
||||
}),
|
||||
});
|
||||
} catch (e) {
|
||||
_centralShowError(t("auth.central_error"));
|
||||
_centralResetPanel();
|
||||
return;
|
||||
}
|
||||
const payload = await _jsonOrEmpty(res);
|
||||
if (_centralCancelled) return;
|
||||
if (!res.ok) {
|
||||
_centralShowError(payload.detail || t("auth.central_error"));
|
||||
_centralResetPanel();
|
||||
return;
|
||||
}
|
||||
if (payload.statut === "approuvee") {
|
||||
AuthManager.saveToken(payload);
|
||||
_centralResetPanel();
|
||||
const central = payload.central_user || {};
|
||||
const name = central.display_name || central.username;
|
||||
showToast(name ? t("auth.central_success", { name: name })
|
||||
: t("auth.central_success", { name: "" }), "success");
|
||||
await _onLoginSuccess();
|
||||
safeCreateIcons();
|
||||
return;
|
||||
}
|
||||
if (payload.statut === "en_attente") {
|
||||
if (Date.now() > state.deadline) {
|
||||
_centralShowError(t("auth.central_expired"));
|
||||
_centralResetPanel();
|
||||
return;
|
||||
}
|
||||
_centralSetStatus(t("auth.central_waiting"), false);
|
||||
_centralPollTimer = setTimeout(() => _centralPollOnce(state), 2000);
|
||||
return;
|
||||
}
|
||||
if (payload.statut === "refusee") {
|
||||
_centralShowError(t("auth.central_refused"));
|
||||
} else if (payload.statut === "expiree" || payload.statut === "livree") {
|
||||
_centralShowError(t("auth.central_expired"));
|
||||
} else {
|
||||
_centralShowError(t("auth.central_error"));
|
||||
}
|
||||
_centralResetPanel();
|
||||
}
|
||||
|
||||
async function startCentralLogin() {
|
||||
if (_centralPollTimer) return;
|
||||
const btn = document.getElementById("login-central-btn");
|
||||
if (btn) btn.disabled = true;
|
||||
const loginError = document.getElementById("login-error");
|
||||
if (loginError) loginError.classList.add("hidden");
|
||||
_centralSetStatus(t("auth.central_starting"), false);
|
||||
|
||||
let start;
|
||||
try {
|
||||
const res = await fetch("/api/desktop/sso/start", {
|
||||
method: "POST",
|
||||
credentials: "include",
|
||||
});
|
||||
const payload = await _jsonOrEmpty(res);
|
||||
if (!res.ok) {
|
||||
_centralShowError(payload.detail || t("auth.central_error"));
|
||||
if (btn) btn.disabled = false;
|
||||
return;
|
||||
}
|
||||
start = payload;
|
||||
} catch (e) {
|
||||
_centralShowError(t("auth.central_error"));
|
||||
if (btn) btn.disabled = false;
|
||||
return;
|
||||
}
|
||||
|
||||
const card = document.querySelector(".login-card");
|
||||
if (!card) return;
|
||||
const old = document.getElementById("login-central-panel");
|
||||
if (old) old.remove();
|
||||
const panel = document.createElement("div");
|
||||
panel.id = "login-central-panel";
|
||||
panel.className = "login-central-panel";
|
||||
panel.innerHTML =
|
||||
'<div>' + escapeHtml(t("auth.central_code")) + '</div>' +
|
||||
'<div class="login-central-code">' + escapeHtml(start.code_utilisateur || "") + '</div>' +
|
||||
'<p class="login-central-status" id="login-central-status"></p>' +
|
||||
'<div class="login-central-actions">' +
|
||||
'<button type="button" id="login-central-open">' + escapeHtml(t("auth.central_open")) + '</button>' +
|
||||
'<button type="button" id="login-central-cancel">' + escapeHtml(t("auth.central_cancel")) + '</button>' +
|
||||
'</div>';
|
||||
card.appendChild(panel);
|
||||
|
||||
const openBtn = document.getElementById("login-central-open");
|
||||
if (openBtn) {
|
||||
openBtn.addEventListener("click", () => openExternalUrl(start.url_liaison_absolue));
|
||||
}
|
||||
const cancelBtn = document.getElementById("login-central-cancel");
|
||||
if (cancelBtn) {
|
||||
cancelBtn.addEventListener("click", () => {
|
||||
_centralSetStatus("");
|
||||
_centralResetPanel();
|
||||
if (btn) btn.disabled = false;
|
||||
});
|
||||
}
|
||||
|
||||
_centralCancelled = false;
|
||||
_centralSetStatus(t("auth.central_waiting"), false);
|
||||
await openExternalUrl(start.url_liaison_absolue);
|
||||
const rememberMe = document.getElementById("remember-me");
|
||||
const state = {
|
||||
demande_id: start.demande_id,
|
||||
code_appareil: start.code_appareil,
|
||||
rememberMe: rememberMe ? rememberMe.checked : true,
|
||||
deadline: Date.now() + Math.max(30, Number(start.expire_dans_s) || 600) * 1000,
|
||||
};
|
||||
_centralPollTimer = setTimeout(() => _centralPollOnce(state), 1500);
|
||||
}
|
||||
|
||||
async function initDesktopCentralLogin() {
|
||||
if (!isTauriEnv()) return;
|
||||
const card = document.querySelector(".login-card");
|
||||
const form = document.getElementById("login-form");
|
||||
if (!card || !form || document.getElementById("login-central")) return;
|
||||
|
||||
const box = document.createElement("div");
|
||||
box.id = "login-central";
|
||||
box.className = "login-central";
|
||||
box.innerHTML =
|
||||
'<button type="button" class="login-central-btn" id="login-central-btn">' +
|
||||
escapeHtml(t("auth.central_login")) + '</button>' +
|
||||
'<p class="login-central-hint" id="login-central-hint"></p>';
|
||||
form.insertAdjacentElement("afterend", box);
|
||||
|
||||
let settings;
|
||||
try { settings = await getDesktopSettings(); } catch (e) { settings = undefined; }
|
||||
const hint = document.getElementById("login-central-hint");
|
||||
if (hint) {
|
||||
if (settings && settings.central_url) {
|
||||
hint.textContent = desktopEnvironmentLabel(
|
||||
settings.environment, settings.central_url);
|
||||
} else {
|
||||
hint.textContent = t("auth.central_unavailable");
|
||||
}
|
||||
}
|
||||
const btn = document.getElementById("login-central-btn");
|
||||
if (btn) {
|
||||
if (!settings || !settings.central_url) btn.disabled = true;
|
||||
btn.addEventListener("click", startCentralLogin);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Login form handler
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -930,8 +1146,15 @@ function initLoginForm() {
|
||||
const form = document.getElementById("login-form");
|
||||
if (!form) return;
|
||||
|
||||
// Desktop uniquement : bouton « compte central » (#230).
|
||||
initDesktopCentralLogin();
|
||||
|
||||
form.addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
// Un login local annule toute tentative de connexion centralisée en cours.
|
||||
if (_centralPollTimer || document.getElementById("login-central-panel")) {
|
||||
_centralResetPanel();
|
||||
}
|
||||
const username = document.getElementById("login-username").value;
|
||||
const password = document.getElementById("login-password").value;
|
||||
const rememberMe = document.getElementById("remember-me").checked;
|
||||
|
||||
Reference in New Issue
Block a user