Skip to main content

orvanta_api/apis/
mcp_oauth_api.rs

1/*
2 * Orvanta API
3 *
4 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
5 *
6 * The version of the OpenAPI document: 1.7.1
7 * Contact: contact@orvanta.cloud
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13use serde::{Deserialize, Serialize};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration};
16
17
18/// struct for typed errors of method [`discover_mcp_o_auth`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum DiscoverMcpOAuthError {
22    UnknownValue(serde_json::Value),
23}
24
25/// struct for typed errors of method [`get_mcp_o_auth_client_metadata`]
26#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(untagged)]
28pub enum GetMcpOAuthClientMetadataError {
29    UnknownValue(serde_json::Value),
30}
31
32/// struct for typed errors of method [`mcp_o_auth_callback`]
33#[derive(Debug, Clone, Serialize, Deserialize)]
34#[serde(untagged)]
35pub enum McpOAuthCallbackError {
36    UnknownValue(serde_json::Value),
37}
38
39/// struct for typed errors of method [`refresh_mcp_o_auth`]
40#[derive(Debug, Clone, Serialize, Deserialize)]
41#[serde(untagged)]
42pub enum RefreshMcpOAuthError {
43    UnknownValue(serde_json::Value),
44}
45
46/// struct for typed errors of method [`start_mcp_o_auth_popup`]
47#[derive(Debug, Clone, Serialize, Deserialize)]
48#[serde(untagged)]
49pub enum StartMcpOAuthPopupError {
50    UnknownValue(serde_json::Value),
51}
52
53
54pub async fn discover_mcp_o_auth(configuration: &configuration::Configuration, discover_mcp_o_auth_request: models::DiscoverMcpOAuthRequest) -> Result<models::DiscoverMcpOAuth200Response, Error<DiscoverMcpOAuthError>> {
55    let local_var_configuration = configuration;
56
57    let local_var_client = &local_var_configuration.client;
58
59    let local_var_uri_str = format!("{}/mcp/oauth/discover", local_var_configuration.base_path);
60    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
61
62    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
63        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
64    }
65    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
66        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
67    };
68    local_var_req_builder = local_var_req_builder.json(&discover_mcp_o_auth_request);
69
70    let local_var_req = local_var_req_builder.build()?;
71    let local_var_resp = local_var_client.execute(local_var_req).await?;
72
73    let local_var_status = local_var_resp.status();
74    let local_var_content = local_var_resp.text().await?;
75
76    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
77        crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).map_err(Error::from)
78    } else {
79        let local_var_entity: Option<DiscoverMcpOAuthError> = crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).ok();
80        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
81        Err(Error::ResponseError(local_var_error))
82    }
83}
84
85/// Static client metadata document served for MCP servers that want to inspect Orvanta's registration parameters ahead of dynamic client registration.
86pub async fn get_mcp_o_auth_client_metadata(configuration: &configuration::Configuration, ) -> Result<models::GetMcpOAuthClientMetadata200Response, Error<GetMcpOAuthClientMetadataError>> {
87    let local_var_configuration = configuration;
88
89    let local_var_client = &local_var_configuration.client;
90
91    let local_var_uri_str = format!("{}/mcp/oauth/client-metadata.json", local_var_configuration.base_path);
92    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
93
94    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
95        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
96    }
97
98    let local_var_req = local_var_req_builder.build()?;
99    let local_var_resp = local_var_client.execute(local_var_req).await?;
100
101    let local_var_status = local_var_resp.status();
102    let local_var_content = local_var_resp.text().await?;
103
104    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
105        crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).map_err(Error::from)
106    } else {
107        let local_var_entity: Option<GetMcpOAuthClientMetadataError> = crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).ok();
108        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
109        Err(Error::ResponseError(local_var_error))
110    }
111}
112
113/// Handles the OAuth redirect from the MCP server, exchanges the code for tokens (PKCE, server-to-server), persists them encrypted per-workspace, and returns an HTML page that posts a message to window.opener and closes itself. Always returns 200 -- success/failure is conveyed via the posted message, never plaintext tokens.
114pub async fn mcp_o_auth_callback(configuration: &configuration::Configuration, state: &str, code: Option<&str>, error: Option<&str>, error_description: Option<&str>) -> Result<String, Error<McpOAuthCallbackError>> {
115    let local_var_configuration = configuration;
116
117    let local_var_client = &local_var_configuration.client;
118
119    let local_var_uri_str = format!("{}/mcp/oauth/callback", local_var_configuration.base_path);
120    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
121
122    if let Some(ref local_var_str) = code {
123        local_var_req_builder = local_var_req_builder.query(&[("code", &local_var_str.to_string())]);
124    }
125    local_var_req_builder = local_var_req_builder.query(&[("state", &state.to_string())]);
126    if let Some(ref local_var_str) = error {
127        local_var_req_builder = local_var_req_builder.query(&[("error", &local_var_str.to_string())]);
128    }
129    if let Some(ref local_var_str) = error_description {
130        local_var_req_builder = local_var_req_builder.query(&[("error_description", &local_var_str.to_string())]);
131    }
132    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
133        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
134    }
135
136    let local_var_req = local_var_req_builder.build()?;
137    let local_var_resp = local_var_client.execute(local_var_req).await?;
138
139    let local_var_status = local_var_resp.status();
140    let local_var_content = local_var_resp.text().await?;
141
142    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
143        crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).map_err(Error::from)
144    } else {
145        let local_var_entity: Option<McpOAuthCallbackError> = crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).ok();
146        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
147        Err(Error::ResponseError(local_var_error))
148    }
149}
150
151/// Uses the stored (encrypted) refresh token to obtain a new access token from the MCP server and updates the stored connection. The caller must be a member of workspace_id.
152pub async fn refresh_mcp_o_auth(configuration: &configuration::Configuration, refresh_mcp_o_auth_request: models::RefreshMcpOAuthRequest) -> Result<models::RefreshMcpOAuth200Response, Error<RefreshMcpOAuthError>> {
153    let local_var_configuration = configuration;
154
155    let local_var_client = &local_var_configuration.client;
156
157    let local_var_uri_str = format!("{}/mcp/oauth/refresh", local_var_configuration.base_path);
158    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
159
160    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
161        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
162    }
163    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
164        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
165    };
166    local_var_req_builder = local_var_req_builder.json(&refresh_mcp_o_auth_request);
167
168    let local_var_req = local_var_req_builder.build()?;
169    let local_var_resp = local_var_client.execute(local_var_req).await?;
170
171    let local_var_status = local_var_resp.status();
172    let local_var_content = local_var_resp.text().await?;
173
174    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
175        crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).map_err(Error::from)
176    } else {
177        let local_var_entity: Option<RefreshMcpOAuthError> = crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).ok();
178        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
179        Err(Error::ResponseError(local_var_error))
180    }
181}
182
183/// Opens in a popup, discovers OAuth metadata, registers a client (if needed), and redirects to the MCP server's authorization endpoint. The caller must be a member of workspace_id.
184pub async fn start_mcp_o_auth_popup(configuration: &configuration::Configuration, mcp_server_url: &str, workspace_id: &str, scopes: Option<&str>) -> Result<(), Error<StartMcpOAuthPopupError>> {
185    let local_var_configuration = configuration;
186
187    let local_var_client = &local_var_configuration.client;
188
189    let local_var_uri_str = format!("{}/mcp/oauth/start", local_var_configuration.base_path);
190    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
191
192    local_var_req_builder = local_var_req_builder.query(&[("mcp_server_url", &mcp_server_url.to_string())]);
193    local_var_req_builder = local_var_req_builder.query(&[("workspace_id", &workspace_id.to_string())]);
194    if let Some(ref local_var_str) = scopes {
195        local_var_req_builder = local_var_req_builder.query(&[("scopes", &local_var_str.to_string())]);
196    }
197    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
198        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
199    }
200    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
201        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
202    };
203
204    let local_var_req = local_var_req_builder.build()?;
205    let local_var_resp = local_var_client.execute(local_var_req).await?;
206
207    let local_var_status = local_var_resp.status();
208    let local_var_content = local_var_resp.text().await?;
209
210    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
211        Ok(())
212    } else {
213        let local_var_entity: Option<StartMcpOAuthPopupError> = crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).ok();
214        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
215        Err(Error::ResponseError(local_var_error))
216    }
217}
218