misskey/src/server/activitypub/inbox.ts

59 lines
1.3 KiB
TypeScript
Raw Normal View History

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';
import User, { IRemoteAccount } from '../../models/user';
import queue from '../../queue';
const app = express();
app.disable('x-powered-by');
app.use(bodyParser.json());
2018-04-01 16:46:33 +09:00
app.post('/@:user/inbox', async (req, res) => {
2018-04-01 15:58:49 +09:00
let parsed;
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);
}
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
};
}
const user = await User.findOne(query);
2018-04-01 15:58:49 +09:00
if (user === null) {
return res.sendStatus(401);
}
if (!verifySignature(parsed, (user.account as IRemoteAccount).publicKey.publicKeyPem)) {
return res.sendStatus(401);
}
queue.create('http', {
type: 'performActivityPub',
actor: user._id,
outbox: req.body,
}).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;