Scoped OAuth 2 tokens

This commit is contained in:
Gregory Schier
2025-07-23 22:03:03 -07:00
parent a258a80fbd
commit 20681e5be3
14 changed files with 232 additions and 86 deletions

View File

@@ -55,11 +55,6 @@ pub async fn send_http_request<R: Runtime>(
let response_id = og_response.id.clone();
let response = Arc::new(Mutex::new(og_response.clone()));
let cb = PluginTemplateCallback::new(
window.app_handle(),
&PluginWindowContext::new(window),
RenderPurpose::Send,
);
let update_source = UpdateSource::from_window(window);
let (resolved_request, auth_context_id) = match resolve_http_request(window, unrendered_request)
@@ -75,6 +70,12 @@ pub async fn send_http_request<R: Runtime>(
}
};
let cb = PluginTemplateCallback::new(
window.app_handle(),
&PluginWindowContext::new(window),
RenderPurpose::Send,
);
let request =
match render_http_request(&resolved_request, &base_environment, environment.as_ref(), &cb)
.await

View File

@@ -35,7 +35,13 @@ use yaak_models::models::{
};
use yaak_models::query_manager::QueryManagerExt;
use yaak_models::util::{BatchUpsertResult, UpdateSource, get_workspace_export_resources};
use yaak_plugins::events::{CallGrpcRequestActionArgs, CallGrpcRequestActionRequest, CallHttpRequestActionArgs, CallHttpRequestActionRequest, Color, FilterResponse, GetGrpcRequestActionsResponse, GetHttpAuthenticationConfigResponse, GetHttpAuthenticationSummaryResponse, GetHttpRequestActionsResponse, GetTemplateFunctionsResponse, InternalEvent, InternalEventPayload, JsonPrimitive, PluginWindowContext, RenderPurpose, ShowToastRequest};
use yaak_plugins::events::{
CallGrpcRequestActionArgs, CallGrpcRequestActionRequest, CallHttpRequestActionArgs,
CallHttpRequestActionRequest, Color, FilterResponse, GetGrpcRequestActionsResponse,
GetHttpAuthenticationConfigResponse, GetHttpAuthenticationSummaryResponse,
GetHttpRequestActionsResponse, GetTemplateFunctionsResponse, InternalEvent,
InternalEventPayload, JsonPrimitive, PluginWindowContext, RenderPurpose, ShowToastRequest,
};
use yaak_plugins::manager::PluginManager;
use yaak_plugins::plugin_meta::PluginMetadata;
use yaak_plugins::template_callback::PluginTemplateCallback;
@@ -818,9 +824,29 @@ async fn cmd_get_http_authentication_config<R: Runtime>(
auth_name: &str,
values: HashMap<String, JsonPrimitive>,
request_id: &str,
environment_id: Option<&str>,
workspace_id: &str,
) -> YaakResult<GetHttpAuthenticationConfigResponse> {
let base_environment = window.db().get_base_environment(&workspace_id)?;
let environment = match environment_id {
Some(id) => match window.db().get_environment(id) {
Ok(env) => Some(env),
Err(e) => {
warn!("Failed to find environment by id {id} {}", e);
None
}
},
None => None,
};
Ok(plugin_manager
.get_http_authentication_config(&window, auth_name, values, request_id)
.get_http_authentication_config(
&window,
&base_environment,
environment.as_ref(),
auth_name,
values,
request_id,
)
.await?)
}
@@ -872,9 +898,30 @@ async fn cmd_call_http_authentication_action<R: Runtime>(
action_index: i32,
values: HashMap<String, JsonPrimitive>,
model_id: &str,
workspace_id: &str,
environment_id: Option<&str>,
) -> YaakResult<()> {
let base_environment = window.db().get_base_environment(&workspace_id)?;
let environment = match environment_id {
Some(id) => match window.db().get_environment(id) {
Ok(env) => Some(env),
Err(e) => {
warn!("Failed to find environment by id {id} {}", e);
None
}
},
None => None,
};
Ok(plugin_manager
.call_http_authentication_action(&window, auth_name, action_index, values, model_id)
.call_http_authentication_action(
&window,
&base_environment,
environment.as_ref(),
auth_name,
action_index,
values,
model_id,
)
.await?)
}
@@ -1238,7 +1285,10 @@ pub fn run() {
let _ = app_handle.emit(
"show_toast",
ShowToastRequest {
message: format!("Error handling deep link: {}", e.to_string()),
message: format!(
"Error handling deep link: {}",
e.to_string()
),
color: Some(Color::Danger),
icon: None,
},

View File

@@ -18,7 +18,9 @@ use crate::native_template_functions::template_function_secure;
use crate::nodejs::start_nodejs_plugin_runtime;
use crate::plugin_handle::PluginHandle;
use crate::server_ws::PluginRuntimeServerWebsocket;
use crate::template_callback::PluginTemplateCallback;
use log::{error, info, warn};
use serde_json::json;
use std::collections::HashMap;
use std::env;
use std::path::{Path, PathBuf};
@@ -30,10 +32,13 @@ use tokio::fs::read_dir;
use tokio::net::TcpListener;
use tokio::sync::{Mutex, mpsc};
use tokio::time::{Instant, timeout};
use yaak_models::models::Environment;
use yaak_models::query_manager::QueryManagerExt;
use yaak_models::render::make_vars_hashmap;
use yaak_models::util::generate_id;
use yaak_templates::error::Error::RenderError;
use yaak_templates::error::Result as TemplateResult;
use yaak_templates::render_json_value_raw;
#[derive(Clone)]
pub struct PluginManager {
@@ -569,6 +574,8 @@ impl PluginManager {
pub async fn get_http_authentication_config<R: Runtime>(
&self,
window: &WebviewWindow<R>,
base_environment: &Environment,
environment: Option<&Environment>,
auth_name: &str,
values: HashMap<String, JsonPrimitive>,
request_id: &str,
@@ -579,13 +586,23 @@ impl PluginManager {
.find_map(|(p, r)| if r.name == auth_name { Some(p) } else { None })
.ok_or(PluginNotFoundErr(auth_name.into()))?;
let vars = &make_vars_hashmap(&base_environment, environment);
let cb = PluginTemplateCallback::new(
window.app_handle(),
&PluginWindowContext::new(&window),
RenderPurpose::Preview,
);
let rendered_values = render_json_value_raw(json!(values), vars, &cb).await?;
let context_id = format!("{:x}", md5::compute(request_id.to_string()));
let event = self
.send_to_plugin_and_wait(
&PluginWindowContext::new(window),
&plugin,
&InternalEventPayload::GetHttpAuthenticationConfigRequest(
GetHttpAuthenticationConfigRequest { values, context_id },
GetHttpAuthenticationConfigRequest {
values: serde_json::from_value(rendered_values)?,
context_id,
},
),
)
.await?;
@@ -602,11 +619,24 @@ impl PluginManager {
pub async fn call_http_authentication_action<R: Runtime>(
&self,
window: &WebviewWindow<R>,
base_environment: &Environment,
environment: Option<&Environment>,
auth_name: &str,
action_index: i32,
values: HashMap<String, JsonPrimitive>,
model_id: &str,
) -> Result<()> {
let vars = &make_vars_hashmap(&base_environment, environment);
let rendered_values = render_json_value_raw(
json!(values),
vars,
&PluginTemplateCallback::new(
window.app_handle(),
&PluginWindowContext::new(&window),
RenderPurpose::Preview,
),
)
.await?;
let results = self.get_http_authentication_summaries(window).await?;
let plugin = results
.iter()
@@ -621,7 +651,10 @@ impl PluginManager {
CallHttpAuthenticationActionRequest {
index: action_index,
plugin_ref_id: plugin.clone().ref_id,
args: CallHttpAuthenticationActionArgs { context_id, values },
args: CallHttpAuthenticationActionArgs {
context_id,
values: serde_json::from_value(rendered_values)?,
},
},
),
)