Resolve account by signature in inbox
This commit is contained in:
parent
ce7efc4dbb
commit
69763ac32b
7 changed files with 72 additions and 56 deletions
|
@ -1,9 +1,7 @@
|
|||
import * as bodyParser from 'body-parser';
|
||||
import * as express from 'express';
|
||||
import { parseRequest, verifySignature } from 'http-signature';
|
||||
import User, { IRemoteUser } from '../../models/user';
|
||||
import { parseRequest } from 'http-signature';
|
||||
import queue from '../../queue';
|
||||
import parseAcct from '../../acct/parse';
|
||||
|
||||
const app = express();
|
||||
|
||||
|
@ -14,48 +12,20 @@ app.post('/@:user/inbox', bodyParser.json({
|
|||
return true;
|
||||
}
|
||||
}), async (req, res) => {
|
||||
let parsed;
|
||||
let signature;
|
||||
|
||||
req.headers.authorization = 'Signature ' + req.headers.signature;
|
||||
|
||||
try {
|
||||
parsed = parseRequest(req);
|
||||
signature = 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) as IRemoteUser;
|
||||
|
||||
if (user === null) {
|
||||
return res.sendStatus(401);
|
||||
}
|
||||
|
||||
if (!verifySignature(parsed, user.account.publicKey.publicKeyPem)) {
|
||||
return res.sendStatus(401);
|
||||
}
|
||||
|
||||
queue.create('http', {
|
||||
type: 'performActivityPub',
|
||||
actor: user._id,
|
||||
outbox: req.body,
|
||||
distribute: true,
|
||||
type: 'processInbox',
|
||||
inbox: req.body,
|
||||
signature,
|
||||
}).save();
|
||||
|
||||
return res.status(202).end();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue