Skip to content

Commit 144faad

Browse files
committed
1 parent 947926c commit 144faad

File tree

12 files changed

+153
-52
lines changed

12 files changed

+153
-52
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"packages/plugin-runtime",
1111
"packages/plugin-runtime-types",
1212
"packages/common-lib",
13+
"plugins/auth-apikey",
1314
"plugins/auth-basic",
1415
"plugins/auth-bearer",
1516
"plugins/auth-jwt",

packages/plugin-runtime-types/src/bindings/gen_events.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ export type CallHttpAuthenticationResponse = {
2121
* HTTP headers to add to the request. Existing headers will be replaced, while
2222
* new headers will be added.
2323
*/
24-
setHeaders: Array<HttpHeader>, };
24+
setHeaders?: Array<HttpHeader>,
25+
/**
26+
* Query parameters to add to the request. Existing params will be replaced, while
27+
* new params will be added.
28+
*/
29+
setQueryParameters?: Array<HttpHeader>, };
2530

2631
export type CallHttpRequestActionArgs = { httpRequest: HttpRequest, };
2732

packages/plugin-runtime/src/PluginInstance.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,11 @@ export class PluginInstance {
253253
const auth = this.#mod.authentication;
254254
if (typeof auth?.onApply === 'function') {
255255
applyFormInputDefaults(auth.args, payload.values);
256-
const result = await auth.onApply(ctx, payload);
257256
this.#sendPayload(
258257
windowContext,
259258
{
260259
type: 'call_http_authentication_response',
261-
setHeaders: result.setHeaders,
260+
...(await auth.onApply(ctx, payload)),
262261
},
263262
replyId,
264263
);
@@ -579,7 +578,7 @@ export class PluginInstance {
579578
event.windowContext,
580579
payload,
581580
);
582-
return result.data;
581+
return result.data as any;
583582
},
584583
},
585584
store: {

plugins/auth-apikey/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "@yaak/auth-apikey",
3+
"displayName": "API Key Authentication",
4+
"description": "Authenticate requests using an API key",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/mountain-loop/yaak.git",
8+
"directory": "plugins/auth-apikey"
9+
},
10+
"private": true,
11+
"version": "0.1.0",
12+
"scripts": {
13+
"build": "yaakcli build",
14+
"dev": "yaakcli dev",
15+
"lint": "eslint . --ext .ts,.tsx"
16+
}
17+
}

plugins/auth-apikey/src/index.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type { PluginDefinition } from '@yaakapp/api';
2+
3+
export const plugin: PluginDefinition = {
4+
authentication: {
5+
name: 'apikey',
6+
label: 'API Key',
7+
shortLabel: 'API Key',
8+
args: [
9+
{
10+
type: 'select',
11+
name: 'location',
12+
label: 'Behavior',
13+
defaultValue: 'header',
14+
options: [
15+
{ label: 'Insert Header', value: 'header' },
16+
{ label: 'Append Query Parameter', value: 'query' },
17+
],
18+
},
19+
{
20+
type: 'text',
21+
name: 'key',
22+
label: 'Key',
23+
dynamic: (_ctx, { values }) => {
24+
return values.location === 'query' ? {
25+
label: 'Parameter Name',
26+
description: 'The name of the query parameter to add to the request',
27+
} : {
28+
label: 'Header Name',
29+
description: 'The name of the header to add to the request',
30+
};
31+
},
32+
},
33+
{
34+
type: 'text',
35+
name: 'value',
36+
label: 'API Key',
37+
optional: true,
38+
password: true,
39+
},
40+
],
41+
async onApply(_ctx, { values }) {
42+
const key = String(values.key ?? '');
43+
const value = String(values.value ?? '');
44+
const location = String(values.location);
45+
46+
if (location === 'query') {
47+
return { setQueryParameters: [{ name: key, value }] };
48+
} else {
49+
return { setHeaders: [{ name: key, value }] };
50+
}
51+
},
52+
},
53+
};

plugins/auth-apikey/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../tsconfig.json"
3+
}

