feat(e2ee): introduce OLM encryption. refactor(storage): moved from fs to localstorage. refactor(config): added as global to registrar. chore(npm): upgraded matrix-js-sdk from 2.4.6 release to 9.5.1 release.

This commit is contained in:
vulet 2021-02-01 16:58:59 +08:00
parent 0cd373fb8a
commit 67b88f9c96
28 changed files with 264 additions and 1413 deletions

66
auth.js
View file

@ -1,41 +1,58 @@
const sdk = require('matrix-js-sdk');
const axios = require('axios');
const fs = require('fs');
const registrar = require('./registrar.js');
const { LocalStorageCryptoStore } = require('matrix-js-sdk/lib/crypto/store/localStorage-crypto-store');
module.exports.getMatrixToken = async () => {
matrixClient = sdk.createClient(registrar.config.matrix.domain);
matrixClient.loginWithPassword(registrar.config.matrix.user, registrar.config.matrix.password)
matrixClient = sdk.createClient(config.matrix.domain);
matrixClient.loginWithPassword(config.matrix.user, config.matrix.password)
.then((response) => {
registrar.matrix_auth.access_token = response.access_token;
registrar.matrix_auth.user_id = response.user_id;
fs.writeFileSync('matrix_auth.json', JSON.stringify(response, null, 2));
matrix_auth = {
user_id: response.user_id,
access_token: response.access_token,
device_id: response.device_id,
};
localStorage.setItem('matrix_auth', JSON.stringify(response, null, 2));
}).then(() => {
matrixTokenLogin();
})
.catch((e) => {
console.log(e);
});
};
matrixTokenLogin = async () => {
matrixClient = sdk.createClient({
baseUrl: config.matrix.domain,
accessToken: matrix_auth.access_token,
userId: matrix_auth.user_id,
deviceId: matrix_auth.device_id,
sessionStore: new sdk.WebStorageSessionStore(localStorage),
cryptoStore: new LocalStorageCryptoStore(localStorage),
});
matrixClient.initCrypto()
.then(() => {
if(!localStorage.getItem('crypto.device_data'))
return console.log(
'====================================================\n'+
'New OLM Encryption Keys created, please restart ligh7hau5.\n'+
'===================================================='
);
matrixClient.startClient();
});
};
module.exports.matrixTokenLogin = async () => {
matrixClient = sdk.createClient({
baseUrl: registrar.config.matrix.domain,
accessToken: registrar.matrix_auth.access_token,
userId: registrar.matrix_auth.user_id,
timelineSupport: true,
});
matrixClient.startClient();
};
module.exports.matrixTokenLogin = matrixTokenLogin;
module.exports.registerFediverseApp = async () => {
axios.post(`${registrar.config.fediverse.domain}/api/v1/apps`,
axios.post(`${config.fediverse.domain}/api/v1/apps`,
{
client_name: registrar.config.fediverse.client_name,
client_name: config.fediverse.client_name,
redirect_uris: 'urn:ietf:wg:oauth:2.0:oob',
scopes: 'read write follow push',
})
.then((response) => {
axios.post(`${registrar.config.fediverse.domain}/oauth/token`,
axios.post(`${config.fediverse.domain}/oauth/token`,
{
username: registrar.config.fediverse.username,
password: registrar.config.fediverse.password,
username: config.fediverse.username,
password: config.fediverse.password,
client_id: response.data.client_id,
client_secret: response.data.client_secret,
scope: 'read write follow push',
@ -43,8 +60,7 @@ module.exports.registerFediverseApp = async () => {
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
})
.then((tokens) => {
registrar.fediverse_auth.access_token = tokens.data.access_token;
fs.writeFileSync('fediverse_auth.json', JSON.stringify(tokens.data, null, 2));
localStorage.setItem('fediverse_auth', JSON.stringify(tokens.data, null, 2));
})
.catch((e) => {
console.log(e);