2021-02-01 11:58:59 +03:00
|
|
|
const { LocalStorageCryptoStore } = require('matrix-js-sdk/lib/crypto/store/localStorage-crypto-store');
|
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);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-09-29 20:53:07 +03:00
|
|
|
const getFediverseLink = (domain,roomId) => {
|
|
|
|
let apps = {}
|
|
|
|
apps = JSON.parse(localStorage.getItem("apps"));
|
|
|
|
if(!apps[domain]){
|
|
|
|
axios.post(`https://${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) => {
|
2024-09-29 20:53:07 +03:00
|
|
|
console.log(response.data)
|
|
|
|
if(!response.data.client_id || !response.data.client_secret) return false;
|
|
|
|
apps[domain] = {
|
|
|
|
client_id: response.data.client_id,
|
|
|
|
client_secret: response.data.client_secret
|
|
|
|
}
|
|
|
|
localStorage.setItem("apps",JSON.stringify(apps))
|
|
|
|
matrixClient.sendHtmlNotice(roomId,"Приложение зарегистрировано. Введите команду еще раз для создания ссылки")
|
|
|
|
// return getFediverseLink(domain)
|
2021-01-17 12:58:02 +03:00
|
|
|
}).catch((e) => {
|
|
|
|
console.log(e);
|
|
|
|
});
|
2024-09-29 20:53:07 +03:00
|
|
|
}else{
|
|
|
|
const app = apps[domain]
|
|
|
|
const uri = "urn:ietf:wg:oauth:2.0:oob".replace(/:/g,"%3A")
|
|
|
|
const scope = "read write follow push".replace(/ /g,"%20")
|
|
|
|
return `https://${domain}/oauth/authorize?client_id=${app.client_id}&response_type=code&redirect_uri=${uri}&scope=${scope}`
|
|
|
|
}
|
|
|
|
return "nothing"
|
2021-01-17 12:58:02 +03:00
|
|
|
};
|
2024-09-29 20:53:07 +03:00
|
|
|
|
|
|
|
const obtainAccessToken = (domain,code,event) => {
|
|
|
|
const apps = JSON.parse(localStorage.getItem("apps"));
|
|
|
|
console.log(domain,code)
|
|
|
|
const app = apps[domain];
|
|
|
|
axios.post(`https://${domain}/oauth/token`, {
|
|
|
|
client_id: app.client_id,
|
|
|
|
client_secret: app.client_secret,
|
|
|
|
redirect_uri: "urn:ietf:wg:oauth:2.0:oob",
|
|
|
|
grant_type: "authorization_code",
|
|
|
|
code: code,
|
|
|
|
scopes: "read write follow push"
|
|
|
|
}).then(response => {
|
|
|
|
if(!response.data.access_token) return console.log(response.data)
|
|
|
|
fediverse.auth[event.getSender()] = {
|
|
|
|
domain: domain,
|
|
|
|
access_token: response.data.access_token
|
|
|
|
}
|
|
|
|
localStorage.setItem("fediverse_auth", JSON.stringify(fediverse.auth))
|
|
|
|
getFediverseUserInfo(event)
|
|
|
|
}).catch(e => console.error(e))
|
|
|
|
}
|
|
|
|
|
|
|
|
const getFediverseUserInfo = (event) => {
|
|
|
|
const user = event.getSender()
|
|
|
|
axios({
|
|
|
|
method: "GET",
|
|
|
|
url: `https://${fediverse.auth[user].domain}/api/v1/accounts/verify_credentials`,
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${fediverse.auth[user].access_token}`
|
|
|
|
}
|
|
|
|
}).then(response => {
|
|
|
|
if(response.data.username){
|
|
|
|
matrixClient.sendHtmlNotice(event.getRoomId(), `Успешный вход в аккаунт ${response.data.display_name || response.data.username} (@${response.data.username}@${fediverse.auth[user].domain})`)
|
|
|
|
}else{
|
|
|
|
console.log(response.data)
|
|
|
|
}
|
|
|
|
}).catch(e => console.error(e))
|
|
|
|
}
|
|
|
|
module.exports.getFediverseLink = getFediverseLink;
|
|
|
|
module.exports.obtainAccessToken = obtainAccessToken;
|