2021-02-01 11:58:59 +03:00
|
|
|
const { LocalStorageCryptoStore } = require('matrix-js-sdk/lib/crypto/store/localStorage-crypto-store');
|
2021-01-17 12:58:02 +03:00
|
|
|
|
2021-02-14 10:57:35 +03:00
|
|
|
const matrixTokenLogin = async () => {
|
2021-01-17 12:58:02 +03:00
|
|
|
matrixClient = sdk.createClient({
|
2021-02-01 11:58:59 +03:00
|
|
|
baseUrl: config.matrix.domain,
|
2021-02-14 10:57:35 +03:00
|
|
|
accessToken: matrix.auth.access_token,
|
|
|
|
userId: matrix.auth.user_id,
|
|
|
|
deviceId: matrix.auth.device_id,
|
2021-02-01 11:58:59 +03:00
|
|
|
cryptoStore: new LocalStorageCryptoStore(localStorage),
|
2021-01-17 12:58:02 +03:00
|
|
|
});
|
2021-02-01 11:58:59 +03:00
|
|
|
matrixClient.initCrypto()
|
2021-02-14 10:57:35 +03:00
|
|
|
.then(() => {
|
|
|
|
if (!localStorage.getItem('crypto.device_data')) {
|
2021-02-01 11:58:59 +03:00
|
|
|
return console.log(
|
2021-02-14 10:57:35 +03:00
|
|
|
'====================================================\n'
|
|
|
|
+ 'New OLM Encryption Keys created, please restart ligh7hau5.\n'
|
|
|
|
+ '====================================================',
|
2021-02-01 11:58:59 +03:00
|
|
|
);
|
2021-02-14 10:57:35 +03:00
|
|
|
}
|
2021-08-06 15:13:36 +03:00
|
|
|
matrixClient.setGlobalErrorOnUnknownDevices(config.matrix.manualVerify);
|
2021-02-01 11:58:59 +03:00
|
|
|
matrixClient.startClient();
|
|
|
|
});
|
2021-01-17 12:58:02 +03:00
|
|
|
};
|
|
|
|
|
2021-02-01 11:58:59 +03:00
|
|
|
module.exports.matrixTokenLogin = matrixTokenLogin;
|
|
|
|
|
2021-02-14 10:57:35 +03:00
|
|
|
module.exports.getMatrixToken = async () => {
|
2023-08-23 10:25:48 +03:00
|
|
|
matrixClient = sdk.createClient({ baseUrl: config.matrix.domain });
|
2021-02-14 10:57:35 +03:00
|
|
|
matrixClient.loginWithPassword(config.matrix.user, config.matrix.password)
|
|
|
|
.then((response) => {
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-01-17 12:58:02 +03:00
|
|
|
module.exports.registerFediverseApp = async () => {
|
2021-02-01 11:58:59 +03:00
|
|
|
axios.post(`${config.fediverse.domain}/api/v1/apps`,
|
2021-01-17 12:58:02 +03:00
|
|
|
{
|
2021-02-01 11:58:59 +03:00
|
|
|
client_name: config.fediverse.client_name,
|
2021-01-17 12:58:02 +03:00
|
|
|
redirect_uris: 'urn:ietf:wg:oauth:2.0:oob',
|
|
|
|
scopes: 'read write follow push',
|
|
|
|
})
|
|
|
|
.then((response) => {
|
2021-02-01 11:58:59 +03:00
|
|
|
axios.post(`${config.fediverse.domain}/oauth/token`,
|
2021-01-17 12:58:02 +03:00
|
|
|
{
|
2021-02-01 11:58:59 +03:00
|
|
|
username: config.fediverse.username,
|
|
|
|
password: config.fediverse.password,
|
2021-01-17 12:58:02 +03:00
|
|
|
client_id: response.data.client_id,
|
|
|
|
client_secret: response.data.client_secret,
|
|
|
|
scope: 'read write follow push',
|
|
|
|
grant_type: 'password',
|
|
|
|
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
|
|
|
|
})
|
|
|
|
.then((tokens) => {
|
2021-02-01 11:58:59 +03:00
|
|
|
localStorage.setItem('fediverse_auth', JSON.stringify(tokens.data, null, 2));
|
2021-01-17 12:58:02 +03:00
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.log(e);
|
|
|
|
});
|
|
|
|
}).catch((e) => {
|
|
|
|
console.log(e);
|
|
|
|
});
|
|
|
|
};
|