2021-08-19 14:55:45 +02:00
|
|
|
import config from '@/config/index';
|
|
|
|
import { getUserKeypair } from '@/misc/keypair-store';
|
2021-10-16 10:16:24 +02:00
|
|
|
import { User } from '@/models/entities/user';
|
|
|
|
import { getResponse } from '../../misc/fetch';
|
|
|
|
import { createSignedPost, createSignedGet } from './ap-request';
|
2019-07-28 02:49:02 +02:00
|
|
|
|
2021-03-24 03:05:37 +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,
|
|
|
|
keyId: `${config.url}/users/${user.id}#main-key`
|
|
|
|
},
|
|
|
|
url,
|
|
|
|
body,
|
|
|
|
additionalHeaders: {
|
|
|
|
'User-Agent': config.userAgent,
|
|
|
|
}
|
|
|
|
});
|
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
|
|
|
|
*/
|
2021-03-24 03:05:37 +01:00
|
|
|
export async function signedGet(url: string, user: { id: User['id'] }) {
|
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,
|
|
|
|
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: {
|
|
|
|
'User-Agent': config.userAgent,
|
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,
|
|
|
|
headers: req.request.headers
|
2020-10-17 18:46:40 +02:00
|
|
|
});
|
|
|
|
|
2021-10-16 10:16:24 +02:00
|
|
|
return await res.json();
|
2020-10-17 18:46:40 +02:00
|
|
|
}
|