2021-01-17 12:58:02 +03:00
|
|
|
const sdk = require('matrix-js-sdk');
|
|
|
|
const axios = require('axios');
|
|
|
|
const fs = require('fs');
|
|
|
|
const registrar = require('./registrar.js');
|
|
|
|
|
|
|
|
module.exports.getMatrixToken = async () => {
|
|
|
|
matrixClient = sdk.createClient(registrar.config.matrix.domain);
|
|
|
|
matrixClient.loginWithPassword(registrar.config.matrix.user, registrar.config.matrix.password)
|
|
|
|
.then((response) => {
|
2021-01-17 17:29:19 +03:00
|
|
|
registrar.matrix_auth.access_token = response.access_token;
|
|
|
|
registrar.matrix_auth.user_id = response.user_id;
|
2021-01-17 12:58:02 +03:00
|
|
|
fs.writeFileSync('matrix_auth.json', JSON.stringify(response, null, 2));
|
|
|
|
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.registerFediverseApp = async () => {
|
|
|
|
axios.post(`${registrar.config.fediverse.domain}/api/v1/apps`,
|
|
|
|
{
|
|
|
|
client_name: registrar.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`,
|
|
|
|
{
|
|
|
|
username: registrar.config.fediverse.username,
|
|
|
|
password: registrar.config.fediverse.password,
|
|
|
|
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-01-17 17:29:19 +03:00
|
|
|
registrar.fediverse_auth.access_token = tokens.data.access_token;
|
2021-01-17 12:58:02 +03:00
|
|
|
fs.writeFileSync('fediverse_auth.json', JSON.stringify(tokens.data, null, 2));
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.log(e);
|
|
|
|
});
|
|
|
|
}).catch((e) => {
|
|
|
|
console.log(e);
|
|
|
|
});
|
|
|
|
};
|