Implement account public key endpoint

This commit is contained in:
Akihiko Odaki 2018-04-01 20:07:04 +09:00
parent 07543da160
commit 92b3501c6b
6 changed files with 66 additions and 52 deletions

View File

@ -3,6 +3,7 @@ import { extractPublic } from '../../../../crypto_key';
import { ILocalAccount } from '../../../../models/user'; import { ILocalAccount } from '../../../../models/user';
export default ({ username, account }) => ({ export default ({ username, account }) => ({
id: `${config.url}/@${username}/publickey`,
type: 'Key', type: 'Key',
owner: `${config.url}/@${username}`, owner: `${config.url}/@${username}`,
publicKeyPem: extractPublic((account as ILocalAccount).keypair) publicKeyPem: extractPublic((account as ILocalAccount).keypair)

View File

@ -3,6 +3,7 @@ import * as express from 'express';
import user from './user'; import user from './user';
import inbox from './inbox'; import inbox from './inbox';
import outbox from './outbox'; import outbox from './outbox';
import publicKey from './publickey';
import post from './post'; import post from './post';
const app = express(); const app = express();
@ -11,6 +12,7 @@ app.disable('x-powered-by');
app.use(user); app.use(user);
app.use(inbox); app.use(inbox);
app.use(outbox); app.use(outbox);
app.use(publicKey);
app.use(post); app.use(post);
export default app; export default app;

View File

@ -2,44 +2,26 @@ import * as express from 'express';
import context from '../../common/remote/activitypub/renderer/context'; import context from '../../common/remote/activitypub/renderer/context';
import renderNote from '../../common/remote/activitypub/renderer/note'; import renderNote from '../../common/remote/activitypub/renderer/note';
import renderOrderedCollection from '../../common/remote/activitypub/renderer/ordered-collection'; import renderOrderedCollection from '../../common/remote/activitypub/renderer/ordered-collection';
import parseAcct from '../../common/user/parse-acct';
import config from '../../conf'; import config from '../../conf';
import Post from '../../models/post'; import Post from '../../models/post';
import User from '../../models/user'; import withUser from './with-user';
const app = express(); const app = express();
app.disable('x-powered-by'); app.disable('x-powered-by');
app.get('/@:user/outbox', async (req, res) => { app.get('/@:user/outbox', withUser(username => {
const { username, host } = parseAcct(req.params.user); return `${config.url}/@${username}/inbox`;
if (host !== null) { }, async (user, req, res) => {
return res.sendStatus(422);
}
const user = await User.findOne({
usernameLower: username.toLowerCase(),
host: null
});
if (user === null) {
return res.sendStatus(404);
}
const id = `${config.url}/@${user.username}/inbox`;
if (username !== user.username) {
return res.redirect(id);
}
const posts = await Post.find({ userId: user._id }, { const posts = await Post.find({ userId: user._id }, {
limit: 20, limit: 20,
sort: { _id: -1 } sort: { _id: -1 }
}); });
const renderedPosts = await Promise.all(posts.map(post => renderNote(user, post))); const renderedPosts = await Promise.all(posts.map(post => renderNote(user, post)));
const rendered = renderOrderedCollection(id, user.postsCount, renderedPosts); const rendered = renderOrderedCollection(`${config.url}/@${user.username}/inbox`, user.postsCount, renderedPosts);
rendered['@context'] = context; rendered['@context'] = context;
res.json(rendered); res.json(rendered);
}); }));
export default app; export default app;

View File

@ -0,0 +1,19 @@
import * as express from 'express';
import context from '../../common/remote/activitypub/renderer/context';
import render from '../../common/remote/activitypub/renderer/key';
import config from '../../conf';
import withUser from './with-user';
const app = express();
app.disable('x-powered-by');
app.get('/@:user/publickey', withUser(username => {
return `${config.url}/@${username}/publickey`;
}, (user, req, res) => {
const rendered = render(user);
rendered['@context'] = context;
res.json(rendered);
}));
export default app;

View File

@ -2,39 +2,26 @@ import * as express from 'express';
import config from '../../conf'; import config from '../../conf';
import context from '../../common/remote/activitypub/renderer/context'; import context from '../../common/remote/activitypub/renderer/context';
import render from '../../common/remote/activitypub/renderer/person'; import render from '../../common/remote/activitypub/renderer/person';
import parseAcct from '../../common/user/parse-acct'; import withUser from './with-user';
import User from '../../models/user';
const app = express();
app.disable('x-powered-by');
app.get('/@:user', async (req, res, next) => {
const accepted = req.accepts(['html', 'application/activity+json', 'application/ld+json']);
if (!(['application/activity+json', 'application/ld+json'] as Array<any>).includes(accepted)) {
return next();
}
const { username, host } = parseAcct(req.params.user);
if (host !== null) {
return res.sendStatus(422);
}
const user = await User.findOne({
usernameLower: username.toLowerCase(),
host: null
});
if (user === null) {
return res.sendStatus(404);
}
if (username !== user.username) {
return res.redirect(`${config.url}/@${user.username}`);
}
const respond = withUser(username => `${config.url}/@${username}`, (user, req, res) => {
const rendered = render(user); const rendered = render(user);
rendered['@context'] = context; rendered['@context'] = context;
res.json(rendered); res.json(rendered);
}); });
const app = express();
app.disable('x-powered-by');
app.get('/@:user', (req, res, next) => {
const accepted = req.accepts(['html', 'application/activity+json', 'application/ld+json']);
if ((['application/activity+json', 'application/ld+json'] as Array<any>).includes(accepted)) {
respond(req, res, next);
} else {
next();
}
});
export default app; export default app;

View File

@ -0,0 +1,23 @@
import parseAcct from '../../common/user/parse-acct';
import User from '../../models/user';
export default (redirect, respond) => async (req, res, next) => {
const { username, host } = parseAcct(req.params.user);
if (host !== null) {
return res.sendStatus(422);
}
const user = await User.findOne({
usernameLower: username.toLowerCase(),
host: null
});
if (user === null) {
return res.sendStatus(404);
}
if (username !== user.username) {
return res.redirect(redirect(user.username));
}
return respond(user, req, res, next);
}