diff --git a/packages/backend/src/core/HttpRequestService.ts b/packages/backend/src/core/HttpRequestService.ts index 73bb3dc7e9..ef2517680f 100644 --- a/packages/backend/src/core/HttpRequestService.ts +++ b/packages/backend/src/core/HttpRequestService.ts @@ -150,11 +150,14 @@ export class HttpRequestService { controller.abort(); }, timeout); - const res = await fetch(url, { + const bearcaps = url.startsWith('bear:?') ? this.parseBearcaps(url) : undefined; + + const res = await fetch(bearcaps?.url ?? url, { method: args.method ?? 'GET', headers: { 'User-Agent': this.config.userAgent, ...(args.headers ?? {}), + ...(bearcaps?.token ? { Authorization: `Bearer ${bearcaps.token}` } : {}), }, body: args.body, size: args.size ?? 10 * 1024 * 1024, @@ -168,4 +171,17 @@ export class HttpRequestService { return res; } + + // Bearcaps https://docs.joinmastodon.org/spec/bearcaps/ + // bear:?t=&u=https://example.com/foo' + // -> GET https://example.com/foo Authorization: Bearer + private parseBearcaps(url: string): { url: string, token: string | undefined } | undefined { + const params = new URLSearchParams(url.split('?')[1]); + if (!params.has('u')) return undefined; + + return { + url: params.get('u')!, + token: params.get('t') ?? undefined, + }; + } }