fix: use access token for all Mastodon routes (#14634)

* fix: use access token for all Mastodon routes

* fix: only send access token to MASTODON_API_HOST
This commit is contained in:
Yufan You 2024-03-04 00:01:14 +08:00 committed by GitHub
parent 6b7b95d1b3
commit 30657490ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 5 deletions

View File

@ -11,7 +11,7 @@ export default async (ctx) => {
const url = `http://${site}/api/v1/timelines/public?local=true&only_media=${only_media}`;
const response = await got.get(url);
const response = await got.get(url, { headers: utils.apiHeaders() });
const list = response.data;
ctx.set('data', {

View File

@ -11,7 +11,7 @@ export default async (ctx) => {
const url = `http://${site}/api/v1/timelines/public?remote=true&only_media=${only_media}`;
const response = await got.get(url);
const response = await got.get(url, { headers: utils.apiHeaders() });
const list = response.data;
ctx.set('data', {

View File

@ -5,6 +5,12 @@ import { config } from '@/config';
const allowSiteList = ['mastodon.social', 'pawoo.net'];
const apiHeaders = (site) => {
const { accessToken, apiHost } = config.mastodon;
// avoid sending API token to other sites
return accessToken && site === apiHost ? { Authorization: `Bearer ${accessToken}` } : {};
};
const mediaParse = (media_attachments) =>
media_attachments
.map((item) => {
@ -59,6 +65,7 @@ async function getAccountStatuses(site, account_id, only_media) {
const statuses_response = await got({
method: 'get',
url: statuses_url,
headers: apiHeaders(site),
});
const data = statuses_response.data;
@ -70,6 +77,7 @@ async function getAccountStatuses(site, account_id, only_media) {
const account_response = await got({
method: 'get',
url: account_url,
headers: apiHeaders(site),
});
account_data = account_response.data;
}
@ -95,9 +103,7 @@ async function getAccountIdByAcct(acct) {
const search_response = await got({
method: 'get',
url: search_url,
headers: {
...(mastodonConfig.accessToken ? { Authorization: `Bearer ${mastodonConfig.accessToken}` } : {}),
},
headers: apiHeaders(site),
searchParams: {
q: acct,
type: 'accounts',
@ -123,6 +129,7 @@ async function getAccountIdByAcct(acct) {
}
module.exports = {
apiHeaders,
parseStatuses,
getAccountStatuses,
getAccountIdByAcct,