Skip to content

Commit

Permalink
feat: use new login endpoint (#49)
Browse files Browse the repository at this point in the history
* feat: use new login endpoint

* typo: makefile run commands

* deps: action-web-ext

* Log using JWT tokens
  • Loading branch information
fmartingr authored Jul 21, 2023
1 parent 77c0744 commit 50b0d38
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/_webext-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ 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: .
filename: "{name}-{version}.zip"

- 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 }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/_webext-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: .
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 10 additions & 10 deletions js/background-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down Expand Up @@ -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
};
}
Expand Down Expand Up @@ -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}`,
}
});

Expand Down Expand Up @@ -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}`,
}
});

Expand All @@ -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")) {
Expand All @@ -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",
Expand Down
37 changes: 19 additions & 18 deletions js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 === "") {
Expand All @@ -41,7 +41,7 @@ async function logout(server, session) {
var response = await fetch(logoutURL, {
method: "post",
headers: {
"X-Session-Id": session,
"Authorization": `Bearer ${token}`,
}
});

Expand Down Expand Up @@ -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`);
Expand All @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down

0 comments on commit 50b0d38

Please sign in to comment.