peertube-plugin-auth-matrix/main.js

85 lines
1.7 KiB
JavaScript
Raw Normal View History

2024-11-22 12:52:48 +03:00
const fs = require('fs/promises')
async function register ({
registerIdAndPassAuth,
registerSetting,
settingsManager,
peertubeHelpers
}) {
registerSetting({
name: 'url',
label: 'Domain',
type: 'input',
descriptionHTML: 'domain of matrix server',
private: true
})
registerSetting({
name: 'mail',
label: 'Mail',
type: 'input',
descriptionHTML: 'mail domain for each matrix user',
private: true
})
2024-11-22 12:52:48 +03:00
registerIdAndPassAuth({
authName: 'matrix',
getWeight: () => 500,
login: options => login(peertubeHelpers, settingsManager, options)
})
}
async function unregister () {
return
}
module.exports = {
register,
unregister
}
async function login (peertubeHelpers, settingsManager, options) {
const logger = peertubeHelpers.logger
const settings = await settingsManager.getSettings([
'url',
'mail'
2024-11-22 12:52:48 +03:00
])
// options.id, options.password
const r = await fetch(`https://${settings.url}/_matrix/client/v3/login`, {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
type: "m.login.password",
identifier: {
type: "m.id.user",
user: options.id,
},
password: options.password,
initial_display_name: "PeerTube"
})}).then(f => f.json())
if(r.access_token){
const r2 = await fetch(`https://${settings.url}/_matrix/client/v3/logout`, {
2024-11-22 12:52:48 +03:00
method: "POST",
headers: {
Authorization: `Bearer ${r.access_token}`
2024-11-22 12:52:48 +03:00
}
});
if(r2.status == 200){
return {
username: options.id,
email: `${options.id}@${settings.mail}`
}
}else{
logger.warn(`Logout of session failed with code ${r2.status}: ${await r2.text()}`);
2024-11-22 12:52:48 +03:00
}
}
}