2021-08-19 21:55:45 +09:00
|
|
|
import config from '@/config/index';
|
|
|
|
import { getUserKeypair } from '@/misc/keypair-store';
|
2021-10-16 17:16:24 +09:00
|
|
|
import { User } from '@/models/entities/user';
|
|
|
|
import { getResponse } from '../../misc/fetch';
|
|
|
|
import { createSignedPost, createSignedGet } from './ap-request';
|
2019-07-28 09:49:02 +09:00
|
|
|
|
2021-03-24 11:05:37 +09:00
|
|
|
export default async (user: { id: User['id'] }, url: string, object: any) => {
|
2021-10-16 17:16:24 +09:00
|
|
|
const body = JSON.stringify(object);
|
2018-08-30 20:52:35 +09:00
|
|
|
|
2021-03-22 15:14:54 +09:00
|
|
|
const keypair = await getUserKeypair(user.id);
|
2019-04-07 21:50:36 +09:00
|
|
|
|
2021-10-16 17:16:24 +09: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-05 01:58:41 +09:00
|
|
|
|
2021-10-16 17:16:24 +09:00
|
|
|
await getResponse({
|
|
|
|
url,
|
|
|
|
method: req.request.method,
|
|
|
|
headers: req.request.headers,
|
|
|
|
body,
|
2018-10-05 01:58:41 +09:00
|
|
|
});
|
2019-03-08 19:45:01 +09:00
|
|
|
};
|
2020-10-18 01:46:40 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get AP object with http-signature
|
|
|
|
* @param user http-signature user
|
|
|
|
* @param url URL to fetch
|
|
|
|
*/
|
2021-03-24 11:05:37 +09:00
|
|
|
export async function signedGet(url: string, user: { id: User['id'] }) {
|
2021-03-22 15:14:54 +09:00
|
|
|
const keypair = await getUserKeypair(user.id);
|
2020-10-18 01:46:40 +09:00
|
|
|
|
2021-10-16 17:16:24 +09:00
|
|
|
const req = createSignedGet({
|
|
|
|
key: {
|
|
|
|
privateKeyPem: keypair.privateKey,
|
|
|
|
keyId: `${config.url}/users/${user.id}#main-key`
|
2020-10-18 01:46:40 +09:00
|
|
|
},
|
2021-10-16 17:16:24 +09:00
|
|
|
url,
|
|
|
|
additionalHeaders: {
|
|
|
|
'User-Agent': config.userAgent,
|
2020-10-18 01:46:40 +09:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-10-16 17:16:24 +09:00
|
|
|
const res = await getResponse({
|
|
|
|
url,
|
|
|
|
method: req.request.method,
|
|
|
|
headers: req.request.headers
|
2020-10-18 01:46:40 +09:00
|
|
|
});
|
|
|
|
|
2021-10-16 17:16:24 +09:00
|
|
|
return await res.json();
|
2020-10-18 01:46:40 +09:00
|
|
|
}
|