From 50b0d388faf9d5257881cfe53b4f2c766dbe5a33 Mon Sep 17 00:00:00 2001 From: Felipe Martin <812088+fmartingr@users.noreply.github.com> Date: Fri, 21 Jul 2023 08:12:38 +0200 Subject: [PATCH] feat: use new login endpoint (#49) * feat: use new login endpoint * typo: makefile run commands * deps: action-web-ext * Log using JWT tokens --- .github/workflows/_webext-build.yml | 4 ++-- .github/workflows/_webext-lint.yml | 2 +- Makefile | 4 ++-- js/background-script.js | 20 ++++++++-------- js/options.js | 37 +++++++++++++++-------------- 5 files changed, 34 insertions(+), 33 deletions(-) diff --git a/.github/workflows/_webext-build.yml b/.github/workflows/_webext-build.yml index 6ef389e..f2cbe0e 100644 --- a/.github/workflows/_webext-build.yml +++ b/.github/workflows/_webext-build.yml @@ -18,7 +18,7 @@ jobs: - name: "web-ext build" id: web-ext-build - uses: kewisch/action-web-ext@v1 + uses: kewisch/action-web-ext@41d7f1231e17976386937cdc01a479f7c79c3c2a with: cmd: build source: . @@ -26,7 +26,7 @@ jobs: - name: "web-ext sign" id: web-ext-sign - uses: kewisch/action-web-ext@v1 + uses: kewisch/action-web-ext@41d7f1231e17976386937cdc01a479f7c79c3c2a with: cmd: sign source: ${{ steps.web-ext-build.outputs.target }} diff --git a/.github/workflows/_webext-lint.yml b/.github/workflows/_webext-lint.yml index c69573e..0bb974d 100644 --- a/.github/workflows/_webext-lint.yml +++ b/.github/workflows/_webext-lint.yml @@ -11,7 +11,7 @@ jobs: uses: actions/checkout@v3 - name: "web-ext lint" - uses: kewisch/action-web-ext@v1 + uses: kewisch/action-web-ext@41d7f1231e17976386937cdc01a479f7c79c3c2a with: cmd: lint source: . diff --git a/Makefile b/Makefile index 152ef9a..179a870 100644 --- a/Makefile +++ b/Makefile @@ -2,10 +2,10 @@ build: web-ext build -a dist run-firefox: - web-ext build run -t firefox-desktop + web-ext run -t firefox-desktop run-chromium: - web-ext build run -t chromium + web-ext run -t chromium lint: web-ext lint diff --git a/js/background-script.js b/js/background-script.js index 9473c16..37f2276 100644 --- a/js/background-script.js +++ b/js/background-script.js @@ -40,7 +40,7 @@ async function getShioriBookmarkFolder() { // In Firefox, its id is `unfiled_____` while in Chrome the id is `2`. var parentId = "", runtimeUrl = await browser.runtime.getURL("/"); - + if (runtimeUrl.startsWith("moz")) { parentId = "unfiled_____"; } else if (runtimeUrl.startsWith("chrome")) { @@ -117,19 +117,19 @@ async function removeLocalBookmark(url) { async function getExtensionConfig() { var items = await browser.storage.local.get(), - session = items.session || "", + token = items.token || "", server = items.server || ""; - - if (session === "") { + + if (token === "") { throw new Error("no active session, please login first"); } - + if (server === "") { throw new Error("server url is not specified"); } return { - session: session, + token: token, server: server }; } @@ -166,7 +166,7 @@ async function removeBookmark() { body: JSON.stringify({url: tab.url}), headers: { "Content-Type": "application/json", - "X-Session-Id": config.session, + "Authorization": `Bearer ${config.token}`, } }); @@ -213,7 +213,7 @@ async function saveBookmark(tags) { body: JSON.stringify(data), headers: { "Content-Type": "application/json", - "X-Session-Id": config.session, + "Authorization": `Bearer ${config.token}`, } }); @@ -237,7 +237,7 @@ async function updateIcon() { 32: "icons/action-default-32.png", 64: "icons/action-default-64.png" }}; - + // Firefox allows using empty object as default icon. // This way, Firefox will use default_icon that defined in manifest.json if (runtimeUrl.startsWith("moz")) { @@ -248,7 +248,7 @@ async function updateIcon() { try { var tab = await getCurrentTab(), local = await findLocalBookmark(tab.url); - + if (local) icon.path = { 16: "icons/action-bookmarked-16.png", 32: "icons/action-bookmarked-32.png", diff --git a/js/options.js b/js/options.js index 0f1affb..b19273b 100644 --- a/js/options.js +++ b/js/options.js @@ -4,7 +4,7 @@ async function getExtensionConfig() { return { server: items.server || "", - session: items.session || "", + token: items.token || "", username: items.username || "", password: items.password || "", remember: items.remember || false, @@ -15,9 +15,9 @@ async function saveExtensionConfig(cfg) { return browser.storage.local.set(cfg); } -async function logout(server, session) { - // Make sure session is exists - if (session === "") return Promise.resolve(); +async function logout(server, token) { + // Make sure token is exists + if (token === "") return Promise.resolve(); // Validate input if (server === "") { @@ -41,7 +41,7 @@ async function logout(server, session) { var response = await fetch(logoutURL, { method: "post", headers: { - "X-Session-Id": session, + "Authorization": `Bearer ${token}`, } }); @@ -73,12 +73,13 @@ async function login(server, username, password, remember) { // Create login URL var loginURL = ""; + var loginPath = "api/v1/auth/login"; try { loginURL = new URL(server); if (loginURL.pathname.slice(-1) == "/") { - loginURL.pathname = loginURL.pathname + "api/login"; + loginURL.pathname = loginURL.pathname + loginPath; } else { - loginURL.pathname = loginURL.pathname + "/api/login"; + loginURL.pathname = loginURL.pathname + "/" + loginPath; } } catch(err) { throw new Error(`${server} is not a valid url`); @@ -90,7 +91,7 @@ async function login(server, username, password, remember) { body: JSON.stringify({ username: username, password: password, - remember: remember, + remember_me: remember, }), headers: { "Content-Type": "application/json", @@ -103,9 +104,9 @@ async function login(server, username, password, remember) { } var jsonResp = await response.json(), - session = jsonResp.session; + token = jsonResp.message.token; - return session; + return token; } // Define function for UI handler @@ -142,8 +143,8 @@ getExtensionConfig() .then(cfg => { config = cfg; - if (cfg.session === "") txtSession.textContent = "No active session"; - else txtSession.textContent = `Active session: ${cfg.session}`; + if (cfg.token === "") txtSession.textContent = "No active session"; + else txtSession.textContent = `Logged in.`; inputServer.value = cfg.server; inputUsername.value = cfg.username; @@ -161,19 +162,19 @@ async function btnLoginClick() { remember = inputRemember.checked; // Make sure to log out first - await logout(server, config.session); - + await logout(server, config.token); + // Login using input value - var newSession = await login(server, username, password, remember); + var token = await login(server, username, password, remember); - // Save input value and session to config + // Save input value and token to config config.server = server; - config.session = newSession; + config.token = token; config.username = username; config.password = password; config.remember = remember; await saveExtensionConfig(config); - txtSession.textContent = `Active session: ${newSession}`; + txtSession.textContent = `Logged in.`; return Promise.resolve(); }