refactor(all): prefer token-based authentication
This commit is contained in:
parent
9e17440abc
commit
f69b52261a
24 changed files with 166 additions and 117 deletions
52
auth.js
Normal file
52
auth.js
Normal file
|
@ -0,0 +1,52 @@
|
|||
const sdk = require('matrix-js-sdk');
|
||||
const axios = require('axios');
|
||||
const fs = require('fs');
|
||||
const registrar = require('./registrar.js');
|
||||
|
||||
module.exports.getMatrixToken = async () => {
|
||||
matrixClient = sdk.createClient(registrar.config.matrix.domain);
|
||||
matrixClient.loginWithPassword(registrar.config.matrix.user, registrar.config.matrix.password)
|
||||
.then((response) => {
|
||||
fs.writeFileSync('matrix_auth.json', JSON.stringify(response, null, 2));
|
||||
matrixClient.startClient();
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.matrixTokenLogin = async () => {
|
||||
matrixClient = sdk.createClient({
|
||||
baseUrl: registrar.config.matrix.domain,
|
||||
accessToken: registrar.matrix_auth.access_token,
|
||||
userId: registrar.matrix_auth.user_id,
|
||||
timelineSupport: true,
|
||||
});
|
||||
matrixClient.startClient();
|
||||
};
|
||||
|
||||
module.exports.registerFediverseApp = async () => {
|
||||
axios.post(`${registrar.config.fediverse.domain}/api/v1/apps`,
|
||||
{
|
||||
client_name: registrar.config.fediverse.client_name,
|
||||
redirect_uris: 'urn:ietf:wg:oauth:2.0:oob',
|
||||
scopes: 'read write follow push',
|
||||
})
|
||||
.then((response) => {
|
||||
axios.post(`${registrar.config.fediverse.domain}/oauth/token`,
|
||||
{
|
||||
username: registrar.config.fediverse.username,
|
||||
password: registrar.config.fediverse.password,
|
||||
client_id: response.data.client_id,
|
||||
client_secret: response.data.client_secret,
|
||||
scope: 'read write follow push',
|
||||
grant_type: 'password',
|
||||
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
|
||||
})
|
||||
.then((tokens) => {
|
||||
fs.writeFileSync('fediverse_auth.json', JSON.stringify(tokens.data, null, 2));
|
||||
})
|
||||
.catch((e) => {
|
||||
console.log(e);
|
||||
});
|
||||
}).catch((e) => {
|
||||
console.log(e);
|
||||
});
|
||||
};
|
|
@ -4,7 +4,7 @@ exports.runQuery = function (matrixClient, room, registrar) {
|
|||
axios({
|
||||
method: 'POST',
|
||||
url: `${registrar.config.fediverse.domain}/api/v1/statuses`,
|
||||
headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` },
|
||||
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
|
||||
data: { status: `@10grans@fedi.cc beg` },
|
||||
}).then((response) => {
|
||||
matrixClient.sendHtmlNotice(room.roomId,
|
||||
|
|
|
@ -4,7 +4,7 @@ exports.runQuery = function (matrixClient, room, userInput, registrar) {
|
|||
axios({
|
||||
method: 'POST',
|
||||
url: `${registrar.config.fediverse.domain}/api/v1/statuses/${userInput}/unfavourite`,
|
||||
headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` },
|
||||
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
|
||||
}).then((response) => {
|
||||
matrixClient.sendHtmlNotice(room.roomId,
|
||||
'',
|
||||
|
|
|
@ -4,7 +4,7 @@ exports.runQuery = function (matrixClient, room, userInput, registrar) {
|
|||
axios({
|
||||
method: 'POST',
|
||||
url: `${registrar.config.fediverse.domain}/api/v1/statuses/${userInput}/favourite`,
|
||||
headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` },
|
||||
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
|
||||
}).then((response) => {
|
||||
matrixClient.sendHtmlNotice(room.roomId,
|
||||
'',
|
||||
|
|
|
@ -4,7 +4,7 @@ exports.runQuery = function (matrixClient, room, userInput, registrar) {
|
|||
axios({
|
||||
method: 'POST',
|
||||
url: `${registrar.config.fediverse.domain}/api/v1/statuses/${userInput}/reblog`,
|
||||
headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` },
|
||||
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
|
||||
}).then((response) => {
|
||||
matrixClient.sendHtmlNotice(room.roomId,
|
||||
'',
|
||||
|
|
|
@ -6,7 +6,7 @@ exports.runQuery = function (matrixClient, room, registrar) {
|
|||
axios({
|
||||
method: 'GET',
|
||||
url: `${registrar.config.fediverse.domain}/api/v1/timelines/home`,
|
||||
headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` },
|
||||
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
|
||||
}).then((events) => {
|
||||
const event = fs.readFileSync('timeline.json', 'utf8');
|
||||
fs.writeFileSync('timeline.json', events.data[0].created_at, 'utf8');
|
||||
|
|
|
@ -5,7 +5,7 @@ exports.runQuery = function (matrixClient, room, userInput, registrar) {
|
|||
axios({
|
||||
method: 'POST',
|
||||
url: `${registrar.config.fediverse.domain}/api/v1/accounts/${findUID.data.id}/follow`,
|
||||
headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` },
|
||||
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
|
||||
})
|
||||
.then((response) => {
|
||||
matrixClient.sendHtmlNotice(room.roomId,
|
||||
|
|
|
@ -26,7 +26,7 @@ const mediaDownload = async (url, { whitelist, blacklist }) => {
|
|||
};
|
||||
};
|
||||
|
||||
const mediaUpload = async ({ domain, token }, { data, filename, mimetype }) => {
|
||||
const mediaUpload = async ({ domain }, { data, filename, mimetype }, registrar) => {
|
||||
const form = new FormData();
|
||||
form.append('file', data, {
|
||||
filename: filename || 'upload',
|
||||
|
@ -35,7 +35,7 @@ const mediaUpload = async ({ domain, token }, { data, filename, mimetype }) => {
|
|||
const upload = await axios({
|
||||
method: 'POST',
|
||||
url: `${domain}/api/v1/media`,
|
||||
headers: form.getHeaders({ Authorization: `Bearer ${token}` }),
|
||||
headers: form.getHeaders({ Authorization: `Bearer ${registrar.fediverse_auth.access_token}` }),
|
||||
data: form,
|
||||
});
|
||||
if(upload.statusText !== 'OK') throw upload;
|
||||
|
@ -47,12 +47,12 @@ const run = async (matrixClient, { roomId }, content, replyId, mediaURL, subject
|
|||
const fediverse = registrar.config.fediverse;
|
||||
if(mediaURL) {
|
||||
const media = await mediaDownload(mediaURL, registrar.config.fediverse.mimetypes);
|
||||
mediaId = await mediaUpload(fediverse, media);
|
||||
mediaId = await mediaUpload(fediverse, media, registrar);
|
||||
}
|
||||
const response = await axios({
|
||||
method: 'POST',
|
||||
url: `${fediverse.domain}/api/v1/statuses`,
|
||||
headers: { Authorization: `Bearer ${fediverse.token}`, 'Content-Type': 'application/x-www-form-urlencoded' },
|
||||
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}`, 'Content-Type': 'application/x-www-form-urlencoded' },
|
||||
data : qs.stringify({
|
||||
status: content,
|
||||
content_type: `text/markdown`,
|
||||
|
|
|
@ -4,7 +4,7 @@ exports.runQuery = function (matrixClient, room, userInput, registrar) {
|
|||
axios({
|
||||
method: 'POST',
|
||||
url: `${registrar.config.fediverse.domain}/api/v1/statuses`,
|
||||
headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` },
|
||||
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
|
||||
data: {
|
||||
status: `@mordekai ${userInput}`,
|
||||
content_type: `text/markdown`,
|
||||
|
|
|
@ -6,7 +6,7 @@ exports.runQuery = function (matrixClient, room, registrar) {
|
|||
axios({
|
||||
method: 'GET',
|
||||
url: `${registrar.config.fediverse.domain}/api/v1/notifications`,
|
||||
headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` },
|
||||
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
|
||||
}).then((notifications) => {
|
||||
const event = fs.readFileSync('notification.json', 'utf8');
|
||||
fs.writeFileSync('notification.json', notifications.data[0].created_at, 'utf8');
|
||||
|
|
|
@ -4,7 +4,7 @@ exports.runQuery = function (matrixClient, room, userInput, registrar) {
|
|||
axios({
|
||||
method: 'POST',
|
||||
url: `${registrar.config.fediverse.domain}/api/v1/statuses/${userInput}/pin`,
|
||||
headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` },
|
||||
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
|
||||
}).then((response) => {
|
||||
matrixClient.sendHtmlNotice(room.roomId,
|
||||
'',
|
||||
|
|
|
@ -4,7 +4,7 @@ exports.runQuery = function (matrixClient, room, userInput, registrar) {
|
|||
axios({
|
||||
method: 'POST',
|
||||
url: `${registrar.config.fediverse.domain}/api/v1/statuses`,
|
||||
headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` },
|
||||
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
|
||||
data: { status: userInput, content_type: `text/markdown` },
|
||||
}).then((response) => {
|
||||
matrixClient.sendHtmlNotice(room.roomId,
|
||||
|
|
|
@ -4,7 +4,7 @@ exports.runQuery = function (matrixClient, room, userInput, registrar) {
|
|||
axios({
|
||||
method: 'DELETE',
|
||||
url: `${registrar.config.fediverse.domain}/api/v1/statuses/${userInput}`,
|
||||
headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` },
|
||||
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
|
||||
}).then((response) => {
|
||||
matrixClient.sendHtmlNotice(room.roomId,
|
||||
'',
|
||||
|
|
|
@ -4,7 +4,7 @@ exports.runQuery = function (matrixClient, room, address, flaggedInput, registra
|
|||
axios({
|
||||
method: 'POST',
|
||||
url: `${registrar.config.fediverse.domain}/api/v1/statuses`,
|
||||
headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` },
|
||||
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
|
||||
data: { status: flaggedInput, in_reply_to_id: address, content_type: `text/markdown` },
|
||||
}).then((response) => {
|
||||
matrixClient.sendHtmlNotice(room.roomId,
|
||||
|
|
|
@ -4,7 +4,7 @@ exports.runQuery = function (matrixClient, room, userInput, registrar) {
|
|||
axios({
|
||||
method: 'GET',
|
||||
url: `${registrar.config.fediverse.domain}/api/v1/statuses/${userInput}`,
|
||||
headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` },
|
||||
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
|
||||
}).then((response) => {
|
||||
matrixClient.sendHtmlNotice(room.roomId,
|
||||
'',
|
||||
|
|
|
@ -4,7 +4,7 @@ exports.runQuery = function (matrixClient, room, address, flaggedInput, registra
|
|||
axios({
|
||||
method: 'POST',
|
||||
url: `${registrar.config.fediverse.domain}/api/v1/statuses`,
|
||||
headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` },
|
||||
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
|
||||
data: { status: `@10grans@fedi.cc tip `+ flaggedInput + ` to `+address },
|
||||
}).then((response) => {
|
||||
matrixClient.sendHtmlNotice(room.roomId,
|
||||
|
|
|
@ -5,7 +5,7 @@ exports.runQuery = function (matrixClient, room, userInput, registrar) {
|
|||
axios({
|
||||
method: 'POST',
|
||||
url: `${registrar.config.fediverse.domain}/api/v1/accounts/${findUID.data.id}/unfollow`,
|
||||
headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` },
|
||||
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
|
||||
})
|
||||
.then((response) => {
|
||||
matrixClient.sendHtmlNotice(room.roomId,
|
||||
|
|
|
@ -4,7 +4,7 @@ exports.runQuery = function (matrixClient, room, userInput, registrar) {
|
|||
axios({
|
||||
method: 'POST',
|
||||
url: `${registrar.config.fediverse.domain}/api/v1/statuses/${userInput}/unpin`,
|
||||
headers: { Authorization: `Bearer ${registrar.config.fediverse.token}` },
|
||||
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
|
||||
}).then((response) => {
|
||||
matrixClient.sendHtmlNotice(room.roomId,
|
||||
'',
|
||||
|
|
|
@ -7,7 +7,9 @@ module.exports = {
|
|||
},
|
||||
fediverse: {
|
||||
domain: 'https://your_federation.com',
|
||||
token: 'your_federation_token',
|
||||
username: '',
|
||||
password: '',
|
||||
client_name: '',
|
||||
subject: '',
|
||||
mimetypes: {
|
||||
whitelist: [],
|
||||
|
|
9
fediverse_auth.json
Normal file
9
fediverse_auth.json
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"access_token": "",
|
||||
"created_at": 0,
|
||||
"expires_in": 0,
|
||||
"me": "",
|
||||
"refresh_token": "",
|
||||
"scope": "",
|
||||
"token_type": ""
|
||||
}
|
38
main.js
38
main.js
|
@ -1,28 +1,10 @@
|
|||
const sdk = require('matrix-js-sdk');
|
||||
const axios = require('axios');
|
||||
const registrar = require('./registrar.js');
|
||||
const auth = require('./auth.js');
|
||||
|
||||
const auth = {
|
||||
type: 'm.login.password',
|
||||
user: registrar.config.matrix.user,
|
||||
password: registrar.config.matrix.password,
|
||||
};
|
||||
registrar.matrix_auth.access_token ? auth.matrixTokenLogin() : auth.getMatrixToken();
|
||||
if (!registrar.fediverse_auth.access_token && registrar.config.fediverse.username) auth.registerFediverseApp();
|
||||
|
||||
axios.post(`${registrar.config.matrix.domain}/_matrix/client/r0/login`, auth).then((response) => {
|
||||
CreateClient(response.data.access_token, response.data.user_id);
|
||||
}).catch((e) => {
|
||||
console.log(e);
|
||||
});
|
||||
|
||||
let CreateClient = (token, user_id) => {
|
||||
const matrixClient = sdk.createClient({
|
||||
baseUrl: registrar.config.matrix.domain,
|
||||
accessToken: token,
|
||||
userId: user_id,
|
||||
timelineSupport: true,
|
||||
});
|
||||
|
||||
matrixClient.on('RoomMember.membership', (event, member) => {
|
||||
matrixClient.on('RoomMember.membership', (event, member) => {
|
||||
if (member.membership === 'invite' && member.userId === matrixClient.credentials.userId) {
|
||||
matrixClient.joinRoom(member.roomId).done(() => {
|
||||
console.log('Auto-joined %s', member.roomId);
|
||||
|
@ -33,9 +15,9 @@ let CreateClient = (token, user_id) => {
|
|||
console.log('Kicked %s', member.roomId);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
matrixClient.on('Room.timeline', (event, room, toStartOfTimeline) => {
|
||||
matrixClient.on('Room.timeline', (event, room, toStartOfTimeline) => {
|
||||
if (toStartOfTimeline) return;
|
||||
if (event.getType() !== 'm.room.message') return;
|
||||
if (event.getSender() === matrixClient.credentials.userId) return;
|
||||
|
@ -63,7 +45,7 @@ let CreateClient = (token, user_id) => {
|
|||
args.push(matrixClient, room, userInput, !!~command.indexOf('re'), registrar);
|
||||
command = 'archive';
|
||||
break;
|
||||
case 'plemara': case 'reply': case 'media': case 'mediareply':
|
||||
case 'post': case 'reply': case 'media': case 'mediareply':
|
||||
case 'random': case 'randomreply': case 'randommedia': case 'randommediareply':
|
||||
args.push(matrixClient, room, userInput, registrar, {
|
||||
isReply: !!~command.indexOf('reply'),
|
||||
|
@ -88,8 +70,4 @@ let CreateClient = (token, user_id) => {
|
|||
|
||||
registrar[command] && registrar[command].runQuery.apply(null, args);
|
||||
}
|
||||
});
|
||||
|
||||
matrixClient.startClient();
|
||||
module.exports = matrixClient;
|
||||
};
|
||||
});
|
||||
|
|
6
matrix_auth.json
Normal file
6
matrix_auth.json
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"user_id": "",
|
||||
"access_token": "",
|
||||
"home_server": "",
|
||||
"device_id": ""
|
||||
}
|
10
package.json
10
package.json
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "plemara",
|
||||
"version": "0.3.0",
|
||||
"name": "ligh7hau5",
|
||||
"version": "0.4.0",
|
||||
"description": "A Matrix to Fediverse client",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
|
@ -9,14 +9,14 @@
|
|||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/vulet/plemara.git"
|
||||
"url": "git+https://github.com/vulet/ligh7hau5.git"
|
||||
},
|
||||
"author": "vul",
|
||||
"license": "AGPL-3.0-only",
|
||||
"bugs": {
|
||||
"url": "https://github.com/vulet/plemara/issues"
|
||||
"url": "https://github.com/vulet/lighthau5/issues"
|
||||
},
|
||||
"homepage": "https://github.com/vulet/plemara#readme",
|
||||
"homepage": "https://github.com/vulet/lighthau5#readme",
|
||||
"dependencies": {
|
||||
"axios": "^0.19.2",
|
||||
"file-system": "^2.2.2",
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
module.exports = {
|
||||
config: require('./config.js'),
|
||||
fediverse_auth: require('./fediverse_auth.json'),
|
||||
matrix_auth: require('./matrix_auth.json'),
|
||||
archive: require('./commands/archive.js'),
|
||||
invidious: require('./commands/invidious.js'),
|
||||
nitter: require('./commands/nitter.js'),
|
||||
|
|
Loading…
Reference in a new issue