src-tauri/src/grpc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub(crate) async fn build_metadata<R: Runtime>(
6969
let auth = request.authentication.clone();
7070
let plugin_req = CallHttpAuthenticationRequest {
7171
context_id: format!("{:x}", md5::compute(authentication_context_id)),
72-
values: serde_json::from_value(serde_json::to_value(&auth).unwrap()).unwrap(),
72+
values: serde_json::from_value(serde_json::to_value(&auth)?)?,
7373
method: "POST".to_string(),
7474
url: request.url.clone(),
7575
headers: metadata
@@ -83,7 +83,7 @@ pub(crate) async fn build_metadata<R: Runtime>(
8383
let plugin_result = plugin_manager
8484
.call_http_authentication(&window, &authentication_type, plugin_req)
8585
.await?;
86-
for header in plugin_result.set_headers {
86+
for header in plugin_result.set_headers.unwrap_or_default() {
8787
metadata.insert(header.name, header.value);
8888
}
8989
}

src-tauri/src/http_request.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,7 @@ pub async fn send_http_request<R: Runtime>(
452452
Some(authentication_type) => {
453453
let req = CallHttpAuthenticationRequest {
454454
context_id: format!("{:x}", md5::compute(auth_context_id)),
455-
values: serde_json::from_value(
456-
serde_json::to_value(&request.authentication).unwrap(),
457-
)
458-
.unwrap(),
455+
values: serde_json::from_value(serde_json::to_value(&request.authentication)?)?,
459456
url: sendable_req.url().to_string(),
460457
method: sendable_req.method().to_string(),
461458
headers: sendable_req
@@ -482,11 +479,19 @@ pub async fn send_http_request<R: Runtime>(
482479
};
483480

484481
let headers = sendable_req.headers_mut();
485-
for header in plugin_result.set_headers {
486-
headers.insert(
487-
HeaderName::from_str(&header.name).unwrap(),
488-
HeaderValue::from_str(&header.value).unwrap(),
489-
);
482+
for header in plugin_result.set_headers.unwrap_or_default() {
483+
match (HeaderName::from_str(&header.name), HeaderValue::from_str(&header.value)) {
484+
(Ok(name), Ok(value)) => {
485+
headers.insert(name, value);
486+
}
487+
_ => continue,
488+
};
489+
}
490+
491+
let mut query_pairs = sendable_req.url_mut().query_pairs_mut();
492+
for p in plugin_result.set_query_parameters.unwrap_or_default() {
493+
println!("Adding query parameter: {:?}", p);
494+
query_pairs.append_pair(&p.name, &p.value);
490495
}
491496
}
492497
}

src-tauri/yaak-plugins/bindings/gen_events.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ export type CallHttpAuthenticationResponse = {
2121
* HTTP headers to add to the request. Existing headers will be replaced, while
2222
* new headers will be added.
2323
*/
24-
setHeaders: Array<HttpHeader>, };
24+
setHeaders?: Array<HttpHeader>,
25+
/**
26+
* Query parameters to add to the request. Existing params will be replaced, while
27+
* new params will be added.
28+
*/
29+
setQueryParameters?: Array<HttpHeader>, };
2530

2631
export type CallHttpRequestActionArgs = { httpRequest: HttpRequest, };
2732

src-tauri/yaak-plugins/src/events.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,13 @@ pub enum JsonPrimitive {
649649
pub struct CallHttpAuthenticationResponse {
650650
/// HTTP headers to add to the request. Existing headers will be replaced, while
651651
/// new headers will be added.
652-
pub set_headers: Vec<HttpHeader>,
652+
#[ts(optional)]
653+
pub set_headers: Option<Vec<HttpHeader>>,
654+
655+
/// Query parameters to add to the request. Existing params will be replaced, while
656+
/// new params will be added.
657+
#[ts(optional)]
658+
pub set_query_parameters: Option<Vec<HttpHeader>>,
653659
}
654660

655661
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]

0 commit comments

Comments
 (0)