MCPcopy Create free account
hub / github.com/clawshell/clawshell / forward_oauth_request

Function forward_oauth_request

src/app.rs:816–1008  ·  view source on GitHub ↗
(
    state: &AppState,
    method: Method,
    uri: &Uri,
    headers: HeaderMap,
    body_bytes: Bytes,
    provider: Provider,
    oauth_provider_id: &str,
)

Source from the content-addressed store, hash-verified

814}
815
816async fn forward_oauth_request(
817 state: &AppState,
818 method: Method,
819 uri: &Uri,
820 headers: HeaderMap,
821 body_bytes: Bytes,
822 provider: Provider,
823 oauth_provider_id: &str,
824) -> Result<Response, String> {
825 // 1. Inject auth headers
826 let mut auth_headers = HeaderMap::new();
827 state
828 .oauth_registry
829 .inject_auth(oauth_provider_id, &mut auth_headers)
830 .await
831 .map_err(|e| format!("OAuth auth injection failed: {e}"))?;
832
833 // 2. Optionally transform the body
834 let body = match state
835 .oauth_registry
836 .prepare_request_body(oauth_provider_id, &body_bytes)
837 .await
838 .map_err(|e| format!("OAuth body preparation failed: {e}"))?
839 {
840 Some(transformed) => Bytes::from(transformed),
841 None => body_bytes.clone(),
842 };
843
844 // 2b. Check if path needs rewriting (e.g., /v1/chat/completions → /v1/responses)
845 let original_path = uri.path().to_string();
846 let rewritten_path = state
847 .oauth_registry
848 .rewrite_request_path(oauth_provider_id, &original_path)
849 .map_err(|e| format!("OAuth path rewrite failed: {e}"))?;
850 let needs_translation = state
851 .oauth_registry
852 .needs_response_translation(oauth_provider_id, &original_path)
853 .map_err(|e| format!("OAuth translation check failed: {e}"))?;
854 let response_format = state
855 .oauth_registry
856 .response_format(oauth_provider_id, &original_path)
857 .map_err(|e| format!("OAuth response format check failed: {e}"))?;
858 // Check the transformed body for stream flag (fixups may force stream: true)
859 let stream_requested = serde_json::from_slice::<serde_json::Value>(&body)
860 .ok()
861 .and_then(|v| v.get("stream")?.as_bool())
862 .unwrap_or(false);
863
864 let effective_uri = if let Some(ref new_path) = rewritten_path {
865 build_rewritten_uri(uri, new_path)?
866 } else {
867 uri.clone()
868 };
869
870 if rewritten_path.is_some() {
871 debug!(
872 oauth_provider = %oauth_provider_id,
873 original_path = %original_path,

Callers 1

handle_requestFunction · 0.85

Calls 11

build_rewritten_uriFunction · 0.85
maybe_translate_responseFunction · 0.85
pathMethod · 0.80
forward_oauthMethod · 0.80
inject_authMethod · 0.45
prepare_request_bodyMethod · 0.45
rewrite_request_pathMethod · 0.45
response_formatMethod · 0.45
upstream_urlMethod · 0.45
refreshMethod · 0.45

Tested by

no test coverage detected