This commit is contained in:
Ashley Graves 2024-10-13 15:18:03 +02:00
parent 880ae5a359
commit 4fee7c368e
2 changed files with 44 additions and 27 deletions

View file

@ -1,6 +1,7 @@
import apfetch from "./auth-fetch.js";
import { encode } from "html-entities";
import * as sdk from "matrix-js-sdk";
import { VerificationMethod } from "matrix-js-sdk/lib/types.js";
import { VerificationPhase, VerificationRequestEvent, VerifierEvent } from "matrix-js-sdk/lib/crypto-api.js";
import sdkExt from "./lib/ext.js";
import { resolve } from "node:path";
import fs from "node:fs";
@ -19,11 +20,21 @@ const localStorage = new LocalStorage("./data/localstorage");
const cryptoStore = new sdk.LocalStorageCryptoStore(localStorage);
const store = new sdk.MemoryStore({ localStorage });
var logger = {
trace: () => { },
debug: () => { },
info: () => { },
warn: () => { },
error: () => { },
}
const client = sdk.createClient({
baseUrl: process.env.BASE_URL,
accessToken: process.env.ACCESS_TOKEN,
deviceId: process.env.DEVICE_ID,
userId: process.env.USER_ID,
verificationMethods: [VerificationMethod.Sas],
logger: logger,
cryptoStore,
store
});
@ -104,6 +115,27 @@ client.once("sync", async function (state, prevState, data) {
prefixes.push(client.name + ": ");
prefixes.push(client.name + " ");
client.initialized = true;
const request = await client.getCrypto().requestOwnUserVerification();
var verifier;
request.on(VerificationRequestEvent.Change, async function () {
switch (request.phase) {
case 3:
console.log("### VERIFICATION ### Starting");
verifier = await request.startVerification(VerificationMethod.Sas);
verifier.on(VerifierEvent.ShowSas, async function () {
console.log("### VERIFICATION ### Confirming");
await verifier.getShowSasCallbacks().confirm();
});
break;
case 5:
console.log("### VERIFICATION ### Cancelled!");
process.exit(1);
case 6:
console.log("### VERIFICATION ### Done!");
break;
}
})
});
client.on(sdk.RoomEvent.Timeline, async function (event, room, toStartOfTimeline) {
@ -150,4 +182,4 @@ client.on("Room.myMembership", function (room, membership, prevMembership) {
});
await client.initCrypto();
await client.startClient();
await client.startClient();

View file

@ -1,38 +1,23 @@
import { LocalStorage } from 'node-localstorage';
import readline from "node:readline/promises";
import { stdin, stdout } from 'node:process';
import * as sdk from "matrix-js-sdk";
import Olm from "@matrix-org/olm";
global.Olm = Olm;
import { configDotenv } from "dotenv";
configDotenv();
const rl = readline.createInterface({ input: stdin, output: stdout });
const host = await rl.question("Server: ");
const user = await rl.question("Username: ");
const pass = await rl.question("Password: ");
const idnt = await rl.question("Device ID: ");
const host = process.env.BASE_URL ?? "https://" + await rl.question("Server: https://");
const user = process.env.USER_ID ?? await rl.question("User ID: ");
const pass = process.env.PASSWORD ?? await rl.question("Password: ");
const userId = `@${user}:${host}`;
const client = sdk.createClient({ baseUrl: host });
const localStorage = new LocalStorage("./data/localstorage");
const cryptoStore = new sdk.LocalStorageCryptoStore(localStorage);
const store = new sdk.MemoryStore({ localStorage });
const client = sdk.createClient({
baseUrl: "https://" + host,
deviceId: idnt,
cryptoStore,
store
});
await client.loginWithPassword(userId, pass);
await client.initCrypto();
await client.startClient();
const credentials = await client.loginWithPassword(user, pass);
console.log(`BASE_URL=${client.getHomeserverUrl()}
USER_ID=${client.getUserId()}
ACCESS_TOKEN=${client.getAccessToken()}
DEVICE_ID=${client.getDeviceId()}`);
USER_ID=${credentials.user_id}
ACCESS_TOKEN=${credentials.access_token}
DEVICE_ID=${credentials.device_id}`);
client.stopClient();
process.exit(0);