From fa8784c038d6295e722d599da2ea485147b921f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=BE=E3=81=A3=E3=81=A1=E3=82=83=E3=81=A8=E3=83=BC?= =?UTF-8?q?=E3=81=AB=E3=82=85?= <17376330+u1-liquid@users.noreply.github.com> Date: Sat, 6 Jan 2024 02:56:42 +0900 Subject: [PATCH] =?UTF-8?q?feat(backend):=20Bearcaps=20URL=E3=81=AB?= =?UTF-8?q?=E5=AF=BE=E5=BF=9C=20(MisskeyIO#330)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/src/core/HttpRequestService.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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, + }; + } }