Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Improve OIDC configuration dialog recovery https://github.com/siyuan-note/siyuan/issues/18542

Daniel41faf3ced6fd
diff --git a/app/src/config/tabs/accessTab.ts b/app/src/config/tabs/accessTab.ts
index 1a78e38c6b..da09b389d8 100644
--- a/app/src/config/tabs/accessTab.ts
+++ b/app/src/config/tabs/accessTab.ts
@@ -147,7 +147,6 @@ const mountOIDCButton = (root: HTMLElement) => {
<div class="b3-dialog__action"><button class="b3-button b3-button--cancel">${window.siyuan.languages.cancel}</button><div class="fn__space"></div><button class="b3-button b3-button--text">${window.siyuan.languages.confirm}</button></div>`,
width: isMobile() ? "92vw" : "620px",
height: "80vh",
- disableClose: true,
destroyCallback: () => destroyOIDCDialog(),
});
const form = dialog.element.querySelector<HTMLElement>("#oidcConfigForm");
@@ -160,6 +159,8 @@ const mountOIDCButton = (root: HTMLElement) => {
let validationPollToken = "";
let validationPollInFlight = false;
let validationActivating = false;
+ let validationPending = false;
+ let validationAttempt = 0;
let mobileCallbackInstalled = false;
let previousMobileCallback: ((callbackURL: string) => void) | undefined;
let previousMobileAuthError: ((message: string) => void) | undefined;
@@ -167,12 +168,18 @@ const mountOIDCButton = (root: HTMLElement) => {
const provider = form.querySelector<HTMLSelectElement>('[data-field="provider"]').value;
const enabled = form.querySelector<HTMLInputElement>('[data-field="enabled"]').checked;
const allowAll = form.querySelector<HTMLInputElement>('[data-field="allowAll"]').checked;
+ form.querySelectorAll<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>(
+ '[data-field]:not([data-field="enabled"])').forEach((field) => {
+ field.disabled = !enabled;
+ });
form.querySelector<HTMLElement>('[data-section="issuer"]').classList.toggle("fn__none",
provider !== "custom" && provider !== "microsoft");
form.querySelector<HTMLElement>('[data-section="mobileCallbackWarning"]').classList.toggle("fn__none",
provider !== "google");
form.querySelector<HTMLElement>('[data-section="claimRules"]').classList.toggle("fn__none", allowAll);
- buttons[1].textContent = enabled ? window.siyuan.languages.oidcVerifyAndSave : window.siyuan.languages.confirm;
+ if (!validationPending) {
+ buttons[1].textContent = enabled ? window.siyuan.languages.oidcVerifyAndSave : window.siyuan.languages.confirm;
+ }
};
form.querySelector<HTMLSelectElement>('[data-field="provider"]').addEventListener("change", refreshSections);
form.querySelector<HTMLInputElement>('[data-field="enabled"]').addEventListener("change", refreshSections);
@@ -193,6 +200,7 @@ const mountOIDCButton = (root: HTMLElement) => {
mobileCallbackInstalled = false;
};
const cancelValidation = () => {
+ validationAttempt += 1;
validationCancelled = true;
stopValidation();
validationWindow?.close();
@@ -207,13 +215,19 @@ const mountOIDCButton = (root: HTMLElement) => {
};
destroyOIDCDialog = cancelValidation;
const setValidationPending = (pending: boolean) => {
- buttons[1].disabled = pending;
+ validationPending = pending;
+ buttons[1].disabled = false;
form.querySelector<HTMLElement>('[data-section="validationStatus"]').classList.toggle("fn__none", !pending);
- if (!pending) {
- refreshSections();
+ if (pending) {
+ buttons[1].textContent = window.siyuan.languages.retry;
+ return;
}
+ refreshSections();
};
- const validationFailed = (message?: string) => {
+ const validationFailed = (message?: string, attempt = validationAttempt) => {
+ if (attempt !== validationAttempt) {
+ return;
+ }
validationActivating = false;
buttons[0].disabled = false;
cancelValidation();
@@ -222,7 +236,7 @@ const mountOIDCButton = (root: HTMLElement) => {
}
showMessage(message || window.siyuan.languages.oidcVerificationFailed, 6000, "error");
};
- const installMobileCallback = () => {
+ const installMobileCallback = (attempt: number) => {
if (!isInMobileApp() || mobileCallbackInstalled) {
return;
}
@@ -230,28 +244,28 @@ const mountOIDCButton = (root: HTMLElement) => {
previousMobileAuthError = window.handleOIDCAuthError;
mobileCallbackInstalled = true;
const handleMobileCallback = (callbackURL: string, retryCount = 0) => {
- if (validationCancelled || !dialog.element.isConnected) {
+ if (attempt !== validationAttempt || validationCancelled || !dialog.element.isConnected) {
return;
}
void fetchSyncPost("/api/system/oidc/mobileCallback", {callbackURL}, undefined, false).then((response) => {
- if (validationCancelled || !dialog.element.isConnected) {
+ if (attempt !== validationAttempt || validationCancelled || !dialog.element.isConnected) {
return;
}
if (response.code !== 0) {
- validationFailed(response.msg);
+ validationFailed(response.msg, attempt);
}
}).catch((error) => {
- if (retryCount < 4 && !validationCancelled && dialog.element.isConnected) {
+ if (retryCount < 4 && attempt === validationAttempt && !validationCancelled && dialog.element.isConnected) {
window.setTimeout(() => handleMobileCallback(callbackURL, retryCount + 1), 1000 * (retryCount + 1));
return;
}
- validationFailed(error instanceof Error ? error.message : String(error));
+ validationFailed(error instanceof Error ? error.message : String(error), attempt);
});
};
window.handleOIDCCallback = handleMobileCallback;
- window.handleOIDCAuthError = (message: string) => validationFailed(message);
+ window.handleOIDCAuthError = (message: string) => validationFailed(message, attempt);
};
- const openValidationURL = (url: string) => {
+ const openValidationURL = (url: string, attempt: number) => {
if (validationWindow) {
validationWindow.location.href = url;
return;
@@ -269,23 +283,23 @@ const mountOIDCButton = (root: HTMLElement) => {
return;
}
/// #if !BROWSER
- void shell.openExternal(url).catch((error: Error) => validationFailed(error.message));
+ void shell.openExternal(url).catch((error: Error) => validationFailed(error.message, attempt));
/// #else
openByMobile(url);
/// #endif
};
- const pollValidation = async (pollToken: string) => {
- if (validationCancelled || validationPollInFlight || validationActivating) {
+ const pollValidation = async (pollToken: string, attempt: number) => {
+ if (attempt !== validationAttempt || validationCancelled || validationPollInFlight || validationActivating) {
return;
}
validationPollInFlight = true;
try {
const response = await fetchSyncPost("/api/system/oidc/validatePoll", {pollToken}, undefined, false);
- if (validationCancelled || !dialog.element.isConnected) {
+ if (attempt !== validationAttempt || validationCancelled || !dialog.element.isConnected) {
return;
}
if (response.code !== 0) {
- validationFailed(response.msg);
+ validationFailed(response.msg, attempt);
return;
}
if (response.data.status !== "completed") {
@@ -295,9 +309,10 @@ const mountOIDCButton = (root: HTMLElement) => {
validationWindow?.close();
validationActivating = true;
buttons[0].disabled = true;
+ buttons[1].disabled = true;
const activation = await fetchSyncPost("/api/system/oidc/validateActivate", {pollToken}, undefined, false);
if (activation.code !== 0) {
- validationFailed(activation.msg);
+ validationFailed(activation.msg, attempt);
return;
}
validationPollToken = "";
@@ -306,7 +321,7 @@ const mountOIDCButton = (root: HTMLElement) => {
showMessage(window.siyuan.languages.oidcVerificationSuccess);
dialog.destroy();
} catch (error) {
- validationFailed(error instanceof Error ? error.message : String(error));
+ validationFailed(error instanceof Error ? error.message : String(error), attempt);
} finally {
validationPollInFlight = false;
}
@@ -331,19 +346,30 @@ const mountOIDCButton = (root: HTMLElement) => {
claimRules,
};
if (nextConfig.enabled) {
+ if (validationPending) {
+ cancelValidation();
+ }
validationCancelled = false;
validationActivating = false;
+ const attempt = ++validationAttempt;
setValidationPending(true);
- buttons[1].textContent = window.siyuan.languages.oidcVerifying;
if (isBrowser() && !isInMobileApp()) {
validationWindow = window.open("about:blank", "_blank");
if (!validationWindow) {
- validationFailed();
+ validationFailed(undefined, attempt);
return;
}
validationWindow.opener = null;
}
void fetchSyncPost("/api/system/oidc/validate", nextConfig, undefined, false).then((response) => {
+ if (attempt !== validationAttempt) {
+ if (response.code === 0 && response.data.pollToken) {
+ void fetchSyncPost("/api/system/oidc/validateCancel", {
+ pollToken: response.data.pollToken,
+ }, undefined, false).catch(() => undefined);
+ }
+ return;
+ }
if (validationCancelled || !dialog.element.isConnected) {
validationWindow?.close();
if (response.code === 0 && response.data.pollToken) {
@@ -354,16 +380,16 @@ const mountOIDCButton = (root: HTMLElement) => {
return;
}
if (response.code !== 0) {
- validationFailed(response.msg);
+ validationFailed(response.msg, attempt);
return;
}
validationPollToken = response.data.pollToken;
- installMobileCallback();
- openValidationURL(response.data.authURL);
+ installMobileCallback(attempt);
+ openValidationURL(response.data.authURL, attempt);
validationPollTimer = window.setInterval(() => {
- void pollValidation(response.data.pollToken);
+ void pollValidation(response.data.pollToken, attempt);
}, 1000);
- }).catch((error) => validationFailed(error instanceof Error ? error.message : String(error)));
+ }).catch((error) => validationFailed(error instanceof Error ? error.message : String(error), attempt));
return;
}
fetchPost("/api/system/setOIDC", nextConfig, (response) => {