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 }) 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' ]) // 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`, { method: "POST", headers: { Authorization: `Bearer ${r.access_token}` } }); 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()}`); } } }