support native encryption

This commit is contained in:
H. Shay 2024-09-30 13:04:01 -07:00
parent 5ecdb8d581
commit c9a757f6e2
3 changed files with 51 additions and 4 deletions

View File

@ -6,10 +6,24 @@ homeserverUrl: "https://matrix.org"
# only set this to the public-internet homeserver client API URL, do NOT set this to the pantalaimon URL.
rawHomeserverUrl: "https://matrix.org"
# Matrix Access Token to use, Mjolnir will only use this if pantalaimon.use is false.
# Matrix Access Token to use
accessToken: "YOUR_TOKEN_HERE"
# Options related to native encryption
encryption:
# whether to use native encryption in mjolnir, rather than using pantalaimon as a proxy
# note that if encryption is enabled here, pantaliamon must be disabled, and vice versa
use: true
# the username to log in with
username: "mjolnir"
# the password to log in with
password: "password"
# Options related to Pantalaimon (https://github.com/matrix-org/pantalaimon)
# Note that this option is now deprecated as native encryption is now supported in mjolnir,
# and will be removed at a later date.
pantalaimon:
# Whether or not Mjolnir will use pantalaimon to access the matrix homeserver,
# set to `true` if you're using pantalaimon.

View File

@ -70,6 +70,11 @@ export interface IConfig {
homeserverUrl: string;
rawHomeserverUrl: string;
accessToken: string;
encryption: {
use: boolean;
username: string;
password: string;
}
pantalaimon: {
use: boolean;
username: string;
@ -189,6 +194,11 @@ const defaultConfig: IConfig = {
homeserverUrl: "http://localhost:8008",
rawHomeserverUrl: "http://localhost:8008",
accessToken: "NONE_PROVIDED",
encryption: {
use: true,
username: "name",
password: "pass",
},
pantalaimon: {
use: false,
username: "",

View File

@ -20,10 +20,10 @@ import { Healthz } from "./health/healthz";
import {
LogLevel,
LogService,
LogService, MatrixAuth,
MatrixClient,
PantalaimonClient,
RichConsoleLogger,
RichConsoleLogger, RustSdkCryptoStorageProvider,
SimpleFsStorageProvider
} from "@vector-im/matrix-bot-sdk";
@ -59,13 +59,36 @@ import { initializeSentry, initializeGlobalPerformanceMetrics, patchMatrixClient
try {
const storagePath = path.isAbsolute(config.dataPath) ? config.dataPath : path.join(__dirname, '../', config.dataPath);
const storage = new SimpleFsStorageProvider(path.join(storagePath, "bot.json"));
const cryptoStorage = new RustSdkCryptoStorageProvider(storagePath, 0)
if (config.encryption.use && config.pantalaimon.use) {
throw Error('Cannot enable both pantalaimon and encryption at the same time. Remove one from the config.');
}
let client: MatrixClient;
if (config.pantalaimon.use) {
const pantalaimon = new PantalaimonClient(config.homeserverUrl, storage);
client = await pantalaimon.createClientWithCredentials(config.pantalaimon.username, config.pantalaimon.password);
} else {
client = new MatrixClient(config.homeserverUrl, config.accessToken, storage);
const accessToken = await Promise.resolve(storage.readValue("access_token"));
if (accessToken) {
client = new MatrixClient(config.homeserverUrl, accessToken, storage, cryptoStorage);
}
else {
const auth = new MatrixAuth(config.homeserverUrl)
const tempClient = await auth.passwordLogin(config.encryption.username, config.encryption.password)
client = new MatrixClient(config.homeserverUrl, tempClient.accessToken, storage, cryptoStorage);
}
try {
LogService.info("index", "Preparing encrypted client...")
await client.crypto.prepare();
} catch (e) {
LogService.error("Index", `Error preparing encrypted client ${e}`)
throw e
}
}
patchMatrixClient();
config.RUNTIME.client = client;