2023-01-13 05:40:33 +01:00
|
|
|
import config from "@/config/index.js";
|
|
|
|
import { getUserKeypair } from "@/misc/keypair-store.js";
|
|
|
|
import type { User } from "@/models/entities/user.js";
|
|
|
|
import { getResponse } from "../../misc/fetch.js";
|
|
|
|
import { createSignedPost, createSignedGet } from "./ap-request.js";
|
2023-06-28 01:02:28 +02:00
|
|
|
import { apLogger } from "@/remote/activitypub/logger.js";
|
2019-07-28 02:49:02 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
export default async (user: { id: User["id"] }, url: string, object: any) => {
|
2021-10-16 10:16:24 +02:00
|
|
|
const body = JSON.stringify(object);
|
2018-08-30 13:52:35 +02:00
|
|
|
|
2021-03-22 07:14:54 +01:00
|
|
|
const keypair = await getUserKeypair(user.id);
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2021-10-16 10:16:24 +02:00
|
|
|
const req = createSignedPost({
|
|
|
|
key: {
|
|
|
|
privateKeyPem: keypair.privateKey,
|
2021-12-09 15:58:30 +01:00
|
|
|
keyId: `${config.url}/users/${user.id}#main-key`,
|
2021-10-16 10:16:24 +02:00
|
|
|
},
|
|
|
|
url,
|
|
|
|
body,
|
|
|
|
additionalHeaders: {
|
2023-01-13 05:40:33 +01:00
|
|
|
"User-Agent": config.userAgent,
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2021-10-16 10:16:24 +02:00
|
|
|
});
|
2018-10-04 18:58:41 +02:00
|
|
|
|
2021-10-16 10:16:24 +02:00
|
|
|
await getResponse({
|
|
|
|
url,
|
|
|
|
method: req.request.method,
|
|
|
|
headers: req.request.headers,
|
|
|
|
body,
|
2018-10-04 18:58:41 +02:00
|
|
|
});
|
2019-03-08 11:45:01 +01:00
|
|
|
};
|
2020-10-17 18:46:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get AP object with http-signature
|
|
|
|
* @param user http-signature user
|
|
|
|
* @param url URL to fetch
|
2023-10-11 20:23:00 +02:00
|
|
|
* @param redirects whether or not to accept redirects
|
2020-10-17 18:46:40 +02:00
|
|
|
*/
|
2023-10-11 20:23:00 +02:00
|
|
|
export async function signedGet(url: string, user: { id: User["id"] }, redirects: boolean = true) {
|
2023-06-28 02:01:55 +02:00
|
|
|
apLogger.debug(`Running signedGet on url: ${url}`);
|
2021-03-22 07:14:54 +01:00
|
|
|
const keypair = await getUserKeypair(user.id);
|
2020-10-17 18:46:40 +02:00
|
|
|
|
2021-10-16 10:16:24 +02:00
|
|
|
const req = createSignedGet({
|
|
|
|
key: {
|
|
|
|
privateKeyPem: keypair.privateKey,
|
2021-12-09 15:58:30 +01:00
|
|
|
keyId: `${config.url}/users/${user.id}#main-key`,
|
2020-10-17 18:46:40 +02:00
|
|
|
},
|
2021-10-16 10:16:24 +02:00
|
|
|
url,
|
|
|
|
additionalHeaders: {
|
2023-01-13 05:40:33 +01:00
|
|
|
"User-Agent": config.userAgent,
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2020-10-17 18:46:40 +02:00
|
|
|
});
|
|
|
|
|
2021-10-16 10:16:24 +02:00
|
|
|
const res = await getResponse({
|
|
|
|
url,
|
|
|
|
method: req.request.method,
|
2021-12-09 15:58:30 +01:00
|
|
|
headers: req.request.headers,
|
2023-10-11 20:23:00 +02:00
|
|
|
redirect: redirects ? "manual" : "error"
|
2020-10-17 18:46:40 +02:00
|
|
|
});
|
|
|
|
|
2023-10-11 20:23:00 +02:00
|
|
|
if (redirects && [301,302,307,308].includes(res.status)) {
|
|
|
|
const newUrl = res.headers.get('location');
|
|
|
|
if (!newUrl) throw new Error('signedGet got redirect but no target location');
|
|
|
|
apLogger.debug(`signedGet is redirecting to ${newUrl}`);
|
|
|
|
return signedGet(newUrl, user, false);
|
|
|
|
}
|
|
|
|
|
2024-02-16 18:22:38 +01:00
|
|
|
const contentType = res.headers.get('content-type');
|
|
|
|
if (contentType == null || (contentType !== 'application/activity+json' && !contentType.startsWith('application/activity+json;') && contentType !== 'application/ld+json' && !contentType.startsWith('application/ld+json;')))
|
|
|
|
throw new Error(`signedGet response had unexpected content-type: ${contentType}`);
|
|
|
|
|
2021-10-16 10:16:24 +02:00
|
|
|
return await res.json();
|
2020-10-17 18:46:40 +02:00
|
|
|
}
|