summaryrefslogtreecommitdiff
path: root/packages/backend/src/remote/activitypub/request.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/backend/src/remote/activitypub/request.ts')
-rw-r--r--packages/backend/src/remote/activitypub/request.ts58
1 files changed, 58 insertions, 0 deletions
diff --git a/packages/backend/src/remote/activitypub/request.ts b/packages/backend/src/remote/activitypub/request.ts
new file mode 100644
index 0000000000..d6ced630c1
--- /dev/null
+++ b/packages/backend/src/remote/activitypub/request.ts
@@ -0,0 +1,58 @@
+import config from '@/config/index';
+import { getUserKeypair } from '@/misc/keypair-store';
+import { User } from '@/models/entities/user';
+import { getResponse } from '../../misc/fetch';
+import { createSignedPost, createSignedGet } from './ap-request';
+
+export default async (user: { id: User['id'] }, url: string, object: any) => {
+ const body = JSON.stringify(object);
+
+ const keypair = await getUserKeypair(user.id);
+
+ const req = createSignedPost({
+ key: {
+ privateKeyPem: keypair.privateKey,
+ keyId: `${config.url}/users/${user.id}#main-key`
+ },
+ url,
+ body,
+ additionalHeaders: {
+ 'User-Agent': config.userAgent,
+ }
+ });
+
+ await getResponse({
+ url,
+ method: req.request.method,
+ headers: req.request.headers,
+ body,
+ });
+};
+
+/**
+ * Get AP object with http-signature
+ * @param user http-signature user
+ * @param url URL to fetch
+ */
+export async function signedGet(url: string, user: { id: User['id'] }) {
+ const keypair = await getUserKeypair(user.id);
+
+ const req = createSignedGet({
+ key: {
+ privateKeyPem: keypair.privateKey,
+ keyId: `${config.url}/users/${user.id}#main-key`
+ },
+ url,
+ additionalHeaders: {
+ 'User-Agent': config.userAgent,
+ }
+ });
+
+ const res = await getResponse({
+ url,
+ method: req.request.method,
+ headers: req.request.headers
+ });
+
+ return await res.json();
+}