mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-23 17:28:29 +02:00
API key auth to copy-as-grpcurl
This commit is contained in:
@@ -68,16 +68,37 @@ export async function convert(request: Partial<GrpcRequest>, allProtoFiles: stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add basic authentication
|
// Add basic authentication
|
||||||
if (request.authenticationType === 'basic') {
|
if (request.authentication?.disabled !== true) {
|
||||||
const user = request.authentication?.username ?? '';
|
if (request.authenticationType === 'basic') {
|
||||||
const pass = request.authentication?.password ?? '';
|
const user = request.authentication?.username ?? '';
|
||||||
const encoded = btoa(`${user}:${pass}`);
|
const pass = request.authentication?.password ?? '';
|
||||||
xs.push('-H', quote(`Authorization: Basic ${encoded}`));
|
const encoded = btoa(`${user}:${pass}`);
|
||||||
xs.push(NEWLINE);
|
xs.push('-H', quote(`Authorization: Basic ${encoded}`));
|
||||||
} else if (request.authenticationType === 'bearer') {
|
xs.push(NEWLINE);
|
||||||
// Add bearer authentication
|
} else if (request.authenticationType === 'bearer') {
|
||||||
xs.push('-H', quote(`Authorization: Bearer ${request.authentication?.token ?? ''}`));
|
// Add bearer authentication
|
||||||
xs.push(NEWLINE);
|
xs.push('-H', quote(`Authorization: Bearer ${request.authentication?.token ?? ''}`));
|
||||||
|
xs.push(NEWLINE);
|
||||||
|
} else if (request.authenticationType === 'apikey') {
|
||||||
|
if (request.authentication?.location === 'query') {
|
||||||
|
const sep = request.url?.includes('?') ? '&' : '?';
|
||||||
|
request.url = [
|
||||||
|
request.url,
|
||||||
|
sep,
|
||||||
|
encodeURIComponent(request.authentication?.key ?? 'token'),
|
||||||
|
'=',
|
||||||
|
encodeURIComponent(request.authentication?.value ?? ''),
|
||||||
|
].join('');
|
||||||
|
} else {
|
||||||
|
xs.push(
|
||||||
|
'-H',
|
||||||
|
quote(
|
||||||
|
`${request.authentication?.key ?? 'X-Api-Key'}: ${request.authentication?.value ?? ''}`,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
xs.push(NEWLINE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add form params
|
// Add form params
|
||||||
|
|||||||
@@ -27,6 +27,55 @@ describe('exporter-curl', () => {
|
|||||||
),
|
),
|
||||||
).toEqual([`grpcurl -H 'aaa: AAA'`, `-H 'bbb: BBB'`, `yaak.app`].join(` \\\n `));
|
).toEqual([`grpcurl -H 'aaa: AAA'`, `-H 'bbb: BBB'`, `yaak.app`].join(` \\\n `));
|
||||||
});
|
});
|
||||||
|
test('Basic auth', async () => {
|
||||||
|
expect(
|
||||||
|
await convert(
|
||||||
|
{
|
||||||
|
url: 'https://yaak.app',
|
||||||
|
authenticationType: 'basic',
|
||||||
|
authentication: {
|
||||||
|
username: 'user',
|
||||||
|
password: 'pass',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
),
|
||||||
|
).toEqual([`grpcurl -H 'Authorization: Basic dXNlcjpwYXNz'`, `yaak.app`].join(` \\\n `));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('API key auth', async () => {
|
||||||
|
expect(
|
||||||
|
await convert(
|
||||||
|
{
|
||||||
|
url: 'https://yaak.app',
|
||||||
|
authenticationType: 'apikey',
|
||||||
|
authentication: {
|
||||||
|
key: 'X-Token',
|
||||||
|
value: 'tok',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
),
|
||||||
|
).toEqual([`grpcurl -H 'X-Token: tok'`, `yaak.app`].join(` \\\n `));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('API key auth', async () => {
|
||||||
|
expect(
|
||||||
|
await convert(
|
||||||
|
{
|
||||||
|
url: 'https://yaak.app',
|
||||||
|
authenticationType: 'apikey',
|
||||||
|
authentication: {
|
||||||
|
location: 'query',
|
||||||
|
key: 'token',
|
||||||
|
value: 'tok 1',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
),
|
||||||
|
).toEqual([`grpcurl`, `yaak.app?token=tok%201`].join(` \\\n `));
|
||||||
|
});
|
||||||
|
|
||||||
test('Single proto file', async () => {
|
test('Single proto file', async () => {
|
||||||
expect(await convert({ url: 'https://yaak.app' }, ['/foo/bar/baz.proto'])).toEqual(
|
expect(await convert({ url: 'https://yaak.app' }, ['/foo/bar/baz.proto'])).toEqual(
|
||||||
[
|
[
|
||||||
|
|||||||
Reference in New Issue
Block a user