Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use network requests for auto-add key to Fynbos #649

Merged
merged 4 commits into from
Oct 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 33 additions & 40 deletions src/content/keyAutoAdd/fynbos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
LOGIN_WAIT_TIMEOUT,
type StepRun as Run,
} from './lib/keyAutoAdd';
import { isTimedOut, waitForElement, waitForURL } from './lib/helpers';
import { isTimedOut, waitForURL } from './lib/helpers';
// #region: Steps

type IndexRouteResponse = {
Expand Down Expand Up @@ -62,51 +62,45 @@ const findWallet: Run<void> = async (
}
};

const findForm: Run<{
form: HTMLFormElement;
nickNameField: HTMLInputElement;
publicKeyField: HTMLTextAreaElement;
}> = async () => {
const pathname = '/settings/keys/add-public';
const link = await waitForElement<HTMLAnchorElement>(`a[href="${pathname}"]`);
link.click();
await waitForURL((url) => url.pathname === pathname);
const addKey: Run<void> = async ({ nickName, publicKey }) => {
const url = `/settings/keys/add-public?_data=${encodeURIComponent('routes/settings_.keys_.add-public')}`;
const csrfToken = await getCSRFToken(url);

const form = await waitForElement<HTMLFormElement>('form#add-public-key');
const nickNameField = await waitForElement<HTMLInputElement>(
'input#applicationName',
{ root: form },
);
const publicKeyField = await waitForElement<HTMLTextAreaElement>(
'textarea#publicKey',
{ root: form },
);
return { form, nickNameField, publicKeyField };
};

const addKey: Run<void> = async ({ publicKey, nickName }, { output }) => {
const { form, nickNameField, publicKeyField } = output(findForm);

nickNameField.focus();
nickNameField.value = nickName;
nickNameField.blur();
const formData = new FormData();
formData.set('csrfToken', csrfToken);
formData.set('applicationName', nickName);
formData.set('publicKey', publicKey);

publicKeyField.focus();
publicKeyField.value = publicKey;
publicKeyField.blur();

const submitButton = await waitForElement<HTMLButtonElement>(
'button[type="submit"]',
{ root: form },
);
submitButton.click();
const res = await fetch(url, {
method: 'POST',
credentials: 'include',
body: formData,
}).catch((error) => {
return Response.json(null, { status: 599, statusText: error.message });
});

await waitForURL((url) => url.pathname === '/settings/keys');
if (!res.ok) {
throw new Error(`Failed to upload public key (${res.statusText})`);
}
};
// #endregion

// #region: Helpers
// anything?
const getCSRFToken = async (url: string) => {
const res = await fetch(url, {
headers: { accept: 'application/json' },
credentials: 'include',
}).catch((error) => {
return Response.json(null, { status: 599, statusText: error.message });
});
if (!res.ok) {
throw new Error(`Failed to retrieve CSRF token (${res.statusText})`);
}

const { csrfToken }: { csrfToken: string } = await res.json();

return csrfToken;
};
// #endregion

// #region: Main
Expand All @@ -117,7 +111,6 @@ new KeyAutoAdd([
maxDuration: LOGIN_WAIT_TIMEOUT,
},
{ name: 'Finding wallet', run: findWallet },
{ name: 'Finding form to add public key', run: findForm },
{ name: 'Adding key', run: addKey },
]).init();
// #endregion