From 907e09a417f501917ab93c94d7046fe2b195f281 Mon Sep 17 00:00:00 2001 From: moshyfawn Date: Tue, 16 Sep 2025 14:20:23 -0400 Subject: [PATCH] [Plugins] [CopyAsCURL] [Auth] [JWT] Include custom bearer prefix to copy CURL (#253) Co-authored-by: Gregory Schier --- plugins/action-copy-curl/src/index.ts | 4 +- plugins/action-copy-curl/tests/index.test.ts | 45 ++++++++++++++++---- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/plugins/action-copy-curl/src/index.ts b/plugins/action-copy-curl/src/index.ts index fba4fbe0..1ad92a68 100644 --- a/plugins/action-copy-curl/src/index.ts +++ b/plugins/action-copy-curl/src/index.ts @@ -93,7 +93,9 @@ export async function convertToCurl(request: Partial) { // Add bearer authentication if (request.authenticationType === 'bearer') { - xs.push('--header', quote(`Authorization: Bearer ${request.authentication?.token ?? ''}`)); + const value = + `${request.authentication?.prefix ?? 'Bearer'} ${request.authentication?.token ?? ''}`.trim(); + xs.push('--header', quote(`Authorization: ${value}`)); xs.push(NEWLINE); } diff --git a/plugins/action-copy-curl/tests/index.test.ts b/plugins/action-copy-curl/tests/index.test.ts index 0fc04607..8220655d 100644 --- a/plugins/action-copy-curl/tests/index.test.ts +++ b/plugins/action-copy-curl/tests/index.test.ts @@ -12,9 +12,7 @@ describe('exporter-curl', () => { { name: 'c', value: 'ccc', enabled: false }, ], }), - ).toEqual( - [`curl 'https://yaak.app?a=aaa&b=bbb'`].join(` \\n `), - ); + ).toEqual([`curl 'https://yaak.app?a=aaa&b=bbb'`].join(` \\n `)); }); test('Exports GET with params and hash', async () => { @@ -27,9 +25,7 @@ describe('exporter-curl', () => { { name: 'c', value: 'ccc', enabled: false }, ], }), - ).toEqual( - [`curl 'https://yaak.app/path?a=aaa&b=bbb#section'`].join(` \\n `), - ); + ).toEqual([`curl 'https://yaak.app/path?a=aaa&b=bbb#section'`].join(` \\n `)); }); test('Exports POST with url form data', async () => { expect( @@ -62,7 +58,10 @@ describe('exporter-curl', () => { }, }), ).toEqual( - [`curl -X POST 'https://yaak.app'`, `--data '{"query":"{foo,bar}","variables":{"a":"aaa","b":"bbb"}}'`].join(` \\\n `), + [ + `curl -X POST 'https://yaak.app'`, + `--data '{"query":"{foo,bar}","variables":{"a":"aaa","b":"bbb"}}'`, + ].join(` \\\n `), ); }); @@ -206,6 +205,34 @@ describe('exporter-curl', () => { ).toEqual([`curl 'https://yaak.app'`, `--header 'Authorization: Bearer tok'`].join(` \\\n `)); }); + test('Bearer auth with custom prefix', async () => { + expect( + await convertToCurl({ + url: 'https://yaak.app', + authenticationType: 'bearer', + authentication: { + token: 'abc123', + prefix: 'Token', + }, + }), + ).toEqual( + [`curl 'https://yaak.app'`, `--header 'Authorization: Token abc123'`].join(` \\\n `), + ); + }); + + test('Bearer auth with empty prefix', async () => { + expect( + await convertToCurl({ + url: 'https://yaak.app', + authenticationType: 'bearer', + authentication: { + token: 'xyz789', + prefix: '', + }, + }), + ).toEqual([`curl 'https://yaak.app'`, `--header 'Authorization: xyz789'`].join(` \\\n `)); + }); + test('Broken bearer auth', async () => { expect( await convertToCurl({ @@ -216,7 +243,7 @@ describe('exporter-curl', () => { password: 'pass', }, }), - ).toEqual([`curl 'https://yaak.app'`, `--header 'Authorization: Bearer '`].join(` \\\n `)); + ).toEqual([`curl 'https://yaak.app'`, `--header 'Authorization: Bearer'`].join(` \\\n `)); }); test('Stale body data', async () => { @@ -226,7 +253,7 @@ describe('exporter-curl', () => { bodyType: 'none', body: { text: 'ignore me', - } + }, }), ).toEqual([`curl 'https://yaak.app'`].join(` \\\n `)); });