2018-04-01 15:58:49 +09:00
|
|
|
import * as bodyParser from 'body-parser';
|
|
|
|
import * as express from 'express';
|
|
|
|
import { parseRequest, verifySignature } from 'http-signature';
|
2018-04-02 04:01:34 +09:00
|
|
|
import User, { IRemoteUser } from '../../models/user';
|
2018-04-01 15:58:49 +09:00
|
|
|
import queue from '../../queue';
|
2018-04-02 13:44:32 +09:00
|
|
|
import parseAcct from '../../acct/parse';
|
2018-04-01 15:58:49 +09:00
|
|
|
|
|
|
|
const app = express();
|
2018-04-02 17:11:14 +09:00
|
|
|
|
2018-04-01 15:58:49 +09:00
|
|
|
app.disable('x-powered-by');
|
|
|
|
|
2018-04-02 17:11:14 +09:00
|
|
|
app.post('/@:user/inbox', bodyParser.json({
|
|
|
|
type() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}), async (req, res) => {
|
2018-04-01 15:58:49 +09:00
|
|
|
let parsed;
|
|
|
|
|
2018-04-02 00:36:36 +09:00
|
|
|
req.headers.authorization = 'Signature ' + req.headers.signature;
|
|
|
|
|
2018-04-01 15:58:49 +09:00
|
|
|
try {
|
|
|
|
parsed = parseRequest(req);
|
|
|
|
} catch (exception) {
|
|
|
|
return res.sendStatus(401);
|
|
|
|
}
|
|
|
|
|
2018-04-02 00:36:36 +09:00
|
|
|
const keyIdLower = parsed.keyId.toLowerCase();
|
|
|
|
let query;
|
|
|
|
|
|
|
|
if (keyIdLower.startsWith('acct:')) {
|
|
|
|
const { username, host } = parseAcct(keyIdLower.slice('acct:'.length));
|
|
|
|
if (host === null) {
|
|
|
|
return res.sendStatus(401);
|
|
|
|
}
|
|
|
|
|
|
|
|
query = { usernameLower: username, hostLower: host };
|
|
|
|
} else {
|
|
|
|
query = {
|
|
|
|
host: { $ne: null },
|
|
|
|
'account.publicKey.id': parsed.keyId
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-02 04:01:34 +09:00
|
|
|
const user = await User.findOne(query) as IRemoteUser;
|
2018-04-01 15:58:49 +09:00
|
|
|
|
|
|
|
if (user === null) {
|
|
|
|
return res.sendStatus(401);
|
|
|
|
}
|
|
|
|
|
2018-04-02 04:01:34 +09:00
|
|
|
if (!verifySignature(parsed, user.account.publicKey.publicKeyPem)) {
|
2018-04-01 15:58:49 +09:00
|
|
|
return res.sendStatus(401);
|
|
|
|
}
|
|
|
|
|
|
|
|
queue.create('http', {
|
|
|
|
type: 'performActivityPub',
|
|
|
|
actor: user._id,
|
|
|
|
outbox: req.body,
|
2018-04-02 17:11:14 +09:00
|
|
|
distribute: true,
|
2018-04-01 15:58:49 +09:00
|
|
|
}).save();
|
|
|
|
|
2018-04-01 18:16:47 +09:00
|
|
|
return res.status(202).end();
|
2018-04-01 15:58:49 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
export default app;
|