refactor: update Dropbox authentication handling with BroadcastChannel for improved token management

This commit is contained in:
Azgaar 2026-03-26 23:14:04 +01:00
parent b6fbc66ec3
commit f3fe260c5b
3 changed files with 30 additions and 10 deletions

6
package-lock.json generated
View file

@ -1362,6 +1362,7 @@
"integrity": "sha512-zWW5KPngR/yvakJgGOmZ5vTBemDoSqF3AcV/LrO5u5wTWyEAVVh+IT39G4gtyAkh3CtTZs8aX/yRM82OfzHJRg==", "integrity": "sha512-zWW5KPngR/yvakJgGOmZ5vTBemDoSqF3AcV/LrO5u5wTWyEAVVh+IT39G4gtyAkh3CtTZs8aX/yRM82OfzHJRg==",
"dev": true, "dev": true,
"license": "MIT", "license": "MIT",
"peer": true,
"dependencies": { "dependencies": {
"undici-types": "~7.16.0" "undici-types": "~7.16.0"
} }
@ -1402,6 +1403,7 @@
"integrity": "sha512-gfajTHVCiwpxRj1qh0Sh/5bbGLG4F/ZH/V9xvFVoFddpITfMta9YGow0W6ZpTTORv2vdJuz9TnrNSmjKvpOf4g==", "integrity": "sha512-gfajTHVCiwpxRj1qh0Sh/5bbGLG4F/ZH/V9xvFVoFddpITfMta9YGow0W6ZpTTORv2vdJuz9TnrNSmjKvpOf4g==",
"dev": true, "dev": true,
"license": "MIT", "license": "MIT",
"peer": true,
"dependencies": { "dependencies": {
"@vitest/browser": "4.0.18", "@vitest/browser": "4.0.18",
"@vitest/mocker": "4.0.18", "@vitest/mocker": "4.0.18",
@ -1883,6 +1885,7 @@
"resolved": "https://registry.npmjs.org/d3-selection/-/d3-selection-3.0.0.tgz", "resolved": "https://registry.npmjs.org/d3-selection/-/d3-selection-3.0.0.tgz",
"integrity": "sha512-fmTRWbNMmsmWq6xJV8D19U/gw/bwrHfNXxrIN+HfZgnzqTHp9jOmKMhsTUjXOJnZOdZY9Q28y4yebKzqDKlxlQ==", "integrity": "sha512-fmTRWbNMmsmWq6xJV8D19U/gw/bwrHfNXxrIN+HfZgnzqTHp9jOmKMhsTUjXOJnZOdZY9Q28y4yebKzqDKlxlQ==",
"license": "ISC", "license": "ISC",
"peer": true,
"engines": { "engines": {
"node": ">=12" "node": ">=12"
} }
@ -2175,6 +2178,7 @@
"integrity": "sha512-QP88BAKvMam/3NxH6vj2o21R6MjxZUAd6nlwAS/pnGvN9IVLocLHxGYIzFhg6fUQ+5th6P4dv4eW9jX3DSIj7A==", "integrity": "sha512-QP88BAKvMam/3NxH6vj2o21R6MjxZUAd6nlwAS/pnGvN9IVLocLHxGYIzFhg6fUQ+5th6P4dv4eW9jX3DSIj7A==",
"dev": true, "dev": true,
"license": "MIT", "license": "MIT",
"peer": true,
"engines": { "engines": {
"node": ">=12" "node": ">=12"
}, },
@ -2486,6 +2490,7 @@
"integrity": "sha512-w+N7Hifpc3gRjZ63vYBXA56dvvRlNWRczTdmCBBa+CotUzAPf5b7YMdMR/8CQoeYE5LX3W4wj6RYTgonm1b9DA==", "integrity": "sha512-w+N7Hifpc3gRjZ63vYBXA56dvvRlNWRczTdmCBBa+CotUzAPf5b7YMdMR/8CQoeYE5LX3W4wj6RYTgonm1b9DA==",
"dev": true, "dev": true,
"license": "MIT", "license": "MIT",
"peer": true,
"dependencies": { "dependencies": {
"esbuild": "^0.27.0", "esbuild": "^0.27.0",
"fdir": "^6.5.0", "fdir": "^6.5.0",
@ -2561,6 +2566,7 @@
"integrity": "sha512-hOQuK7h0FGKgBAas7v0mSAsnvrIgAvWmRFjmzpJ7SwFHH3g1k2u37JtYwOwmEKhK6ZO3v9ggDBBm0La1LCK4uQ==", "integrity": "sha512-hOQuK7h0FGKgBAas7v0mSAsnvrIgAvWmRFjmzpJ7SwFHH3g1k2u37JtYwOwmEKhK6ZO3v9ggDBBm0La1LCK4uQ==",
"dev": true, "dev": true,
"license": "MIT", "license": "MIT",
"peer": true,
"dependencies": { "dependencies": {
"@vitest/expect": "4.0.18", "@vitest/expect": "4.0.18",
"@vitest/mocker": "4.0.18", "@vitest/mocker": "4.0.18",

View file

@ -1,4 +1,4 @@
<!DOCTYPE html> <!doctype html>
<html lang="en" dir="ltr"> <html lang="en" dir="ltr">
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
@ -17,7 +17,7 @@
const error = params.get("error"); const error = params.get("error");
if (code) getToken(); if (code) getToken();
else if (error) window.opener.Cloud.providers.dropbox.returnError(params.get("error_description")); else if (error) returnError(params.get("error_description"));
else startAuth(); else startAuth();
function startAuth() { function startAuth() {
@ -31,13 +31,23 @@
.catch(error => console.error(error)); .catch(error => console.error(error));
} }
function returnError(description) {
const channel = new BroadcastChannel("dropbox-auth");
channel.postMessage({type: "error", description});
channel.close();
window.close();
}
function getToken() { function getToken() {
auth.setCodeVerifier(window.sessionStorage.getItem("codeVerifier")); auth.setCodeVerifier(window.sessionStorage.getItem("codeVerifier"));
auth auth
.getAccessTokenFromCode(REDIRECT_URI, code) .getAccessTokenFromCode(REDIRECT_URI, code)
.then(resp => { .then(resp => {
const token = resp.result.access_token; const token = resp.result.access_token;
window.opener.Cloud.providers.dropbox.setDropBoxToken(token); const channel = new BroadcastChannel("dropbox-auth");
channel.postMessage({type: "token", token});
channel.close();
window.close();
}) })
.catch(error => { .catch(error => {
console.error(error); console.error(error);

View file

@ -95,26 +95,30 @@ window.Cloud = (function () {
reject(new Error("Timeout. No auth for Dropbox")); reject(new Error("Timeout. No auth for Dropbox"));
}, 120 * 1000); }, 120 * 1000);
window.addEventListener("dropboxauth", e => { const channel = new BroadcastChannel("dropbox-auth");
channel.onmessage = async ({data}) => {
channel.close();
clearTimeout(watchDog); clearTimeout(watchDog);
resolve(); if (data.type === "token") {
}); await this.setDropBoxToken(data.token);
resolve();
} else {
this.returnError(data.description);
reject(new Error(data.description));
}
};
}); });
}, },
// Callback function for auth window
async setDropBoxToken(token) { async setDropBoxToken(token) {
DEBUG.cloud && console.info("Access token:", token); DEBUG.cloud && console.info("Access token:", token);
setToken(this.name, token); setToken(this.name, token);
await this.connect(token); await this.connect(token);
this.authWindow.close();
window.dispatchEvent(new Event("dropboxauth"));
}, },
returnError(errorDescription) { returnError(errorDescription) {
console.error(errorDescription); console.error(errorDescription);
tip(errorDescription.replaceAll("+", " "), true, "error", 4000); tip(errorDescription.replaceAll("+", " "), true, "error", 4000);
this.authWindow.close();
}, },
async getLink(path) { async getLink(path) {