-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
151 lines (130 loc) · 5.1 KB
/
Copy pathserver.js
File metadata and controls
151 lines (130 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const config = require('./config');
const ssb_bridge = require('./ssb_bridge');
DOMAIN = config.DOMAIN;
let chars_to_encode = "_!*'();:@&=+$,/?#[]"; // basically anything covered by %-encoding plus underscore
function decode_webfinger_name(name) {
name = name + "=.ed25519";
for (let i in chars_to_encode) {
name = name.replace("_" + chars_to_encode[i].charCodeAt(0).toString(16) + "_", chars_to_encode[i]);
}
return name
}
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 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.');
} else {
let name = resource.replace('acct:', '');
name = name.substr(0, name.indexOf('@'));
let encoded_name = encodeURIComponent(name);
let p = ssb_bridge.check_if_in_friends(decode_webfinger_name(name));
p.then((result) => {
if (result) {
res.json(
{
'subject': `acct:${encoded_name}@${DOMAIN}`,
links: [
{
rel: 'self',
type: 'application/activity+json',
href: `https://${DOMAIN}/u/${encoded_name}`
}
],
}
);
} else {
return res.status(404).send(`No record found for ${encoded_name}.`);
}
}).catch((err) => {
return res.status(500).send(`An error occured: ${err}.`);
})
}
}
function get_user(req, res) {
let name = req.params.name;
if (!name) {
return res.status(400).send('Bad request.');
} else {
let p = ssb_bridge.check_if_in_friends(decode_webfinger_name(name));
p.then((result) => {
if (result) {
let uname_p = ssb_bridge.get_username(decode_webfinger_name(name));
let uname = '';
uname_p.then((uname_res) => {
uname = uname_res;
});
if (uname === '') {
uname = name;
}
res.json(
{
'@context': [
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1'
],
id: `https://${DOMAIN}/u/${name}`,
type: 'Person',
preferredUsername: uname,
name: uname,
// inbox: `https://${DOMAIN}/u/${name}/inbox`,
inbox: `https://${DOMAIN}/inbox`,
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-----'
}
}
);
} else {
return res.status(404).send(`No record found for ${name}.`);
}
}).catch((err) => {
return res.status(500).send(`An error occured: ${err}.`);
})
}
}
function get_users(req, res) {
let p = ssb_bridge.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');
}
module.exports = {
get_user,
get_webfinger,
get_users,
post_inbox,
};