Bind Reddit and Matrix session identities
This commit is contained in:
1 parent
34ae532cdd
commit
492efe0f8a
12 files changed
+234
-55
No files matched your search
@@ -1,4 +1,7 @@
|
||||
import { createSessionExportDownload } from './session-export.js';
|
||||
import {
|
||||
assertMatchingSessionIdentity,
|
||||
createSessionExportDownload,
|
||||
} from './session-export.js';
|
||||
|
||||
const MATRIX_URLS = [
|
||||
'https://matrix.redditspace.com/*',
|
||||
@@ -27,6 +30,29 @@ chrome.webRequest.onBeforeSendHeaders.addListener(
|
||||
['requestHeaders', 'extraHeaders'],
|
||||
);
|
||||
|
||||
chrome.cookies.onChanged.addListener(async ({ cookie }) => {
|
||||
if (!['reddit_session', 'token_v2'].includes(cookie.name)) return;
|
||||
await chrome.storage.local.remove('matrixCapture');
|
||||
await chrome.action.setBadgeText({ text: '' });
|
||||
await chrome.action.setTitle({
|
||||
title: 'Reddit account changed. Open Reddit Chat before exporting.',
|
||||
});
|
||||
});
|
||||
|
||||
async function getRedditIdentity() {
|
||||
const response = await fetch('https://www.reddit.com/api/me.json', {
|
||||
credentials: 'include',
|
||||
cache: 'no-store',
|
||||
});
|
||||
if (!response.ok) throw new Error('Reddit cookies did not authenticate. Sign in and try again.');
|
||||
const payload = await response.json();
|
||||
const account = payload?.data ?? payload;
|
||||
if (!account?.name || !account?.id) {
|
||||
throw new Error('Reddit did not return an authenticated account identity.');
|
||||
}
|
||||
return { username: account.name, accountId: account.id };
|
||||
}
|
||||
|
||||
async function showError(message) {
|
||||
await chrome.action.setBadgeText({ text: 'ERR' });
|
||||
await chrome.action.setBadgeBackgroundColor({ color: '#dc3545' });
|
||||
@@ -48,6 +74,14 @@ chrome.action.onClicked.addListener(async () => {
|
||||
const identity = await whoami.json();
|
||||
if (!identity?.user_id) throw new Error('Reddit Chat did not return an account identity.');
|
||||
|
||||
const redditIdentity = await getRedditIdentity();
|
||||
try {
|
||||
assertMatchingSessionIdentity(redditIdentity, identity.user_id);
|
||||
} catch (error) {
|
||||
await chrome.storage.local.remove('matrixCapture');
|
||||
throw error;
|
||||
}
|
||||
|
||||
await chrome.downloads.download({
|
||||
...createSessionExportDownload(cookies, { ...matrixCapture, userId: identity.user_id }),
|
||||
saveAs: true,
|
||||
|
||||
Reference in new issue
Block a user