peertube-plugin-auth-matrix/main.js

80 lines
1.5 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
})
registerIdAndPassAuth({
authName: 'matrix',
getWeight: () => 500,
login: options => login(peertubeHelpers, settingsManager, options)
})
setWeight(500)
}
async function unregister () {
return
}
module.exports = {
register,
unregister
}
async function login (peertubeHelpers, settingsManager, options) {
const logger = peertubeHelpers.logger
const settings = await settingsManager.getSettings([
'url',
])
// 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){
2024-11-22 12:56:48 +03:00
const r2 = fetch(`https://${settings.url}/_matrix/client/v3/logout`, {
2024-11-22 12:52:48 +03:00
method: "POST",
headers: {
Authorization: r.access_token
}
});
if(r2.statusCode == 200){
return res(
options.id,
`${options.id}@0ut0f.space`
)
}
}
}