Skip to content

Commit

Permalink
fix bugs, SSO
Browse files Browse the repository at this point in the history
  • Loading branch information
crkos committed Jan 8, 2024
1 parent 9deb2d6 commit c3850a0
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 27 deletions.
54 changes: 49 additions & 5 deletions src/@/components/BookmarkForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const BookmarkForm = () => {
if (config.usingSSO) {
const session = await getSession(config.baseUrl);
if (!session) {
throw new Error('You are not logged in!');
return;
}
await postLink(config.baseUrl, values);

Expand Down Expand Up @@ -158,13 +158,49 @@ const BookmarkForm = () => {
useEffect(() => {
const syncBookmarks = async () => {
try {
const { syncBookmarks, baseUrl } = await getConfig();
const { syncBookmarks, baseUrl, password, usingSSO, username } = await getConfig();
if (!syncBookmarks) {
return;
}
if (await isConfigured()) {
await saveLinksInCache(baseUrl);
//await syncLocalBookmarks(baseUrl);
if (usingSSO) {
const session = await getSession(baseUrl);
if (!session) {
return;
}
await saveLinksInCache(baseUrl);
//await syncLocalBookmarks(baseUrl);
} else {
const csrfToken = await getCsrfToken(baseUrl);
const session = await getSession(baseUrl);
HAD_PREVIOUS_SESSION = !!session;
if (!HAD_PREVIOUS_SESSION) {
await performLoginOrLogout(
`${baseUrl}/api/v1/auth/callback/credentials`,
{
username,
password,
redirect: false,
csrfToken,
callbackUrl: `${baseUrl}/login`,
json: true,
}
);
}
await saveLinksInCache(baseUrl);
//await syncLocalBookmarks(baseUrl);
if (!HAD_PREVIOUS_SESSION) {
const url = `${baseUrl}/api/v1/auth/signout`;
await performLoginOrLogout(url, {
username,
password,
redirect: false,
csrfToken,
callbackUrl: `${baseUrl}/login`,
json: true,
});
}
}
}
} catch (error) {
console.error(error);
Expand All @@ -182,9 +218,17 @@ const BookmarkForm = () => {
queryFn: async () => {
const config = await getConfig();

const csrfToken = await getCsrfToken(config.baseUrl);
const session = await getSession(config.baseUrl);

if (!session && config.usingSSO) {
return [];
} else if (session && config.usingSSO) {
const data = await getCollections(config.baseUrl);
return data.data;
}

const csrfToken = await getCsrfToken(config.baseUrl);

HAD_PREVIOUS_SESSION = !!session;

if (!HAD_PREVIOUS_SESSION) {
Expand Down
31 changes: 15 additions & 16 deletions src/@/components/OptionsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,26 +102,25 @@ const OptionsForm = () => {
throw new Error('Not logged in');
}
return values;
} else {
const csrfToken = await getCsrfToken(values.baseUrl);
}
const csrfToken = await getCsrfToken(values.baseUrl);

const url = `${values.baseUrl}/api/v1/auth/callback/credentials`;
const data: DataLogin = {
username: username,
password: password,
redirect: false,
csrfToken: csrfToken,
callbackUrl: `${values.baseUrl}/login`,
json: true,
};
const url = `${values.baseUrl}/api/v1/auth/callback/credentials`;
const data: DataLogin = {
username: username,
password: password,
redirect: false,
csrfToken: csrfToken,
callbackUrl: `${values.baseUrl}/login`,
json: true,
};

const session = await getSession(values.baseUrl);
HAD_PREVIOUS_SESSION = !!session;
const session = await getSession(values.baseUrl);
HAD_PREVIOUS_SESSION = !!session;

await performLoginOrLogout(url, data);
await performLoginOrLogout(url, data);
return values;

return values;
}
},
onError: (error) => {
// Do proper errors of axios instance here
Expand Down
6 changes: 6 additions & 0 deletions src/@/lib/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,9 @@ export async function getSession(url: string) {
const session = await axios.get(`${url}/api/v1/auth/session`);
return session.data.user;
}

export async function getSessionFetch(url: string) {
const session = await fetch(`${url}/api/v1/auth/session`);
const sessionJson = await session.json();
return sessionJson.user;
}
1 change: 1 addition & 0 deletions src/@/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const DEFAULTS: optionsFormValues = {
username: '',
password: '',
syncBookmarks: false,
usingSSO: false,
};
const CONFIG_KEY = 'lw_config_key';

Expand Down
4 changes: 2 additions & 2 deletions src/@/lib/validators/optionsForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export const optionsFormSchema = z.object({
baseUrl: z.string().url('This has to be a URL'),
username: z.string().min(1, 'This cannot be empty'),
password: z.string().min(1, 'This cannot be empty'),
syncBookmarks: z.boolean().default(false).optional(),
usingSSO: z.boolean().default(false).optional(),
syncBookmarks: z.boolean().default(false),
usingSSO: z.boolean().default(false)
});

export type optionsFormValues = z.infer<typeof optionsFormSchema>;
77 changes: 73 additions & 4 deletions src/pages/Background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ import {
} from '../../@/lib/cache.ts';
import ContextType = chrome.contextMenus.ContextType;
import OnClickData = chrome.contextMenus.OnClickData;
import { getCsrfTokenFetch, getSessionFetch, performLoginOrLogoutFetch } from '../../@/lib/auth/auth.ts';

const browser = getBrowser();

// This is the main function that will be called when a bookmark is created
// idk why wont work with axios...
browser.bookmarks.onCreated.addListener(async (_id: string, bookmark: BookmarkTreeNode) => {
try {
const { syncBookmarks, baseUrl } = await getConfig();
const { syncBookmarks, baseUrl, username, password, usingSSO } = await getConfig();
if (!syncBookmarks || !bookmark.url) {
return;
}

const session = await getSessionFetch(baseUrl);

// Check if the bookmark already exists in the server by checking the url so, it doesn't create duplicates
// I know, could use the method search from the api, but I want to avoid as much api specific calls as possible
// in case isn't supported, so I prefer to do it this way, if performance is an issue I will think of change to that.
Expand All @@ -30,6 +33,21 @@ browser.bookmarks.onCreated.addListener(async (_id: string, bookmark: BookmarkTr
return;
}

if (!session && !usingSSO) {
const csrfToken = await getCsrfTokenFetch(baseUrl);

await performLoginOrLogoutFetch(`${baseUrl}/api/v1/auth/callback/credentials`, {
csrfToken: csrfToken,
callbackUrl: `${baseUrl}/api/v1/auth/callback`,
json: true,
redirect: false,
username: username,
password: password,
})
} else if (!session && usingSSO) {
return;
}

const newLink = await postLinkFetch(baseUrl, {
url: bookmark.url,
collection: {
Expand All @@ -53,7 +71,7 @@ browser.bookmarks.onCreated.addListener(async (_id: string, bookmark: BookmarkTr

browser.bookmarks.onChanged.addListener(async (id: string, changeInfo: chrome.bookmarks.BookmarkChangeInfo) => {
try {
const { syncBookmarks, baseUrl } = await getConfig();
const { syncBookmarks, baseUrl, username, password, usingSSO } = await getConfig();
if (!syncBookmarks || !changeInfo.url) {
return;
}
Expand All @@ -64,6 +82,23 @@ browser.bookmarks.onChanged.addListener(async (id: string, changeInfo: chrome.bo
return;
}

const session = await getSessionFetch(baseUrl);

if (!session && !usingSSO) {
const csrfToken = await getCsrfTokenFetch(baseUrl);

await performLoginOrLogoutFetch(`${baseUrl}/api/v1/auth/callback/credentials`, {
csrfToken: csrfToken,
callbackUrl: `${baseUrl}/api/v1/auth/callback`,
json: true,
redirect: false,
username: username,
password: password,
})
} else if (!session && usingSSO) {
return;
}

const updatedLink = await updateLinkFetch(baseUrl, link.id, {
url: changeInfo.url,
collection: {
Expand All @@ -87,7 +122,7 @@ browser.bookmarks.onChanged.addListener(async (id: string, changeInfo: chrome.bo

browser.bookmarks.onRemoved.addListener(async (id: string, removeInfo: chrome.bookmarks.BookmarkRemoveInfo) => {
try {
const { syncBookmarks, baseUrl } = await getConfig();
const { syncBookmarks, baseUrl, username, password, usingSSO } = await getConfig();
if (!syncBookmarks || !removeInfo.node.url) {
return;
}
Expand All @@ -97,6 +132,23 @@ browser.bookmarks.onRemoved.addListener(async (id: string, removeInfo: chrome.bo
return;
}

const session = await getSessionFetch(baseUrl);

if (!session && !usingSSO) {
const csrfToken = await getCsrfTokenFetch(baseUrl);

await performLoginOrLogoutFetch(`${baseUrl}/api/v1/auth/callback/credentials`, {
csrfToken: csrfToken,
callbackUrl: `${baseUrl}/api/v1/auth/callback`,
json: true,
redirect: false,
username: username,
password: password,
})
} else if (!session && usingSSO) {
return;
}

await Promise.all([
deleteBookmarkMetadata(link.bookmarkId),
deleteLinkFetch(baseUrl, link.id)
Expand All @@ -115,7 +167,7 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {

// A generic onclick callback function.
async function genericOnClick(info: OnClickData, tab: chrome.tabs.Tab | undefined) {
const { syncBookmarks, baseUrl } = await getConfig();
const { syncBookmarks, baseUrl, username, password, usingSSO } = await getConfig();
const configured = await isConfigured();
if (!tab?.url || !tab?.title || !configured) {
return;
Expand All @@ -139,6 +191,23 @@ async function genericOnClick(info: OnClickData, tab: chrome.tabs.Tab | undefine
});
} else {
try {
const session = await getSessionFetch(baseUrl);

if (!session && !usingSSO) {
const csrfToken = await getCsrfTokenFetch(baseUrl);

await performLoginOrLogoutFetch(`${baseUrl}/api/v1/auth/callback/credentials`, {
csrfToken: csrfToken,
callbackUrl: `${baseUrl}/api/v1/auth/callback`,
json: true,
redirect: false,
username: username,
password: password,
})
} else if (!session && usingSSO) {
return;
}

const newLink = await postLinkFetch(baseUrl, {
url: tab.url,
collection: {
Expand Down

0 comments on commit c3850a0

Please sign in to comment.