2019-09-06 12:25:50 +02:00
|
|
|
const config = require('./config');
|
|
|
|
const ssbClient = require('ssb-client');
|
|
|
|
const pull = require('pull-stream');
|
2019-09-24 15:08:03 +02:00
|
|
|
const ssb_bridge = require('./save_to_ssb');
|
2019-09-06 12:25:50 +02:00
|
|
|
|
|
|
|
DOMAIN = config.DOMAIN;
|
|
|
|
|
2019-09-24 15:08:03 +02:00
|
|
|
let chars_to_encode = "_!*'();:@&=+$,/?#[]"; // basically anything covered by %-encoding plus underscore
|
|
|
|
|
|
|
|
function encode_webfinger_name(name) {
|
|
|
|
for (let i in chars_to_encode) {
|
|
|
|
name = name.replace(chars_to_encode[i], "_" + chars_to_encode[i].charCodeAt(0).toString(16) + "_");
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
function decode_webfinger_name(name) {
|
|
|
|
for (let i in chars_to_encode) {
|
|
|
|
name = name.replace("_" + chars_to_encode[i].charCodeAt(0).toString(16) + "_", chars_to_encode[i]);
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
async function check_if_in_friends(name) {
|
2019-09-06 12:25:50 +02:00
|
|
|
let result = false;
|
2019-09-24 15:08:03 +02:00
|
|
|
name = decode_webfinger_name(name);
|
2019-09-06 12:25:50 +02:00
|
|
|
let out = new Promise((resolve, reject) => {
|
|
|
|
ssbClient((err, sbot) => {
|
|
|
|
if (err) reject(err);
|
|
|
|
|
|
|
|
pull(
|
|
|
|
sbot.friends.createFriendStream(),
|
|
|
|
pull.collect((err, array) => {
|
|
|
|
array.forEach(function (actor) {
|
|
|
|
let short_actor = (actor.substr(1));
|
|
|
|
if (short_actor === name) {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
sbot.close();
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return await out;
|
|
|
|
}
|
|
|
|
|
2019-09-24 15:08:03 +02:00
|
|
|
async function get_friends() {
|
|
|
|
let out = new Promise((resolve, reject) => {
|
|
|
|
ssbClient((err, sbot) => {
|
|
|
|
if (err) reject(err);
|
|
|
|
|
|
|
|
pull(
|
|
|
|
sbot.friends.createFriendStream(),
|
|
|
|
pull.collect((err, array) => {
|
|
|
|
sbot.close();
|
|
|
|
resolve(array);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return await out;
|
|
|
|
}
|
|
|
|
|
2019-09-06 12:25:50 +02:00
|
|
|
function get_webfinger(req, res) {
|
|
|
|
let resource = req.query.resource;
|
|
|
|
if (!resource || !resource.includes('acct:')) {
|
|
|
|
return res.status(400).send('Bad request. Please make sure "acct:USER@DOMAIN" is what you are sending as the "resource" query parameter.');
|
2019-09-24 15:08:03 +02:00
|
|
|
} else {
|
|
|
|
let name = resource.replace('acct:', '');
|
|
|
|
name = name.substr(0, name.indexOf('@'));
|
|
|
|
let encoded_name = encodeURIComponent(name);
|
2019-09-06 12:25:50 +02:00
|
|
|
|
2019-09-09 16:56:34 +02:00
|
|
|
let p = check_if_in_friends(name + "=.ed25519");
|
2019-09-06 12:25:50 +02:00
|
|
|
|
|
|
|
p.then((result) => {
|
|
|
|
if (result) {
|
|
|
|
res.json(
|
|
|
|
{
|
2019-09-24 15:08:03 +02:00
|
|
|
'subject': `acct:${encoded_name}@${DOMAIN}`,
|
2019-09-06 12:25:50 +02:00
|
|
|
links: [
|
|
|
|
{
|
|
|
|
rel: 'self',
|
2019-09-09 16:56:34 +02:00
|
|
|
type: 'application/activity+json',
|
2019-09-24 15:08:03 +02:00
|
|
|
href: `https://${DOMAIN}/u/${encoded_name}`
|
2019-09-06 12:25:50 +02:00
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
2019-09-24 15:08:03 +02:00
|
|
|
return res.status(404).send(`No record found for ${encoded_name}.`);
|
2019-09-06 12:25:50 +02:00
|
|
|
}
|
|
|
|
}).catch((err) => {
|
|
|
|
return res.status(500).send(`An error occured: ${err}.`);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 15:08:03 +02:00
|
|
|
function get_user(req, res) {
|
2019-09-06 12:25:50 +02:00
|
|
|
let name = req.params.name;
|
|
|
|
if (!name) {
|
|
|
|
return res.status(400).send('Bad request.');
|
2019-09-24 15:08:03 +02:00
|
|
|
} else {
|
2019-09-06 12:25:50 +02:00
|
|
|
|
2019-09-09 16:56:34 +02:00
|
|
|
let p = check_if_in_friends(name + "=.ed25519");
|
2019-09-06 12:25:50 +02:00
|
|
|
|
|
|
|
p.then((result) => {
|
|
|
|
if (result) {
|
|
|
|
|
|
|
|
res.json(
|
|
|
|
{
|
|
|
|
'@context': [
|
|
|
|
'https://www.w3.org/ns/activitystreams',
|
|
|
|
'https://w3id.org/security/v1'
|
|
|
|
],
|
|
|
|
|
|
|
|
id: `https://${DOMAIN}/u/${name}`,
|
|
|
|
type: 'Person',
|
2019-09-24 15:08:03 +02:00
|
|
|
preferredUsername: 'TESTPERSON', //todo: read from latest about message
|
|
|
|
// inbox: `https://${DOMAIN}/u/${name}/inbox`,
|
|
|
|
inbox: `https://${DOMAIN}/inbox`,
|
2019-09-06 12:25:50 +02:00
|
|
|
|
|
|
|
publicKey: {
|
|
|
|
id: `https://${DOMAIN}/u/${name}#main-key`,
|
|
|
|
owner: `https://${DOMAIN}/u/${name}`,
|
|
|
|
publicKeyPem: '-----BEGIN PUBLIC KEY-----\n' +
|
|
|
|
'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsT4MEtffaV0uFr9jgSnx\n' +
|
|
|
|
'kb1+MMd1/jGYBugJ4jkHc9rvQRJLA2C8O2LbrdNb/00TRHh0pkR7AJW7DWMhIF/P\n' +
|
|
|
|
'WmsHcdouAUhovAVO+4yrRK5fMA96JP6k2YwqJK+yjK4SMm9iwvcdBlrkZif0KWvA\n' +
|
|
|
|
'Qf4eU24n64NSEdVu48cgZwMvQeYKaAtf2LIhXYOE4pA16C05z3BAar+9m2e1yZMG\n' +
|
|
|
|
'+JzhoywmpqlrB+XK55wjAIhvwVGgOMtUg5FbHU5sH7wZv7H945t40x7HjNCBxU6d\n' +
|
|
|
|
'yrF7Bl6nMg+ifT5a6SzPSJ0f3g99AyfMVL5fnhSodjpsnjohfIsx9Vzd4oO1JhDx\n' +
|
|
|
|
'SwIDAQAB\n' +
|
|
|
|
'-----END PUBLIC KEY-----'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2019-09-24 15:08:03 +02:00
|
|
|
} else {
|
2019-09-06 12:25:50 +02:00
|
|
|
return res.status(404).send(`No record found for ${name}.`);
|
|
|
|
}
|
|
|
|
}).catch((err) => {
|
|
|
|
return res.status(500).send(`An error occured: ${err}.`);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 15:08:03 +02:00
|
|
|
function get_users(req, res) {
|
|
|
|
let p = get_friends();
|
|
|
|
|
|
|
|
p.then((result) => {
|
|
|
|
if (result) {
|
|
|
|
let out = {};
|
|
|
|
for (let i in result) {
|
|
|
|
out[i] = encode_webfinger_name(result[i].substr(1).replace("=.ed25519", ""));
|
|
|
|
}
|
|
|
|
res.json(out);
|
|
|
|
} else {
|
|
|
|
return res.status(404).send(`No record found for ${name}.`);
|
|
|
|
}
|
|
|
|
}).catch((err) => {
|
|
|
|
return res.status(500).send(`An error occured: ${err}.`);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function post_inbox(req, res) {
|
|
|
|
console.log("Saved activity to ssb log.");
|
|
|
|
try {
|
|
|
|
let in_activity = JSON.parse(req.body.toString());
|
|
|
|
ssb_bridge.save(in_activity);
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
return res.status(200).send('ayy\n');
|
|
|
|
}
|
|
|
|
|
2019-09-06 12:25:50 +02:00
|
|
|
module.exports = {
|
|
|
|
get_user,
|
2019-09-24 15:08:03 +02:00
|
|
|
get_webfinger,
|
|
|
|
get_users,
|
|
|
|
post_inbox,
|
2019-09-06 12:25:50 +02:00
|
|
|
};
|