feat: replace browser login with imported Reddit sessions
This commit is contained in:
1 parent
2b0545afcf
commit
c483c7b8be
76 files changed
+2048
-2828
No files matched your search
@@ -0,0 +1,48 @@
|
||||
import type { Cookie } from 'playwright';
|
||||
import type { MatrixClient } from './chat/matrix/client.js';
|
||||
import type { MatrixCredentials } from './chat/matrix/types.js';
|
||||
|
||||
interface BrowserSessionTarget {
|
||||
importRedditCookies(cookies: Cookie[]): Promise<void>;
|
||||
isSessionValid(): Promise<boolean>;
|
||||
}
|
||||
|
||||
interface ChatSessionTarget {
|
||||
setMatrixClient(client: MatrixClient): void;
|
||||
}
|
||||
|
||||
type MatrixClientFactory = (credentials: MatrixCredentials) => MatrixClient;
|
||||
type CredentialStore = (credentials: MatrixCredentials) => void;
|
||||
|
||||
export class RedditSessionImporter {
|
||||
constructor(
|
||||
private readonly browser: BrowserSessionTarget,
|
||||
private readonly chat: ChatSessionTarget,
|
||||
private readonly createMatrixClient: MatrixClientFactory,
|
||||
private readonly saveCredentials: CredentialStore,
|
||||
) {}
|
||||
|
||||
async import(cookies: Cookie[], credentials: MatrixCredentials): Promise<void> {
|
||||
const homeserver = new URL(credentials.homeserver);
|
||||
if (
|
||||
homeserver.protocol !== 'https:' ||
|
||||
!/^matrix\.(?:redditspace|reddit)\.com$/i.test(homeserver.hostname)
|
||||
) {
|
||||
throw new Error('Invalid Reddit Matrix homeserver');
|
||||
}
|
||||
|
||||
const matrixClient = this.createMatrixClient(credentials);
|
||||
const userId = await matrixClient.whoami();
|
||||
if (!userId || userId !== credentials.userId) {
|
||||
throw new Error('Matrix credentials did not verify the exported account');
|
||||
}
|
||||
|
||||
await this.browser.importRedditCookies(cookies);
|
||||
if (!(await this.browser.isSessionValid())) {
|
||||
throw new Error('Imported cookies did not authenticate with Reddit');
|
||||
}
|
||||
|
||||
this.saveCredentials(credentials);
|
||||
this.chat.setMatrixClient(matrixClient);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user