Skip to content

Commit

Permalink
JWT auth plugin and necessary updates
Browse files Browse the repository at this point in the history
  • Loading branch information
gschier committed Jan 17, 2025
1 parent bd32216 commit 07ff709
Show file tree
Hide file tree
Showing 24 changed files with 327 additions and 104 deletions.
2 changes: 1 addition & 1 deletion packages/plugin-runtime-types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yaakapp/api",
"version": "0.2.26",
"version": "0.2.27",
"main": "lib/index.js",
"typings": "./lib/index.d.ts",
"files": [
Expand Down
30 changes: 27 additions & 3 deletions packages/plugin-runtime-types/src/bindings/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export type Color = "custom" | "default" | "primary" | "secondary" | "info" | "s

export type CopyTextRequest = { text: string, };

export type EditorLanguage = "text" | "javascript" | "json" | "html" | "xml" | "graphql" | "markdown";

export type EmptyPayload = {};

export type ExportHttpRequestRequest = { httpRequest: HttpRequest, };
Expand All @@ -49,7 +51,7 @@ export type FindHttpResponsesRequest = { requestId: string, limit?: number, };

export type FindHttpResponsesResponse = { httpResponses: Array<HttpResponse>, };

export type FormInput = { "type": "text" } & FormInputText | { "type": "select" } & FormInputSelect | { "type": "checkbox" } & FormInputCheckbox | { "type": "file" } & FormInputFile | { "type": "http_request" } & FormInputHttpRequest;
export type FormInput = { "type": "text" } & FormInputText | { "type": "editor" } & FormInputEditor | { "type": "select" } & FormInputSelect | { "type": "checkbox" } & FormInputCheckbox | { "type": "file" } & FormInputFile | { "type": "http_request" } & FormInputHttpRequest;

export type FormInputBase = { name: string,
/**
Expand Down Expand Up @@ -79,6 +81,24 @@ label?: string,
*/
defaultValue?: string, };

export type FormInputEditor = {
/**
* Placeholder for the text input
*/
placeholder?: string | null, language: EditorLanguage, name: string,
/**
* Whether the user must fill in the argument
*/
optional?: boolean,
/**
* The label of the input
*/
label?: string,
/**
* The default value
*/
defaultValue?: string, };

export type FormInputFile = {
/**
* The title of the file selection window
Expand Down Expand Up @@ -139,7 +159,11 @@ export type FormInputText = {
/**
* Placeholder for the text input
*/
placeholder?: string | null, name: string,
placeholder?: string | null,
/**
* Placeholder for the text input
*/
password?: boolean, name: string,
/**
* Whether the user must fill in the argument
*/
Expand All @@ -153,7 +177,7 @@ label?: string,
*/
defaultValue?: string, };

export type GetHttpAuthenticationResponse = { name: string, pluginName: string, config: Array<FormInput>, };
export type GetHttpAuthenticationResponse = { name: string, label: string, shortLabel: string, config: Array<FormInput>, };

export type GetHttpRequestActionsRequest = Record<string, never>;

Expand Down
8 changes: 5 additions & 3 deletions packages/plugin-runtime/src/index.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,13 @@ async function initialize() {
}

if (payload.type === 'get_http_authentication_request' && mod.plugin?.authentication) {
const auth = mod.plugin.authentication;
const replyPayload: InternalEventPayload = {
...auth,
type: 'get_http_authentication_response',
name: mod.plugin.authentication.name,
pluginName: pkg.name,
config: mod.plugin.authentication.config,

// Remove unneeded attrs
onApply: undefined,
};
sendPayload(windowContext, replyPayload, replyId);
return;
Expand Down
14 changes: 4 additions & 10 deletions src-tauri/src/http_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,8 @@ pub async fn send_http_request<R: Runtime>(
};

// Apply authentication

// Map legacy auth name values from before they were plugins
let auth_plugin_name = match request.authentication_type.clone() {
Some(s) if s == "basic" => Some("@yaakapp/auth-basic".to_string()),
Some(s) if s == "bearer" => Some("@yaakapp/auth-bearer".to_string()),
_ => request.authentication_type.to_owned(),
};
if let Some(plugin_name) = auth_plugin_name {

if let Some(auth_name) = request.authentication_type.to_owned() {
let req = CallHttpAuthenticationRequest {
config: serde_json::to_value(&request.authentication)
.unwrap()
Expand All @@ -395,13 +389,13 @@ pub async fn send_http_request<R: Runtime>(
.collect(),
};
let plugin_result =
match plugin_manager.call_http_authentication(window, &plugin_name, req).await {
match plugin_manager.call_http_authentication(window, &auth_name, req).await {
Ok(r) => r,
Err(e) => {
return Ok(response_err(&*response.lock().await, e.to_string(), window).await);
}
};

{
let url = sendable_req.url_mut();
*url = Url::parse(&plugin_result.url).unwrap();
Expand Down
21 changes: 7 additions & 14 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,10 @@ async fn cmd_grpc_go<R: Runtime>(
metadata.insert(h.name, h.value);
}

// Map legacy auth name values from before they were plugins
let auth_plugin_name = match req.authentication_type.clone() {
Some(s) if s == "basic" => Some("@yaakapp/auth-basic".to_string()),
Some(s) if s == "bearer" => Some("@yaakapp/auth-bearer".to_string()),
_ => req.authentication_type.to_owned(),
};
if let Some(plugin_name) = auth_plugin_name {
if let Some(auth_name) = req.authentication_type.clone() {
let auth = req.authentication.clone();
let plugin_req = CallHttpAuthenticationRequest {
config: serde_json::to_value(&req.authentication)
.unwrap()
.as_object()
.unwrap()
.to_owned(),
config: serde_json::to_value(&auth).unwrap().as_object().unwrap().to_owned(),
method: "POST".to_string(),
url: req.url.clone(),
headers: metadata
Expand All @@ -259,7 +250,7 @@ async fn cmd_grpc_go<R: Runtime>(
.collect(),
};
let plugin_result = plugin_manager
.call_http_authentication(&window, &plugin_name, plugin_req)
.call_http_authentication(&window, &auth_name, plugin_req)
.await
.map_err(|e| e.to_string())?;

Expand Down Expand Up @@ -980,7 +971,9 @@ async fn cmd_get_http_authentication<R: Runtime>(
window: WebviewWindow<R>,
plugin_manager: State<'_, PluginManager>,
) -> Result<Vec<GetHttpAuthenticationResponse>, String> {
plugin_manager.get_http_authentication(&window).await.map_err(|e| e.to_string())
let results =
plugin_manager.get_http_authentication(&window).await.map_err(|e| e.to_string())?;
Ok(results.into_iter().map(|(_, a)| a).collect())
}

#[tauri::command]
Expand Down
5 changes: 3 additions & 2 deletions src-tauri/src/template_callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ impl TemplateCallback for PluginTemplateCallback {
let mut args_with_defaults = args.clone();

// Fill in default values for all args
for a_def in function.args {
let base = match a_def {
for arg in function.args {
let base = match arg {
FormInput::Text(a) => a.base,
FormInput::Editor(a) => a.base,
FormInput::Select(a) => a.base,
FormInput::Checkbox(a) => a.base,
FormInput::File(a) => a.base,
Expand Down
7 changes: 5 additions & 2 deletions src-tauri/vendored/plugins/auth-basic/build/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions src-tauri/vendored/plugins/auth-bearer/build/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 48 additions & 8 deletions src-tauri/vendored/plugins/auth-jwt/build/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/yaak-grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tokio-stream = "0.1.14"
prost-types = "0.13.4"
serde = { version = "1.0.196", features = ["derive"] }
serde_json = "1.0.113"
prost-reflect = { version = "0.14.4", features = ["serde", "derive"] }
prost-reflect = { version = "0.14.4", default-features = false, features = ["serde", "derive"] }
log = "0.4.20"
anyhow = "1.0.79"
hyper = "1.5.2"
Expand Down
30 changes: 27 additions & 3 deletions src-tauri/yaak-plugins/bindings/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export type Color = "custom" | "default" | "primary" | "secondary" | "info" | "s

export type CopyTextRequest = { text: string, };

export type EditorLanguage = "text" | "javascript" | "json" | "html" | "xml" | "graphql" | "markdown";

export type EmptyPayload = {};

export type ExportHttpRequestRequest = { httpRequest: HttpRequest, };
Expand All @@ -49,7 +51,7 @@ export type FindHttpResponsesRequest = { requestId: string, limit?: number, };

export type FindHttpResponsesResponse = { httpResponses: Array<HttpResponse>, };

export type FormInput = { "type": "text" } & FormInputText | { "type": "select" } & FormInputSelect | { "type": "checkbox" } & FormInputCheckbox | { "type": "file" } & FormInputFile | { "type": "http_request" } & FormInputHttpRequest;
export type FormInput = { "type": "text" } & FormInputText | { "type": "editor" } & FormInputEditor | { "type": "select" } & FormInputSelect | { "type": "checkbox" } & FormInputCheckbox | { "type": "file" } & FormInputFile | { "type": "http_request" } & FormInputHttpRequest;

export type FormInputBase = { name: string,
/**
Expand Down Expand Up @@ -79,6 +81,24 @@ label?: string,
*/
defaultValue?: string, };

export type FormInputEditor = {
/**
* Placeholder for the text input
*/
placeholder?: string | null, language: EditorLanguage, name: string,
/**
* Whether the user must fill in the argument
*/
optional?: boolean,
/**
* The label of the input
*/
label?: string,
/**
* The default value
*/
defaultValue?: string, };

export type FormInputFile = {
/**
* The title of the file selection window
Expand Down Expand Up @@ -139,7 +159,11 @@ export type FormInputText = {
/**
* Placeholder for the text input
*/
placeholder?: string | null, name: string,
placeholder?: string | null,
/**
* Placeholder for the text input
*/
password?: boolean, name: string,
/**
* Whether the user must fill in the argument
*/
Expand All @@ -153,7 +177,7 @@ label?: string,
*/
defaultValue?: string, };

export type GetHttpAuthenticationResponse = { name: string, pluginName: string, config: Array<FormInput>, };
export type GetHttpAuthenticationResponse = { name: string, label: string, shortLabel: string, config: Array<FormInput>, };

export type GetHttpRequestActionsRequest = Record<string, never>;

Expand Down
3 changes: 3 additions & 0 deletions src-tauri/yaak-plugins/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pub enum Error {

#[error("Plugin not found: {0}")]
PluginNotFoundErr(String),

#[error("Auth plugin not found: {0}")]
AuthPluginNotFound(String),

#[error("Plugin error: {0}")]
PluginErr(String),
Expand Down
Loading

0 comments on commit 07ff709

Please sign in to comment.