ligh7hau5/auth.js

113 lines
4.1 KiB
JavaScript
Raw Permalink Normal View History

const { LocalStorageCryptoStore } = require('matrix-js-sdk/lib/crypto/store/localStorage-crypto-store');
const matrixTokenLogin = async () => {
matrixClient = sdk.createClient({
baseUrl: config.matrix.domain,
accessToken: matrix.auth.access_token,
userId: matrix.auth.user_id,
deviceId: matrix.auth.device_id,
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.setGlobalErrorOnUnknownDevices(config.matrix.manualVerify);
matrixClient.startClient();
});
};
module.exports.matrixTokenLogin = matrixTokenLogin;
module.exports.getMatrixToken = async () => {
matrixClient = sdk.createClient({ baseUrl: config.matrix.domain });
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`,
{
client_name: config.fediverse.client_name,
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)
}).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"
};
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;